-
Notifications
You must be signed in to change notification settings - Fork 4
/
params.go
90 lines (73 loc) · 2.26 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
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
coinswaptypes "github.com/ODIN-PROTOCOL/odin-core/x/coinswap/types"
)
const (
DefaultThresholdDenom = "loki"
DefaultFromExchange = DefaultThresholdDenom
DefaultToExchange = "minigeo"
)
var (
KeyAuctionStartThreshold = []byte("AuctionStartThreshold")
KeyExchangeRates = []byte("ExchangeRates")
)
var DefaultAuctionStartThreshold = sdk.NewCoins(sdk.NewInt64Coin(DefaultThresholdDenom, 100000000000000))
// ParamKeyTable param table for auction module.
func ParamKeyTable() paramstypes.KeyTable {
return paramstypes.NewKeyTable().RegisterParamSet(&Params{})
}
func (p *Params) ParamSetPairs() paramstypes.ParamSetPairs {
return paramstypes.ParamSetPairs{
paramstypes.NewParamSetPair(KeyExchangeRates, &p.ExchangeRates, validateExchangeRates),
paramstypes.NewParamSetPair(KeyAuctionStartThreshold, &p.AuctionStartThreshold, validateAuctionStartThreshold),
}
}
func DefaultParams() Params {
return Params{
ExchangeRates: []coinswaptypes.Exchange{
{
From: DefaultFromExchange,
To: DefaultToExchange,
RateMultiplier: sdk.NewDec(1),
},
},
AuctionStartThreshold: DefaultAuctionStartThreshold,
}
}
func (p Params) Validate() error {
if err := validateExchangeRates(p.ExchangeRates); err != nil {
return err
}
return validateAuctionStartThreshold(p.AuctionStartThreshold)
}
func validateExchangeRates(i interface{}) error {
exchanges, ok := i.([]coinswaptypes.Exchange)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
for _, ex := range exchanges {
if !ex.RateMultiplier.IsPositive() && !ex.RateMultiplier.IsZero() {
return fmt.Errorf("rate multiplier %s must be positive or zero", ex)
}
if ex.From == "" || ex.To == "" {
return fmt.Errorf("one or both denoms are empty. From: %s, To: %s", ex.From, ex.To)
}
}
return nil
}
func validateAuctionStartThreshold(i interface{}) error {
v, ok := i.(sdk.Coins)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsAnyNegative() {
return fmt.Errorf("threshold amount must be positive: %v", v)
}
if v.IsZero() {
return fmt.Errorf("threshold amount must be greater than zero: %v", v)
}
return nil
}