-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
257 lines (222 loc) · 6.62 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
package keeper
import (
"fmt"
"math/big"
errorsmod "cosmossdk.io/errors"
sdklog "cosmossdk.io/log"
"github.com/cometbft/cometbft/libs/log"
"github.com/jinxprotocol/v4-chain/protocol/dtypes"
"github.com/jinxprotocol/v4-chain/protocol/lib"
"github.com/jinxprotocol/v4-chain/protocol/x/ratelimit/types"
"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"
)
var (
// TempTVLPlacerholder is a placeholder value for TVL.
// TODO(CORE-836): Remove this after `GetBaseline` is fully implemented.
TempTVLPlacerholder = big.NewInt(20_000_000_000_000)
)
type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
// TODO(CORE-824): Implement `x/ratelimit` keeper
// the addresses capable of executing a MsgUpdateParams message.
authorities map[string]struct{}
}
)
func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
authorities []string,
) *Keeper {
return &Keeper{
cdc: cdc,
storeKey: storeKey,
authorities: lib.UniqueSliceToSet(authorities),
}
}
// ProcessWithdrawal processes an outbound IBC transfer,
// by updating the capacity lists for the denom.
func (k Keeper) ProcessWithdrawal(
ctx sdk.Context,
denom string,
amount *big.Int,
) error {
denomCapacity := k.GetDenomCapacity(ctx, denom)
newCapacityList := make([]dtypes.SerializableInt,
0,
len(denomCapacity.CapacityList),
)
for i, capacity := range denomCapacity.CapacityList {
// Check that the withdrawal amount does not exceed each capacity.
if capacity.BigInt().Cmp(amount) < 0 {
return errorsmod.Wrapf(
types.ErrWithdrawalExceedsCapacity,
"denom = %v, capacity(index: %v) = %v, amount = %v",
denom,
i,
capacity.BigInt(),
amount,
)
}
// Debit each capacity in the list by the amount of withdrawal.
newCapacityList[i] = dtypes.NewIntFromBigInt(
new(big.Int).Sub(
capacity.BigInt(),
amount,
),
)
}
k.SetDenomCapacity(ctx, types.DenomCapacity{
Denom: denom,
CapacityList: newCapacityList,
})
return nil
}
// ProcessDeposit processes a inbound IBC transfer,
// by updating the capacity lists for the denom.
func (k Keeper) ProcessDeposit(
ctx sdk.Context,
denom string,
amount *big.Int,
) {
denomCapacity := k.GetDenomCapacity(ctx, denom)
newCapacityList := make([]dtypes.SerializableInt,
0,
len(denomCapacity.CapacityList),
)
// Credit each capacity in the list by the amount of deposit.
for i, capacity := range denomCapacity.CapacityList {
newCapacityList[i] = dtypes.NewIntFromBigInt(
new(big.Int).Add(
capacity.BigInt(),
amount,
),
)
}
k.SetDenomCapacity(ctx, types.DenomCapacity{
Denom: denom,
CapacityList: newCapacityList,
})
}
// GetBaseline returns the current capacity baseline for the given limiter.
// `baseline` formula:
//
// baseline = max(baseline_minimum, baseline_tvl_ppm * current_tvl)
func (k Keeper) GetBaseline(
ctx sdk.Context,
denom string,
limiter types.Limiter,
) *big.Int {
// Get the current TVL.
// TODO(CORE-836): Query bank Keeper to get current supply of the token.
currentTVL := TempTVLPlacerholder
return lib.BigMax(
limiter.BaselineMinimum.BigInt(),
lib.BigIntMulPpm(
currentTVL,
limiter.BaselineTvlPpm,
),
)
}
// SetLimitParams sets `LimitParams` for the given denom.
// Also overwrites the existing `DenomCapacity` object for the denom with a default `capacity_list` of the
// same length as the `limiters` list. Each `capacity` is initialized to the current baseline.
func (k Keeper) SetLimitParams(
ctx sdk.Context,
limitParams types.LimitParams,
) {
limitParamsStore := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.LimitParamsKeyPrefix))
denomCapacityStore := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.DenomCapacityKeyPrefix))
denomKey := []byte(limitParams.Denom)
// If the length of input limit params is zero, then remove both the
// limit params and the denom capacity in state.
if len(limitParams.Limiters) == 0 {
if limitParamsStore.Has(denomKey) {
limitParamsStore.Delete(denomKey)
}
if denomCapacityStore.Has(denomKey) {
denomCapacityStore.Delete(denomKey)
}
return
}
// Initialize the capacity list with the current baseline.
newCapacityList := make([]dtypes.SerializableInt, len(limitParams.Limiters))
for i, limiter := range limitParams.Limiters {
newCapacityList[i] = dtypes.NewIntFromBigInt(
k.GetBaseline(ctx, limitParams.Denom, limiter),
)
}
// Set correspondong `DenomCapacity` in state.
k.SetDenomCapacity(ctx, types.DenomCapacity{
Denom: limitParams.Denom,
CapacityList: newCapacityList,
})
b := k.cdc.MustMarshal(&limitParams)
limitParamsStore.Set(denomKey, b)
}
// GetLimitParams returns `LimitParams` for the given denom.
func (k Keeper) GetLimitParams(
ctx sdk.Context,
denom string,
) (val types.LimitParams) {
// Check state for the LimitParams.
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.LimitParamsKeyPrefix))
b := store.Get([]byte(denom))
// If LimitParams does not exist in state, return a default value.
if b == nil {
return types.LimitParams{
Denom: denom,
}
}
// If LimitParams does exist in state, unmarshall and return the value.
k.cdc.MustUnmarshal(b, &val)
return val
}
// SetDenomCapacity sets `DenomCapacity` for the given denom.
func (k Keeper) SetDenomCapacity(
ctx sdk.Context,
denomCapacity types.DenomCapacity,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.DenomCapacityKeyPrefix))
key := []byte(denomCapacity.Denom)
// If there's no capacity entry to set, delete the key.
if len(denomCapacity.CapacityList) == 0 {
if store.Has(key) {
store.Delete(key)
}
} else {
b := k.cdc.MustMarshal(&denomCapacity)
store.Set(key, b)
}
}
// GetDenomCapacity returns `DenomCapacity` for the given denom.
func (k Keeper) GetDenomCapacity(
ctx sdk.Context,
denom string,
) (val types.DenomCapacity) {
// Check state for the DenomCapacity.
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.DenomCapacityKeyPrefix))
b := store.Get([]byte(denom))
// If DenomCapacity does not exist in state, return a default value.
if b == nil {
return types.DenomCapacity{
Denom: denom,
}
}
// If DenomCapacity does exist in state, unmarshall and return the value.
k.cdc.MustUnmarshal(b, &val)
return val
}
func (k Keeper) HasAuthority(authority string) bool {
_, ok := k.authorities[authority]
return ok
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With(sdklog.ModuleKey, fmt.Sprintf("x/%s", types.ModuleName))
}
func (k Keeper) InitializeForGenesis(ctx sdk.Context) {
}