-
Notifications
You must be signed in to change notification settings - Fork 4
/
params.go
233 lines (196 loc) · 5.99 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package types
import (
"errors"
"fmt"
"strings"
yaml "gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
// Parameter store keys
var (
KeyMintDenom = []byte("MintDenom")
KeyInflationRateChange = []byte("InflationRateChange")
KeyInflationMax = []byte("InflationMax")
KeyInflationMin = []byte("InflationMin")
KeyGoalBonded = []byte("GoalBonded")
KeyBlocksPerYear = []byte("BlocksPerYear")
KeyDistributionProportions = []byte("DistributionProportions")
)
// ParamTable for minting module.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
func NewParams(
mintDenom string, inflationRateChange, inflationMax, inflationMin, goalBonded sdk.Dec, blocksPerYear uint64,
distrProportions DistributionProportions,
) Params {
return Params{
MintDenom: mintDenom,
InflationRateChange: inflationRateChange,
InflationMax: inflationMax,
InflationMin: inflationMin,
GoalBonded: goalBonded,
BlocksPerYear: blocksPerYear,
DistributionProportions: distrProportions,
}
}
// default minting module parameters
func DefaultParams() Params {
return Params{
MintDenom: sdk.DefaultBondDenom,
InflationRateChange: sdk.NewDecWithPrec(13, 2),
InflationMax: sdk.NewDecWithPrec(20, 2),
InflationMin: sdk.NewDecWithPrec(7, 2),
GoalBonded: sdk.NewDecWithPrec(67, 2),
BlocksPerYear: uint64(60 * 60 * 8766 / 6), // assuming 5second block times
DistributionProportions: DistributionProportions{
Staking: sdk.NewDecWithPrec(5, 1), // 0.5
LpIncentives: sdk.NewDecWithPrec(4, 1), // 0.4
CommunityPool: sdk.NewDecWithPrec(1, 1), // 0.1
},
}
}
// validate params
func (p Params) Validate() error {
if err := validateMintDenom(p.MintDenom); err != nil {
return err
}
if err := validateInflationRateChange(p.InflationRateChange); err != nil {
return err
}
if err := validateInflationMax(p.InflationMax); err != nil {
return err
}
if err := validateInflationMin(p.InflationMin); err != nil {
return err
}
if err := validateGoalBonded(p.GoalBonded); err != nil {
return err
}
if err := validateBlocksPerYear(p.BlocksPerYear); err != nil {
return err
}
if err := validateDistributionProportions(p.DistributionProportions); err != nil {
return err
}
if p.InflationMax.LT(p.InflationMin) {
return fmt.Errorf(
"max inflation (%s) must be greater than or equal to min inflation (%s)",
p.InflationMax, p.InflationMin,
)
}
return nil
}
// String implements the Stringer interface.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// Implements params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyMintDenom, &p.MintDenom, validateMintDenom),
paramtypes.NewParamSetPair(KeyInflationRateChange, &p.InflationRateChange, validateInflationRateChange),
paramtypes.NewParamSetPair(KeyInflationMax, &p.InflationMax, validateInflationMax),
paramtypes.NewParamSetPair(KeyInflationMin, &p.InflationMin, validateInflationMin),
paramtypes.NewParamSetPair(KeyGoalBonded, &p.GoalBonded, validateGoalBonded),
paramtypes.NewParamSetPair(KeyBlocksPerYear, &p.BlocksPerYear, validateBlocksPerYear),
paramtypes.NewParamSetPair(KeyDistributionProportions, &p.DistributionProportions, validateDistributionProportions),
}
}
func validateMintDenom(i interface{}) error {
v, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if strings.TrimSpace(v) == "" {
return errors.New("mint denom cannot be blank")
}
if err := sdk.ValidateDenom(v); err != nil {
return err
}
return nil
}
func validateInflationRateChange(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("inflation rate change cannot be negative: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("inflation rate change too large: %s", v)
}
return nil
}
func validateInflationMax(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("max inflation cannot be negative: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("max inflation too large: %s", v)
}
return nil
}
func validateInflationMin(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 inflation cannot be negative: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("min inflation too large: %s", v)
}
return nil
}
func validateGoalBonded(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("goal bonded cannot be negative: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("goal bonded too large: %s", v)
}
return nil
}
func validateBlocksPerYear(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("blocks per year must be positive: %d", v)
}
return nil
}
func validateDistributionProportions(i interface{}) error {
v, ok := i.(DistributionProportions)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.Staking.IsNegative() {
return errors.New("staking distribution ratio should not be negative")
}
if v.LpIncentives.IsNegative() {
return errors.New("pool incentives distribution ratio should not be negative")
}
if v.CommunityPool.IsNegative() {
return errors.New("community pool distribution ratio should not be negative")
}
totalProportions := v.Staking.Add(v.LpIncentives).Add(v.CommunityPool)
if !totalProportions.Equal(sdk.NewDec(1)) {
return errors.New("total distributions ratio should be 1")
}
return nil
}