-
Notifications
You must be signed in to change notification settings - Fork 48
/
commitments.go
330 lines (281 loc) · 9.65 KB
/
commitments.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
package types
import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (c Commitments) IsEmpty() bool {
if len(c.CommittedTokens) > 0 {
return false
}
if len(c.RewardsUnclaimed) > 0 {
return false
}
if len(c.Claimed) > 0 {
return false
}
if len(c.VestingTokens) > 0 {
return false
}
return true
}
func (c *Commitments) GetCommittedAmountForDenom(denom string) math.Int {
for _, token := range c.CommittedTokens {
if token.Denom == denom {
return token.Amount
}
}
return sdk.NewInt(0)
}
func (c *Commitments) GetCommittedLockUpsForDenom(denom string) []Lockup {
for _, token := range c.CommittedTokens {
if token.Denom == denom {
return token.Lockups
}
}
return nil
}
func (c *Commitments) AddCommittedTokens(denom string, amount math.Int, unlockTime uint64) {
for i, token := range c.CommittedTokens {
if token.Denom == denom {
c.CommittedTokens[i].Amount = token.Amount.Add(amount)
c.CommittedTokens[i].Lockups = append(token.Lockups, Lockup{
Amount: amount,
UnlockTimestamp: unlockTime,
})
li := int(0)
for _, lockup := range token.Lockups {
if lockup.UnlockTimestamp < unlockTime {
li++
continue
} else if lockup.UnlockTimestamp == unlockTime {
c.CommittedTokens[i].Lockups[li].Amount = lockup.Amount.Add(amount)
return
} else {
break
}
}
c.CommittedTokens[i].Lockups = append(token.Lockups[:li+1], token.Lockups[li:]...)
c.CommittedTokens[i].Lockups[li] = Lockup{
Amount: amount,
UnlockTimestamp: unlockTime,
}
return
}
}
c.CommittedTokens = append(c.CommittedTokens, &CommittedTokens{
Denom: denom,
Amount: amount,
Lockups: []Lockup{
{
Amount: amount,
UnlockTimestamp: unlockTime,
},
},
})
}
func (c *Commitments) DeductFromCommitted(denom string, amount math.Int, currTime uint64) error {
for i, token := range c.CommittedTokens {
if token.Denom == denom {
c.CommittedTokens[i].Amount = token.Amount.Sub(amount)
if c.CommittedTokens[i].Amount.IsNegative() {
return ErrInsufficientCommittedTokens
}
withdrawnTokens := sdk.ZeroInt()
newLockups := []Lockup{}
for _, lockup := range token.Lockups {
if withdrawnTokens.LT(amount) {
if lockup.UnlockTimestamp <= currTime {
withdrawAmount := lockup.Amount
if withdrawAmount.GT(amount.Sub(withdrawnTokens)) {
withdrawAmount = amount.Sub(withdrawnTokens)
newLockups = append(newLockups, Lockup{
Amount: lockup.Amount.Sub(withdrawAmount),
UnlockTimestamp: lockup.UnlockTimestamp,
})
}
withdrawnTokens = withdrawnTokens.Add(withdrawAmount)
} else {
return ErrInsufficientWithdrawableTokens
}
} else {
newLockups = append(newLockups, lockup)
}
}
c.CommittedTokens[i].Lockups = newLockups
return nil
}
}
return ErrInsufficientCommittedTokens
}
func (c *Commitments) GetRewardUnclaimedForDenom(denom string) math.Int {
return c.RewardsUnclaimed.AmountOf(denom)
}
// Sub bucket rewards query - Elys
func (c *Commitments) GetElysSubBucketRewardUnclaimedForDenom(denom string) math.Int {
return c.RewardsByElysUnclaimed.AmountOf(denom)
}
// Sub bucket rewards query - Eden
func (c *Commitments) GetEdenSubBucketRewardUnclaimedForDenom(denom string) math.Int {
return c.RewardsByEdenUnclaimed.AmountOf(denom)
}
// Sub bucket rewards query - EdenB
func (c *Commitments) GetEdenBSubBucketRewardUnclaimedForDenom(denom string) math.Int {
return c.RewardsByEdenbUnclaimed.AmountOf(denom)
}
// Sub bucket rewards query - Usdc
func (c *Commitments) GetUsdcSubBucketRewardUnclaimedForDenom(denom string) math.Int {
return c.RewardsByUsdcUnclaimed.AmountOf(denom)
}
func (c *Commitments) GetLPMiningSubBucketRewardUnclaimedForDenom(denom string) math.Int {
lpMiningRewards := c.RewardsUnclaimed.
Sub(c.RewardsByElysUnclaimed...).
Sub(c.RewardsByEdenUnclaimed...).
Sub(c.RewardsByEdenbUnclaimed...).
Sub(c.RewardsByUsdcUnclaimed...)
return lpMiningRewards.AmountOf(denom)
}
func (c *Commitments) AddRewardsUnclaimed(amount sdk.Coin) {
c.RewardsUnclaimed = c.RewardsUnclaimed.Add(amount)
}
func (c *Commitments) AddSubBucketRewardsByElysUnclaimed(amount sdk.Coins) {
c.RewardsByElysUnclaimed = c.RewardsByElysUnclaimed.Add(amount...)
}
func (c *Commitments) AddSubBucketRewardsByEdenUnclaimed(amount sdk.Coins) {
c.RewardsByEdenUnclaimed = c.RewardsByEdenUnclaimed.Add(amount...)
}
func (c *Commitments) AddSubBucketRewardsByEdenBUnclaimed(amount sdk.Coins) {
c.RewardsByEdenbUnclaimed = c.RewardsByEdenbUnclaimed.Add(amount...)
}
func (c *Commitments) AddSubBucketRewardsByUsdcUnclaimed(amount sdk.Coins) {
c.RewardsByUsdcUnclaimed = c.RewardsByUsdcUnclaimed.Add(amount...)
}
func (c *Commitments) SubRewardsUnclaimed(amount sdk.Coin) error {
if c.RewardsUnclaimed.AmountOf(amount.Denom).LT(amount.Amount) {
return ErrInsufficientRewardsUnclaimed
}
c.RewardsUnclaimed = c.RewardsUnclaimed.Sub(amount)
// First deduct from elys staking bucket
allDeducted, remainedAmount := c.DeductSubBucketRewardsByElysUnclaimed(amount)
if allDeducted {
return nil
}
// If there is still remaining amount, deduct from eden commited bucket
allDeducted, remainedAmount = c.DeductSubBucketRewardsByEdenUnclaimed(remainedAmount)
if allDeducted {
return nil
}
// If there is still remaining amount, deduct from edenb commited bucket
allDeducted, remainedAmount = c.DeductSubBucketRewardsByEdenBUnclaimed(remainedAmount)
if allDeducted {
return nil
}
// If there is still remaining amount, deduct from usdc deposit bucket
c.DeductSubBucketRewardsByUsdcUnclaimed(remainedAmount)
return nil
}
// This is the function used when we withdraw the reward by program
func (c *Commitments) SubRewardsUnclaimedForElysStaking(amount sdk.Coin) error {
if c.RewardsUnclaimed.AmountOf(amount.Denom).LT(amount.Amount) {
return ErrInsufficientRewardsUnclaimed
}
c.RewardsUnclaimed = c.RewardsUnclaimed.Sub(amount)
c.DeductSubBucketRewardsByElysUnclaimed(amount)
return nil
}
// This is the function used when we withdraw the reward by program
func (c *Commitments) SubRewardsUnclaimedForEdenCommitted(amount sdk.Coin) error {
if c.RewardsUnclaimed.AmountOf(amount.Denom).LT(amount.Amount) {
return ErrInsufficientRewardsUnclaimed
}
c.RewardsUnclaimed = c.RewardsUnclaimed.Sub(amount)
c.DeductSubBucketRewardsByEdenUnclaimed(amount)
return nil
}
// This is the function used when we withdraw the reward by program
func (c *Commitments) SubRewardsUnclaimedForEdenBCommitted(amount sdk.Coin) error {
if c.RewardsUnclaimed.AmountOf(amount.Denom).LT(amount.Amount) {
return ErrInsufficientRewardsUnclaimed
}
c.RewardsUnclaimed = c.RewardsUnclaimed.Sub(amount)
c.DeductSubBucketRewardsByEdenBUnclaimed(amount)
return nil
}
// This is the function used when we withdraw the reward by program
func (c *Commitments) SubRewardsUnclaimedForUSDCDeposit(amount sdk.Coin) error {
if c.RewardsUnclaimed.AmountOf(amount.Denom).LT(amount.Amount) {
return ErrInsufficientRewardsUnclaimed
}
c.RewardsUnclaimed = c.RewardsUnclaimed.Sub(amount)
c.DeductSubBucketRewardsByUsdcUnclaimed(amount)
return nil
}
func (c *Commitments) DeductSubBucketRewardsByElysUnclaimed(amount sdk.Coin) (bool, sdk.Coin) {
amountToDeduct := amount
availableToDeduct := c.RewardsByElysUnclaimed.AmountOf(amount.Denom)
if availableToDeduct.LT(amountToDeduct.Amount) {
amountToDeduct = sdk.NewCoin(amount.Denom, availableToDeduct)
}
c.RewardsByElysUnclaimed = c.RewardsByElysUnclaimed.Sub(amountToDeduct)
remainedAmount := amount.Sub(amountToDeduct)
if remainedAmount.Amount.LTE(sdk.ZeroInt()) {
return true, remainedAmount
}
return false, remainedAmount
}
func (c *Commitments) DeductSubBucketRewardsByEdenUnclaimed(amount sdk.Coin) (bool, sdk.Coin) {
amountToDeduct := amount
availableToDeduct := c.RewardsByEdenUnclaimed.AmountOf(amount.Denom)
if availableToDeduct.LT(amountToDeduct.Amount) {
amountToDeduct = sdk.NewCoin(amount.Denom, availableToDeduct)
}
c.RewardsByEdenUnclaimed = c.RewardsByEdenUnclaimed.Sub(amountToDeduct)
remainedAmount := amount.Sub(amountToDeduct)
if remainedAmount.Amount.LTE(sdk.ZeroInt()) {
return true, remainedAmount
}
return false, remainedAmount
}
func (c *Commitments) DeductSubBucketRewardsByEdenBUnclaimed(amount sdk.Coin) (bool, sdk.Coin) {
amountToDeduct := amount
availableToDeduct := c.RewardsByEdenbUnclaimed.AmountOf(amount.Denom)
if availableToDeduct.LT(amountToDeduct.Amount) {
amountToDeduct = sdk.NewCoin(amount.Denom, availableToDeduct)
}
c.RewardsByEdenbUnclaimed = c.RewardsByEdenbUnclaimed.Sub(amountToDeduct)
remainedAmount := amount.Sub(amountToDeduct)
if remainedAmount.Amount.LTE(sdk.ZeroInt()) {
return true, remainedAmount
}
return false, remainedAmount
}
func (c *Commitments) DeductSubBucketRewardsByUsdcUnclaimed(amount sdk.Coin) (bool, sdk.Coin) {
amountToDeduct := amount
availableToDeduct := c.RewardsByUsdcUnclaimed.AmountOf(amount.Denom)
if availableToDeduct.LT(amountToDeduct.Amount) {
amountToDeduct = sdk.NewCoin(amount.Denom, availableToDeduct)
}
c.RewardsByUsdcUnclaimed = c.RewardsByUsdcUnclaimed.Sub(amountToDeduct)
remainedAmount := amount.Sub(amountToDeduct)
if remainedAmount.Amount.LTE(sdk.ZeroInt()) {
return true, remainedAmount
}
return false, remainedAmount
}
func (c *Commitments) GetClaimedForDenom(denom string) math.Int {
for _, token := range c.Claimed {
if token.Denom == denom {
return token.Amount
}
}
return sdk.ZeroInt()
}
func (c *Commitments) AddClaimed(amount sdk.Coin) {
c.Claimed = c.Claimed.Add(amount)
}
func (c *Commitments) SubClaimed(amount sdk.Coin) error {
if c.Claimed.AmountOf(amount.Denom).LT(amount.Amount) {
return ErrInsufficientClaimed
}
c.Claimed = c.Claimed.Sub(amount)
return nil
}