/
keeper.go
420 lines (354 loc) · 13.3 KB
/
keeper.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
package keeper
import (
"fmt"
"cosmossdk.io/math"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/gogoproto/proto"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
ibclocalhosttypes "github.com/cosmos/ibc-go/v7/modules/light-clients/09-localhost"
"github.com/persistenceOne/pstake-native/v2/x/liquidstakeibc/types"
)
type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
epochsKeeper types.EpochsKeeper
icaControllerKeeper types.ICAControllerKeeper
ibcKeeper *ibckeeper.Keeper
ibcTransferKeeper types.IBCTransferKeeper
icqKeeper types.ICQKeeper
paramSpace paramtypes.Subspace
msgRouter *baseapp.MsgServiceRouter
hooks types.LiquidStakeIBCHooks
authority string
}
func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
epochsKeeper types.EpochsKeeper,
icaControllerKeeper types.ICAControllerKeeper,
ibcKeeper *ibckeeper.Keeper,
ibcTransferKeeper types.IBCTransferKeeper,
icqKeeper types.ICQKeeper,
paramSpace paramtypes.Subspace,
msgRouter *baseapp.MsgServiceRouter,
authority string,
) Keeper {
return Keeper{
cdc: cdc,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
epochsKeeper: epochsKeeper,
icaControllerKeeper: icaControllerKeeper,
ibcKeeper: ibcKeeper,
ibcTransferKeeper: ibcTransferKeeper,
icqKeeper: icqKeeper,
storeKey: storeKey,
paramSpace: paramSpace,
msgRouter: msgRouter,
hooks: nil,
authority: authority,
}
}
// Logger returns a module-specific logger.
func (k *Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// GetParams gets the total set of liquidstakeibc parameters.
func (k *Keeper) GetParams(ctx sdk.Context) (params types.Params) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamsKey)
if bz == nil {
return params
}
k.cdc.MustUnmarshal(bz, ¶ms)
return params
}
// SetParams sets the total set of liquidstakeibc parameters.
func (k *Keeper) SetParams(ctx sdk.Context, params types.Params) {
store := ctx.KVStore(k.storeKey)
bytes := k.cdc.MustMarshal(¶ms)
store.Set(types.ParamsKey, bytes)
}
// GetDepositModuleAccount returns deposit module account interface
func (k *Keeper) GetDepositModuleAccount(ctx sdk.Context) authtypes.ModuleAccountI {
return k.accountKeeper.GetModuleAccount(ctx, types.DepositModuleAccount)
}
// GetUndelegationModuleAccount returns undelegation module account interface
func (k *Keeper) GetUndelegationModuleAccount(ctx sdk.Context) authtypes.ModuleAccountI {
return k.accountKeeper.GetModuleAccount(ctx, types.UndelegationModuleAccount)
}
// SendProtocolFee to the community pool
func (k *Keeper) SendProtocolFee(ctx sdk.Context, protocolFee sdk.Coins, moduleAccount, feeAddress string) error {
addr, err := sdk.AccAddressFromBech32(feeAddress)
if err != nil {
return err
}
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, moduleAccount, addr, protocolFee)
if err != nil {
return err
}
return nil
}
// GetClientState retrieves the client state given a connection id
func (k *Keeper) GetClientState(ctx sdk.Context, connectionID string) (exported.ClientState, error) {
conn, found := k.ibcKeeper.ConnectionKeeper.GetConnection(ctx, connectionID)
if !found {
return nil, fmt.Errorf("invalid connection id, \"%s\" not found", connectionID)
}
clientState, found := k.ibcKeeper.ClientKeeper.GetClientState(ctx, conn.ClientId)
if !found {
return nil, fmt.Errorf("client id \"%s\" not found for connection \"%s\"", conn.ClientId, connectionID)
}
return clientState, nil
}
// GetChainID gets the id of the host chain given a connection id
func (k *Keeper) GetChainID(ctx sdk.Context, connectionID string) (string, error) {
clientState, err := k.GetClientState(ctx, connectionID)
if err != nil {
return "", fmt.Errorf("client state not found for connection \"%s\": \"%s\"", connectionID, err.Error())
}
switch clientType := clientState.(type) {
case *ibctmtypes.ClientState:
return clientType.ChainId, nil
case *ibclocalhosttypes.ClientState:
return ctx.ChainID(), nil
default:
return "", fmt.Errorf("unexpected type of client, cannot determine chain-id: clientType: %s, connectionid: %s", clientState.ClientType(), connectionID)
}
}
// GetPortID constructs a port id given the port owner
func (k *Keeper) GetPortID(owner string) string {
return fmt.Sprintf("%s%s", icatypes.ControllerPortPrefix, owner)
}
// RegisterICAAccount registers an ICA
func (k *Keeper) RegisterICAAccount(ctx sdk.Context, connectionID, owner string) error {
return k.icaControllerKeeper.RegisterInterchainAccount(
ctx,
connectionID,
owner,
"",
)
}
// SetWithdrawAddress sends a MsgSetWithdrawAddress to set the withdrawal address to the rewards account
func (k *Keeper) SetWithdrawAddress(ctx sdk.Context, hc *types.HostChain) error {
msgSetWithdrawAddress := &distributiontypes.MsgSetWithdrawAddress{
DelegatorAddress: hc.DelegationAccount.Address,
WithdrawAddress: hc.RewardsAccount.Address,
}
_, err := k.GenerateAndExecuteICATx(
ctx,
hc.ConnectionId,
hc.DelegationAccount.Owner,
[]proto.Message{msgSetWithdrawAddress},
)
if err != nil {
return err
}
return nil
}
// IsICAChannelActive checks if an ICA channel is active
func (k *Keeper) IsICAChannelActive(ctx sdk.Context, hc *types.HostChain, portID string) bool {
_, isActive := k.icaControllerKeeper.GetOpenActiveChannel(ctx, hc.ConnectionId, portID)
return isActive
}
func (k *Keeper) GetEpochNumber(ctx sdk.Context, epoch string) int64 {
return k.epochsKeeper.GetEpochInfo(ctx, epoch).CurrentEpoch
}
func (k *Keeper) SendICATransfer(
ctx sdk.Context,
hc *types.HostChain,
amount sdk.Coin,
sender string,
receiver string,
portOwner string,
) (string, error) {
channel, found := k.ibcKeeper.ChannelKeeper.GetChannel(ctx, hc.PortId, hc.ChannelId)
if !found {
return "", fmt.Errorf(
"could not retrieve channel for host chain %s while sending ICA transfer",
hc.ChainId,
)
}
timeoutTimestamp := uint64(ctx.BlockTime().UnixNano() + (types.IBCTimeoutTimestamp).Nanoseconds())
// prepare the msg transfer to bring the undelegation back
msgTransfer := ibctransfertypes.NewMsgTransfer(
channel.Counterparty.PortId,
channel.Counterparty.ChannelId,
amount,
sender,
receiver,
clienttypes.ZeroHeight(),
timeoutTimestamp,
"",
)
// execute the transfers
sequenceID, err := k.GenerateAndExecuteICATx(
ctx,
hc.ConnectionId,
portOwner,
[]proto.Message{msgTransfer},
)
if err != nil {
return "", fmt.Errorf(
"could not send ICA transfer for host chain %s",
hc.ChainId,
)
}
return sequenceID, nil
}
func (k *Keeper) UpdateCValues(ctx sdk.Context) {
hostChains := k.GetAllHostChains(ctx)
for _, hc := range hostChains {
k.UpdateCValue(ctx, hc)
}
}
func (k *Keeper) UpdateCValue(ctx sdk.Context, hc *types.HostChain) {
// total stk tokens minted
mintedAmount := k.bankKeeper.GetSupply(ctx, hc.MintDenom()).Amount
// total tokenized staked amount
tokenizedStakedAmount := k.GetLSMDepositAmountUntokenized(ctx, hc.ChainId)
// amount staked by the module in any of the validators of the host chain
stakedAmount := hc.GetHostChainTotalDelegations()
// amount that is in the staking flow and hasn't left Persistence yet
amountOnPersistence := k.GetDepositAmountOnPersistence(ctx, hc.ChainId)
// amount that is in the staking flow and has arrived to the host chain, but hasn't been staked yet
amountOnHostChain := k.GetDepositAmountOnHostChain(ctx, hc.ChainId)
// amount unbonded from a validator that has been in the Unbonding state for more than 4 unbonding epochs
totalUnbondingAmount := k.GetAllValidatorUnbondedAmount(ctx, hc)
// total amount staked
liquidStakedAmount := tokenizedStakedAmount.Add(stakedAmount).Add(amountOnPersistence).Add(amountOnHostChain).Add(totalUnbondingAmount)
var cValue sdk.Dec
if mintedAmount.IsZero() || liquidStakedAmount.IsZero() {
cValue = sdk.OneDec()
} else {
cValue = sdk.NewDecFromInt(mintedAmount).Quo(sdk.NewDecFromInt(liquidStakedAmount))
}
k.Logger(ctx).Info(
fmt.Sprintf(
"Updated CValue for %s. Total minted amount: %v. Total liquid staked amount: %v. Composed of %v staked tokens, %v tokens on Persistence, %v tokens on the host chain, %v tokens from a validator total unbonding. New c_value: %v - Old c_value: %v",
hc.ChainId,
mintedAmount,
liquidStakedAmount,
stakedAmount,
amountOnPersistence,
amountOnHostChain,
totalUnbondingAmount,
cValue,
hc.CValue,
),
)
hc.LastCValue = hc.CValue
hc.CValue = cValue
k.SetHostChain(ctx, hc)
if err := k.Hooks().PostCValueUpdate(ctx, hc.MintDenom(), hc.HostDenom, hc.CValue); err != nil {
k.Logger(ctx).Error("PostCValueUpdate hook failed with ", "err:", err)
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCValueUpdate,
sdk.NewAttribute(types.AttributeChainID, hc.ChainId),
sdk.NewAttribute(types.AttributeModuleMintedAmount, sdk.NewCoin(hc.MintDenom(), mintedAmount).String()),
sdk.NewAttribute(types.AttributeModuleLSMTokenizedAmount, sdk.NewCoin(hc.HostDenom, tokenizedStakedAmount).String()),
sdk.NewAttribute(types.AttributeModuleStakedAmount, sdk.NewCoin(hc.HostDenom, stakedAmount).String()),
sdk.NewAttribute(types.AttributeModuleAmountOnPersistence, sdk.NewCoin(hc.HostDenom, amountOnPersistence).String()),
sdk.NewAttribute(types.AttributeModuleAmountOnHostChain, sdk.NewCoin(hc.HostDenom, amountOnHostChain).String()),
sdk.NewAttribute(types.AttributeModuleUnbondingAmount, sdk.NewCoin(hc.HostDenom, totalUnbondingAmount).String()),
sdk.NewAttribute(types.AttributeOldCValue, hc.LastCValue.String()),
sdk.NewAttribute(types.AttributeNewCValue, hc.CValue.String()),
),
)
// if the c value is out of bounds, disable the chain
if !k.CValueWithinLimits(hc) {
hc.Active = false
k.SetHostChain(ctx, hc)
k.Logger(ctx).Error(
"C value out of limits !!!",
types.HostChainKeyVal,
hc.ChainId,
types.CValueKeyVal,
hc.CValue,
)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeChainDisabled,
sdk.NewAttribute(types.AttributeChainID, hc.ChainId),
sdk.NewAttribute(types.AttributeOldCValue, hc.LastCValue.String()),
sdk.NewAttribute(types.AttributeNewCValue, hc.CValue.String()),
),
)
} else {
k.RecalculateCValueLimits(ctx, hc, mintedAmount, liquidStakedAmount)
}
}
func (k *Keeper) CValueWithinLimits(hc *types.HostChain) bool {
return hc.CValue.LT(hc.Params.UpperCValueLimit) && hc.CValue.GT(hc.Params.LowerCValueLimit)
}
func (k *Keeper) RecalculateCValueLimits(ctx sdk.Context, hc *types.HostChain, mintedAmount, lsAmount math.Int) {
// if there has been no activity on the chain yet, leave the limits as they are
if hc.GetHostChainTotalDelegations().IsZero() {
return
}
// calculate twice the amount of rewards which will be transferred next rewards epoch
diff := sdk.NewDecFromInt(hc.GetHostChainTotalDelegations()).
Mul(hc.AutoCompoundFactor).Mul(sdk.NewDec(types.CValueDynamicLowerDiff))
// calculate the new lower and upper limit
newLowerLimit := sdk.NewDecFromInt(mintedAmount).Quo(sdk.NewDecFromInt(lsAmount).Add(diff))
newUpperLimit := hc.CValue.Add(hc.CValue.Sub(newLowerLimit).Mul(sdk.NewDec(types.CValueDynamicUpperDiff)))
// update the limits on the host chain
hc.Params.LowerCValueLimit = newLowerLimit
hc.Params.UpperCValueLimit = newUpperLimit
k.SetHostChain(ctx, hc)
k.Logger(ctx).Info("Updated CValue limits.",
types.HostChainKeyVal,
hc.ChainId,
types.CValueKeyVal,
hc.CValue,
types.LowerLimitKeyVal,
hc.Params.LowerCValueLimit,
types.UpperLimitKeyVal,
hc.Params.UpperCValueLimit,
)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCValueLimitsUpdated,
sdk.NewAttribute(types.AttributeChainID, hc.ChainId),
sdk.NewAttribute(types.AttributeLowerLimit, hc.Params.LowerCValueLimit.String()),
sdk.NewAttribute(types.AttributeUpperLimit, hc.Params.UpperCValueLimit.String()),
),
)
}
func (k *Keeper) CalculateAutocompoundLimit(autocompoundFactor sdk.Dec) sdk.Dec {
return autocompoundFactor.Quo(sdk.NewDec(types.Percentage)).Quo(sdk.NewDec(types.DaysInYear))
}
// Hooks gets the hooks for liquidstakeibc *Keeper {
func (k *Keeper) Hooks() types.LiquidStakeIBCHooks {
if k.hooks == nil {
// return a no-op implementation if no hooks are set
return types.MultiLiquidStakeIBCHooks{}
}
return k.hooks
}
// Set the liquidstakeibc hooks
func (k *Keeper) SetHooks(hooks types.LiquidStakeIBCHooks) *Keeper {
if k.hooks != nil {
panic("cannot set hooks twice")
}
k.hooks = hooks
return k
}