-
Notifications
You must be signed in to change notification settings - Fork 13
/
params.go
136 lines (117 loc) · 3.37 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
package types
import (
"fmt"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gitopia/gitopia/v3/app/params"
"gopkg.in/yaml.v2"
)
type DefaultAcc struct {
Name string
Address string
Mnemonic string
}
var Acc1 = DefaultAcc{
"acc1",
"gitopia1mnswtu0ueq7xw90u060ccfujvk04e8rv9vc47t",
"catch ship moment silk oak kingdom program matrix wire sleep rabbit tank camp sauce heart uncle school letter segment feel mean empower develop short",
}
// NewParams creates a new Params instance
func NewParams(evaluatorAddress string, rewardSeries []*RewardPool) Params {
return Params{
EvaluatorAddress: evaluatorAddress,
RewardSeries: rewardSeries,
}
}
// DefaultParams returns a default set of parameters
// NOTE: contains sensitive data. DO NOT use in production
func DefaultParams() Params {
return NewParams(Acc1.Address, []*RewardPool{
{
TotalAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(1000)),
ClaimedAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(0)),
Series: Series_ONE,
},
{
TotalAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(1000)),
ClaimedAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(0)),
Series: Series_TWO,
},
{
TotalAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(1000)),
ClaimedAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(0)),
Series: Series_THREE,
},
{
TotalAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(1000)),
ClaimedAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(0)),
Series: Series_FOUR,
},
{
TotalAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(1000)),
ClaimedAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(0)),
Series: Series_FIVE,
},
{
TotalAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(1000)),
ClaimedAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(0)),
Series: Series_SIX,
},
{
TotalAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(1000)),
ClaimedAmount: sdk.NewCoin(params.BaseCoinUnit, math.NewInt(0)),
Series: Series_SEVEN,
},
})
}
// validate params
func (p Params) Validate() error {
if err := validateEvaluatorAddress(p.EvaluatorAddress); err != nil {
return err
}
if err := validateRewardSeries(p.RewardSeries); err != nil {
return err
}
return nil
}
// String implements the Stringer interface.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
func validateEvaluatorAddress(i interface{}) error {
v, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
// validate v is an address
_, err := sdk.AccAddressFromBech32(v)
if err != nil {
return fmt.Errorf("invalid evaluator address: %s", err)
}
return nil
}
func validateRewardSeries(i interface{}) error {
v, ok := i.([]*RewardPool)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
for _, pool := range v {
if err := validateRewardPool(pool); err != nil {
return err
}
}
return nil
}
func validateRewardPool(pool *RewardPool) error {
if pool.TotalAmount.IsZero() {
return fmt.Errorf("total amount cannot be zero")
}
if pool.ClaimedAmount.IsNegative() {
return fmt.Errorf("claimed amount cannot be negative")
}
if pool.ClaimedAmount.Amount.GT(pool.TotalAmount.Amount) {
return fmt.Errorf("claimed amount cannot exceed total amount")
}
return nil
}