-
Notifications
You must be signed in to change notification settings - Fork 30
/
params.go
156 lines (125 loc) · 4.72 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
package types
import (
fmt "fmt"
"strings"
time "time"
"gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
const (
FormatHTLTAssetPrefix = "htlt"
)
// Parameter store keys
var (
KeyAssetParams = []byte("AssetParams") // asset params key
DefaultPreviousBlockTime = time.Now()
)
// NewParams is the HTLC params constructor
func NewParams(assetParams []AssetParam) Params {
return Params{
AssetParams: assetParams,
}
}
// ParamKeyTable returns the TypeTable for coinswap module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyAssetParams, &p.AssetParams, validateAssetParams),
}
}
// DefaultParams returns the default coinswap module parameters
func DefaultParams() Params {
return Params{[]AssetParam{}}
}
// String returns a human readable string representation of the parameters.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
func NewAssetParam(
denom string, coinID int, limit SupplyLimit, active bool,
deputyAddr string, fixedFee sdk.Int, minSwapAmount sdk.Int,
maxSwapAmount sdk.Int, minBlockLock uint64, maxBlockLock uint64,
) AssetParam {
return AssetParam{
Denom: denom,
SupplyLimit: limit,
Active: active,
DeputyAddress: deputyAddr,
FixedFee: fixedFee,
MinSwapAmount: minSwapAmount,
MaxSwapAmount: maxSwapAmount,
MinBlockLock: minBlockLock,
MaxBlockLock: maxBlockLock,
}
}
// String returns a human readable string representation of the parameters.
func (p AssetParam) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// String returns a human readable string representation of the parameters.
func (p SupplyLimit) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// Validate returns err if Params is invalid
func (p Params) Validate() error {
return validateAssetParams(p.AssetParams)
}
func validateAssetParams(i interface{}) error {
assetParams, ok := i.([]AssetParam)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
coinDenoms := make(map[string]bool)
for _, asset := range assetParams {
if err := sdk.ValidateDenom(asset.Denom); err != nil ||
!strings.HasPrefix(asset.Denom, FormatHTLTAssetPrefix) ||
strings.ToLower(asset.Denom) != asset.Denom ||
len(asset.Denom) < MinDenomLength {
return fmt.Errorf(fmt.Sprintf("invalid asset denom: %s", asset.Denom))
}
if asset.SupplyLimit.Limit.IsNegative() {
return fmt.Errorf(fmt.Sprintf("asset %s has invalid (negative) supply limit: %s", asset.Denom, asset.SupplyLimit.Limit))
}
if asset.SupplyLimit.TimeBasedLimit.IsNegative() {
return fmt.Errorf(fmt.Sprintf("asset %s has invalid (negative) supply time limit: %s", asset.Denom, asset.SupplyLimit.TimeBasedLimit))
}
if asset.SupplyLimit.TimeBasedLimit.GT(asset.SupplyLimit.Limit) {
return fmt.Errorf(fmt.Sprintf("asset %s cannot have supply time limit greater than supply limit: %s>%s", asset.Denom, asset.SupplyLimit.TimeBasedLimit, asset.SupplyLimit.Limit))
}
if _, found := coinDenoms[asset.Denom]; found {
return fmt.Errorf(fmt.Sprintf("asset %s cannot have duplicate denom", asset.Denom))
}
coinDenoms[asset.Denom] = true
if _, err := sdk.AccAddressFromBech32(asset.DeputyAddress); err != nil {
return fmt.Errorf("invalid deputy address %s", asset.DeputyAddress)
}
if asset.FixedFee.IsNegative() {
return fmt.Errorf("asset %s cannot have a negative fixed fee %s", asset.Denom, asset.FixedFee)
}
if asset.MinBlockLock < MinTimeLock {
return fmt.Errorf("asset %s has minimum time lock %d less than min htlc time lock %d", asset.Denom, asset.MinBlockLock, MinTimeLock)
}
if asset.MaxBlockLock > MaxTimeLock {
return fmt.Errorf("asset %s has maximum time lock %d greater than max htlc time lock %d", asset.Denom, asset.MaxBlockLock, MaxTimeLock)
}
if asset.MinBlockLock > asset.MaxBlockLock {
return fmt.Errorf("asset %s has minimum time lock %d greater than maximum time lock %d", asset.Denom, asset.MinBlockLock, asset.MaxBlockLock)
}
if !asset.MinSwapAmount.IsPositive() {
return fmt.Errorf(fmt.Sprintf("asset %s must have a positive minimum swap amount, got %s", asset.Denom, asset.MinSwapAmount))
}
if !asset.MaxSwapAmount.IsPositive() {
return fmt.Errorf(fmt.Sprintf("asset %s must have a positive maximum swap amount, got %s", asset.Denom, asset.MaxSwapAmount))
}
if asset.MinSwapAmount.GT(asset.MaxSwapAmount) {
return fmt.Errorf("asset %s has minimum swap amount %s greater than maximum swap amount %s", asset.Denom, asset.MinSwapAmount, asset.MaxSwapAmount)
}
}
return nil
}