-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
106 lines (88 loc) · 2.68 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
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/fastock/fastock-chain/x/params"
)
const (
// default paramspace for params keeper
DefaultParamspace = ModuleName
)
// Parameter keys
var (
ParamStoreKeyCommunityTax = []byte("communitytax")
ParamStoreKeyWithdrawAddrEnabled = []byte("withdrawaddrenabled")
)
// Params defines the set of distribution parameters.
type Params struct {
CommunityTax sdk.Dec `json:"community_tax" yaml:"community_tax"`
WithdrawAddrEnabled bool `json:"withdraw_addr_enabled" yaml:"withdraw_addr_enabled"`
}
// ParamKeyTable returns the parameter key table.
func ParamKeyTable() params.KeyTable {
return params.NewKeyTable().RegisterParamSet(&Params{})
}
// DefaultParams returns default distribution parameters
func DefaultParams() Params {
return Params{
CommunityTax: sdk.NewDecWithPrec(2, 2), // 2%
WithdrawAddrEnabled: true,
}
}
// String returns a human readable string representation of Params
func (p Params) String() string {
return fmt.Sprintf(`Distribution Params:
Community Tax: %s
Withdraw Addr Enabled: %t`,
p.CommunityTax, p.WithdrawAddrEnabled)
}
// ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() params.ParamSetPairs {
return params.ParamSetPairs{
params.NewParamSetPair(ParamStoreKeyCommunityTax, &p.CommunityTax, validateCommunityTax),
params.NewParamSetPair(ParamStoreKeyWithdrawAddrEnabled, &p.WithdrawAddrEnabled, validateWithdrawAddrEnabled),
}
}
// ValidateBasic performs basic validation on distribution parameters.
func (p Params) ValidateBasic() error {
if p.CommunityTax.IsNegative() || p.CommunityTax.GT(sdk.OneDec()) {
return fmt.Errorf(
"community tax should non-negative and less than one: %s", p.CommunityTax,
)
}
return nil
}
func validateCommunityTax(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("community tax must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("community tax must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("community tax too large: %s", v)
}
return nil
}
func validateWithdrawAddrEnabled(i interface{}) error {
_, ok := i.(bool)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
// NewParams creates a new instance of Params
func NewParams(communityTax sdk.Dec, withdrawAddrEnabled bool) Params {
return Params{
CommunityTax: communityTax,
WithdrawAddrEnabled: withdrawAddrEnabled,
}
}
// MarshalYAML implements the text format for yaml marshaling
func (p Params) MarshalYAML() (interface{}, error) {
return p.String(), nil
}