forked from crescent-network/crescent
/
liquidstaking.go
431 lines (377 loc) 路 17.8 KB
/
liquidstaking.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package keeper
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/jesse-pinkman-cre/crescent/x/liquidstaking/types"
)
func (k Keeper) LiquidBondDenom(ctx sdk.Context) (res string) {
k.paramSpace.Get(ctx, types.KeyLiquidBondDenom, &res)
return
}
// GetNetAmountState calculates the sum of bondedDenom balance, total delegation tokens(slash applied LiquidTokens), total remaining reward of types.LiquidStakingProxyAcc
// During liquid unstaking, btoken immediately burns and the unbonding queue belongs to the requester, so the liquid staker's unbonding values are excluded on netAmount
// It is used only for calculation and query and is not stored in kv.
func (k Keeper) GetNetAmountState(ctx sdk.Context) (nas types.NetAmountState) {
totalRemainingRewards, totalDelShares, totalLiquidTokens := k.CheckDelegationStates(ctx, types.LiquidStakingProxyAcc)
totalUnbondingBalance := sdk.ZeroInt()
ubds := k.stakingKeeper.GetAllUnbondingDelegations(ctx, types.LiquidStakingProxyAcc)
for _, ubd := range ubds {
for _, entry := range ubd.Entries {
// use Balance(slashing applied) not InitialBalance(without slashing)
totalUnbondingBalance = totalUnbondingBalance.Add(entry.Balance)
}
}
nas = types.NetAmountState{
BtokenTotalSupply: k.bankKeeper.GetSupply(ctx, k.LiquidBondDenom(ctx)).Amount,
TotalDelShares: totalDelShares,
TotalLiquidTokens: totalLiquidTokens,
TotalRemainingRewards: totalRemainingRewards,
TotalUnbondingBalance: totalUnbondingBalance,
ProxyAccBalance: k.GetProxyAccBalance(ctx, types.LiquidStakingProxyAcc).Amount,
}
nas.NetAmount = nas.CalcNetAmount()
nas.MintRate = nas.CalcMintRate()
return
}
// LiquidStake mints bToken worth of staking coin value according to NetAmount and performs LiquidDelegate.
func (k Keeper) LiquidStake(
ctx sdk.Context, proxyAcc, liquidStaker sdk.AccAddress, stakingCoin sdk.Coin) (newShares sdk.Dec, bTokenMintAmount sdk.Int, err error) {
params := k.GetParams(ctx)
// check minimum liquid staking amount
if stakingCoin.Amount.LT(params.MinLiquidStakingAmount) {
return sdk.ZeroDec(), sdk.ZeroInt(), types.ErrLessThanMinLiquidStakingAmount
}
// check bond denomination
bondDenom := k.stakingKeeper.BondDenom(ctx)
if stakingCoin.Denom != bondDenom {
return sdk.ZeroDec(), sdk.ZeroInt(), sdkerrors.Wrapf(
types.ErrInvalidBondDenom, "invalid coin denomination: got %s, expected %s", stakingCoin.Denom, bondDenom,
)
}
whitelistedValsMap := types.GetWhitelistedValsMap(params.WhitelistedValidators)
activeVals := k.GetActiveLiquidValidators(ctx, whitelistedValsMap)
if activeVals.Len() == 0 || !activeVals.TotalWeight(whitelistedValsMap).IsPositive() {
return sdk.ZeroDec(), sdk.ZeroInt(), types.ErrActiveLiquidValidatorsNotExists
}
// NetAmount must be calculated before send
nas := k.GetNetAmountState(ctx)
// send staking coin to liquid staking proxy account to proxy delegation, need sufficient spendable balances
err = k.bankKeeper.SendCoins(ctx, liquidStaker, proxyAcc, sdk.NewCoins(stakingCoin))
if err != nil {
return sdk.ZeroDec(), sdk.ZeroInt(), err
}
// mint btoken, MintAmount = TotalSupply * StakeAmount/NetAmount
liquidBondDenom := k.LiquidBondDenom(ctx)
bTokenMintAmount = stakingCoin.Amount
if nas.BtokenTotalSupply.IsPositive() {
bTokenMintAmount = types.NativeTokenToBToken(stakingCoin.Amount, nas.BtokenTotalSupply, nas.NetAmount)
}
if !bTokenMintAmount.IsPositive() {
return sdk.ZeroDec(), sdk.ZeroInt(), types.ErrTooSmallLiquidStakingAmount
}
// mint on module acc and send
mintCoin := sdk.NewCoins(sdk.NewCoin(liquidBondDenom, bTokenMintAmount))
err = k.bankKeeper.MintCoins(ctx, types.ModuleName, mintCoin)
if err != nil {
return sdk.ZeroDec(), bTokenMintAmount, err
}
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, liquidStaker, mintCoin)
if err != nil {
return sdk.ZeroDec(), bTokenMintAmount, err
}
newShares, err = k.LiquidDelegate(ctx, proxyAcc, activeVals, stakingCoin.Amount, whitelistedValsMap)
return newShares, bTokenMintAmount, err
}
// LiquidDelegate delegates staking amount to active validators by proxy account.
func (k Keeper) LiquidDelegate(ctx sdk.Context, proxyAcc sdk.AccAddress, activeVals types.ActiveLiquidValidators, stakingAmt sdk.Int, whitelistedValsMap types.WhitelistedValsMap) (newShares sdk.Dec, err error) {
totalNewShares := sdk.ZeroDec()
// crumb may occur due to a decimal point error in dividing the staking amount into the weight of liquid validators, It added on first active liquid validator
weightedAmt, crumb := types.DivideByWeight(activeVals, stakingAmt, whitelistedValsMap)
if len(weightedAmt) == 0 {
return sdk.ZeroDec(), types.ErrInvalidActiveLiquidValidators
}
weightedAmt[0] = weightedAmt[0].Add(crumb)
for i, val := range activeVals {
if !weightedAmt[i].IsPositive() {
continue
}
validator, _ := k.stakingKeeper.GetValidator(ctx, val.GetOperator())
newShares, err = k.stakingKeeper.Delegate(ctx, proxyAcc, weightedAmt[i], stakingtypes.Unbonded, validator, true)
if err != nil {
return sdk.ZeroDec(), err
}
totalNewShares = totalNewShares.Add(newShares)
}
return totalNewShares, nil
}
// LiquidUnstake burns unstakingBtoken and performs LiquidUnbond to active liquid validators with del shares worth of shares according to NetAmount with each validators current weight.
func (k Keeper) LiquidUnstake(
ctx sdk.Context, proxyAcc, liquidStaker sdk.AccAddress, unstakingBtoken sdk.Coin,
) (time.Time, sdk.Int, []stakingtypes.UnbondingDelegation, sdk.Int, error) {
// check bond denomination
params := k.GetParams(ctx)
liquidBondDenom := k.LiquidBondDenom(ctx)
if unstakingBtoken.Denom != liquidBondDenom {
return time.Time{}, sdk.ZeroInt(), []stakingtypes.UnbondingDelegation{}, sdk.ZeroInt(), sdkerrors.Wrapf(
types.ErrInvalidLiquidBondDenom, "invalid coin denomination: got %s, expected %s", unstakingBtoken.Denom, liquidBondDenom,
)
}
// Get NetAmount states
nas := k.GetNetAmountState(ctx)
if unstakingBtoken.Amount.GT(nas.BtokenTotalSupply) {
return time.Time{}, sdk.ZeroInt(), []stakingtypes.UnbondingDelegation{}, sdk.ZeroInt(), types.ErrInvalidBTokenSupply
}
// UnstakeAmount = NetAmount * BTokenAmount/TotalSupply * (1-UnstakeFeeRate)
unbondingAmount := types.BTokenToNativeToken(unstakingBtoken.Amount, nas.BtokenTotalSupply, nas.NetAmount)
unbondingAmount = types.DeductFeeRate(unbondingAmount, params.UnstakeFeeRate)
unbondingAmountInt := unbondingAmount.TruncateInt()
if !unbondingAmountInt.IsPositive() {
return time.Time{}, sdk.ZeroInt(), []stakingtypes.UnbondingDelegation{}, sdk.ZeroInt(), types.ErrTooSmallLiquidUnstakingAmount
}
// burn btoken
err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, liquidStaker, types.ModuleName, sdk.NewCoins(unstakingBtoken))
if err != nil {
return time.Time{}, sdk.ZeroInt(), []stakingtypes.UnbondingDelegation{}, sdk.ZeroInt(), err
}
err = k.bankKeeper.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(liquidBondDenom, unstakingBtoken.Amount)))
if err != nil {
return time.Time{}, sdk.ZeroInt(), []stakingtypes.UnbondingDelegation{}, sdk.ZeroInt(), err
}
liquidVals := k.GetAllLiquidValidators(ctx)
totalLiquidTokens, liquidTokenMap := liquidVals.TotalLiquidTokens(ctx, k.stakingKeeper, false)
// if no totalLiquidTokens, withdraw directly from balance of proxy acc
if !totalLiquidTokens.IsPositive() {
if nas.ProxyAccBalance.GTE(unbondingAmountInt) {
err = k.bankKeeper.SendCoins(ctx, types.LiquidStakingProxyAcc, liquidStaker,
sdk.NewCoins(sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), unbondingAmountInt)))
if err != nil {
return time.Time{}, sdk.ZeroInt(), []stakingtypes.UnbondingDelegation{}, sdk.ZeroInt(), err
} else {
return time.Time{}, sdk.ZeroInt(), []stakingtypes.UnbondingDelegation{}, unbondingAmountInt, nil
}
} else {
// error case where there is a quantity that are unbonding balance or remaining rewards that is not re-stake or withdrawn in netAmount.
return time.Time{}, sdk.ZeroInt(), []stakingtypes.UnbondingDelegation{}, sdk.ZeroInt(), types.ErrInsufficientProxyAccBalance
}
}
// fail when no liquid validators to unbond
if liquidVals.Len() == 0 {
return time.Time{}, sdk.ZeroInt(), []stakingtypes.UnbondingDelegation{}, sdk.ZeroInt(), types.ErrLiquidValidatorsNotExists
}
// crumb may occur due to a decimal error in dividing the unstaking bToken into the weight of liquid validators, it will remain in the NetAmount
unbondingAmounts, crumb := types.DivideByCurrentWeight(liquidVals, unbondingAmount, totalLiquidTokens, liquidTokenMap)
if !unbondingAmount.Sub(crumb).IsPositive() {
return time.Time{}, sdk.ZeroInt(), []stakingtypes.UnbondingDelegation{}, sdk.ZeroInt(), types.ErrTooSmallLiquidUnstakingAmount
}
totalReturnAmount := sdk.ZeroInt()
var ubdTime time.Time
var ubds []stakingtypes.UnbondingDelegation
for i, val := range liquidVals {
// skip zero weight liquid validator
if !unbondingAmounts[i].IsPositive() {
continue
}
var ubd stakingtypes.UnbondingDelegation
var returnAmount sdk.Int
var weightedShare sdk.Dec
// calculate delShares from tokens with validation
weightedShare, err = k.stakingKeeper.ValidateUnbondAmount(ctx, proxyAcc, val.GetOperator(), unbondingAmounts[i].TruncateInt())
if err != nil {
return time.Time{}, sdk.ZeroInt(), []stakingtypes.UnbondingDelegation{}, sdk.ZeroInt(), err
}
if !weightedShare.IsPositive() {
continue
}
// unbond with weightedShare
ubdTime, returnAmount, ubd, err = k.LiquidUnbond(ctx, proxyAcc, liquidStaker, val.GetOperator(), weightedShare, true)
if err != nil {
return time.Time{}, sdk.ZeroInt(), []stakingtypes.UnbondingDelegation{}, sdk.ZeroInt(), err
}
ubds = append(ubds, ubd)
totalReturnAmount = totalReturnAmount.Add(returnAmount)
}
return ubdTime, totalReturnAmount, ubds, sdk.ZeroInt(), nil
}
// LiquidUnbond unbond delegation shares to active validators by proxy account.
func (k Keeper) LiquidUnbond(
ctx sdk.Context, proxyAcc, liquidStaker sdk.AccAddress, valAddr sdk.ValAddress, shares sdk.Dec, checkMaxEntries bool,
) (time.Time, sdk.Int, stakingtypes.UnbondingDelegation, error) {
validator, found := k.stakingKeeper.GetValidator(ctx, valAddr)
if !found {
return time.Time{}, sdk.ZeroInt(), stakingtypes.UnbondingDelegation{}, stakingtypes.ErrNoDelegatorForAddress
}
// If checkMaxEntries is true, perform a maximum limit unbonding entries check.
if checkMaxEntries && k.stakingKeeper.HasMaxUnbondingDelegationEntries(ctx, liquidStaker, valAddr) {
return time.Time{}, sdk.ZeroInt(), stakingtypes.UnbondingDelegation{}, stakingtypes.ErrMaxUnbondingDelegationEntries
}
// unbond from proxy account
returnAmount, err := k.stakingKeeper.Unbond(ctx, proxyAcc, valAddr, shares)
if err != nil {
return time.Time{}, sdk.ZeroInt(), stakingtypes.UnbondingDelegation{}, err
}
// transfer the validator tokens to the not bonded pool
if validator.IsBonded() {
coins := sdk.NewCoins(sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), returnAmount))
if err = k.bankKeeper.SendCoinsFromModuleToModule(ctx, stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, coins); err != nil {
panic(err)
}
}
// Unbonding from proxy account, but queues to liquid staker.
completionTime := ctx.BlockHeader().Time.Add(k.stakingKeeper.UnbondingTime(ctx))
ubd := k.stakingKeeper.SetUnbondingDelegationEntry(ctx, liquidStaker, valAddr, ctx.BlockHeight(), completionTime, returnAmount)
k.stakingKeeper.InsertUBDQueue(ctx, ubd, completionTime)
return completionTime, returnAmount, ubd, nil
}
// CheckDelegationStates returns total remaining rewards, delshares, liquid tokens of delegations by proxy account
func (k Keeper) CheckDelegationStates(ctx sdk.Context, proxyAcc sdk.AccAddress) (sdk.Dec, sdk.Dec, sdk.Int) {
bondDenom := k.stakingKeeper.BondDenom(ctx)
totalRewards := sdk.ZeroDec()
totalDelShares := sdk.ZeroDec()
totalLiquidTokens := sdk.ZeroInt()
// Cache ctx for calculate rewards
cachedCtx, _ := ctx.CacheContext()
k.stakingKeeper.IterateDelegations(
cachedCtx, proxyAcc,
func(_ int64, del stakingtypes.DelegationI) (stop bool) {
valAddr := del.GetValidatorAddr()
val := k.stakingKeeper.Validator(cachedCtx, valAddr)
endingPeriod := k.distrKeeper.IncrementValidatorPeriod(cachedCtx, val)
delReward := k.distrKeeper.CalculateDelegationRewards(cachedCtx, val, del, endingPeriod)
delShares := del.GetShares()
if delShares.IsPositive() {
totalDelShares = totalDelShares.Add(delShares)
liquidTokens := val.TokensFromSharesTruncated(delShares).TruncateInt()
totalLiquidTokens = totalLiquidTokens.Add(liquidTokens)
totalRewards = totalRewards.Add(delReward.AmountOf(bondDenom))
}
return false
},
)
return totalRewards, totalDelShares, totalLiquidTokens
}
func (k Keeper) WithdrawLiquidRewards(ctx sdk.Context, proxyAcc sdk.AccAddress) sdk.Int {
totalRewards := sdk.ZeroInt()
bondDenom := k.stakingKeeper.BondDenom(ctx)
k.stakingKeeper.IterateDelegations(
ctx, proxyAcc,
func(_ int64, del stakingtypes.DelegationI) (stop bool) {
valAddr := del.GetValidatorAddr()
reward, err := k.distrKeeper.WithdrawDelegationRewards(ctx, proxyAcc, valAddr)
if err != nil {
panic(err)
}
totalRewards = totalRewards.Add(reward.AmountOf(bondDenom))
return false
},
)
return totalRewards
}
// GetLiquidValidator get a single liquid validator
func (k Keeper) GetLiquidValidator(ctx sdk.Context, addr sdk.ValAddress) (val types.LiquidValidator, found bool) {
store := ctx.KVStore(k.storeKey)
value := store.Get(types.GetLiquidValidatorKey(addr))
if value == nil {
return val, false
}
val = types.MustUnmarshalLiquidValidator(k.cdc, value)
return val, true
}
// SetLiquidValidator set the main record holding liquid validator details
func (k Keeper) SetLiquidValidator(ctx sdk.Context, val types.LiquidValidator) {
store := ctx.KVStore(k.storeKey)
bz := types.MustMarshalLiquidValidator(k.cdc, &val)
store.Set(types.GetLiquidValidatorKey(val.GetOperator()), bz)
}
// RemoveLiquidValidator remove a liquid validator on kv store
func (k Keeper) RemoveLiquidValidator(ctx sdk.Context, val types.LiquidValidator) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetLiquidValidatorKey(val.GetOperator()))
}
// GetAllLiquidValidators get the set of all liquid validators with no limits, used during genesis dump
func (k Keeper) GetAllLiquidValidators(ctx sdk.Context) (vals types.LiquidValidators) {
store := ctx.KVStore(k.storeKey)
vals = types.LiquidValidators{}
iterator := sdk.KVStorePrefixIterator(store, types.LiquidValidatorsKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
val := types.MustUnmarshalLiquidValidator(k.cdc, iterator.Value())
vals = append(vals, val)
}
return vals
}
// GetActiveLiquidValidators get the set of active liquid validators.
func (k Keeper) GetActiveLiquidValidators(ctx sdk.Context, whitelistedValsMap types.WhitelistedValsMap) (vals types.ActiveLiquidValidators) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.LiquidValidatorsKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
val := types.MustUnmarshalLiquidValidator(k.cdc, iterator.Value())
if k.IsActiveLiquidValidator(ctx, val, whitelistedValsMap) {
vals = append(vals, val)
}
}
return vals
}
func (k Keeper) GetAllLiquidValidatorStates(ctx sdk.Context) (liquidValidatorStates []types.LiquidValidatorState) {
lvs := k.GetAllLiquidValidators(ctx)
whitelistedValsMap := k.GetParams(ctx).WhitelistedValsMap()
for _, lv := range lvs {
active := k.IsActiveLiquidValidator(ctx, lv, whitelistedValsMap)
lvState := types.LiquidValidatorState{
OperatorAddress: lv.OperatorAddress,
Weight: lv.GetWeight(whitelistedValsMap, active),
Status: lv.GetStatus(active),
DelShares: lv.GetDelShares(ctx, k.stakingKeeper),
LiquidTokens: lv.GetLiquidTokens(ctx, k.stakingKeeper, false),
}
liquidValidatorStates = append(liquidValidatorStates, lvState)
}
return
}
func (k Keeper) GetLiquidValidatorState(ctx sdk.Context, addr sdk.ValAddress) (liquidValidatorState types.LiquidValidatorState, found bool) {
lv, found := k.GetLiquidValidator(ctx, addr)
if !found {
return types.LiquidValidatorState{
OperatorAddress: addr.String(),
Weight: sdk.ZeroInt(),
Status: types.ValidatorStatusUnspecified,
DelShares: sdk.ZeroDec(),
LiquidTokens: sdk.ZeroInt(),
}, false
}
whitelistedValsMap := k.GetParams(ctx).WhitelistedValsMap()
active := k.IsActiveLiquidValidator(ctx, lv, whitelistedValsMap)
return types.LiquidValidatorState{
OperatorAddress: lv.OperatorAddress,
Weight: lv.GetWeight(whitelistedValsMap, active),
Status: lv.GetStatus(active),
DelShares: lv.GetDelShares(ctx, k.stakingKeeper),
LiquidTokens: lv.GetLiquidTokens(ctx, k.stakingKeeper, false),
}, true
}
func (k Keeper) IsActiveLiquidValidator(ctx sdk.Context, lv types.LiquidValidator, whitelistedValsMap types.WhitelistedValsMap) bool {
val, found := k.stakingKeeper.GetValidator(ctx, lv.GetOperator())
if !found {
return false
}
return types.ActiveCondition(val, whitelistedValsMap.IsListed(lv.OperatorAddress), k.IsTombstoned(ctx, val))
}
func (k Keeper) IsTombstoned(ctx sdk.Context, val stakingtypes.Validator) bool {
consPk, err := val.ConsPubKey()
if err != nil {
return false
}
return k.slashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(consPk.Address()))
}
func (k Keeper) GetWeightMap(ctx sdk.Context, liquidVals types.LiquidValidators, whitelistedValsMap types.WhitelistedValsMap) (map[string]sdk.Int, sdk.Int) {
weightMap := map[string]sdk.Int{}
totalWeight := sdk.ZeroInt()
for _, val := range liquidVals {
weight := val.GetWeight(whitelistedValsMap, k.IsActiveLiquidValidator(ctx, val, whitelistedValsMap))
totalWeight = totalWeight.Add(weight)
weightMap[val.OperatorAddress] = weight
}
return weightMap, totalWeight
}