-
Notifications
You must be signed in to change notification settings - Fork 55
/
params.go
153 lines (135 loc) 路 4.57 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
package types
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
utils "github.com/crescent-network/crescent/v5/types"
)
var _ paramstypes.ParamSet = (*Params)(nil)
var (
KeyMarketCreationFee = []byte("MarketCreationFee")
KeyFees = []byte("Fees")
KeyMaxOrderLifespan = []byte("MaxOrderLifespan")
KeyMaxOrderPriceRatio = []byte("MaxOrderPriceRatio")
KeyMaxSwapRoutesLen = []byte("MaxSwapRoutesLen")
KeyMaxNumMMOrders = []byte("MaxNumMMOrders")
)
var (
DefaultMarketCreationFee = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000000))
DefaultFees = Fees{
DefaultMakerFeeRate: sdk.NewDecWithPrec(15, 4), // 0.15%
DefaultTakerFeeRate: sdk.NewDecWithPrec(3, 3), // 0.3%
DefaultOrderSourceFeeRatio: sdk.NewDecWithPrec(5, 1), // 50%
}
DefaultMaxOrderLifespan = 24 * time.Hour
DefaultMaxOrderPriceRatio = sdk.NewDecWithPrec(1, 1) // 10%
DefaultMaxSwapRoutesLen uint32 = 3
DefaultMaxNumMMOrders uint32 = 15
MinPrice = sdk.NewDecWithPrec(1, 14)
MaxPrice = sdk.NewDecFromInt(sdk.NewIntWithDecimal(1, 40))
)
func ParamKeyTable() paramstypes.KeyTable {
return paramstypes.NewKeyTable().RegisterParamSet(&Params{})
}
func NewParams(
marketCreationFee sdk.Coins, fees Fees, maxOrderLifespan time.Duration, maxOrderPriceRatio sdk.Dec,
maxSwapRoutesLen, maxNumMMOrders uint32) Params {
return Params{
MarketCreationFee: marketCreationFee,
Fees: fees,
MaxOrderLifespan: maxOrderLifespan,
MaxOrderPriceRatio: maxOrderPriceRatio,
MaxSwapRoutesLen: maxSwapRoutesLen,
MaxNumMMOrders: maxNumMMOrders,
}
}
// DefaultParams returns a default params for the module.
func DefaultParams() Params {
return NewParams(
DefaultMarketCreationFee, DefaultFees, DefaultMaxOrderLifespan, DefaultMaxOrderPriceRatio,
DefaultMaxSwapRoutesLen, DefaultMaxNumMMOrders)
}
// ParamSetPairs implements ParamSet.
func (params *Params) ParamSetPairs() paramstypes.ParamSetPairs {
return paramstypes.ParamSetPairs{
paramstypes.NewParamSetPair(KeyMarketCreationFee, ¶ms.MarketCreationFee, validateMarketCreationFee),
paramstypes.NewParamSetPair(KeyFees, ¶ms.Fees, validateFees),
paramstypes.NewParamSetPair(KeyMaxOrderLifespan, ¶ms.MaxOrderLifespan, validateMaxOrderLifespan),
paramstypes.NewParamSetPair(KeyMaxOrderPriceRatio, ¶ms.MaxOrderPriceRatio, validateMaxOrderPriceRatio),
paramstypes.NewParamSetPair(KeyMaxSwapRoutesLen, ¶ms.MaxSwapRoutesLen, validateMaxSwapRoutesLen),
paramstypes.NewParamSetPair(KeyMaxNumMMOrders, ¶ms.MaxNumMMOrders, validateMaxNumMMOrders),
}
}
// Validate validates Params.
func (params Params) Validate() error {
for _, field := range []struct {
val interface{}
validateFunc func(i interface{}) error
}{
{params.MarketCreationFee, validateMarketCreationFee},
{params.Fees, validateFees},
{params.MaxOrderLifespan, validateMaxOrderLifespan},
{params.MaxOrderPriceRatio, validateMaxOrderPriceRatio},
{params.MaxSwapRoutesLen, validateMaxSwapRoutesLen},
{params.MaxNumMMOrders, validateMaxNumMMOrders},
} {
if err := field.validateFunc(field.val); err != nil {
return err
}
}
return nil
}
func validateMarketCreationFee(i interface{}) error {
v, ok := i.(sdk.Coins)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if err := v.Validate(); err != nil {
return fmt.Errorf("invalid market creation fee: %w", err)
}
return nil
}
func validateFees(i interface{}) error {
v, ok := i.(Fees)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return ValidateFees(v.DefaultMakerFeeRate, v.DefaultTakerFeeRate, v.DefaultOrderSourceFeeRatio)
}
func validateMaxOrderLifespan(i interface{}) error {
v, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v < 0 {
return fmt.Errorf("max order lifespan must not be negative: %v", v)
}
return nil
}
func validateMaxOrderPriceRatio(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if !(v.IsPositive() && v.LT(utils.OneDec)) {
return fmt.Errorf("max order price ratio must be in range (0.0, 1.0): %s", v)
}
return nil
}
func validateMaxSwapRoutesLen(i interface{}) error {
v, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("max swap routes len must not be 0")
}
return nil
}
func validateMaxNumMMOrders(i interface{}) error {
if _, ok := i.(uint32); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}