forked from tendermint/liquidity
/
params.go
187 lines (155 loc) · 5.3 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
package types
import (
"fmt"
"gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
const DefaultBatchSize uint32 = 1
// Parameter store keys
var (
KeyLiquidityPoolTypes = []byte("LiquidityPoolTypes")
KeyMinInitDepositToPool = []byte("MinInitDepositToPool")
KeyInitPoolCoinMintAmount = []byte("InitPoolCoinMintAmount")
KeySwapFeeRate = []byte("SwapFeeRate")
KeyLiquidityPoolFeeRate = []byte("LiquidityPoolFeeRate")
KeyLiquidityPoolCreationFee = []byte("LiquidityPoolCreationFee")
KeyUnitBatchSize = []byte("UnitBatchSize")
LiquidityPoolTypeConstantProduct = LiquidityPoolType{
PoolTypeIndex: 0,
Name: "ConstantProductLiquidityPool",
MinReserveCoinNum: 2,
MaxReserveCoinNum: 2,
}
)
// NewParams liquidity paramtypes constructor
func NewParams(liquidityPoolTypes []LiquidityPoolType, minInitDeposit, initPoolCoinMint sdk.Int, swapFeeRate, poolFeeRate sdk.Dec, creationFee sdk.Coins, unitBatchSize uint32) Params {
return Params{
LiquidityPoolTypes: liquidityPoolTypes,
MinInitDepositToPool: minInitDeposit,
InitPoolCoinMintAmount: initPoolCoinMint,
SwapFeeRate: swapFeeRate,
LiquidityPoolFeeRate: poolFeeRate,
LiquidityPoolCreationFee: creationFee,
UnitBatchSize: unitBatchSize,
}
}
// ParamTypeTable returns the TypeTable for liquidity module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// KeyValuePairs implements paramtypes.KeyValuePairs
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyLiquidityPoolTypes, &p.LiquidityPoolTypes, validateLiquidityPoolTypes),
paramtypes.NewParamSetPair(KeyMinInitDepositToPool, &p.MinInitDepositToPool, validateMinInitDepositToPool),
paramtypes.NewParamSetPair(KeyInitPoolCoinMintAmount, &p.InitPoolCoinMintAmount, validateInitPoolCoinMintAmount),
paramtypes.NewParamSetPair(KeySwapFeeRate, &p.SwapFeeRate, validateSwapFeeRate),
paramtypes.NewParamSetPair(KeyLiquidityPoolFeeRate, &p.LiquidityPoolFeeRate, validateLiquidityPoolFeeRate),
paramtypes.NewParamSetPair(KeyLiquidityPoolCreationFee, &p.LiquidityPoolCreationFee, validateLiquidityPoolCreationFee),
paramtypes.NewParamSetPair(KeyUnitBatchSize, &p.UnitBatchSize, validateUnitBatchSize),
}
}
// DefaultParams returns the default liquidity module parameters
func DefaultParams() Params {
var defaultLiquidityPoolTypes []LiquidityPoolType
defaultLiquidityPoolTypes = append(defaultLiquidityPoolTypes, LiquidityPoolTypeConstantProduct)
return Params{
LiquidityPoolTypes: defaultLiquidityPoolTypes,
MinInitDepositToPool: sdk.NewInt(1000000),
InitPoolCoinMintAmount: sdk.NewInt(1000000),
SwapFeeRate: sdk.NewDecWithPrec(1, 3), // "0.001000000000000000"
LiquidityPoolFeeRate: sdk.NewDecWithPrec(1, 3), // "0.001000000000000000"
LiquidityPoolCreationFee: sdk.NewCoins(sdk.NewCoin("uatom", sdk.NewInt(100000000))),
UnitBatchSize: 1,
}
}
// String returns a human readable string representation of the parameters.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// Validate returns err if Params is invalid
func (p Params) Validate() error {
// TODO: add detail validate logic
return nil
}
func validateLiquidityPoolTypes(i interface{}) error {
v, ok := i.([]LiquidityPoolType)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
for i, p := range v {
if i != int(p.PoolTypeIndex) {
return fmt.Errorf("LiquidityPoolTypes index must be sorted")
}
}
return nil
}
func validateMinInitDepositToPool(i interface{}) error {
v, ok := i.(sdk.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == sdk.ZeroInt() {
return fmt.Errorf("MinInitDepositToPool must be positive: %d", v)
}
return nil
}
func validateInitPoolCoinMintAmount(i interface{}) error {
v, ok := i.(sdk.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == sdk.ZeroInt() {
return fmt.Errorf("InitPoolCoinMintAmount must be positive: %d", v)
}
return nil
}
func validateSwapFeeRate(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("SwapFeeRate cannot be negative: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("SwapFeeRate too large: %s", v)
}
return nil
}
func validateLiquidityPoolFeeRate(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("LiquidityPoolFeeRate cannot be negative: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("LiquidityPoolFeeRate too large: %s", v)
}
return nil
}
func validateLiquidityPoolCreationFee(i interface{}) error {
v, ok := i.([]sdk.Coin)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
coins := (sdk.Coins)(v)
if coins.Empty() {
return fmt.Errorf("LiquidityPoolCreationFee cannot be Empty: %s", v)
}
return nil
}
func validateUnitBatchSize(i interface{}) error {
v, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if 0 >= v {
return fmt.Errorf("UnitBatchSize cannot be negative: %d", v)
}
return nil
}