forked from umee-network/umee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invariants.go
304 lines (253 loc) · 9.39 KB
/
invariants.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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/mokitanetwork/katana/x/leverage/types"
)
const (
routeInterestScalars = "interest-scalars"
routeExchangeRates = "exchange-rates"
routeReserveAmount = "reserve-amount"
routeCollateralAmount = "collateral-amount"
routeBorrowAmount = "borrow-amount"
routeBorrowAPY = "borrow-apy"
routeSupplyAPY = "supply-apy"
)
// RegisterInvariants registers the leverage module invariants
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
// these invariants run in O(N) time, with N = number of registered tokens
ir.RegisterRoute(types.ModuleName, routeReserveAmount, ReserveAmountInvariant(k))
ir.RegisterRoute(types.ModuleName, routeBorrowAPY, BorrowAPYInvariant(k))
ir.RegisterRoute(types.ModuleName, routeSupplyAPY, SupplyAPYInvariant(k))
ir.RegisterRoute(types.ModuleName, routeInterestScalars, InterestScalarsInvariant(k))
ir.RegisterRoute(types.ModuleName, routeExchangeRates, ExchangeRatesInvariant(k))
}
// ReserveAmountInvariant checks that reserve amounts have non-negative balances
func ReserveAmountInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
count int
)
// Iterate through all denoms which have an reserve amount stored
// in the keeper. If a token is registered but its reserve amount is
// negative or it has some error doing the unmarshal it
// adds the denom invariant count and message description
err := k.iterate(ctx, types.KeyPrefixReserveAmount, func(key, val []byte) error {
// remove types.KeyPrefixReserveAmount and null-terminator
denom := types.DenomFromKey(key, types.KeyPrefixReserveAmount)
amount := sdk.ZeroInt()
if err := amount.Unmarshal(val); err != nil {
count++
msg += fmt.Sprintf("\tfailed to unmarshal bytes for %s: %+v\n", denom, val)
return nil
}
if amount.IsNegative() {
count++
msg += fmt.Sprintf("\t%s reserve amount %s is negative\n", denom, amount.String())
}
return nil
})
if err != nil {
msg += fmt.Sprintf("\tSome error occurred while iterating through the reserve amount %+v\n", err)
}
broken := count != 0
return sdk.FormatInvariant(
types.ModuleName, routeReserveAmount,
fmt.Sprintf("number of negative reserve amount found %d\n%s", count, msg),
), broken
}
}
// InefficientCollateralAmountInvariant checks that collateral amounts have all positive values.
// This runs in O(N) time where N is the number of participating addresses,
// so it should not be enabled in production.
func InefficientCollateralAmountInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
count int
)
// Iterate through all collateral amounts stored in the keeper,
// ensuring all successfully unmarshal to positive values.
err := k.iterate(ctx, types.KeyPrefixCollateralAmount, func(key, val []byte) error {
// remove prefix | lengthPrefixed(addr) and null-terminator
denom := types.DenomFromKeyWithAddress(key, types.KeyPrefixCollateralAmount)
// remove prefix | denom and null-terminator
address := types.AddressFromKey(key, types.KeyPrefixCollateralAmount)
amount := sdk.ZeroInt()
if err := amount.Unmarshal(val); err != nil {
count++
msg += fmt.Sprintf("\tfailed to unmarshal bytes for %s - %s: %+v\n", denom, address.String(), val)
return nil
}
if !amount.IsPositive() {
count++
msg += fmt.Sprintf("\t%s - %s collateral amount %s is not positive\n", denom, address.String(), amount.String())
}
return nil
})
if err != nil {
msg += fmt.Sprintf("\tSome error occurred while iterating through the collateral amount %+v\n", err)
}
broken := count != 0
return sdk.FormatInvariant(
types.ModuleName, routeCollateralAmount,
fmt.Sprintf("number of not positive collateral amount found %d\n%s", count, msg),
), broken
}
}
// InefficientBorrowAmountInvariant checks that borrow amounts have all positive values
// This runs in O(N) time where N is the number of participating addresses,
// so it should not be enabled in production.
func InefficientBorrowAmountInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
count int
)
borrowPrefix := types.KeyPrefixAdjustedBorrow
// Iterate through all adjusted borrow amounts stored in the keeper,
// ensuring all successfully unmarshal to positive values.
err := k.iterate(ctx, borrowPrefix, func(key, val []byte) error {
// remove prefix | lengthPrefixed(addr) and null-terminator
denom := types.DenomFromKeyWithAddress(key, borrowPrefix)
// remove prefix | denom and null-terminator
address := types.AddressFromKey(key, borrowPrefix)
amount := sdk.ZeroDec()
if err := amount.Unmarshal(val); err != nil {
count++
msg += fmt.Sprintf("\tfailed to unmarshal bytes for %s - %s: %+v\n", denom, address.String(), val)
return nil
}
if !amount.IsPositive() {
count++
msg += fmt.Sprintf("\t%s - %s adjusted borrow %s is not positive\n", denom, address.String(), amount.String())
}
return nil
})
if err != nil {
msg += fmt.Sprintf("\tSome error occurred while iterating through adjusted borrow amounts %+v\n", err)
}
broken := count != 0
return sdk.FormatInvariant(
types.ModuleName, routeBorrowAmount,
fmt.Sprintf("number of not positive adjusted borrow amounts found %d\n%s", count, msg),
), broken
}
}
// BorrowAPYInvariant checks that Borrow APY have all positive values
func BorrowAPYInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
count int
)
tokenPrefix := types.KeyPrefixRegisteredToken
// Iterate through all denoms of registered tokens in the
// keeper, ensuring none have a negative borrow APY.
err := k.iterate(ctx, tokenPrefix, func(key, _ []byte) error {
denom := types.DenomFromKey(key, tokenPrefix)
borrowAPY := k.DeriveBorrowAPY(ctx, denom)
if borrowAPY.IsNegative() {
count++
msg += fmt.Sprintf("\t%s borrow APY %s is negative\n", denom, borrowAPY.String())
}
return nil
})
if err != nil {
msg += fmt.Sprintf("\tSome error occurred while iterating through the borrow APY %+v\n", err)
}
broken := count != 0
return sdk.FormatInvariant(
types.ModuleName, routeBorrowAPY,
fmt.Sprintf("number of negative borrow APY found %d\n%s", count, msg),
), broken
}
}
// SupplyAPYInvariant checks that Supply APY have all positive values
func SupplyAPYInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
count int
)
tokenPrefix := types.KeyPrefixRegisteredToken
// Iterate through all denoms of registered tokens in the
// keeper, ensuring none have a negative supply APY.
err := k.iterate(ctx, tokenPrefix, func(key, _ []byte) error {
denom := types.DenomFromKey(key, tokenPrefix)
supplyAPY := k.DeriveSupplyAPY(ctx, denom)
if supplyAPY.IsNegative() {
count++
msg += fmt.Sprintf("\t%s supply APY %s is negative\n", denom, supplyAPY.String())
}
return nil
})
if err != nil {
msg += fmt.Sprintf("\tSome error occurred while iterating through the supply APY %+v\n", err)
}
broken := count != 0
return sdk.FormatInvariant(
types.ModuleName, routeSupplyAPY,
fmt.Sprintf("number of negative supply APY found %d\n%s", count, msg),
), broken
}
}
// InterestScalarsInvariant checks that all denoms have an interest scalar >= 1
func InterestScalarsInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
count int
)
tokenPrefix := types.KeyPrefixRegisteredToken
// Iterate through all denoms of registered tokens in the
// keeper, ensuring none have an interest scalar less than one.
err := k.iterate(ctx, tokenPrefix, func(key, _ []byte) error {
denom := types.DenomFromKey(key, tokenPrefix)
scalar := k.getInterestScalar(ctx, denom)
if scalar.LT(sdk.OneDec()) {
count++
msg += fmt.Sprintf("\t%s interest scalar %s is less than one\n", denom, scalar.String())
}
return nil
})
if err != nil {
msg += fmt.Sprintf("\tSome error occurred while iterating through the interest scalars %+v\n", err)
}
broken := count != 0
return sdk.FormatInvariant(
types.ModuleName, routeInterestScalars,
fmt.Sprintf("amount of interest scalars lower than one %d\n%s", count, msg),
), broken
}
}
// ExchangeRatesInvariant checks that all denoms have an uToken exchange rate >= 1
func ExchangeRatesInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
count int
)
tokenPrefix := types.KeyPrefixRegisteredToken
// Iterate through all denoms of registered tokens in the
// keeper, ensuring none have an interest scalar less than one.
err := k.iterate(ctx, tokenPrefix, func(key, _ []byte) error {
denom := types.DenomFromKey(key, tokenPrefix)
exchangeRate := k.DeriveExchangeRate(ctx, denom)
if exchangeRate.LT(sdk.OneDec()) {
count++
msg += fmt.Sprintf("\t%s exchange rate %s is less than one\n", denom, exchangeRate.String())
}
return nil
})
if err != nil {
msg += fmt.Sprintf("\tSome error occurred while iterating through the uToken exchange rates %+v\n", err)
}
broken := count != 0
return sdk.FormatInvariant(
types.ModuleName, routeExchangeRates,
fmt.Sprintf("amount of uToken exchange rates lower than one %d\n%s", count, msg),
), broken
}
}