/
genesis.go
378 lines (344 loc) · 9.71 KB
/
genesis.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package incentive
import (
"encoding/json"
"fmt"
"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
leveragetypes "github.com/umee-network/umee/v5/x/leverage/types"
)
// NewGenesisState creates a new GenesisState object
func NewGenesisState(
params Params,
completedPrograms []IncentiveProgram,
ongoingPrograms []IncentiveProgram,
upcomingPrograms []IncentiveProgram,
nextProgramID uint32,
lastRewardTime int64,
bonds []Bond,
rewardTrackers []RewardTracker,
rewardAccumulators []RewardAccumulator,
accountUnbondings []AccountUnbondings,
) *GenesisState {
return &GenesisState{
Params: params,
CompletedPrograms: completedPrograms,
OngoingPrograms: ongoingPrograms,
UpcomingPrograms: upcomingPrograms,
NextProgramId: nextProgramID,
LastRewardsTime: lastRewardTime,
Bonds: bonds,
RewardTrackers: rewardTrackers,
RewardAccumulators: rewardAccumulators,
AccountUnbondings: accountUnbondings,
}
}
// DefaultGenesis returns the default genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
NextProgramId: 1,
LastRewardsTime: 0,
}
}
// Validate checks a genesis state for basic issues
func (gs GenesisState) Validate() error {
if err := gs.Params.Validate(); err != nil {
return err
}
if gs.NextProgramId == 0 {
return ErrInvalidProgramID.Wrap("next program ID must not be zero")
}
if gs.LastRewardsTime < 0 {
return ErrDecreaseLastRewardTime.Wrap("last reward time must not be negative")
}
m := map[string]bool{}
for _, rt := range gs.RewardTrackers {
// enforce no duplicate (account,denom)
s := rt.Account + rt.UToken
if err := noDuplicateString(m, s, "reward trackers"); err != nil {
return err
}
if err := rt.Validate(); err != nil {
return err
}
}
m = map[string]bool{}
for _, ra := range gs.RewardAccumulators {
if err := ra.Validate(); err != nil {
return err
}
// enforce no duplicate denoms
s := ra.UToken
if err := noDuplicateString(m, s, "reward accumulators"); err != nil {
return err
}
}
m = map[string]bool{}
// enforce no duplicate program IDs
for _, up := range gs.UpcomingPrograms {
if err := up.ValidatePassed(); err != nil {
return err
}
s := fmt.Sprintf("%d", up.ID)
if err := noDuplicateString(m, s, "upcoming program ID"); err != nil {
return err
}
}
for _, op := range gs.OngoingPrograms {
if err := op.ValidatePassed(); err != nil {
return err
}
s := fmt.Sprintf("%d", op.ID)
if err := noDuplicateString(m, s, "ongoing program ID"); err != nil {
return err
}
}
for _, cp := range gs.CompletedPrograms {
if err := cp.ValidatePassed(); err != nil {
return err
}
s := fmt.Sprintf("%d", cp.ID)
if err := noDuplicateString(m, s, "completed program ID"); err != nil {
return err
}
}
m = map[string]bool{}
for _, b := range gs.Bonds {
if err := b.Validate(); err != nil {
return err
}
// enforce no duplicate (account,denom)
s := b.Account + b.UToken.Denom
if err := noDuplicateString(m, s, "bonds"); err != nil {
return err
}
}
m = map[string]bool{}
for _, au := range gs.AccountUnbondings {
if err := au.Validate(); err != nil {
return err
}
// enforce no duplicate (account,denom)
s := au.Account + au.UToken
if err := noDuplicateString(m, s, "account unbondings"); err != nil {
return err
}
}
return nil
}
// noDuplicateString checks to see if a string is already present in a map
// and then adds it to the map. If it was already present, an error is returned.
// used to check all uniqueness requirements in genesis state.
func noDuplicateString(m map[string]bool, s, errMsg string) error {
if _, found := m[s]; found {
return fmt.Errorf("duplicate %s: %s", errMsg, s)
}
m[s] = true
return nil
}
// GetGenesisStateFromAppState returns x/incentive GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *GenesisState {
var genesisState GenesisState
if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}
return &genesisState
}
// NewIncentiveProgram creates the IncentiveProgram struct used in GenesisState
func NewIncentiveProgram(
id uint32,
startTime int64,
duration int64,
uDenom string,
totalRewards, remainingRewards sdk.Coin,
funded bool,
) IncentiveProgram {
return IncentiveProgram{
ID: id,
StartTime: startTime,
Duration: duration,
UToken: uDenom,
TotalRewards: totalRewards,
RemainingRewards: remainingRewards,
Funded: funded,
}
}
// Validate performs validation on an IncentiveProgram type returning an error
// if the program is invalid.
func (ip IncentiveProgram) Validate() error {
if err := sdk.ValidateDenom(ip.UToken); err != nil {
return err
}
if !leveragetypes.HasUTokenPrefix(ip.UToken) {
// only allow uToken denoms as bonded denoms
return errors.Wrap(leveragetypes.ErrNotUToken, ip.UToken)
}
if err := ip.TotalRewards.Validate(); err != nil {
return err
}
if leveragetypes.HasUTokenPrefix(ip.TotalRewards.Denom) {
// only allow base token denoms as rewards
return errors.Wrap(leveragetypes.ErrUToken, ip.TotalRewards.Denom)
}
if ip.TotalRewards.IsZero() {
return ErrProgramWithoutRewards
}
if err := ip.RemainingRewards.Validate(); err != nil {
return err
}
if ip.RemainingRewards.Denom != ip.TotalRewards.Denom {
return ErrProgramRewardMismatch
}
if !ip.Funded && ip.RemainingRewards.IsPositive() {
return ErrNonfundedProgramRewards
}
if ip.Duration <= 0 {
return errors.Wrapf(ErrInvalidProgramDuration, "%d", ip.Duration)
}
if ip.StartTime <= 0 {
return ErrInvalidProgramStart.Wrapf("%d", ip.StartTime)
}
return nil
}
// ValidateProposed runs IncentiveProgram.Validate and also checks additional requirements applying
// to incentive programs which have not yet been funded or passed by governance
func (ip IncentiveProgram) ValidateProposed() error {
if ip.ID != 0 {
return ErrInvalidProgramID.Wrapf("%d", ip.ID)
}
if !ip.RemainingRewards.IsZero() {
return ErrNonzeroRemainingRewards.Wrap(ip.RemainingRewards.String())
}
if ip.Funded {
return ErrProposedFundedProgram
}
return ip.Validate()
}
// ValidatePassed runs IncentiveProgram.Validate and also checks additional requirements applying
// to incentive programs which have already been passed by governance
func (ip IncentiveProgram) ValidatePassed() error {
if ip.ID == 0 {
return ErrInvalidProgramID.Wrapf("%d", ip.ID)
}
return ip.Validate()
}
// NewBond creates the Bond struct used in GenesisState
func NewBond(addr string, coin sdk.Coin) Bond {
return Bond{
Account: addr,
UToken: coin,
}
}
func (b Bond) Validate() error {
if _, err := sdk.AccAddressFromBech32(b.Account); err != nil {
return err
}
if err := b.UToken.Validate(); err != nil {
return err
}
if !leveragetypes.HasUTokenPrefix(b.UToken.Denom) {
return leveragetypes.ErrNotUToken.Wrap(b.UToken.Denom)
}
return nil
}
// NewRewardTracker creates the RewardTracker struct used in GenesisState
func NewRewardTracker(addr, uDenom string, coins sdk.DecCoins) RewardTracker {
return RewardTracker{
Account: addr,
UToken: uDenom,
Rewards: coins,
}
}
func (rt RewardTracker) Validate() error {
if _, err := sdk.AccAddressFromBech32(rt.Account); err != nil {
return err
}
if err := sdk.ValidateDenom(rt.UToken); err != nil {
return err
}
if !leveragetypes.HasUTokenPrefix(rt.UToken) {
return leveragetypes.ErrNotUToken.Wrap(rt.UToken)
}
if err := rt.Rewards.Validate(); err != nil {
return err
}
for _, r := range rt.Rewards {
if leveragetypes.HasUTokenPrefix(r.Denom) {
return leveragetypes.ErrUToken.Wrap(r.Denom)
}
}
return nil
}
// NewRewardAccumulator creates the RewardAccumulator struct used in GenesisState
func NewRewardAccumulator(uDenom string, exponent uint32, coins sdk.DecCoins) RewardAccumulator {
return RewardAccumulator{
UToken: uDenom,
Exponent: exponent,
Rewards: coins,
}
}
func (ra RewardAccumulator) Validate() error {
if err := sdk.ValidateDenom(ra.UToken); err != nil {
return err
}
if !leveragetypes.HasUTokenPrefix(ra.UToken) {
return leveragetypes.ErrNotUToken.Wrap(ra.UToken)
}
if err := ra.Rewards.Validate(); err != nil {
return err
}
for _, r := range ra.Rewards {
if leveragetypes.HasUTokenPrefix(r.Denom) {
return leveragetypes.ErrUToken.Wrap(r.Denom)
}
}
return nil
}
// NewUnbonding creates the Unbonding struct used in GenesisState
func NewUnbonding(startTime, endTime int64, coin sdk.Coin) Unbonding {
return Unbonding{
Start: startTime,
End: endTime,
UToken: coin,
}
}
func (u Unbonding) Validate() error {
if u.End < u.Start {
return ErrInvalidUnbonding.Wrap("start time > end time")
}
if !leveragetypes.HasUTokenPrefix(u.UToken.Denom) {
return leveragetypes.ErrNotUToken.Wrap(u.UToken.Denom)
}
return u.UToken.Validate()
}
// NewAccountUnbondings creates the AccountUnbondings struct used in GenesisState
func NewAccountUnbondings(addr, uDenom string, unbondings []Unbonding) AccountUnbondings {
return AccountUnbondings{
Account: addr,
UToken: uDenom,
Unbondings: unbondings,
}
}
func (au AccountUnbondings) Validate() error {
if _, err := sdk.AccAddressFromBech32(au.Account); err != nil {
return err
}
if err := sdk.ValidateDenom(au.UToken); err != nil {
return err
}
if !leveragetypes.HasUTokenPrefix(au.UToken) {
return leveragetypes.ErrNotUToken.Wrap(au.UToken)
}
for _, u := range au.Unbondings {
if u.UToken.Denom != au.UToken {
return ErrInvalidUnbonding.Wrapf("unbonding denom %s does not match accountUnbondings denom %s",
u.UToken.Denom, au.UToken)
}
if err := u.Validate(); err != nil {
return err
}
}
return nil
}