Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add vesting util functions #11652

Merged
merged 10 commits into from
Apr 17, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (x/auth/vesting) [\#11652](https://github.com/cosmos/cosmos-sdk/pull/11652) Add util functions for `Period(s)`
* [\#11630](https://github.com/cosmos/cosmos-sdk/pull/11630) Add SafeSub method to sdk.Coin.
* [\#11511](https://github.com/cosmos/cosmos-sdk/pull/11511) Add api server flags to start command.
* [\#11484](https://github.com/cosmos/cosmos-sdk/pull/11484) Implement getter for keyring backend option.
Expand Down
42 changes: 37 additions & 5 deletions x/auth/vesting/types/period.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,55 @@ package types
import (
"fmt"
"strings"
"time"

"sigs.k8s.io/yaml"

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())
}

Expand Down