-
Notifications
You must be signed in to change notification settings - Fork 55
/
params.go
178 lines (143 loc) 路 5.17 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
package types
import (
"fmt"
"strings"
"gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
farmingtypes "github.com/crescent-network/crescent/v2/x/farming/types"
)
// Parameter store keys
var (
KeyLiquidBondDenom = []byte("LiquidBondDenom")
KeyWhitelistedValidators = []byte("WhitelistedValidators")
KeyUnstakeFeeRate = []byte("UnstakeFeeRate")
KeyMinLiquidStakingAmount = []byte("MinLiquidStakingAmount")
DefaultLiquidBondDenom = "bstake"
// DefaultUnstakeFeeRate is the default Unstake Fee Rate.
DefaultUnstakeFeeRate = sdk.NewDecWithPrec(1, 3) // "0.001000000000000000"
// DefaultMinLiquidStakingAmount is the default minimum liquid staking amount.
DefaultMinLiquidStakingAmount = sdk.NewInt(1000000)
// Const variables
// RebalancingTrigger if the maximum difference and needed each redelegation amount exceeds it, asset rebalacing will be executed.
RebalancingTrigger = sdk.NewDecWithPrec(1, 3) // "0.001000000000000000"
// RewardTrigger If the sum of balance and the upcoming rewards of LiquidStakingProxyAcc exceeds it, the reward is automatically withdrawn and re-stake according to the weights.
RewardTrigger = sdk.NewDecWithPrec(1, 3) // "0.001000000000000000"
// LiquidStakingProxyAcc is a proxy reserve account for delegation and undelegation.
LiquidStakingProxyAcc = farmingtypes.DeriveAddress(farmingtypes.AddressType32Bytes, ModuleName, "LiquidStakingProxyAcc")
)
var _ paramstypes.ParamSet = (*Params)(nil)
// ParamKeyTable returns the parameter key table.
func ParamKeyTable() paramstypes.KeyTable {
return paramstypes.NewKeyTable().RegisterParamSet(&Params{})
}
// DefaultParams returns the default liquidstaking module parameters.
func DefaultParams() Params {
return Params{
WhitelistedValidators: []WhitelistedValidator{},
LiquidBondDenom: DefaultLiquidBondDenom,
UnstakeFeeRate: DefaultUnstakeFeeRate,
MinLiquidStakingAmount: DefaultMinLiquidStakingAmount,
}
}
// ParamSetPairs implements paramstypes.ParamSet.
func (p *Params) ParamSetPairs() paramstypes.ParamSetPairs {
return paramstypes.ParamSetPairs{
paramstypes.NewParamSetPair(KeyLiquidBondDenom, &p.LiquidBondDenom, validateLiquidBondDenom),
paramstypes.NewParamSetPair(KeyWhitelistedValidators, &p.WhitelistedValidators, validateWhitelistedValidators),
paramstypes.NewParamSetPair(KeyUnstakeFeeRate, &p.UnstakeFeeRate, validateUnstakeFeeRate),
paramstypes.NewParamSetPair(KeyMinLiquidStakingAmount, &p.MinLiquidStakingAmount, validateMinLiquidStakingAmount),
}
}
// String returns a human-readable string representation of the parameters.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
func (p Params) WhitelistedValsMap() WhitelistedValsMap {
return GetWhitelistedValsMap(p.WhitelistedValidators)
}
// Validate validates parameters.
func (p Params) Validate() error {
for _, v := range []struct {
value interface{}
validator func(interface{}) error
}{
{p.LiquidBondDenom, validateLiquidBondDenom},
{p.WhitelistedValidators, validateWhitelistedValidators},
{p.UnstakeFeeRate, validateUnstakeFeeRate},
{p.MinLiquidStakingAmount, validateMinLiquidStakingAmount},
} {
if err := v.validator(v.value); err != nil {
return err
}
}
return nil
}
func validateLiquidBondDenom(i interface{}) error {
v, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if strings.TrimSpace(v) == "" {
return fmt.Errorf("liquid bond denom cannot be blank")
}
if err := sdk.ValidateDenom(v); err != nil {
return err
}
return nil
}
// validateWhitelistedValidators validates liquidstaking validator and total weight.
func validateWhitelistedValidators(i interface{}) error {
wvs, ok := i.([]WhitelistedValidator)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
valsMap := map[string]struct{}{}
for _, wv := range wvs {
_, valErr := sdk.ValAddressFromBech32(wv.ValidatorAddress)
if valErr != nil {
return valErr
}
if wv.TargetWeight.IsNil() {
return fmt.Errorf("liquidstaking validator target weight must not be nil")
}
if !wv.TargetWeight.IsPositive() {
return fmt.Errorf("liquidstaking validator target weight must be positive: %s", wv.TargetWeight)
}
if _, ok := valsMap[wv.ValidatorAddress]; ok {
return fmt.Errorf("liquidstaking validator cannot be duplicated: %s", wv.ValidatorAddress)
}
valsMap[wv.ValidatorAddress] = struct{}{}
}
return nil
}
func validateUnstakeFeeRate(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("unstake fee rate must not be nil")
}
if v.IsNegative() {
return fmt.Errorf("unstake fee rate must not be negative: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("unstake fee rate too large: %s", v)
}
return nil
}
func validateMinLiquidStakingAmount(i interface{}) error {
v, ok := i.(sdk.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("min liquid staking amount must not be nil")
}
if v.IsNegative() {
return fmt.Errorf("min liquid staking amount must not be negative: %s", v)
}
return nil
}