-
Notifications
You must be signed in to change notification settings - Fork 200
/
params.go
289 lines (238 loc) · 8.12 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package types
import (
"fmt"
time "time"
"gopkg.in/yaml.v2"
"github.com/NibiruChain/nibiru/x/common/asset"
"github.com/NibiruChain/nibiru/x/common/denoms"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
// Parameter keys
var (
KeyVotePeriod = []byte("VotePeriod")
KeyVoteThreshold = []byte("VoteThreshold")
KeyMinVoters = []byte("MinVoters")
KeyRewardBand = []byte("RewardBand")
KeyWhitelist = []byte("Whitelist")
KeySlashFraction = []byte("SlashFraction")
KeySlashWindow = []byte("SlashWindow")
KeyMinValidPerWindow = []byte("MinValidPerWindow")
KeyTwapLookbackWindow = []byte("TwapLookbackWindow")
)
// Default parameter values
// TODO(mercilex): need to adjust this based on expected genesis parameters, this assumes block times are 1s
// DefaultVotePeriod: 10s
// DefaultSlashWindow: 1 Week
const (
DefaultVotePeriod = 10 // vote every 10s
DefaultSlashWindow = 604800 // 1 week
DefaultMinVoters = 4 // minimum of 4 voters for a pair to become valid
)
// Default parameter values
var (
DefaultVoteThreshold = sdk.OneDec().Quo(sdk.NewDec(3)) // 33.33%
DefaultRewardBand = sdk.NewDecWithPrec(2, 2) // 2% (-1, 1)
DefaultWhitelist = []asset.Pair{
// paired against NUSD
asset.Registry.Pair(denoms.NIBI, denoms.NUSD),
asset.Registry.Pair(denoms.BTC, denoms.NUSD),
asset.Registry.Pair(denoms.ETH, denoms.NUSD),
asset.Registry.Pair(denoms.ATOM, denoms.NUSD),
asset.Registry.Pair(denoms.BNB, denoms.NUSD),
asset.Registry.Pair(denoms.USDC, denoms.NUSD),
asset.Registry.Pair(denoms.USDT, denoms.NUSD),
// asset.Registry.Pair(denoms.OSMO, denoms.NUSD),
// asset.Registry.Pair(denoms.AVAX, denoms.NUSD),
// asset.Registry.Pair(denoms.SOL, denoms.NUSD),
// asset.Registry.Pair(denoms.ADA, denoms.NUSD),
// paired against the US fiat dollar
asset.Registry.Pair(denoms.NIBI, denoms.USD),
asset.Registry.Pair(denoms.BTC, denoms.USD),
asset.Registry.Pair(denoms.ETH, denoms.USD),
asset.Registry.Pair(denoms.ATOM, denoms.USD),
asset.Registry.Pair(denoms.BNB, denoms.USD),
asset.Registry.Pair(denoms.USDC, denoms.USD),
asset.Registry.Pair(denoms.USDT, denoms.USD),
// asset.Registry.Pair(denoms.OSMO, denoms.USD),
// asset.Registry.Pair(denoms.AVAX, denoms.USD),
// asset.Registry.Pair(denoms.SOL, denoms.USD),
// asset.Registry.Pair(denoms.ADA, denoms.USD),
}
DefaultSlashFraction = sdk.NewDecWithPrec(1, 4) // 0.01%
DefaultMinValidPerWindow = sdk.NewDecWithPrec(5, 2) // 5%
DefaultTwapLookbackWindow = time.Duration(15 * time.Minute) // 15 minutes
)
var _ paramstypes.ParamSet = &Params{}
// DefaultParams creates default oracle module parameters
func DefaultParams() Params {
return Params{
VotePeriod: DefaultVotePeriod,
VoteThreshold: DefaultVoteThreshold,
MinVoters: DefaultMinVoters,
RewardBand: DefaultRewardBand,
Whitelist: DefaultWhitelist,
SlashFraction: DefaultSlashFraction,
SlashWindow: DefaultSlashWindow,
MinValidPerWindow: DefaultMinValidPerWindow,
TwapLookbackWindow: DefaultTwapLookbackWindow,
}
}
// ParamKeyTable returns the parameter key table.
func ParamKeyTable() paramstypes.KeyTable {
return paramstypes.NewKeyTable().RegisterParamSet(&Params{})
}
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
// pairs of oracle module's parameters.
func (p *Params) ParamSetPairs() paramstypes.ParamSetPairs {
return paramstypes.ParamSetPairs{
paramstypes.NewParamSetPair(KeyVotePeriod, &p.VotePeriod, validateVotePeriod),
paramstypes.NewParamSetPair(KeyVoteThreshold, &p.VoteThreshold, validateVoteThreshold),
paramstypes.NewParamSetPair(KeyMinVoters, &p.MinVoters, validateMinVoters),
paramstypes.NewParamSetPair(KeyRewardBand, &p.RewardBand, validateRewardBand),
paramstypes.NewParamSetPair(KeyWhitelist, &p.Whitelist, validateWhitelist),
paramstypes.NewParamSetPair(KeySlashFraction, &p.SlashFraction, validateSlashFraction),
paramstypes.NewParamSetPair(KeySlashWindow, &p.SlashWindow, validateSlashWindow),
paramstypes.NewParamSetPair(KeyMinValidPerWindow, &p.MinValidPerWindow, validateMinValidPerWindow),
paramstypes.NewParamSetPair(KeyTwapLookbackWindow, &p.TwapLookbackWindow, validateTwapLookbackWindow),
}
}
// String implements fmt.Stringer interface
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// Validate performs basic validation on oracle parameters.
func (p Params) Validate() error {
if p.VotePeriod == 0 {
return fmt.Errorf("oracle parameter VotePeriod must be > 0, is %d", p.VotePeriod)
}
if p.VoteThreshold.LTE(sdk.NewDecWithPrec(33, 2)) {
return fmt.Errorf("oracle parameter VoteThreshold must be greater than 33 percent")
}
if p.MinVoters <= 0 {
return fmt.Errorf("oracle parameter MinVoters must be greater than 0")
}
if p.RewardBand.GT(sdk.OneDec()) || p.RewardBand.IsNegative() {
return fmt.Errorf("oracle parameter RewardBand must be between [0, 1]")
}
if p.SlashFraction.GT(sdk.OneDec()) || p.SlashFraction.IsNegative() {
return fmt.Errorf("oracle parameter SlashFraction must be between [0, 1]")
}
if p.SlashWindow < p.VotePeriod {
return fmt.Errorf("oracle parameter SlashWindow must be greater than or equal with VotePeriod")
}
if p.MinValidPerWindow.GT(sdk.OneDec()) || p.MinValidPerWindow.IsNegative() {
return fmt.Errorf("oracle parameter MinValidPerWindow must be between [0, 1]")
}
for _, pair := range p.Whitelist {
if err := pair.Validate(); err != nil {
return fmt.Errorf("oracle parameter Whitelist Pair invalid format: %w", err)
}
}
return nil
}
func validateVotePeriod(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("vote period must be positive: %d", v)
}
return nil
}
func validateMinVoters(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("min voters must be positive: %d", v)
}
return nil
}
func validateVoteThreshold(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.LT(sdk.NewDecWithPrec(33, 2)) {
return fmt.Errorf("vote threshold must be bigger than 33%%: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("vote threshold too large: %s", v)
}
return nil
}
func validateRewardBand(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("reward band must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("reward band is too large: %s", v)
}
return nil
}
func validateWhitelist(i interface{}) error {
v, ok := i.([]asset.Pair)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
for _, d := range v {
if err := d.Validate(); err != nil {
return fmt.Errorf("oracle parameter Whitelist Pair invalid format: %w", err)
}
}
return nil
}
func validateSlashFraction(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("slash fraction must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("slash fraction is too large: %s", v)
}
return nil
}
func validateSlashWindow(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("slash window must be positive: %d", v)
}
return nil
}
func validateMinValidPerWindow(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 valid per window must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("min valid per window is too large: %s", v)
}
return nil
}
func validateTwapLookbackWindow(i interface{}) error {
v, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v < 0 {
return fmt.Errorf("look back twap duration should be positive: %s", v)
}
return nil
}