-
Notifications
You must be signed in to change notification settings - Fork 28
/
params_legacy.go
115 lines (96 loc) · 3.09 KB
/
params_legacy.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
package types
import (
"errors"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
// Parameter store keys
var (
KeyDistributionProportions = []byte("DistributionProportions")
KeyDeveloperRewardsReceiver = []byte("DeveloperRewardsReceiver")
KeyNftIncentivesReceiver = []byte("NftIncentivesReceiver")
KeyNodeHostIncentivesReceiver = []byte("NodeHostIncentivesReceiver")
)
// ParamKeyTable ParamTable for module.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// ParamSetPairs Implements params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyDistributionProportions, &p.DistributionProportions, validateDistributionProportions),
paramtypes.NewParamSetPair(
KeyDeveloperRewardsReceiver,
&p.WeightedDeveloperRewardsReceivers,
validateWeightedAddresses,
),
paramtypes.NewParamSetPair(
KeyNftIncentivesReceiver,
&p.WeightedNftIncentivesReceivers,
validateWeightedAddresses,
),
paramtypes.NewParamSetPair(
KeyNodeHostIncentivesReceiver,
&p.WeightedNodeHostsIncentivesReceivers,
validateWeightedAddresses,
),
}
}
func validateDistributionProportions(i interface{}) error {
v, ok := i.(DistributionProportions)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.StakingRewards.IsNegative() {
return errors.New("staking rewards distribution ratio should not be negative")
}
if v.NftIncentives.IsNegative() {
return errors.New("NFT incentives distribution ratio should not be negative")
}
if v.NodeHostsIncentives.IsNegative() {
return errors.New("node hosts incentives distribution ratio should not be negative")
}
if v.DeveloperRewards.IsNegative() {
return errors.New("developer rewards distribution ratio should not be negative")
}
if v.CommunityPool.IsNegative() {
return errors.New("community pool distribution ratio should not be negative")
}
totalProportions := v.StakingRewards.Add(v.NftIncentives).Add(v.NodeHostsIncentives).Add(v.DeveloperRewards).Add(v.CommunityPool)
if !totalProportions.Equal(sdk.NewDec(1)) {
return errors.New("total distributions ratio should be equal to 100%")
}
return nil
}
func validateWeightedAddresses(i interface{}) error {
v, ok := i.([]WeightedAddress)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
// fund community pool when rewards address is empty
if len(v) == 0 {
return nil
}
weightSum := sdk.NewDec(0)
for i, w := range v {
// we allow address to be "" to go to community pool
if w.Address != "" {
_, err := sdk.AccAddressFromBech32(w.Address)
if err != nil {
return fmt.Errorf("invalid address at %dth", i)
}
}
if !w.Weight.IsPositive() {
return fmt.Errorf("non-positive weight at %dth", i)
}
if w.Weight.GT(sdk.NewDec(1)) {
return fmt.Errorf("more than 1 weight at %dth", i)
}
weightSum = weightSum.Add(w.Weight)
}
if !weightSum.Equal(sdk.NewDec(1)) {
return fmt.Errorf("invalid weight sum: %s", weightSum.String())
}
return nil
}