forked from osmosis-labs/osmosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stake.go
427 lines (369 loc) · 15 KB
/
stake.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
package keeper
import (
"fmt"
"github.com/MonOsmosis/osmosis/v10/osmoutils"
lockuptypes "github.com/MonOsmosis/osmosis/v10/x/lockup/types"
"github.com/MonOsmosis/osmosis/v10/x/superfluid/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
func (k Keeper) GetTotalSyntheticAssetsLocked(ctx sdk.Context, denom string) sdk.Int {
return k.lk.GetPeriodLocksAccumulation(ctx, lockuptypes.QueryCondition{
LockQueryType: lockuptypes.ByDuration,
Denom: denom,
Duration: k.sk.UnbondingTime(ctx),
})
}
func (k Keeper) GetExpectedDelegationAmount(ctx sdk.Context, acc types.SuperfluidIntermediaryAccount) sdk.Int {
// Get total number of Osmo this account should have delegated after refresh
// (1) Find how many tokens total T are locked for (denom, validator) pair
totalSuperfluidDelegation := k.GetTotalSyntheticAssetsLocked(ctx, stakingSyntheticDenom(acc.Denom, acc.ValAddr))
// (2) Multiply the T tokens, by the number of superfluid osmo per token, to get the total amount
// of osmo we expect.
refreshedAmount := k.GetSuperfluidOSMOTokens(ctx, acc.Denom, totalSuperfluidDelegation)
return refreshedAmount
}
func (k Keeper) RefreshIntermediaryDelegationAmounts(ctx sdk.Context) {
// iterate over every (denom, validator) pair
accs := k.GetAllIntermediaryAccounts(ctx)
for _, acc := range accs {
mAddr := acc.GetAccAddress()
valAddress, err := sdk.ValAddressFromBech32(acc.ValAddr)
if err != nil {
panic(err)
}
validator, found := k.sk.GetValidator(ctx, valAddress)
if !found {
k.Logger(ctx).Error(fmt.Sprintf("validator not found or %s", acc.ValAddr))
continue
}
currentAmount := sdk.NewInt(0)
delegation, found := k.sk.GetDelegation(ctx, mAddr, valAddress)
if !found {
// continue if current delegation is 0, in case its really a dust delegation
// that becomes worth something after refresh.
k.Logger(ctx).Info(fmt.Sprintf("Existing delegation not found for %s with %s during superfluid refresh."+
" It may have been previously bonded, but now unbonded.", mAddr.String(), acc.ValAddr))
} else {
// TODO: Be consistent withn TokensFromShares vs ValidateFromUnbondAmount
currentAmount = validator.TokensFromShares(delegation.Shares).RoundInt()
}
refreshedAmount := k.GetExpectedDelegationAmount(ctx, acc)
if refreshedAmount.GT(currentAmount) {
adjustment := refreshedAmount.Sub(currentAmount)
err = k.mintOsmoTokensAndDelegate(ctx, adjustment, acc)
if err != nil {
ctx.Logger().Error("Error in forceUndelegateAndBurnOsmoTokens, state update reverted", err)
}
} else if currentAmount.GT(refreshedAmount) {
// In this case, we want to change the IA's delegated balance to be refreshed Amount
// which is less than what it already has.
// This means we need to "InstantUndelegate" some of its delegation (not going through the unbonding queue)
// and then burn that excessly delegated bits.
adjustment := currentAmount.Sub(refreshedAmount)
err := k.forceUndelegateAndBurnOsmoTokens(ctx, adjustment, acc)
if err != nil {
ctx.Logger().Error("Error in forceUndelegateAndBurnOsmoTokens, state update reverted", err)
}
} else {
ctx.Logger().Info("Intermediary account already has correct delegation amount?" +
" This with high probability implies the exact same spot price as the last epoch," +
"and no delegation changes.")
}
}
}
func (k Keeper) IncreaseSuperfluidDelegation(ctx sdk.Context, lockID uint64, amount sdk.Coins) error {
acc, found := k.GetIntermediaryAccountFromLockId(ctx, lockID)
if !found {
return nil
}
// mint OSMO token based on TWAP of locked denom to denom module account
osmoAmt := k.GetSuperfluidOSMOTokens(ctx, acc.Denom, amount.AmountOf(acc.Denom))
if osmoAmt.IsZero() {
return nil
}
err := k.mintOsmoTokensAndDelegate(ctx, osmoAmt, acc)
if err != nil {
return err
}
return nil
}
// basic validation for locks, that sender is correct, and that the lock length is correct.
func (k Keeper) validateLockForSF(ctx sdk.Context, lock *lockuptypes.PeriodLock, sender string) error {
if lock.Owner != sender {
return lockuptypes.ErrNotLockOwner
}
if lock.Coins.Len() != 1 {
return types.ErrMultipleCoinsLockupNotSupported
}
return nil
}
func (k Keeper) validateLockForSFDelegate(ctx sdk.Context, lock *lockuptypes.PeriodLock, sender string) error {
err := k.validateLockForSF(ctx, lock, sender)
if err != nil {
return err
}
defaultSuperfluidAsset := types.SuperfluidAsset{}
if k.GetSuperfluidAsset(ctx, lock.Coins[0].Denom) == defaultSuperfluidAsset {
return types.ErrNonSuperfluidAsset
}
// prevent unbonding lockups to be not able to be used for superfluid staking
if lock.IsUnlocking() {
return types.ErrUnbondingLockupNotSupported
}
// ensure that lock duration >= staking.UnbondingTime
if lock.Duration < k.sk.GetParams(ctx).UnbondingTime {
return types.ErrNotEnoughLockupDuration
}
// Thus when we stake now, this will be the only superfluid position for this lockID.
if k.alreadySuperfluidStaking(ctx, lock.ID) {
return types.ErrAlreadyUsedSuperfluidLockup
}
return nil
}
// ensure the valAddr is correctly formatted & corresponds to a real validator on chain.
func (k Keeper) validateValAddrForDelegate(ctx sdk.Context, valAddr string) (stakingtypes.Validator, error) {
valAddress, err := sdk.ValAddressFromBech32(valAddr)
if err != nil {
return stakingtypes.Validator{}, err
}
validator, found := k.sk.GetValidator(ctx, valAddress)
if !found {
return stakingtypes.Validator{}, stakingtypes.ErrNoValidatorFound
}
return validator, nil
}
// TODO: Merge a lot of logic with IncreaseSuperfluidDelegation.
func (k Keeper) SuperfluidDelegate(ctx sdk.Context, sender string, lockID uint64, valAddr string) error {
lock, err := k.lk.GetLockByID(ctx, lockID)
if err != nil {
return err
}
// This guarantees the lockID does not already have a superfluid stake position
// associated with it, the lock is sufficiently long, the lock only locks one asset, etc.
// Thus when we stake this lock, it will be the only superfluid position for this lockID.
err = k.validateLockForSFDelegate(ctx, lock, sender)
if err != nil {
return err
}
lockedCoin := lock.Coins[0]
// get the intermediate account for this (denom, validator) pair.
// This account tracks the amount of osmo being considered as staked.
// If an intermediary account doesn't exist, then create it + a perpetual gauge.
acc, err := k.GetOrCreateIntermediaryAccount(ctx, lockedCoin.Denom, valAddr)
if err != nil {
return err
}
// create connection record between lock id and intermediary account
k.SetLockIdIntermediaryAccountConnection(ctx, lockID, acc)
// Register a synthetic lockup for superfluid staking
err = k.createSyntheticLockup(ctx, lockID, acc, bondedStatus)
if err != nil {
return err
}
// Find how many new osmo tokens this delegation is worth at superfluids current risk adjustment
// and twap of the denom.
amount := k.GetSuperfluidOSMOTokens(ctx, acc.Denom, lockedCoin.Amount)
if amount.IsZero() {
return types.ErrOsmoEquivalentZeroNotAllowed
}
return k.mintOsmoTokensAndDelegate(ctx, amount, acc)
}
func (k Keeper) SuperfluidUndelegate(ctx sdk.Context, sender string, lockID uint64) error {
lock, err := k.lk.GetLockByID(ctx, lockID)
if err != nil {
return err
}
err = k.validateLockForSF(ctx, lock, sender)
if err != nil {
return err
}
lockedCoin := lock.Coins[0]
// get the intermediate acct asscd. with lock id, and delete the connection.
intermediaryAcc, found := k.GetIntermediaryAccountFromLockId(ctx, lockID)
if !found {
return types.ErrNotSuperfluidUsedLockup
}
k.DeleteLockIdIntermediaryAccountConnection(ctx, lockID)
// Delete the old synthetic lockup, and create a new synthetic lockup representing the unstaking
synthdenom := stakingSyntheticDenom(lockedCoin.Denom, intermediaryAcc.ValAddr)
err = k.lk.DeleteSyntheticLockup(ctx, lockID, synthdenom)
if err != nil {
return err
}
// undelegate this lock's delegation amount, and burn the minted osmo.
amount := k.GetSuperfluidOSMOTokens(ctx, intermediaryAcc.Denom, lockedCoin.Amount)
err = k.forceUndelegateAndBurnOsmoTokens(ctx, amount, intermediaryAcc)
if err != nil {
return err
}
// Create a new synthetic lockup representing the unstaking side.
return k.createSyntheticLockup(ctx, lockID, intermediaryAcc, unlockingStatus)
}
func (k Keeper) SuperfluidUnbondLock(ctx sdk.Context, underlyingLockId uint64, sender string) error {
lock, err := k.lk.GetLockByID(ctx, underlyingLockId)
if err != nil {
return err
}
err = k.validateLockForSF(ctx, lock, sender)
if err != nil {
return err
}
synthLocks := k.lk.GetAllSyntheticLockupsByLockup(ctx, underlyingLockId)
if len(synthLocks) != 1 {
return types.ErrNotSuperfluidUsedLockup
}
if !synthLocks[0].IsUnlocking() {
return types.ErrBondingLockupNotSupported
}
return k.lk.BeginForceUnlock(ctx, underlyingLockId, sdk.Coins{})
}
func (k Keeper) alreadySuperfluidStaking(ctx sdk.Context, lockID uint64) bool {
// We need to catch two cases:
// (1) lockID has another superfluid bond
// (2) lockID has a superfluid unbonding
// we check (1) by looking for presence of an intermediary account lock ID connection
// we check (2) (and re-check 1 for suredness) by looking for the existence of
// synthetic locks for this.
intermediaryAccAddr := k.GetLockIdIntermediaryAccountConnection(ctx, lockID)
if !intermediaryAccAddr.Empty() {
return true
}
synthLocks := k.lk.GetAllSyntheticLockupsByLockup(ctx, lockID)
return len(synthLocks) > 0
}
// mint osmoAmount of OSMO tokens, and immediately delegate them to validator on behalf of intermediary account.
func (k Keeper) mintOsmoTokensAndDelegate(ctx sdk.Context, osmoAmount sdk.Int, intermediaryAccount types.SuperfluidIntermediaryAccount) error {
validator, err := k.validateValAddrForDelegate(ctx, intermediaryAccount.ValAddr)
if err != nil {
return err
}
err = osmoutils.ApplyFuncIfNoError(ctx, func(cacheCtx sdk.Context) error {
bondDenom := k.sk.BondDenom(cacheCtx)
coins := sdk.Coins{sdk.NewCoin(bondDenom, osmoAmount)}
err = k.bk.MintCoins(cacheCtx, types.ModuleName, coins)
if err != nil {
return err
}
k.bk.AddSupplyOffset(cacheCtx, bondDenom, osmoAmount.Neg())
err = k.bk.SendCoinsFromModuleToAccount(cacheCtx, types.ModuleName, intermediaryAccount.GetAccAddress(), coins)
if err != nil {
return err
}
// make delegation from module account to the validator
// TODO: What happens here if validator is jailed, tombstoned, or unbonding
// For now, we don't worry since worst case it errors, in which case we revert mint.
_, err = k.sk.Delegate(cacheCtx,
intermediaryAccount.GetAccAddress(),
osmoAmount, stakingtypes.Unbonded, validator, true)
return err
})
return err
}
// force undelegate osmoAmount worth of delegation shares from delegations between intermediary account and valAddr
// We take the returned tokens, and then immediately burn them.
func (k Keeper) forceUndelegateAndBurnOsmoTokens(ctx sdk.Context,
osmoAmount sdk.Int, intermediaryAcc types.SuperfluidIntermediaryAccount,
) error {
valAddr, err := sdk.ValAddressFromBech32(intermediaryAcc.ValAddr)
if err != nil {
return err
}
// TODO: Better understand and decide between ValidateUnbondAmount and SharesFromTokens
// briefly looked into it, did not understand whats correct.
// TODO: ensure that intermediate account has at least osmoAmount staked.
shares, err := k.sk.ValidateUnbondAmount(
ctx, intermediaryAcc.GetAccAddress(), valAddr, osmoAmount,
)
if err == stakingtypes.ErrNoDelegation {
return nil
} else if err != nil {
return err
}
err = osmoutils.ApplyFuncIfNoError(ctx, func(cacheCtx sdk.Context) error {
undelegatedCoins, err := k.sk.InstantUndelegate(cacheCtx, intermediaryAcc.GetAccAddress(), valAddr, shares)
if err != nil {
return err
}
// TODO: Should we compare undelegatedCoins vs osmoAmount?
err = k.bk.SendCoinsFromAccountToModule(cacheCtx, intermediaryAcc.GetAccAddress(), types.ModuleName, undelegatedCoins)
if err != nil {
return err
}
err = k.bk.BurnCoins(cacheCtx, types.ModuleName, undelegatedCoins)
if err != nil {
return err
}
bondDenom := k.sk.BondDenom(cacheCtx)
k.bk.AddSupplyOffset(cacheCtx, bondDenom, undelegatedCoins.AmountOf(bondDenom))
return err
})
return err
}
// TODO: Need to (eventually) override the existing staking messages and queries, for undelegating, delegating, rewards, and redelegating, to all be going through all superfluid module.
// Want integrators to be able to use the same staking queries and messages
// Eugen’s point: Only rewards message needs to be updated. Rest of messages are fine
// Queries need to be updated
// We can do this at the very end though, since it just relates to queries.
// IterateBondedValidatorsByPower implements govtypes.StakingKeeper
func (k Keeper) IterateBondedValidatorsByPower(ctx sdk.Context, fn func(int64, stakingtypes.ValidatorI) bool) {
k.sk.IterateBondedValidatorsByPower(ctx, fn)
}
// TotalBondedTokens implements govtypes.StakingKeeper
func (k Keeper) TotalBondedTokens(ctx sdk.Context) sdk.Int {
return k.sk.TotalBondedTokens(ctx)
}
// IterateDelegations implements govtypes.StakingKeeper
// Iterates through staking keeper's delegations, and then all of the superfluid delegations.
func (k Keeper) IterateDelegations(ctx sdk.Context, delegator sdk.AccAddress, fn func(int64, stakingtypes.DelegationI) bool) {
// call the callback with the non-superfluid delegations
var index int64
k.sk.IterateDelegations(ctx, delegator, func(i int64, delegation stakingtypes.DelegationI) (stop bool) {
index = i
return fn(i, delegation)
})
synthlocks := k.lk.GetAllSyntheticLockupsByAddr(ctx, delegator)
for i, lock := range synthlocks {
// get locked coin from the lock ID
interim, ok := k.GetIntermediaryAccountFromLockId(ctx, lock.UnderlyingLockId)
if !ok {
continue
}
lock, err := k.lk.GetLockByID(ctx, lock.UnderlyingLockId)
if err != nil {
ctx.Logger().Error("lockup retrieval failed with underlying lock", "Lock", lock, "Error", err)
continue
}
coin, err := lock.SingleCoin()
if err != nil {
ctx.Logger().Error("lock fails to meet expected invariant, it contains multiple coins", "Lock", lock, "Error", err)
continue
}
// get osmo-equivalent token amount
amount := k.GetSuperfluidOSMOTokens(ctx, interim.Denom, coin.Amount)
// get validator shares equivalent to the token amount
valAddr, err := sdk.ValAddressFromBech32(interim.ValAddr)
if err != nil {
ctx.Logger().Error("failed to decode validator address", "Intermediary", interim.ValAddr, "LockID", lock.ID, "Error", err)
continue
}
validator, found := k.sk.GetValidator(ctx, valAddr)
if !found {
ctx.Logger().Error("validator does not exist for lock", "Validator", valAddr, "LockID", lock.ID)
continue
}
shares, err := validator.SharesFromTokens(amount)
if err != nil {
// tokens are not valid. continue.
continue
}
// construct delegation and call callback
delegation := stakingtypes.Delegation{
DelegatorAddress: delegator.String(),
ValidatorAddress: interim.ValAddr,
Shares: shares,
}
// if valid delegation has been found, increment delegation index
fn(index+int64(i), delegation)
}
}