-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
108 lines (92 loc) · 2.82 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
package types
import (
"fmt"
"gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
var _ paramtypes.ParamSet = (*Params)(nil)
// parameter keys
var (
KeyTokenTaxRate = []byte("TokenTaxRate")
KeyIssueTokenBaseFee = []byte("IssueTokenBaseFee")
KeyMintTokenFeeRatio = []byte("MintTokenFeeRatio")
)
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyTokenTaxRate, &p.TokenTaxRate, validateTaxRate),
paramtypes.NewParamSetPair(KeyIssueTokenBaseFee, &p.IssueTokenBaseFee, validateIssueTokenBaseFee),
paramtypes.NewParamSetPair(KeyMintTokenFeeRatio, &p.MintTokenFeeRatio, validateMintTokenFeeRatio),
}
}
// NewParams constructs a new Params instance
func NewParams(tokenTaxRate sdk.Dec, issueTokenBaseFee sdk.Coin,
mintTokenFeeRatio sdk.Dec,
) Params {
return Params{
TokenTaxRate: tokenTaxRate,
IssueTokenBaseFee: issueTokenBaseFee,
MintTokenFeeRatio: mintTokenFeeRatio,
}
}
// ParamKeyTable returns the TypeTable for the token module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// DefaultParams return the default params
func DefaultParams() Params {
defaultToken := GetNativeToken()
return Params{
TokenTaxRate: sdk.NewDecWithPrec(4, 1), // 0.4 (40%)
IssueTokenBaseFee: sdk.NewCoin(defaultToken.Symbol, sdk.NewInt(60000)),
MintTokenFeeRatio: sdk.NewDecWithPrec(1, 1), // 0.1 (10%)
}
}
// String returns a human readable string representation of the parameters.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// ValidateParams validates the given params
func ValidateParams(p Params) error {
if err := validateTaxRate(p.TokenTaxRate); err != nil {
return err
}
if err := validateMintTokenFeeRatio(p.MintTokenFeeRatio); err != nil {
return err
}
if err := validateIssueTokenBaseFee(p.IssueTokenBaseFee); err != nil {
return err
}
return nil
}
func validateTaxRate(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.GT(sdk.NewDec(1)) || v.LT(sdk.ZeroDec()) {
return fmt.Errorf("token tax rate [%s] should be between [0, 1]", v.String())
}
return nil
}
func validateMintTokenFeeRatio(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.GT(sdk.NewDec(1)) || v.LT(sdk.ZeroDec()) {
return fmt.Errorf("fee ratio for minting tokens [%s] should be between [0, 1]", v.String())
}
return nil
}
func validateIssueTokenBaseFee(i interface{}) error {
v, ok := i.(sdk.Coin)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("base fee for issuing token should not be negative")
}
return nil
}