-
Notifications
You must be signed in to change notification settings - Fork 6
/
params.go
146 lines (120 loc) · 3.2 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
package types
import (
"fmt"
yaml "gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
DefaultReserveAccount = ""
DefaultRewardDistributeAccount = ""
)
var (
DefaultRateFeePool = sdk.NewDecWithPrec(20, 2) // 20%
DefaultRateCommunityPool = sdk.NewDecWithPrec(80, 2) // 80%
DefaultRateReserve = sdk.NewDecWithPrec(0, 2) // 0%
)
// DefaultParams returns default reward parameters
func DefaultParams() Params {
return Params{
FeePoolRate: DefaultRateFeePool,
CommunityPoolRate: DefaultRateCommunityPool,
ReserveRate: DefaultRateReserve,
ReserveAccount: DefaultReserveAccount,
RewardDistributeAccount: DefaultRewardDistributeAccount,
}
}
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
func (p Params) TotalRate() sdk.Dec {
return p.CommunityPoolRate.Add(p.FeePoolRate).Add(p.ReserveRate)
}
// ValidateBasic performs basic validation on reward parameters.
func (p Params) ValidateBasic() error {
if p.ReserveAccount == "" && p.ReserveRate.GT(sdk.ZeroDec()) {
return fmt.Errorf("reserve account must be set up for reserve compensation")
}
if p.CommunityPoolRate.IsNegative() {
return fmt.Errorf(
"community pool rate should be positive: %s", p.CommunityPoolRate,
)
}
if p.FeePoolRate.IsNegative() {
return fmt.Errorf(
"fee pool rate should be positive: %s", p.FeePoolRate,
)
}
if p.ReserveRate.IsNegative() {
return fmt.Errorf(
"reserve rate should be positive: %s", p.ReserveRate,
)
}
if p.TotalRate().GT(sdk.OneDec()) {
return fmt.Errorf(
"sum of fee pool, community pool and reserve cannot be greater than one: %s", p.TotalRate(),
)
}
return nil
}
func validateFeePoolRate(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid fee pool rate parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("fee pool rate must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("fee pool rate must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("fee pool rate too large: %s", v)
}
return nil
}
func validateCommunityPoolRate(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid community pool rate parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("community pool rate must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("community pool rate must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("community pool rate too large: %s", v)
}
return nil
}
func validateReserveRate(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("reserve rate parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("reserve rate must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("reserve rate must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("reserve rate too large: %s", v)
}
return nil
}
func validateAccount(i interface{}) error {
v, ok := i.(string)
if !ok {
return fmt.Errorf("invalid reserve account parameter type: %T", i)
}
if v != "" {
_, err := sdk.AccAddressFromBech32(v)
if err != nil {
return fmt.Errorf("invalid reserve account: %s", err.Error())
}
}
return nil
}