-
Notifications
You must be signed in to change notification settings - Fork 7
/
params.go
189 lines (155 loc) · 5.07 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package types
import (
"fmt"
"github.com/anathatech/project-anatha/config"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
)
const (
DefaultParamspace = ModuleName
DefaultNameInfoDuration = time.Hour * 24 * 365
DefaultNameInfoMaxDuration = time.Hour * 24 * 365 * 3
NameConstraintBlock = 750
)
var (
DefaultAddressCredits = sdk.NewInt(20)
DefaultRegisteredBlockchainIds = []string {
"omg",
"tusd",
"zrx",
"btc",
"bch",
"eth",
"dash",
"ltc",
"atom",
"xrp",
"bnb",
}
KeyNameInfoDuration = []byte("NameInfoDuration")
KeyNameInfoRegistrationFee = []byte("NameInfoRegistrationFee")
KeyNameInfoRenewalFee = []byte("NameInfoRenewalFee")
KeyAddressCredits = []byte("AddressCredits")
KeyAddressRegistrationFee = []byte("AddressRegistrationFee")
KeyNameInfoMaxDuration = []byte("NameInfoMaxDuration")
)
func ParamKeyTable() params.KeyTable {
return params.NewKeyTable().RegisterParamSet(&Params{})
}
type Params struct {
NameInfoDuration time.Duration `json:"nameinfo_duration" yaml:"nameinfo_duration"`
NameInfoMaxDuration time.Duration `json:"nameinfo_max_duration" yaml:"nameinfo_max_duration"`
NameInfoRegistrationFee sdk.Coins `json:"registration_fee" yaml:"registration_fee"`
NameInfoRenewalFee sdk.Coins `json:"renewal_fee" yaml:"renewal_fee"`
AddressCredits sdk.Int `json:"address_credits" yaml:"address_credits"`
AddressRegistrationFee sdk.Coins `json:"address_registration_fee" yaml:"address_registration_fee"`
}
func NewParams(nameInfoDuration time.Duration, nameInfoMaxDuration time.Duration, nameInfoRegistrationFee sdk.Coins,
nameInfoRenewalFee sdk.Coins, addressCredits sdk.Int, addressRegistrationFee sdk.Coins) Params {
return Params{
NameInfoDuration: nameInfoDuration,
NameInfoMaxDuration: nameInfoMaxDuration,
NameInfoRegistrationFee: nameInfoRegistrationFee,
NameInfoRenewalFee: nameInfoRenewalFee,
AddressCredits: addressCredits,
AddressRegistrationFee: addressRegistrationFee,
}
}
// String implements the stringer interface for Params
func (p Params) String() string {
return fmt.Sprintf(`Params:
NameInfo Duration: %s
NameInfo Max Duration: %s
NameInfoRegistrationFee: %s
NameInfoRenewalFee: %s
AddressCredits: %s
AddressRegistrationFee: %s`,
p.NameInfoDuration,
p.NameInfoMaxDuration,
p.NameInfoRegistrationFee,
p.NameInfoRenewalFee,
p.AddressCredits,
p.AddressRegistrationFee,
)
}
// ParamSetPairs - Implements params.ParamSet
func (p *Params) ParamSetPairs() params.ParamSetPairs {
return params.ParamSetPairs{
params.NewParamSetPair(KeyNameInfoDuration, &p.NameInfoDuration, validateNameInfoDuration),
params.NewParamSetPair(KeyNameInfoMaxDuration, &p.NameInfoMaxDuration, validateNameInfoDuration),
params.NewParamSetPair(KeyNameInfoRegistrationFee, &p.NameInfoRegistrationFee, validateFee),
params.NewParamSetPair(KeyNameInfoRenewalFee, &p.NameInfoRenewalFee, validateFee),
params.NewParamSetPair(KeyAddressCredits, &p.AddressCredits, validateAddressCredits),
params.NewParamSetPair(KeyAddressRegistrationFee, &p.AddressRegistrationFee, validateFee),
}
}
// DefaultParams defines the parameters for this module
func DefaultParams() Params {
defaultNameInfoCoinFee, _ := sdk.ConvertCoin(sdk.NewInt64Coin("anatha", 1), "pin")
defaultNameInfoCoinsFee := sdk.NewCoins(defaultNameInfoCoinFee)
defaultAddressRegistrationCoinFee, _ := sdk.ConvertCoin(sdk.NewInt64Coin("anatha", 1), "pin")
defaultAddressRegistrationCoinsFee := sdk.NewCoins(defaultAddressRegistrationCoinFee)
return NewParams(
DefaultNameInfoDuration,
DefaultNameInfoMaxDuration,
defaultNameInfoCoinsFee,
defaultNameInfoCoinsFee,
DefaultAddressCredits,
defaultAddressRegistrationCoinsFee,
)
}
func (p Params) Validate() error {
if err := validateNameInfoDuration(p.NameInfoDuration); err != nil {
return err
}
if err := validateNameInfoDuration(p.NameInfoMaxDuration); err != nil {
return err
}
if err := validateFee(p.NameInfoRegistrationFee); err != nil {
return err
}
if err := validateFee(p.NameInfoRenewalFee); err != nil {
return err
}
if err := validateAddressCredits(p.AddressCredits); err != nil {
return err
}
if err := validateFee(p.AddressRegistrationFee); err != nil {
return err
}
return nil
}
func validateNameInfoDuration(i interface{}) error {
v, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 {
return fmt.Errorf("name info duration must be positive: %d", v)
}
return nil
}
func validateFee(i interface{}) error {
v, ok := i.(sdk.Coins)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if ! v.IsValid() {
return fmt.Errorf("invalid name info fee: %s", v)
}
if ! v.AmountOf(config.DefaultDenom).IsPositive() {
return fmt.Errorf("invalid fee denomination. expected: %s", config.DefaultDenom)
}
return nil
}
func validateAddressCredits(i interface{}) error {
v, ok := i.(sdk.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("address credits must be positive: %d", v)
}
return nil
}