/
aliase_coin_exchain.go
117 lines (97 loc) · 2.7 KB
/
aliase_coin_exchain.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
package types
import (
"fmt"
"math/big"
"regexp"
"strings"
)
type Coin = DecCoin
type Coins = DecCoins
type SysCoin = DecCoin
type SysCoins = DecCoins
var (
reDnmString = fmt.Sprintf(`[a-z][a-z0-9]{0,9}(\-[a-f0-9]{3})?`)
reDecAmt = `[[:digit:]]*\.?[[:digit:]]+`
rePoolTokenDnmString = fmt.Sprintf(`(ammswap_)[a-z][a-z0-9]{0,9}(\-[a-f0-9]{3})?_[a-z][a-z0-9]{0,9}(\-[a-f0-9]{3})?`)
rePoolTokenDnm = regexp.MustCompile(fmt.Sprintf(`^%s$`, rePoolTokenDnmString))
reDecCoinPoolToken = regexp.MustCompile(fmt.Sprintf(`^(%s)%s(%s)$`, reDecAmt, reSpc, rePoolTokenDnmString))
reDecCoin = &EnvRegexp{
regexp.MustCompile(fmt.Sprintf(`^(%s)%s(%s)$`, reDecAmt, reSpc, reDnmString)),
}
)
var (
ParseCoin = ParseDecCoin
ParseCoins = ParseDecCoins
)
func NewCoin(denom string, amount interface{}) DecCoin {
switch amount := amount.(type) {
case Int:
return NewDecCoin(denom, amount)
case Dec:
return NewDecCoinFromDec(denom, amount)
default:
panic("Invalid amount")
}
}
func NewDecCoinsFromDec(denom string, amount Dec) DecCoins {
return DecCoins{NewDecCoinFromDec(denom, amount)}
}
func (dec DecCoin) ToCoins() Coins {
return NewCoins(dec)
}
func newDecFromInt(i Int) Dec {
return newDecFromIntWithPrec(i, 0)
}
func newDecFromIntWithPrec(i Int, prec int64) Dec {
return Dec{
new(big.Int).Mul(i.BigInt(), precisionMultiplier(prec)),
}
}
// Round a decimal with precision, perform bankers rounding (gaussian rounding)
func (d Dec) RoundDecimal(precision int64) Dec {
precisionMul := NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(precision), nil))
return newDecFromInt(d.MulInt(precisionMul).RoundInt()).QuoInt(precisionMul)
}
func MustParseCoins(denom, amount string) Coins {
coins, err := ParseCoins(amount + denom)
if err != nil {
panic(err)
}
return coins
}
type EnvRegexp struct {
*regexp.Regexp
}
func (r *EnvRegexp) FindStringSubmatch(coinStr string) []string {
matches := r.Regexp.FindStringSubmatch(coinStr)
if matches != nil {
return matches
}
return reDecCoinPoolToken.FindStringSubmatch(coinStr)
}
func GetSystemFee() Coin {
return NewDecCoinFromDec(DefaultBondDenom, NewDecWithPrec(125, 4))
}
func ZeroFee() Coin {
return NewCoin(DefaultBondDenom, ZeroInt())
}
// ValidateDenom validates a denomination string returning an error if it is
// invalid.
func ValidateDenom(denom string) error {
// TODO ,height
if denom == DefaultBondDenom {
return nil
}
if !reDnm.MatchString(denom) && !rePoolTokenDnm.MatchString(denom) {
if strings.HasPrefix(denom, "ibc") {
if validIBCCoinDenom(denom) {
return nil
}
}
return fmt.Errorf("invalid denom: %s", denom)
}
return nil
}
func (coins DecCoins) Add2(coinsB DecCoins) DecCoins {
return coins.safeAdd(coinsB)
}