github.com/gravity-devs/liquidity@v1.5.3/x/liquidity/types/params.go (about) 1 package types 2 3 import ( 4 "fmt" 5 6 sdk "github.com/cosmos/cosmos-sdk/types" 7 paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" 8 "gopkg.in/yaml.v2" 9 ) 10 11 const ( 12 // CancelOrderLifeSpan is the lifespan of order cancellation. 13 CancelOrderLifeSpan int64 = 0 14 15 // MinReserveCoinNum is the minimum number of reserve coins in each liquidity pool. 16 MinReserveCoinNum uint32 = 2 17 18 // MaxReserveCoinNum is the maximum number of reserve coins in each liquidity pool. 19 MaxReserveCoinNum uint32 = 2 20 21 // DefaultUnitBatchHeight is the default number of blocks in one batch. This param is used for scalability. 22 DefaultUnitBatchHeight uint32 = 1 23 24 // DefaultPoolTypeID is the default pool type id. The only supported pool type id is 1. 25 DefaultPoolTypeID uint32 = 1 26 27 // DefaultSwapTypeID is the default swap type id. The only supported swap type (instant swap) id is 1. 28 DefaultSwapTypeID uint32 = 1 29 30 // DefaultCircuitBreakerEnabled is the default circuit breaker status. This param is used for a contingency plan. 31 DefaultCircuitBreakerEnabled = false 32 ) 33 34 // Parameter store keys 35 var ( 36 KeyPoolTypes = []byte("PoolTypes") 37 KeyMinInitDepositAmount = []byte("MinInitDepositAmount") 38 KeyInitPoolCoinMintAmount = []byte("InitPoolCoinMintAmount") 39 KeyMaxReserveCoinAmount = []byte("MaxReserveCoinAmount") 40 KeySwapFeeRate = []byte("SwapFeeRate") 41 KeyPoolCreationFee = []byte("PoolCreationFee") 42 KeyUnitBatchHeight = []byte("UnitBatchHeight") 43 KeyWithdrawFeeRate = []byte("WithdrawFeeRate") 44 KeyMaxOrderAmountRatio = []byte("MaxOrderAmountRatio") 45 KeyCircuitBreakerEnabled = []byte("CircuitBreakerEnabled") 46 ) 47 48 var ( 49 DefaultMinInitDepositAmount = sdk.NewInt(1000000) 50 DefaultInitPoolCoinMintAmount = sdk.NewInt(1000000) 51 DefaultMaxReserveCoinAmount = sdk.ZeroInt() 52 DefaultSwapFeeRate = sdk.NewDecWithPrec(3, 3) // "0.003000000000000000" 53 DefaultWithdrawFeeRate = sdk.ZeroDec() 54 DefaultMaxOrderAmountRatio = sdk.NewDecWithPrec(1, 1) // "0.100000000000000000" 55 DefaultPoolCreationFee = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(40000000))) 56 DefaultPoolType = PoolType{ 57 Id: 1, 58 Name: "StandardLiquidityPool", 59 MinReserveCoinNum: MinReserveCoinNum, 60 MaxReserveCoinNum: MaxReserveCoinNum, 61 Description: "Standard liquidity pool with pool price function X/Y, ESPM constraint, and two kinds of reserve coins", 62 } 63 DefaultPoolTypes = []PoolType{DefaultPoolType} 64 65 MinOfferCoinAmount = sdk.NewInt(100) 66 ) 67 68 var _ paramstypes.ParamSet = (*Params)(nil) 69 70 // ParamKeyTable returns the parameter key table. 71 func ParamKeyTable() paramstypes.KeyTable { 72 return paramstypes.NewKeyTable().RegisterParamSet(&Params{}) 73 } 74 75 // DefaultParams returns the default liquidity module parameters. 76 func DefaultParams() Params { 77 return Params{ 78 PoolTypes: DefaultPoolTypes, 79 MinInitDepositAmount: DefaultMinInitDepositAmount, 80 InitPoolCoinMintAmount: DefaultInitPoolCoinMintAmount, 81 MaxReserveCoinAmount: DefaultMaxReserveCoinAmount, 82 PoolCreationFee: DefaultPoolCreationFee, 83 SwapFeeRate: DefaultSwapFeeRate, 84 WithdrawFeeRate: DefaultWithdrawFeeRate, 85 MaxOrderAmountRatio: DefaultMaxOrderAmountRatio, 86 UnitBatchHeight: DefaultUnitBatchHeight, 87 CircuitBreakerEnabled: DefaultCircuitBreakerEnabled, 88 } 89 } 90 91 // ParamSetPairs implements paramstypes.ParamSet. 92 func (p *Params) ParamSetPairs() paramstypes.ParamSetPairs { 93 return paramstypes.ParamSetPairs{ 94 paramstypes.NewParamSetPair(KeyPoolTypes, &p.PoolTypes, validatePoolTypes), 95 paramstypes.NewParamSetPair(KeyMinInitDepositAmount, &p.MinInitDepositAmount, validateMinInitDepositAmount), 96 paramstypes.NewParamSetPair(KeyInitPoolCoinMintAmount, &p.InitPoolCoinMintAmount, validateInitPoolCoinMintAmount), 97 paramstypes.NewParamSetPair(KeyMaxReserveCoinAmount, &p.MaxReserveCoinAmount, validateMaxReserveCoinAmount), 98 paramstypes.NewParamSetPair(KeyPoolCreationFee, &p.PoolCreationFee, validatePoolCreationFee), 99 paramstypes.NewParamSetPair(KeySwapFeeRate, &p.SwapFeeRate, validateSwapFeeRate), 100 paramstypes.NewParamSetPair(KeyWithdrawFeeRate, &p.WithdrawFeeRate, validateWithdrawFeeRate), 101 paramstypes.NewParamSetPair(KeyMaxOrderAmountRatio, &p.MaxOrderAmountRatio, validateMaxOrderAmountRatio), 102 paramstypes.NewParamSetPair(KeyUnitBatchHeight, &p.UnitBatchHeight, validateUnitBatchHeight), 103 paramstypes.NewParamSetPair(KeyCircuitBreakerEnabled, &p.CircuitBreakerEnabled, validateCircuitBreakerEnabled), 104 } 105 } 106 107 // String returns a human readable string representation of the parameters. 108 func (p Params) String() string { 109 out, _ := yaml.Marshal(p) 110 return string(out) 111 } 112 113 // Validate validates parameters. 114 func (p Params) Validate() error { 115 for _, v := range []struct { 116 value interface{} 117 validator func(interface{}) error 118 }{ 119 {p.PoolTypes, validatePoolTypes}, 120 {p.MinInitDepositAmount, validateMinInitDepositAmount}, 121 {p.InitPoolCoinMintAmount, validateInitPoolCoinMintAmount}, 122 {p.MaxReserveCoinAmount, validateMaxReserveCoinAmount}, 123 {p.PoolCreationFee, validatePoolCreationFee}, 124 {p.SwapFeeRate, validateSwapFeeRate}, 125 {p.WithdrawFeeRate, validateWithdrawFeeRate}, 126 {p.MaxOrderAmountRatio, validateMaxOrderAmountRatio}, 127 {p.UnitBatchHeight, validateUnitBatchHeight}, 128 {p.CircuitBreakerEnabled, validateCircuitBreakerEnabled}, 129 } { 130 if err := v.validator(v.value); err != nil { 131 return err 132 } 133 } 134 return nil 135 } 136 137 func validatePoolTypes(i interface{}) error { 138 v, ok := i.([]PoolType) 139 if !ok { 140 return fmt.Errorf("invalid parameter type: %T", i) 141 } 142 143 if len(v) == 0 { 144 return fmt.Errorf("pool types must not be empty") 145 } 146 147 for i, p := range v { 148 if int(p.Id) != i+1 { 149 return fmt.Errorf("pool type ids must be sorted") 150 } 151 if p.MaxReserveCoinNum > MaxReserveCoinNum || MinReserveCoinNum > p.MinReserveCoinNum { 152 return fmt.Errorf("min, max reserve coin num value of pool types are out of bounds") 153 } 154 } 155 156 if len(v) > 1 || !v[0].Equal(DefaultPoolType) { 157 return fmt.Errorf("the only supported pool type is 1") 158 } 159 160 return nil 161 } 162 163 func validateMinInitDepositAmount(i interface{}) error { 164 v, ok := i.(sdk.Int) 165 if !ok { 166 return fmt.Errorf("invalid parameter type: %T", i) 167 } 168 169 if v.IsNil() { 170 return fmt.Errorf("minimum initial deposit amount must not be nil") 171 } 172 173 if !v.IsPositive() { 174 return fmt.Errorf("minimum initial deposit amount must be positive: %s", v) 175 } 176 177 return nil 178 } 179 180 func validateInitPoolCoinMintAmount(i interface{}) error { 181 v, ok := i.(sdk.Int) 182 if !ok { 183 return fmt.Errorf("invalid parameter type: %T", i) 184 } 185 186 if v.IsNil() { 187 return fmt.Errorf("initial pool coin mint amount must not be nil") 188 } 189 190 if !v.IsPositive() { 191 return fmt.Errorf("initial pool coin mint amount must be positive: %s", v) 192 } 193 194 if v.LT(DefaultInitPoolCoinMintAmount) { 195 return fmt.Errorf("initial pool coin mint amount must be greater than or equal to 1000000: %s", v) 196 } 197 198 return nil 199 } 200 201 func validateMaxReserveCoinAmount(i interface{}) error { 202 v, ok := i.(sdk.Int) 203 if !ok { 204 return fmt.Errorf("invalid parameter type: %T", i) 205 } 206 207 if v.IsNil() { 208 return fmt.Errorf("max reserve coin amount must not be nil") 209 } 210 211 if v.IsNegative() { 212 return fmt.Errorf("max reserve coin amount must not be negative: %s", v) 213 } 214 215 return nil 216 } 217 218 func validateSwapFeeRate(i interface{}) error { 219 v, ok := i.(sdk.Dec) 220 if !ok { 221 return fmt.Errorf("invalid parameter type: %T", i) 222 } 223 224 if v.IsNil() { 225 return fmt.Errorf("swap fee rate must not be nil") 226 } 227 228 if v.IsNegative() { 229 return fmt.Errorf("swap fee rate must not be negative: %s", v) 230 } 231 232 if v.GT(sdk.OneDec()) { 233 return fmt.Errorf("swap fee rate too large: %s", v) 234 } 235 236 return nil 237 } 238 239 func validateWithdrawFeeRate(i interface{}) error { 240 v, ok := i.(sdk.Dec) 241 if !ok { 242 return fmt.Errorf("invalid parameter type: %T", i) 243 } 244 245 if v.IsNil() { 246 return fmt.Errorf("withdraw fee rate must not be nil") 247 } 248 249 if v.IsNegative() { 250 return fmt.Errorf("withdraw fee rate must not be negative: %s", v) 251 } 252 253 if v.GT(sdk.OneDec()) { 254 return fmt.Errorf("withdraw fee rate too large: %s", v) 255 } 256 257 return nil 258 } 259 260 func validateMaxOrderAmountRatio(i interface{}) error { 261 v, ok := i.(sdk.Dec) 262 if !ok { 263 return fmt.Errorf("invalid parameter type: %T", i) 264 } 265 266 if v.IsNil() { 267 return fmt.Errorf("max order amount ratio must not be nil") 268 } 269 270 if v.IsNegative() { 271 return fmt.Errorf("max order amount ratio must not be negative: %s", v) 272 } 273 274 if v.GT(sdk.OneDec()) { 275 return fmt.Errorf("max order amount ratio too large: %s", v) 276 } 277 278 return nil 279 } 280 281 func validatePoolCreationFee(i interface{}) error { 282 v, ok := i.(sdk.Coins) 283 if !ok { 284 return fmt.Errorf("invalid parameter type: %T", i) 285 } 286 287 if err := v.Validate(); err != nil { 288 return err 289 } 290 291 if v.Empty() { 292 return fmt.Errorf("pool creation fee must not be empty") 293 } 294 295 return nil 296 } 297 298 func validateUnitBatchHeight(i interface{}) error { 299 v, ok := i.(uint32) 300 if !ok { 301 return fmt.Errorf("invalid parameter type: %T", i) 302 } 303 304 if v == 0 { 305 return fmt.Errorf("unit batch height must be positive: %d", v) 306 } 307 308 return nil 309 } 310 311 func validateCircuitBreakerEnabled(i interface{}) error { 312 _, ok := i.(bool) 313 if !ok { 314 return fmt.Errorf("invalid parameter type: %T", i) 315 } 316 317 return nil 318 }