/
params.go
130 lines (105 loc) · 3.49 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
package types
import (
"fmt"
"gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
core "github.com/terra-project/core/types"
)
// DefaultParamspace nolint
const DefaultParamspace = ModuleName
// Parameter keys
var (
//Terra liquidity pool(usdr unit) made available per ${poolrecoveryperiod} (usdr unit)
ParamStoreKeyBasePool = []byte("basepool")
// The period required to recover BasePool
ParamStoreKeyPoolRecoveryPeriod = []byte("poolrecoveryperiod")
// Min spread
ParamStoreKeyMinStabilitySpread = []byte("minstabilityspread")
)
// Default parameter values
var (
DefaultBasePool = sdk.NewDec(250000 * core.MicroUnit) // 250,000sdr = 250,000,000,000usdr
DefaultPoolRecoveryPeriod = core.BlocksPerDay // 14,400
DefaultMinStabilitySpread = sdk.NewDecWithPrec(2, 2) // 2%
)
var _ params.ParamSet = &Params{}
// Params market parameters
type Params struct {
BasePool sdk.Dec `json:"base_pool" yaml:"base_pool"`
PoolRecoveryPeriod int64 `json:"pool_recovery_period" yaml:"pool_recovery_period"`
MinStabilitySpread sdk.Dec `json:"min_spread" yaml:"min_spread"`
}
// DefaultParams creates default market module parameters
func DefaultParams() Params {
return Params{
BasePool: DefaultBasePool,
PoolRecoveryPeriod: DefaultPoolRecoveryPeriod,
MinStabilitySpread: DefaultMinStabilitySpread,
}
}
// ParamKeyTable returns the parameter key table.
func ParamKeyTable() params.KeyTable {
return params.NewKeyTable().RegisterParamSet(&Params{})
}
// String implements fmt.Stringer interface
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
// pairs of market module's parameters.
// nolint
func (p *Params) ParamSetPairs() params.ParamSetPairs {
return params.ParamSetPairs{
params.NewParamSetPair(ParamStoreKeyBasePool, &p.BasePool, validateBasePool),
params.NewParamSetPair(ParamStoreKeyPoolRecoveryPeriod, &p.PoolRecoveryPeriod, validatePoolRecoveryPeriod),
params.NewParamSetPair(ParamStoreKeyMinStabilitySpread, &p.MinStabilitySpread, validateMinStatbilitySpread),
}
}
// ValidateBasic a set of params
func (p Params) ValidateBasic() error {
if p.BasePool.IsNegative() {
return fmt.Errorf("base pool should be positive or zero, is %s", p.BasePool)
}
if p.PoolRecoveryPeriod <= 0 {
return fmt.Errorf("pool recovery period should be positive, is %d", p.PoolRecoveryPeriod)
}
if p.MinStabilitySpread.IsNegative() || p.MinStabilitySpread.GT(sdk.OneDec()) {
return fmt.Errorf("market minimum stability spead should be a value between [0,1], is %s", p.MinStabilitySpread)
}
return nil
}
func validateBasePool(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("base pool must be positive or zero: %s", v)
}
return nil
}
func validatePoolRecoveryPeriod(i interface{}) error {
v, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 {
return fmt.Errorf("pool recovery period must be positive: %d", v)
}
return nil
}
func validateMinStatbilitySpread(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("min spread must be positive or zero: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("min spread is too large: %s", v)
}
return nil
}