forked from bandprotocol/bandchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
71 lines (61 loc) · 1.64 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
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
)
const (
DefaultFromExchange = "geo"
DefaultToExchange = "odin"
)
type Params struct {
RateMultiplier sdk.Dec `json:"rate_multiplier" yaml:"rate_multiplier"`
ValidExchanges ValidExchanges `json:"valid_exchanges" yaml:"valid_exchanges"`
}
// nolint
var (
KeyRateMultiplier = []byte("RateMultiplier")
KeyValidExchanges = []byte("ValidExchanges")
)
// ParamTable for coinswap module.
func ParamKeyTable() params.KeyTable {
return params.NewKeyTable().RegisterParamSet(&Params{})
}
func (p *Params) ParamSetPairs() params.ParamSetPairs {
return params.ParamSetPairs{
params.NewParamSetPair(KeyRateMultiplier, &p.RateMultiplier, validateRateMultiplier),
params.NewParamSetPair(KeyValidExchanges, &p.ValidExchanges, validatePossibleExchanges),
}
}
func DefaultParams() Params {
return Params{
RateMultiplier: sdk.NewDec(1),
ValidExchanges: ValidExchanges{
DefaultFromExchange: []string{DefaultToExchange},
},
}
}
func validateRateMultiplier(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if !v.IsPositive() && !v.IsZero() {
return fmt.Errorf("rate multiplier %s must be positive or zero", v)
}
return nil
}
func validatePossibleExchanges(i interface{}) error {
exchanges, ok := i.(ValidExchanges)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
for k, valid := range exchanges {
for _, v := range valid {
if k == "" || v == "" {
return fmt.Errorf("one or both denoms are empty. From: %s, To: %s", k, v)
}
}
}
return nil
}