-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
params.go
432 lines (364 loc) · 13 KB
/
params.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
428
429
430
431
432
package types
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramstype "github.com/cosmos/cosmos-sdk/x/params/types"
)
// Parameter keys
var (
KeyGlobalDebtLimit = []byte("GlobalDebtLimit")
KeyCollateralParams = []byte("CollateralParams")
KeyDebtParam = []byte("DebtParam")
KeyCircuitBreaker = []byte("CircuitBreaker")
KeyDebtThreshold = []byte("DebtThreshold")
KeyDebtLot = []byte("DebtLot")
KeySurplusThreshold = []byte("SurplusThreshold")
KeySurplusLot = []byte("SurplusLot")
DefaultCollateralParams = CollateralParams{}
DefaultDebtParams = DebtParams{
DebtParam{
Denom: "jpu",
ReferenceAsset: "jpy",
ConversionFactor: sdk.NewInt(6),
DebtFloor: sdk.NewInt(1),
GlobalDebtLimit: sdk.NewCoin(DefaultStableDenom, sdk.ZeroInt()),
DebtDenom: "debtjpu",
SurplusAuctionThreshold: sdk.NewInt(500000000000),
SurplusAuctionLot: sdk.NewInt(10000000000),
DebtAuctionThreshold: sdk.NewInt(100000000000),
DebtAuctionLot: sdk.NewInt(10000000000),
CircuitBreaker: false,
},
DebtParam{
Denom: "euu",
ReferenceAsset: "eur",
ConversionFactor: sdk.NewInt(6),
DebtFloor: sdk.NewInt(1),
GlobalDebtLimit: sdk.NewCoin("euu", sdk.ZeroInt()),
DebtDenom: "debteuu",
SurplusAuctionThreshold: sdk.NewInt(500000000000),
SurplusAuctionLot: sdk.NewInt(10000000000),
DebtAuctionThreshold: sdk.NewInt(100000000000),
DebtAuctionLot: sdk.NewInt(10000000000),
CircuitBreaker: false,
},
}
DefaultCdpStartingID = uint64(1)
DefaultGovDenom = "uguu"
DefaultStableDenom = "jpu"
minCollateralPrefix = 0
maxCollateralPrefix = 255
stabilityFeeMax = sdk.MustNewDecFromStr("1.000000051034942716") // 500% APR
)
// NewParams returns a new params object
func NewParams(
collateralParams CollateralParams, debtParams DebtParams,
) Params {
return Params{
CollateralParams: collateralParams,
DebtParams: debtParams,
}
}
// DefaultParams returns default params for cdp module
func DefaultParams() Params {
return NewParams(
DefaultCollateralParams, DefaultDebtParams,
)
}
// NewCollateralParam returns a new CollateralParam
func NewCollateralParam(
denom, ctype string, liqRatio sdk.Dec, debtLimit sdk.Coin, stabilityFee sdk.Dec, auctionSize sdk.Int,
liqPenalty sdk.Dec, prefix byte, spotMarketID, liquidationMarketID string, keeperReward sdk.Dec, checkIndexCount sdk.Int, conversionFactor sdk.Int) CollateralParam {
return CollateralParam{
Denom: denom,
Type: ctype,
LiquidationRatio: liqRatio,
DebtLimit: debtLimit,
StabilityFee: stabilityFee,
AuctionSize: auctionSize,
LiquidationPenalty: liqPenalty,
Prefix: uint32(prefix),
SpotMarketId: spotMarketID,
LiquidationMarketId: liquidationMarketID,
KeeperRewardPercentage: keeperReward,
CheckCollateralizationIndexCount: checkIndexCount,
ConversionFactor: conversionFactor,
}
}
// CollateralParams array of CollateralParam
type CollateralParams []CollateralParam
// String implements fmt.Stringer
func (cps CollateralParams) String() string {
out := "Collateral Params\n"
for _, cp := range cps {
out += fmt.Sprintf("%s\n", cp.String())
}
return out
}
// NewDebtParam returns a new DebtParam
func NewDebtParam(denom, refAsset string, conversionFactor, debtFloor sdk.Int, globalDebtLimit sdk.Coin) DebtParam {
return DebtParam{
Denom: denom,
ReferenceAsset: refAsset,
ConversionFactor: conversionFactor,
DebtFloor: debtFloor,
GlobalDebtLimit: globalDebtLimit,
}
}
// DebtParams array of DebtParam
type DebtParams []DebtParam
// String implements fmt.Stringer
func (dps DebtParams) String() string {
out := "Debt Params\n"
for _, dp := range dps {
out += fmt.Sprintf("%v\n", dp)
}
return out
}
// find the element that returns TRUE in the injected function
func (dps DebtParams) find(f func(DebtParam, string) bool, search_denom string) (DebtParam, bool) {
for _, debtParam := range dps {
if (DebtParam{}) != debtParam {
if f(debtParam, search_denom) {
return debtParam, true
}
}
}
return DebtParam{}, false
}
// Returns an element with a denom that matches search_denom
func (dps DebtParams) FindDenom(search_denom string) (DebtParam, bool) {
findDenom := func(ele DebtParam, target string) bool {
return ele.Denom == target
}
return dps.find(findDenom, search_denom)
}
// Returns an element with a GlobalDebtLimit.Denom that matches search_denom
func (dps DebtParams) FindGlobalDebtLimitDenom(search_denom string) (DebtParam, bool) {
FindGlobalDebtLimitDenomDenom := func(ele DebtParam, target string) bool {
return ele.GlobalDebtLimit.Denom == target
}
return dps.find(FindGlobalDebtLimitDenomDenom, search_denom)
}
// ParamKeyTable Key declaration for parameters
func ParamKeyTable() paramstype.KeyTable {
return paramstype.NewKeyTable().RegisterParamSet(&Params{})
}
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
// pairs of auth module's parameters.
// nolint
func (p *Params) ParamSetPairs() paramstype.ParamSetPairs {
return paramstype.ParamSetPairs{
paramstype.NewParamSetPair(KeyCollateralParams, &p.CollateralParams, validateCollateralParams),
paramstype.NewParamSetPair(KeyDebtParam, &p.DebtParams, validateDebtParams),
}
}
// Validate checks that the parameters have valid values.
func (p Params) Validate() error {
if err := validateCollateralParams(p.CollateralParams); err != nil {
return err
}
if err := validateDebtParams(p.DebtParams); err != nil {
return err
}
if len(p.CollateralParams) == 0 { // default value OK
return nil
}
for _, debtParam := range p.DebtParams {
if (DebtParam{}) != debtParam {
if debtParam.Denom != debtParam.GlobalDebtLimit.Denom {
return fmt.Errorf("debt denom %s does not match global debt denom %s",
debtParam.Denom, debtParam.GlobalDebtLimit.Denom)
}
}
}
// validate collateral params
collateralTypeDupMap := make(map[string]bool)
collateralParamsDebtLimitMap := make(map[string]sdk.Int)
for _, cp := range p.CollateralParams {
// Collateral type eg busd-a should be unique, but denom can be same eg busd
_, typeExists := collateralTypeDupMap[cp.Type]
if typeExists {
return fmt.Errorf("duplicate collateral type %s", cp.Denom)
}
collateralTypeDupMap[cp.Type] = true
collateralParamsDebtLimit, denomLimitExists := collateralParamsDebtLimitMap[cp.DebtLimit.Denom]
if !denomLimitExists {
collateralParamsDebtLimit = sdk.ZeroInt()
}
debtParams := DebtParams(p.DebtParams)
debtParam, exists := debtParams.FindGlobalDebtLimitDenom(cp.DebtLimit.Denom)
if !exists {
return fmt.Errorf("collateral debt limit denom %s does not match global debt limit denoms.", cp.DebtLimit.Denom)
}
collateralParamsDebtLimitMap[cp.DebtLimit.Denom] = collateralParamsDebtLimit.Add(cp.DebtLimit.Amount)
if cp.DebtLimit.Amount.GT(debtParam.GlobalDebtLimit.Amount) {
return fmt.Errorf("collateral debt limit %s exceeds global debt limit: %s denom: %s",
cp.DebtLimit, debtParam.GlobalDebtLimit.Amount, debtParam.GlobalDebtLimit.Denom)
}
}
for _, debtParam := range p.DebtParams {
collateralParamsDebtLimit, exists := collateralParamsDebtLimitMap[debtParam.GlobalDebtLimit.Denom]
if !exists {
continue
}
if collateralParamsDebtLimit.GT(debtParam.GlobalDebtLimit.Amount) {
return fmt.Errorf("sum of collateral debt limits %s exceeds global debt limit %s denom: %s",
collateralParamsDebtLimit, debtParam.GlobalDebtLimit.Amount, debtParam.GlobalDebtLimit.Denom)
}
}
return nil
}
func validateGlobalDebtLimitParam(i interface{}) error {
globalDebtLimit, ok := i.(sdk.Coin)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if !globalDebtLimit.IsValid() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "global debt limit %s", globalDebtLimit.String())
}
return nil
}
func validateCollateralParams(i interface{}) error {
collateralParams, ok := i.(CollateralParams)
if !ok {
collateralParams, ok = i.([]CollateralParam)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
}
prefixDupMap := make(map[int]bool)
typeDupMap := make(map[string]bool)
for _, cp := range collateralParams {
if err := sdk.ValidateDenom(cp.Denom); err != nil {
return fmt.Errorf("collateral denom invalid %s", cp.Denom)
}
if strings.TrimSpace(cp.SpotMarketId) == "" {
return fmt.Errorf("spot market id cannot be blank %s", cp.String())
}
if strings.TrimSpace(cp.Type) == "" {
return fmt.Errorf("collateral type cannot be blank %s", cp.String())
}
if strings.TrimSpace(cp.LiquidationMarketId) == "" {
return fmt.Errorf("liquidation market id cannot be blank %s", cp.String())
}
prefix := int(cp.Prefix)
if prefix < minCollateralPrefix || prefix > maxCollateralPrefix {
return fmt.Errorf("invalid prefix for collateral denom %s: %b", cp.Denom, cp.Prefix)
}
_, found := prefixDupMap[prefix]
if found {
return fmt.Errorf("duplicate prefix for collateral denom %s: %v", cp.Denom, []byte{byte(cp.Prefix)})
}
prefixDupMap[prefix] = true
_, found = typeDupMap[cp.Type]
if found {
return fmt.Errorf("duplicate cdp collateral type: %s", cp.Type)
}
typeDupMap[cp.Type] = true
if !cp.DebtLimit.IsValid() {
return fmt.Errorf("debt limit for all collaterals should be positive, is %s for %s", cp.DebtLimit, cp.Denom)
}
if cp.LiquidationPenalty.LT(sdk.ZeroDec()) || cp.LiquidationPenalty.GT(sdk.OneDec()) {
return fmt.Errorf("liquidation penalty should be between 0 and 1, is %s for %s", cp.LiquidationPenalty, cp.Denom)
}
if !cp.AuctionSize.IsPositive() {
return fmt.Errorf("auction size should be positive, is %s for %s", cp.AuctionSize, cp.Denom)
}
if cp.StabilityFee.LT(sdk.OneDec()) || cp.StabilityFee.GT(stabilityFeeMax) {
return fmt.Errorf("stability fee must be ≥ 1.0, ≤ %s, is %s for %s", stabilityFeeMax, cp.StabilityFee, cp.Denom)
}
if cp.KeeperRewardPercentage.IsNegative() || cp.KeeperRewardPercentage.GT(sdk.OneDec()) {
return fmt.Errorf("keeper reward percentage should be between 0 and 1, is %s for %s", cp.KeeperRewardPercentage, cp.Denom)
}
if cp.CheckCollateralizationIndexCount.IsNegative() {
return fmt.Errorf("keeper reward percentage should be positive, is %s for %s", cp.CheckCollateralizationIndexCount, cp.Denom)
}
}
return nil
}
func validateDebtParams(i interface{}) error {
debtParams, ok := i.(DebtParams)
if !ok {
debtParams, ok = i.([]DebtParam)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
}
for _, debtParam := range debtParams {
if err := sdk.ValidateDenom(debtParam.Denom); err != nil {
return fmt.Errorf("debtParam Denom invalid %s", debtParam.Denom)
}
if err := validateGlobalDebtLimitParam(debtParam.GlobalDebtLimit); err != nil {
return err
}
if err := sdk.ValidateDenom(debtParam.DebtDenom); err != nil {
return fmt.Errorf("debtParam DebtDenom invalid %s", debtParam.DebtDenom)
}
if err := validateCircuitBreakerParam(debtParam.CircuitBreaker); err != nil {
return err
}
if err := validateSurplusAuctionThresholdParam(debtParam.SurplusAuctionThreshold); err != nil {
return err
}
if err := validateSurplusAuctionLotParam(debtParam.SurplusAuctionLot); err != nil {
return err
}
if err := validateDebtAuctionThresholdParam(debtParam.DebtAuctionThreshold); err != nil {
return err
}
if err := validateDebtAuctionLotParam(debtParam.DebtAuctionLot); err != nil {
return err
}
}
return nil
}
func validateCircuitBreakerParam(i interface{}) error {
_, ok := i.(bool)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateSurplusAuctionThresholdParam(i interface{}) error {
sat, ok := i.(sdk.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if !sat.IsPositive() {
return fmt.Errorf("surplus auction threshold should be positive: %s", sat)
}
return nil
}
func validateSurplusAuctionLotParam(i interface{}) error {
sal, ok := i.(sdk.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if !sal.IsPositive() {
return fmt.Errorf("surplus auction lot should be positive: %s", sal)
}
return nil
}
func validateDebtAuctionThresholdParam(i interface{}) error {
dat, ok := i.(sdk.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if !dat.IsPositive() {
return fmt.Errorf("debt auction threshold should be positive: %s", dat)
}
return nil
}
func validateDebtAuctionLotParam(i interface{}) error {
dal, ok := i.(sdk.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if !dal.IsPositive() {
return fmt.Errorf("debt auction lot should be positive: %s", dal)
}
return nil
}