Skip to content

Commit e73b7ee

Browse files
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 c676952) # 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>
1 parent ac970f5 commit e73b7ee

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
3737

3838
## [Unreleased]
3939

40+
### Improvements
41+
42+
* (x/auth/vesting) [\#11652](https://github.com/cosmos/cosmos-sdk/pull/11652) Add util functions for `Period(s)`
43+
4044
## [v0.45.3](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.45.3) - 2022-04-12
4145

4246
### Improvements

x/auth/vesting/types/period.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,55 @@ package types
33
import (
44
"fmt"
55
"strings"
6+
"time"
67

78
yaml "gopkg.in/yaml.v2"
9+
10+
sdk "github.com/cosmos/cosmos-sdk/types"
811
)
912

1013
// Periods stores all vesting periods passed as part of a PeriodicVestingAccount
1114
type Periods []Period
1215

13-
// String Period implements stringer interface
16+
// Duration is converts the period Length from seconds to a time.Duration
17+
func (p Period) Duration() time.Duration {
18+
return time.Duration(p.Length) * time.Second
19+
}
20+
21+
// String implements the fmt.Stringer interface
1422
func (p Period) String() string {
1523
out, _ := yaml.Marshal(p)
1624
return string(out)
1725
}
1826

19-
// String Periods implements stringer interface
20-
func (vp Periods) String() string {
21-
periodsListString := make([]string, len(vp))
22-
for _, period := range vp {
27+
// TotalLength return the total length in seconds for a period
28+
func (p Periods) TotalLength() int64 {
29+
var total int64
30+
for _, period := range p {
31+
total += period.Length
32+
}
33+
return total
34+
}
35+
36+
// TotalDuration returns the total duration of the period
37+
func (p Periods) TotalDuration() time.Duration {
38+
len := p.TotalLength()
39+
return time.Duration(len) * time.Second
40+
}
41+
42+
// TotalDuration returns the sum of coins for the period
43+
func (p Periods) TotalAmount() sdk.Coins {
44+
total := sdk.Coins{}
45+
for _, period := range p {
46+
total = total.Add(period.Amount...)
47+
}
48+
return total
49+
}
50+
51+
// String implements the fmt.Stringer interface
52+
func (p Periods) String() string {
53+
periodsListString := make([]string, len(p))
54+
for _, period := range p {
2355
periodsListString = append(periodsListString, period.String())
2456
}
2557

0 commit comments

Comments
 (0)