-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
385 lines (305 loc) · 11.5 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
package keeper
import (
stdmath "math"
"time"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
"github.com/cometbft/cometbft/libs/log"
"github.com/celinium-network/restaking_protocol/x/multitokenstaking/types"
)
type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.Codec
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
epochKeeper types.EpochKeeper
stakingKeeper types.StakingKeeper
distributionKeeper types.DistributionKeeper
EquivalentNativeCoinMultiplier EquivalentNativeCoinMultiplier
}
func NewKeeper(
cdc codec.Codec,
storeKey storetypes.StoreKey,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
epochKeeper types.EpochKeeper,
stakingKeeper types.StakingKeeper,
distributionKeeper types.DistributionKeeper,
) Keeper {
return Keeper{
storeKey: storeKey,
cdc: cdc,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
epochKeeper: epochKeeper,
stakingKeeper: stakingKeeper,
distributionKeeper: distributionKeeper,
EquivalentNativeCoinMultiplier: defaultEquivalentCoinMultiplier,
}
}
// TODO Temporarily use this method to feed prices !!!
type EquivalentNativeCoinMultiplier func(ctx sdk.Context, denom string) (sdk.Dec, error)
func defaultEquivalentCoinMultiplier(ctx sdk.Context, denom string) (sdk.Dec, error) {
return sdk.OneDec(), nil
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
func (k Keeper) SetEquivalentNativeCoinMultiplier(ctx sdk.Context, epoch int64, denom string, multiplier sdk.Dec) {
store := ctx.KVStore(k.storeKey)
record := types.EquivalentMultiplierRecord{
EpochNumber: epoch,
Denom: denom,
Multiplier: multiplier,
}
bz := k.cdc.MustMarshal(&record)
store.Set(types.GetMTTokenMultiplierKey(denom), bz)
}
func (k Keeper) GetEquivalentNativeCoinMultiplier(ctx sdk.Context, denom string) (multiplier sdk.Dec, found bool) {
var record types.EquivalentMultiplierRecord
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMTTokenMultiplierKey(denom))
if bz == nil {
return multiplier, false
}
if err := k.cdc.Unmarshal(bz, &record); err != nil {
return multiplier, false
}
multiplier = record.Multiplier
return multiplier, true
}
func (k Keeper) CalculateEquivalentNativeCoin(ctx sdk.Context, coin sdk.Coin) (targetCoin sdk.Coin, err error) {
multiplier, found := k.GetEquivalentNativeCoinMultiplier(ctx, coin.Denom)
if !found {
return targetCoin, types.ErrNoCoinMultiplierFound
}
targetCoin.Denom = k.stakingKeeper.BondDenom(ctx)
targetCoin.Amount = multiplier.MulInt(coin.Amount).TruncateInt()
return targetCoin, nil
}
func (k Keeper) GetMTStakingAgentByAddress(ctx sdk.Context, agentAccAddr sdk.AccAddress) (*types.MTStakingAgent, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMTStakingAgentKey(agentAccAddr))
if bz == nil {
return nil, false
}
agent := &types.MTStakingAgent{}
k.cdc.MustUnmarshal(bz, agent)
return agent, true
}
func (k Keeper) GetMTStakingAgent(ctx sdk.Context, denom string, valAddr sdk.ValAddress) (*types.MTStakingAgent, bool) {
agentID, found := k.GetMTStakingAgentAddressByDenomAndVal(ctx, denom, valAddr)
if !found {
return nil, false
}
return k.GetMTStakingAgentByAddress(ctx, agentID)
}
func (k Keeper) SetMTStakingAgent(ctx sdk.Context, agentAccAddr sdk.AccAddress, agent *types.MTStakingAgent) {
bz := k.cdc.MustMarshal(agent)
store := ctx.KVStore(k.storeKey)
store.Set(types.GetMTStakingAgentKey(agentAccAddr), bz)
}
func (k Keeper) GetMTStakingAgentAddressByDenomAndVal(ctx sdk.Context, denom string, valAddr sdk.ValAddress) ([]byte, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMTStakingAgentAddressKey(denom, valAddr))
if bz == nil {
return nil, false
}
return bz, true
}
func (k Keeper) SetMTStakingDenomAndValWithAgentAddress(ctx sdk.Context, agentAddress sdk.AccAddress, denom string, valAddr sdk.ValAddress) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetMTStakingAgentAddressKey(denom, valAddr), agentAddress)
}
func (k Keeper) GetMTStakingUnbonding(ctx sdk.Context, agentAddress sdk.AccAddress, delegatorAddr sdk.AccAddress) (*types.MTStakingUnbondingDelegation, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMTStakingUnbondingKey(agentAddress, delegatorAddr))
if bz == nil {
return nil, false
}
unbonding := &types.MTStakingUnbondingDelegation{}
k.cdc.MustUnmarshal(bz, unbonding)
return unbonding, true
}
func (k Keeper) GetUnbondingDelegationFromAgent(ctx sdk.Context, agentAddress sdk.AccAddress) (ubds []types.MTStakingUnbondingDelegation) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetMTStakingUnbondingByAgentIndexKey(agentAddress))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
unbonding := types.MTStakingUnbondingDelegation{}
bz := iterator.Value()
k.cdc.MustUnmarshal(bz, &unbonding)
ubds = append(ubds, unbonding)
}
return ubds
}
func (k Keeper) GetOrCreateMTStakingUnbonding(ctx sdk.Context, agentAddress, delegatorAddr sdk.AccAddress) *types.MTStakingUnbondingDelegation {
unbonding, found := k.GetMTStakingUnbonding(ctx, agentAddress, delegatorAddr)
if found {
return unbonding
}
unbonding = &types.MTStakingUnbondingDelegation{
AgentAddress: string(agentAddress),
DelegatorAddress: string(delegatorAddr),
Entries: []types.MTStakingUnbondingDelegationEntry{},
}
return unbonding
}
func (k Keeper) SetMTStakingUnbondingDelegation(ctx sdk.Context, unbonding *types.MTStakingUnbondingDelegation) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(unbonding)
agentAccAddr := sdk.AccAddress(unbonding.AgentAddress)
delegatorAccAddr := sdk.AccAddress(unbonding.DelegatorAddress)
store.Set(types.GetMTStakingUnbondingKey(agentAccAddr, delegatorAccAddr), bz)
}
func (k Keeper) RemoveMTStakingUnbonding(ctx sdk.Context, agentAddress, delegatorAddr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetMTStakingUnbondingKey(agentAddress, delegatorAddr))
}
func (k Keeper) GetDelegatorAgentShares(ctx sdk.Context, agentAddress, delegator sdk.AccAddress) math.Int {
delegation, found := k.GetDelegation(ctx, agentAddress, delegator)
if !found {
return math.ZeroInt()
}
return delegation.Shares
}
func (k Keeper) GetDelegation(ctx sdk.Context, agentAddress, delegator sdk.AccAddress) (*types.MTStakingDelegation, bool) {
store := ctx.KVStore(k.storeKey)
key := types.GetMTStakingDelegationKey(agentAddress, delegator)
bz := store.Get(key)
if bz == nil {
return nil, false
}
var delegation types.MTStakingDelegation
k.cdc.MustUnmarshal(bz, &delegation)
return &delegation, true
}
func (k Keeper) IncreaseDelegatorAgentShares(ctx sdk.Context, shares math.Int, agentAddress, delegator sdk.AccAddress) error {
store := ctx.KVStore(k.storeKey)
key := types.GetMTStakingDelegationKey(agentAddress, delegator)
var delegation types.MTStakingDelegation
bz := store.Get(key)
if bz != nil {
k.cdc.MustUnmarshal(bz, &delegation)
delegation.Shares = delegation.Shares.Add(shares)
} else {
delegation = types.MTStakingDelegation{
DelegatorAddress: string(agentAddress),
AgentAddress: string(delegator),
Shares: shares,
}
}
bz = k.cdc.MustMarshal(&delegation)
store.Set(key, bz)
return nil
}
func (k Keeper) DecreaseDelegatorAgentShares(ctx sdk.Context, shares math.Int, agentAddress, delegator sdk.AccAddress) error {
var delegation types.MTStakingDelegation
store := ctx.KVStore(k.storeKey)
key := types.GetMTStakingDelegationKey(agentAddress, delegator)
bz := store.Get(key)
if bz == nil {
return types.ErrInsufficientShares
}
k.cdc.MustUnmarshal(bz, &delegation)
if delegation.Shares.LT(shares) {
return types.ErrInsufficientShares
}
delegation.Shares = delegation.Shares.Sub(shares)
bz = k.cdc.MustMarshal(&delegation)
store.Set(key, bz)
return nil
}
// sendCoinsFromAccountToAccount preform send coins form sender to receiver.
func (k Keeper) sendCoinsFromAccountToAccount(ctx sdk.Context, senderAddr sdk.AccAddress, receiverAddr sdk.AccAddress, amt sdk.Coins) error {
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, senderAddr, types.ModuleName, amt); err != nil {
return err
}
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, receiverAddr, amt)
}
// UBDQueueIterator returns all the unbonding queue timeslices from time 0 until endTime.
func (k Keeper) UBDQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return store.Iterator(types.UnbondingQueueKey,
sdk.InclusiveEndBytes(types.GetMTStakingUnbondingDelegationTimeKey(endTime)))
}
func (k Keeper) InsertUBDQueue(ctx sdk.Context, ubd *types.MTStakingUnbondingDelegation, completionTime time.Time) {
daPair := types.DAPair{DelegatorAddress: ubd.DelegatorAddress, AgentAddress: ubd.AgentAddress}
timeSlice := k.GetUBDQueueTimeSlice(ctx, completionTime)
if len(timeSlice) == 0 {
k.SetUBDQueueTimeSlice(ctx, completionTime, []types.DAPair{daPair})
} else {
timeSlice = append(timeSlice, daPair)
k.SetUBDQueueTimeSlice(ctx, completionTime, timeSlice)
}
}
func (k Keeper) GetUBDQueueTimeSlice(ctx sdk.Context, timestamp time.Time) (dvPairs []types.DAPair) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMTStakingUnbondingDelegationTimeKey(timestamp))
if bz == nil {
return []types.DAPair{}
}
pairs := types.DAPairs{}
k.cdc.MustUnmarshal(bz, &pairs)
return pairs.Pairs
}
func (k Keeper) SetUBDQueueTimeSlice(ctx sdk.Context, timestamp time.Time, keys []types.DAPair) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&types.DAPairs{Pairs: keys})
store.Set(types.GetMTStakingUnbondingDelegationTimeKey(timestamp), bz)
}
func (k Keeper) GetAllAgent(ctx sdk.Context) []types.MTStakingAgent {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, types.AgentPrefix)
iterator := sdk.KVStorePrefixIterator(prefixStore, nil)
defer iterator.Close()
agents := []types.MTStakingAgent{}
for ; iterator.Valid(); iterator.Next() {
agent := types.MTStakingAgent{}
err := proto.Unmarshal(iterator.Value(), &agent)
if err != nil {
panic(err)
}
agents = append(agents, agent)
}
return agents
}
func (k Keeper) GetDelegatorWithdrawRewardHeight(ctx sdk.Context, delegatorAccAddr, agentAccAddr sdk.AccAddress) (int64, bool) {
store := ctx.KVStore(k.storeKey)
key := types.GetMTWithdrawRewardHeightKey(delegatorAccAddr, agentAccAddr)
bz := store.Get(key)
if bz == nil {
return 0, false
}
height := sdk.BigEndianToUint64(bz)
return safeUint64ToInt64(height)
}
func (k Keeper) SetDelegatorWithdrawRewardHeight(ctx sdk.Context, delegatorAccAddr, agentAccAddr sdk.AccAddress, height int64) bool {
h, ok := safeInt64ToUint64(height)
if !ok {
return false
}
bz := sdk.Uint64ToBigEndian(h)
store := ctx.KVStore(k.storeKey)
key := types.GetMTWithdrawRewardHeightKey(delegatorAccAddr, agentAccAddr)
store.Set(key, bz)
return true
}
// TODO BlockHeight should be always unsigned, and maxInt64 is big enough.
func safeUint64ToInt64(value uint64) (int64, bool) {
if value > stdmath.MaxInt64 {
return 0, false
}
return int64(value), true
}
func safeInt64ToUint64(value int64) (uint64, bool) {
if value < 0 {
return 0, false
}
return uint64(value), true
}