-
Notifications
You must be signed in to change notification settings - Fork 43
/
schedule.go
120 lines (94 loc) · 2.85 KB
/
schedule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package types
import (
"errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)
//-----------------------------------------------------------------------------
// Schedule
// NewSchedule returns new Schedule instance
func NewSchedule(startTime, endTime int64, ratio sdk.Dec) Schedule {
return Schedule{
StartTime: startTime,
EndTime: endTime,
Ratio: ratio,
}
}
// GetStartTime returns start time
func (s Schedule) GetStartTime() int64 {
return s.StartTime
}
// GetEndTime returns end time
func (s Schedule) GetEndTime() int64 {
return s.EndTime
}
// GetRatio returns ratio
func (s Schedule) GetRatio() sdk.Dec {
return s.Ratio
}
// Validate checks that the lazy schedule is valid.
func (s Schedule) Validate() error {
startTime := s.GetStartTime()
endTime := s.GetEndTime()
ratio := s.GetRatio()
if startTime < 0 {
return errors.New("vesting start-time cannot be negative")
}
if endTime < startTime {
return errors.New("vesting start-time cannot be before end-time")
}
if ratio.LTE(sdk.ZeroDec()) {
return errors.New("vesting ratio cannot be smaller than or equal with zero")
}
return nil
}
// Schedules stores all lazy schedules
type Schedules []Schedule
//-----------------------------------------------------------------------------
// Vesting Schedule
// NewVestingSchedule creates a new vesting lazy schedule instance.
func NewVestingSchedule(denom string, schedules Schedules) VestingSchedule {
return VestingSchedule{
Denom: denom,
Schedules: schedules,
}
}
// GetVestedRatio returns the ratio of tokens that have vested by blockTime.
func (vs VestingSchedule) GetVestedRatio(blockTime int64) sdk.Dec {
sumRatio := sdk.ZeroDec()
for _, lazySchedule := range vs.Schedules {
startTime := lazySchedule.GetStartTime()
endTime := lazySchedule.GetEndTime()
ratio := lazySchedule.GetRatio()
if blockTime < startTime {
continue
}
if blockTime < endTime {
ratio = ratio.MulInt64(blockTime - startTime).QuoInt64(endTime - startTime)
}
sumRatio = sumRatio.Add(ratio)
}
return sumRatio
}
// GetDenom returns the denom of vesting schedule
func (vs VestingSchedule) GetDenom() string {
return vs.Denom
}
// Validate checks that the vesting lazy schedule is valid.
func (vs VestingSchedule) Validate() error {
sumRatio := sdk.ZeroDec()
for _, lazySchedule := range vs.Schedules {
if err := lazySchedule.Validate(); err != nil {
return err
}
sumRatio = sumRatio.Add(lazySchedule.GetRatio())
}
// add rounding to allow language specific calculation errors
const fixedPointDecimals = 1000000000
if !sdk.NewDec(sumRatio.MulInt64(fixedPointDecimals).RoundInt64()).
QuoInt64(fixedPointDecimals).Equal(sdk.OneDec()) {
return errors.New("vesting total ratio must be one")
}
return nil
}
// VestingSchedules stores all vesting schedules passed as part of a LazyGradedVestingAccount
type VestingSchedules []VestingSchedule