Skip to content

Commit a69292c

Browse files
mergify[bot]fedekunze
authored andcommitted
feat: add vesting util functions (backport cosmos#11652) (cosmos#11667)
* feat: add vesting util functions (cosmos#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 a9403e3 commit a69292c

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,24 +3,56 @@ package types
33
import (
44
"fmt"
55
"strings"
6+
"time"
67

78
sdk "github.com/cosmos/cosmos-sdk/types"
89
yaml "gopkg.in/yaml.v2"
10+
11+
sdk "github.com/cosmos/cosmos-sdk/types"
912
)
1013

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

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

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

0 commit comments

Comments
 (0)