/
params.go
121 lines (102 loc) · 3.13 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
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")
)
// ParamTable for module.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
func NewParams(
distrProportions DistributionProportions,
weightedDevRewardsReceivers []WeightedAddress,
) Params {
return Params{
DistributionProportions: distrProportions,
WeightedDeveloperRewardsReceivers: weightedDevRewardsReceivers,
}
}
// default module parameters
func DefaultParams() Params {
return Params{
DistributionProportions: DistributionProportions{
NftIncentives: sdk.NewDecWithPrec(45, 2), // 45%
DeveloperRewards: sdk.NewDecWithPrec(15, 2), // 15%
},
WeightedDeveloperRewardsReceivers: []WeightedAddress{},
}
}
// validate params
func (p Params) Validate() error {
if err := validateDistributionProportions(p.DistributionProportions); err != nil {
return err
}
err := validateWeightedDeveloperRewardsReceivers(p.WeightedDeveloperRewardsReceivers)
return err
}
// Implements params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyDistributionProportions, &p.DistributionProportions, validateDistributionProportions),
paramtypes.NewParamSetPair(
KeyDeveloperRewardsReceiver, &p.WeightedDeveloperRewardsReceivers, validateWeightedDeveloperRewardsReceivers),
}
}
func validateDistributionProportions(i interface{}) error {
v, ok := i.(DistributionProportions)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.NftIncentives.IsNegative() {
return errors.New("NFT incentives distribution ratio should not be negative")
}
if v.DeveloperRewards.IsNegative() {
return errors.New("developer rewards distribution ratio should not be negative")
}
totalProportions := v.NftIncentives.Add(v.DeveloperRewards)
// 60% is allocated to this module
// 35% validators
// 5% community pool
if !totalProportions.Equal(sdk.NewDecWithPrec(60, 2)) {
return errors.New("total distributions ratio should be 60%")
}
return nil
}
func validateWeightedDeveloperRewardsReceivers(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
}