feat: add vesting util functions (backport #11652) (#11667)

* feat: add vesting util functions (#11652)

* feat: add vesting util functions

* changelog

* revert string deletion

* fix build

* Update x/auth/vesting/types/period.go

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Marko <marbar3778@yahoo.com>
(cherry picked from commit c676952278)

# Conflicts:
#	CHANGELOG.md
#	x/auth/vesting/types/period.go

* changelog

* Update x/auth/vesting/types/period.go

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Federico Kunze Küllmer <federico.kunze94@gmail.com>
This commit is contained in:
mergify[bot] 2022-04-18 11:25:42 +02:00 committed by GitHub
parent ac970f52be
commit e73b7eece0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 5 deletions

View File

@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
## [Unreleased]
### Improvements
* (x/auth/vesting) [\#11652](https://github.com/cosmos/cosmos-sdk/pull/11652) Add util functions for `Period(s)`
## [v0.45.3](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.45.3) - 2022-04-12
### Improvements

View File

@ -3,23 +3,55 @@ package types
import (
"fmt"
"strings"
"time"
yaml "gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Periods stores all vesting periods passed as part of a PeriodicVestingAccount
type Periods []Period
// String Period implements stringer interface
// Duration is converts the period Length from seconds to a time.Duration
func (p Period) Duration() time.Duration {
return time.Duration(p.Length) * time.Second
}
// String implements the fmt.Stringer interface
func (p Period) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// String Periods implements stringer interface
func (vp Periods) String() string {
periodsListString := make([]string, len(vp))
for _, period := range vp {
// TotalLength return the total length in seconds for a period
func (p Periods) TotalLength() int64 {
var total int64
for _, period := range p {
total += period.Length
}
return total
}
// TotalDuration returns the total duration of the period
func (p Periods) TotalDuration() time.Duration {
len := p.TotalLength()
return time.Duration(len) * time.Second
}
// TotalDuration returns the sum of coins for the period
func (p Periods) TotalAmount() sdk.Coins {
total := sdk.Coins{}
for _, period := range p {
total = total.Add(period.Amount...)
}
return total
}
// String implements the fmt.Stringer interface
func (p Periods) String() string {
periodsListString := make([]string, len(p))
for _, period := range p {
periodsListString = append(periodsListString, period.String())
}