-
Notifications
You must be signed in to change notification settings - Fork 180
/
coin_ibc.go
391 lines (325 loc) · 9.59 KB
/
coin_ibc.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package types
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/okex/exchain/libs/cosmos-sdk/types/errors"
)
var (
// Denominations can be 3 ~ 128 characters long and support letters, followed by either
// a letter, a number or a separator ('/').
ibcReDnmString = `[a-zA-Z][a-zA-Z0-9/-]{2,127}`
ibcReDecAmt = `[[:digit:]]+(?:\.[[:digit:]]+)?|\.[[:digit:]]+`
ibcReSpc = `[[:space:]]*`
ibcReDnm *regexp.Regexp
ibcReDecCoin *regexp.Regexp
)
var ibcCoinDenomRegex = DefaultCoinDenomRegex
func init() {
SetIBCCoinDenomRegex(DefaultIBCCoinDenomRegex)
}
func IBCParseDecCoin(coinStr string) (coin DecCoin, err error) {
coinStr = strings.TrimSpace(coinStr)
matches := ibcReDecCoin.FindStringSubmatch(coinStr)
if matches == nil {
return DecCoin{}, fmt.Errorf("invalid decimal coin expression: %s", coinStr)
}
amountStr, denomStr := matches[1], matches[2]
amount, err := NewDecFromStr(amountStr)
if err != nil {
return DecCoin{}, errors.Wrap(err, fmt.Sprintf("failed to parse decimal coin amount: %s", amountStr))
}
if err := ValidateDenom(denomStr); err != nil {
return DecCoin{}, fmt.Errorf("invalid denom cannot contain upper case characters or spaces: %s", err)
}
return NewDecCoinFromDec(denomStr, amount), nil
}
// DefaultCoinDenomRegex returns the default regex string
func DefaultIBCCoinDenomRegex() string {
return ibcReDnmString
}
func SetIBCCoinDenomRegex(reFn func() string) {
ibcCoinDenomRegex = reFn
ibcReDnm = regexp.MustCompile(fmt.Sprintf(`^%s$`, ibcCoinDenomRegex()))
ibcReDecCoin = regexp.MustCompile(fmt.Sprintf(`^(%s)%s(%s)$`, ibcReDecAmt, ibcReSpc, ibcCoinDenomRegex()))
}
type CoinAdapters []CoinAdapter
// NewCoin returns a new coin with a denomination and amount. It will panic if
// the amount is negative or if the denomination is invalid.
func NewCoinAdapter(denom string, amount Int) CoinAdapter {
coin := CoinAdapter{
Denom: denom,
Amount: amount,
}
if err := coin.Validate(); err != nil {
panic(err)
}
return coin
}
func (cs CoinAdapter) ToCoin() Coin {
if cs.Denom == DefaultIbcWei {
cs.Denom = DefaultBondDenom
}
return CoinAdapterToCoin(cs)
}
func (cs CoinAdapters) ToCoins() Coins {
ret := make([]Coin, 0)
for _, v := range cs {
ret = append(ret, v.ToCoin())
}
return ret
}
func (cas CoinAdapters) IsAnyNegative() bool {
for _, coin := range cas {
if coin.Amount.IsNegative() {
return true
}
}
return false
}
func (cas CoinAdapters) IsAnyNil() bool {
for _, coin := range cas {
if coin.Amount.IsNil() {
return true
}
}
return false
}
func (coins CoinAdapters) Add(coinsB ...CoinAdapter) CoinAdapters {
return coins.safeAdd(coinsB)
}
func (coins CoinAdapters) safeAdd(coinsB CoinAdapters) CoinAdapters {
// probably the best way will be to make Coins and interface and hide the structure
// definition (type alias)
if !coins.isSorted() {
panic("Coins (self) must be sorted")
}
if !coinsB.isSorted() {
panic("Wrong argument: coins must be sorted")
}
sum := ([]CoinAdapter)(nil)
indexA, indexB := 0, 0
lenA, lenB := len(coins), len(coinsB)
for {
if indexA == lenA {
if indexB == lenB {
// return nil coins if both sets are empty
return sum
}
// return set B (excluding zero coins) if set A is empty
return append(sum, removeZeroCoinAdapters(coinsB[indexB:])...)
} else if indexB == lenB {
// return set A (excluding zero coins) if set B is empty
return append(sum, removeZeroCoinAdapters(coins[indexA:])...)
}
coinA, coinB := coins[indexA], coinsB[indexB]
switch strings.Compare(coinA.Denom, coinB.Denom) {
case -1: // coin A denom < coin B denom
if !coinA.IsZero() {
sum = append(sum, coinA)
}
indexA++
case 0: // coin A denom == coin B denom
res := coinA.Add(coinB)
if !res.IsZero() {
sum = append(sum, res)
}
indexA++
indexB++
case 1: // coin A denom > coin B denom
if !coinB.IsZero() {
sum = append(sum, coinB)
}
indexB++
}
}
}
// ParseCoinsNormalized will parse out a list of coins separated by commas, and normalize them by converting to smallest
// unit. If the parsing is successuful, the provided coins will be sanitized by removing zero coins and sorting the coin
// set. Lastly a validation of the coin set is executed. If the check passes, ParseCoinsNormalized will return the
// sanitized coins.
// Otherwise it will return an error.
// If an empty string is provided to ParseCoinsNormalized, it returns nil Coins.
// ParseCoinsNormalized supports decimal coins as inputs, and truncate them to int after converted to smallest unit.
// Expected format: "{amount0}{denomination},...,{amountN}{denominationN}"
func ParseCoinsNormalized(coinStr string) (Coins, error) {
coins, err := ParseDecCoins(coinStr)
if err != nil {
return Coins{}, err
}
return NormalizeCoins(coins), nil
}
// ParseCoinNormalized parses and normalize a cli input for one coin type, returning errors if invalid or on an empty string
// as well.
// Expected format: "{amount}{denomination}"
func ParseCoinNormalized(coinStr string) (coin Coin, err error) {
decCoin, err := ParseDecCoin(coinStr)
if err != nil {
return Coin{}, err
}
coin, _ = NormalizeDecCoin(decCoin).TruncateDecimal()
return coin, nil
}
// IsValid calls Validate and returns true when the Coins are sorted, have positive amount, with a
// valid and unique denomination (i.e no duplicates).
func (coins CoinAdapters) IsValid() bool {
return coins.Validate() == nil
}
func (coins CoinAdapters) Validate() error {
switch len(coins) {
case 0:
return nil
case 1:
if err := ValidateDenom(coins[0].Denom); err != nil {
return err
}
if coins[0].Amount.IsNil() {
return fmt.Errorf("coin %s amount is nil", coins[0])
}
if !coins[0].IsPositive() {
return fmt.Errorf("coin %s amount is not positive", coins[0])
}
return nil
default:
// check single coin case
if err := (CoinAdapters{coins[0]}).Validate(); err != nil {
return err
}
lowDenom := coins[0].Denom
seenDenoms := make(map[string]bool)
seenDenoms[lowDenom] = true
for _, coin := range coins[1:] {
if seenDenoms[coin.Denom] {
return fmt.Errorf("duplicate denomination %s", coin.Denom)
}
if err := ValidateDenom(coin.Denom); err != nil {
return err
}
if coin.Denom <= lowDenom {
return fmt.Errorf("denomination %s is not sorted", coin.Denom)
}
if !coin.IsPositive() {
return fmt.Errorf("coin %s amount is not positive", coin.Denom)
}
// we compare each coin against the last denom
lowDenom = coin.Denom
seenDenoms[coin.Denom] = true
}
return nil
}
}
func (coins CoinAdapters) isSorted() bool {
for i := 1; i < len(coins); i++ {
if coins[i-1].Denom > coins[i].Denom {
return false
}
}
return true
}
func (coins CoinAdapters) String() string {
if len(coins) == 0 {
return ""
}
out := ""
for _, coin := range coins {
out += fmt.Sprintf("%v,", coin.String())
}
return out[:len(out)-1]
}
// IsAllPositive returns true if there is at least one coin and all currencies
// have a positive value.
func (coins CoinAdapters) IsAllPositive() bool {
if len(coins) == 0 {
return false
}
for _, coin := range coins {
if !coin.IsPositive() {
return false
}
}
return true
}
type coinAdaptersJSON CoinAdapters
// MarshalJSON implements a custom JSON marshaller for the Coins type to allow
// nil Coins to be encoded as an empty array.
func (coins CoinAdapters) MarshalJSON() ([]byte, error) {
if coins == nil {
return json.Marshal(coinAdaptersJSON(CoinAdapters{}))
}
return json.Marshal(coinAdaptersJSON(coins))
}
func (coins CoinAdapters) Copy() CoinAdapters {
copyCoins := make(CoinAdapters, len(coins))
for i, coin := range coins {
copyCoins[i] = coin
}
return copyCoins
}
func ConvWei2TOkt(adapters CoinAdapters) (CoinAdapters, error) {
copyAdapters := adapters.Copy()
for index, _ := range copyAdapters {
if copyAdapters[index].Denom == DefaultIbcWei {
copyAdapters[index].Denom = DefaultBondDenom
} else if strings.ToLower(copyAdapters[index].Denom) == DefaultBondDenom {
return nil, errors.Wrap(errors.ErrInvalidCoins, "not support okt denom")
}
}
return copyAdapters, nil
}
// AmountOf returns the amount of a denom from coins
func (coins CoinAdapters) AmountOf(denom string) Int {
mustValidateDenom(denom)
return coins.AmountOfNoDenomValidation(denom)
}
// AmountOfNoDenomValidation returns the amount of a denom from coins
// without validating the denomination.
func (coins CoinAdapters) AmountOfNoDenomValidation(denom string) Int {
switch len(coins) {
case 0:
return ZeroInt()
case 1:
coin := coins[0]
if coin.Denom == denom {
return coin.Amount
}
return ZeroInt()
default:
// Binary search the amount of coins remaining
midIdx := len(coins) / 2 // 2:1, 3:1, 4:2
coin := coins[midIdx]
switch {
case denom < coin.Denom:
return coins[:midIdx].AmountOfNoDenomValidation(denom)
case denom == coin.Denom:
return coin.Amount
default:
return coins[midIdx+1:].AmountOfNoDenomValidation(denom)
}
}
}
func (coins CoinAdapters) Sub(coinsB CoinAdapters) CoinAdapters {
diff, hasNeg := coins.SafeSub(coinsB)
if hasNeg {
panic("negative coin amount")
}
return diff
}
func (coins CoinAdapters) SafeSub(coinsB CoinAdapters) (CoinAdapters, bool) {
diff := coins.safeAdd(coinsB.negative())
return diff, diff.IsAnyNegative()
}
func (coins CoinAdapters) negative() CoinAdapters {
res := make([]CoinAdapter, 0, len(coins))
for _, coin := range coins {
res = append(res, CoinAdapter{
Denom: coin.Denom,
Amount: coin.Amount.Neg(),
})
}
return res
}
// AddAmount adds an amount to the Coin.
func (coin CoinAdapter) AddAmount(amount Int) CoinAdapter {
return CoinAdapter{coin.Denom, coin.Amount.Add(amount)}
}