-
Notifications
You must be signed in to change notification settings - Fork 11
/
rebalancing.go
278 lines (242 loc) · 9.81 KB
/
rebalancing.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
package keeper
import (
"fmt"
"strconv"
"time"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/persistenceOne/pstake-native/v2/x/liquidstake/types"
)
func (k Keeper) GetProxyAccBalance(ctx sdk.Context, proxyAcc sdk.AccAddress) (balance sdk.Coin) {
bondDenom := k.stakingKeeper.BondDenom(ctx)
return sdk.NewCoin(bondDenom, k.bankKeeper.SpendableCoins(ctx, proxyAcc).AmountOf(bondDenom))
}
// TryRedelegation attempts redelegation, which is applied only when successful through cached context because there is a constraint that fails if already receiving redelegation.
func (k Keeper) TryRedelegation(ctx sdk.Context, re types.Redelegation) (completionTime time.Time, err error) {
dstVal := re.DstValidator.GetOperator()
srcVal := re.SrcValidator.GetOperator()
// check the source validator already has receiving transitive redelegation
hasReceiving := k.stakingKeeper.HasReceivingRedelegation(ctx, re.Delegator, srcVal)
if hasReceiving {
return time.Time{}, stakingtypes.ErrTransitiveRedelegation
}
// calculate delShares from tokens with validation
_, err = k.stakingKeeper.ValidateUnbondAmount(
ctx, re.Delegator, srcVal, re.Amount,
)
if err != nil {
return time.Time{}, fmt.Errorf("failed to validate unbond amount: %w", err)
}
// when last, full redelegation of shares from delegation
amt := re.Amount
if re.Last {
amt = re.SrcValidator.GetLiquidTokens(ctx, k.stakingKeeper, false)
}
cachedCtx, writeCache := ctx.CacheContext()
completionTime, err = k.RedelegateWithCap(cachedCtx, re.Delegator, srcVal, dstVal, amt)
if err != nil {
return time.Time{}, fmt.Errorf("failed to begin redelegation: %w", err)
}
writeCache()
return completionTime, nil
}
// Rebalance argument liquidVals containing ValidatorStatusActive which is containing just added on whitelist(liquidToken 0) and ValidatorStatusInactive to delist
func (k Keeper) Rebalance(
ctx sdk.Context,
proxyAcc sdk.AccAddress,
liquidVals types.LiquidValidators,
whitelistedValsMap types.WhitelistedValsMap,
rebalancingTrigger math.LegacyDec,
) (redelegations []types.Redelegation) {
totalLiquidTokens, liquidTokenMap := liquidVals.TotalLiquidTokens(ctx, k.stakingKeeper, false)
if !totalLiquidTokens.IsPositive() {
return redelegations
}
weightMap, totalWeight := k.GetWeightMap(ctx, liquidVals, whitelistedValsMap)
// no active liquid validators
if !totalWeight.IsPositive() {
return redelegations
}
// calculate rebalancing target map
targetMap := map[string]math.Int{}
totalTargetMap := sdk.ZeroInt()
for _, val := range liquidVals {
targetMap[val.OperatorAddress] = totalLiquidTokens.Mul(weightMap[val.OperatorAddress]).Quo(totalWeight)
totalTargetMap = totalTargetMap.Add(targetMap[val.OperatorAddress])
}
crumb := totalLiquidTokens.Sub(totalTargetMap)
if !totalTargetMap.IsPositive() {
return redelegations
}
// crumb to first non zero liquid validator
for _, val := range liquidVals {
if targetMap[val.OperatorAddress].IsPositive() {
targetMap[val.OperatorAddress] = targetMap[val.OperatorAddress].Add(crumb)
break
}
}
failCount := 0
rebalancingThresholdAmt := rebalancingTrigger.Mul(math.LegacyNewDecFromInt(totalLiquidTokens)).TruncateInt()
redelegations = make([]types.Redelegation, 0, liquidVals.Len())
for i := 0; i < liquidVals.Len(); i++ {
// get min, max of liquid token gap
minVal, maxVal, amountNeeded, last := liquidVals.MinMaxGap(targetMap, liquidTokenMap)
if amountNeeded.IsZero() || (i == 0 && !amountNeeded.GT(rebalancingThresholdAmt)) {
break
}
// sync liquidTokenMap applied rebalancing
liquidTokenMap[maxVal.OperatorAddress] = liquidTokenMap[maxVal.OperatorAddress].Sub(amountNeeded)
liquidTokenMap[minVal.OperatorAddress] = liquidTokenMap[minVal.OperatorAddress].Add(amountNeeded)
// try redelegation from max validator to min validator
redelegation := types.Redelegation{
Delegator: proxyAcc,
SrcValidator: maxVal,
DstValidator: minVal,
Amount: amountNeeded,
Last: last,
}
_, err := k.TryRedelegation(ctx, redelegation)
if err != nil {
redelegation.Error = err
failCount++
k.Logger(ctx).Info(
"redelegation failed",
types.DelegatorKeyVal, proxyAcc.String(),
types.SrcValidatorKeyVal, maxVal.OperatorAddress,
types.DstValidatorKeyVal, minVal.OperatorAddress,
types.AmountKeyVal, amountNeeded.String(),
types.ErrorKeyVal, err.Error(),
)
}
redelegations = append(redelegations, redelegation)
}
if len(redelegations) != 0 {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeBeginRebalancing,
sdk.NewAttribute(types.AttributeKeyDelegator, types.LiquidStakeProxyAcc.String()),
sdk.NewAttribute(types.AttributeKeyRedelegationCount, strconv.Itoa(len(redelegations))),
sdk.NewAttribute(types.AttributeKeyRedelegationFailCount, strconv.Itoa(failCount)),
),
})
k.Logger(ctx).Info(types.EventTypeBeginRebalancing,
types.AttributeKeyDelegator, types.LiquidStakeProxyAcc.String(),
types.AttributeKeyRedelegationCount, strconv.Itoa(len(redelegations)),
types.AttributeKeyRedelegationFailCount, strconv.Itoa(failCount))
}
return redelegations
}
func (k Keeper) UpdateLiquidValidatorSet(ctx sdk.Context, redelegate bool) (redelegations []types.Redelegation) {
params := k.GetParams(ctx)
liquidValidators := k.GetAllLiquidValidators(ctx)
liquidValsMap := liquidValidators.Map()
whitelistedValsMap := types.GetWhitelistedValsMap(params.WhitelistedValidators)
// Set Liquid validators for added whitelist validators
for _, wv := range params.WhitelistedValidators {
if _, ok := liquidValsMap[wv.ValidatorAddress]; !ok {
lv := types.LiquidValidator{
OperatorAddress: wv.ValidatorAddress,
}
if k.IsActiveLiquidValidator(ctx, lv, whitelistedValsMap) {
k.SetLiquidValidator(ctx, lv)
liquidValidators = append(liquidValidators, lv)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAddLiquidValidator,
sdk.NewAttribute(types.AttributeKeyLiquidValidator, lv.OperatorAddress),
),
})
k.Logger(ctx).Info(types.EventTypeAddLiquidValidator, types.AttributeKeyLiquidValidator, lv.OperatorAddress)
}
}
}
// rebalancing based updated liquid validators status with threshold, try by cachedCtx
// tombstone status also handled on Rebalance
if redelegate {
redelegations = k.Rebalance(
ctx,
types.LiquidStakeProxyAcc,
liquidValidators,
whitelistedValsMap,
types.RebalancingTrigger,
)
// if there are inactive liquid validators, do not unbond,
// instead let validator selection and rebalancing take care of it.
return redelegations
}
return nil
}
// AutocompoundStakingRewards withdraws staking rewards and re-stakes when over threshold.
func (k Keeper) AutocompoundStakingRewards(ctx sdk.Context, whitelistedValsMap types.WhitelistedValsMap) {
// withdraw rewards of LiquidStakeProxyAcc
k.WithdrawLiquidRewards(ctx, types.LiquidStakeProxyAcc)
// skip when no active liquid validator
activeVals := k.GetActiveLiquidValidators(ctx, whitelistedValsMap)
if len(activeVals) == 0 {
return
}
// get all the APY components
bondDenom := k.stakingKeeper.BondDenom(ctx)
totalSupply := k.bankKeeper.GetSupply(ctx, bondDenom).Amount
bondedTokens := k.bankKeeper.GetBalance(ctx, k.stakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount
inflation := k.mintKeeper.GetMinter(ctx).Inflation
// calculate the hourly APY
bondRatio := math.LegacyDec(bondedTokens).Quo(math.LegacyDec(totalSupply))
hourlyApy := inflation.Quo(bondRatio).
Quo(types.DefaultLimitAutocompoundPeriodDays).
Quo(types.DefaultLimitAutocompoundPeriodHours)
// calculate autocompoundable amount by limiting the current net amount with the calculated APY
autoCompoundableAmount := k.GetNetAmountState(ctx).NetAmount.Mul(hourlyApy).TruncateInt()
// use the calculated autocompoundable amount as the limit for the transfer
proxyAccBalance := k.GetProxyAccBalance(ctx, types.LiquidStakeProxyAcc)
if proxyAccBalance.Amount.LT(autoCompoundableAmount) {
autoCompoundableAmount = proxyAccBalance.Amount
}
// calculate autocompounding fee
params := k.GetParams(ctx)
autocompoundFee := sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), math.ZeroInt())
if !params.AutocompoundFeeRate.IsZero() && autoCompoundableAmount.IsPositive() {
autocompoundFee = sdk.NewCoin(
k.stakingKeeper.BondDenom(ctx),
params.AutocompoundFeeRate.MulInt(autoCompoundableAmount).TruncateInt(),
)
}
// re-staking of the accumulated rewards
cachedCtx, writeCache := ctx.CacheContext()
delegableAmount := autoCompoundableAmount.Sub(autocompoundFee.Amount)
err := k.LiquidDelegate(cachedCtx, types.LiquidStakeProxyAcc, activeVals, delegableAmount, whitelistedValsMap)
if err != nil {
k.Logger(ctx).Error(
"failed to re-stake the accumulated rewards",
types.ErrorKeyVal,
err,
)
return
// skip errors as they might occur due to reaching global liquid cap
}
writeCache()
// move autocompounding fee from the balance to fee account
feeAccountAddr := sdk.MustAccAddressFromBech32(params.FeeAccountAddress)
err = k.bankKeeper.SendCoins(ctx, types.LiquidStakeProxyAcc, feeAccountAddr, sdk.NewCoins(autocompoundFee))
if err != nil {
k.Logger(ctx).Error(
"failed to send autocompound fee to fee account",
types.ErrorKeyVal,
err,
)
return
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAutocompound,
sdk.NewAttribute(types.AttributeKeyDelegator, types.LiquidStakeProxyAcc.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, delegableAmount.String()),
sdk.NewAttribute(types.AttributeKeyAutocompoundFee, autocompoundFee.String()),
),
})
k.Logger(ctx).Info(types.EventTypeAutocompound,
types.AttributeKeyDelegator, types.LiquidStakeProxyAcc.String(),
sdk.AttributeKeyAmount, delegableAmount.String(),
types.AttributeKeyAutocompoundFee, autocompoundFee.String())
}