-
Notifications
You must be signed in to change notification settings - Fork 182
/
util.go
185 lines (161 loc) · 4.8 KB
/
util.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
package types
import (
"encoding/json"
"fmt"
"regexp"
"sort"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/tendermint/tendermint/crypto"
)
var (
notAllowedPrefix = "ammswap"
notAllowedOriginSymbol = regexp.MustCompile(fmt.Sprintf("^%s.*?", notAllowedPrefix))
regOriginalSymbol = regexp.MustCompile("^[a-z][a-z0-9]{0,5}$")
reWholeName = `[a-zA-Z0-9[:space:]]{1,30}`
reWhole = regexp.MustCompile(fmt.Sprintf(`^%s$`, reWholeName))
)
func WholeNameCheck(wholeName string) (newName string, isValid bool) {
wholeName = strings.TrimSpace(wholeName)
strs := strings.Fields(wholeName)
wholeName = strings.Join(strs, " ")
if !reWhole.MatchString(wholeName) {
return wholeName, false
}
return wholeName, true
}
func wholeNameValid(wholeName string) bool {
return reWhole.MatchString(wholeName)
}
type BaseAccount struct {
Address sdk.AccAddress `json:"address"`
Coins sdk.Coins `json:"coins"`
PubKey crypto.PubKey `json:"public_key"`
AccountNumber uint64 `json:"account_number"`
Sequence uint64 `json:"sequence"`
}
type DecAccount struct {
Address sdk.AccAddress `json:"address"`
Coins sdk.SysCoins `json:"coins"`
PubKey crypto.PubKey `json:"public_key"`
AccountNumber uint64 `json:"account_number"`
Sequence uint64 `json:"sequence"`
}
// String implements fmt.Stringer
func (acc DecAccount) String() string {
var pubkey string
if acc.PubKey != nil {
pubkey = sdk.MustBech32ifyAccPub(acc.PubKey)
}
return fmt.Sprintf(`Account:
Address: %s
Pubkey: %s
Coins: %v
AccountNumber: %d
Sequence: %d`,
acc.Address, pubkey, acc.Coins, acc.AccountNumber, acc.Sequence,
)
}
func NotAllowedOriginSymbol(name string) bool {
return notAllowedOriginSymbol.MatchString(name)
}
func ValidOriginalSymbol(name string) bool {
return !NotAllowedOriginSymbol(name) && regOriginalSymbol.MatchString(name)
}
// Convert a formatted json string into a TransferUnit array
// e.g.) [{"to": "addr", "amount": "1BNB,2BTC"}, ...]
func StrToTransfers(str string) (transfers []TransferUnit, err error) {
var transfer []Transfer
err = json.Unmarshal([]byte(str), &transfer)
if err != nil {
return transfers, err
}
for _, trans := range transfer {
var t TransferUnit
to, err := sdk.AccAddressFromBech32(trans.To)
if err != nil {
return transfers, fmt.Errorf("invalid address:%s", trans.To)
}
t.To = to
t.Coins, err = sdk.ParseDecCoins(trans.Amount)
if err != nil {
return transfers, err
}
transfers = append(transfers, t)
}
return transfers, nil
}
func BaseAccountToDecAccount(account auth.BaseAccount) DecAccount {
var decCoins sdk.SysCoins
for _, coin := range account.Coins {
dec := coin.Amount
decCoin := sdk.NewDecCoinFromDec(coin.Denom, dec)
decCoins = append(decCoins, decCoin)
}
decAccount := DecAccount{
Address: account.Address,
PubKey: account.PubKey,
Coins: decCoins,
AccountNumber: account.AccountNumber,
Sequence: account.Sequence,
}
return decAccount
}
func (acc *DecAccount) ToBaseAccount() *auth.BaseAccount {
decAccount := auth.BaseAccount{
Address: acc.Address,
PubKey: acc.PubKey,
Coins: acc.Coins,
AccountNumber: acc.AccountNumber,
Sequence: acc.Sequence,
}
return &decAccount
}
func DecAccountArrToBaseAccountArr(decAccounts []DecAccount) (baseAccountArr []auth.Account) {
for _, decAccount := range decAccounts {
baseAccountArr = append(baseAccountArr, decAccount.ToBaseAccount())
}
return baseAccountArr
}
func MergeCoinInfo(availableCoins, lockedCoins sdk.SysCoins) (coinsInfo CoinsInfo) {
m := make(map[string]CoinInfo)
for _, availableCoin := range availableCoins {
coinInfo, ok := m[availableCoin.Denom]
if !ok {
coinInfo.Symbol = availableCoin.Denom
coinInfo.Available = availableCoin.Amount.String()
coinInfo.Locked = "0"
m[availableCoin.Denom] = coinInfo
}
}
for _, lockedCoin := range lockedCoins {
coinInfo, ok := m[lockedCoin.Denom]
if ok {
coinInfo.Locked = lockedCoin.Amount.String()
m[lockedCoin.Denom] = coinInfo
} else {
coinInfo.Symbol = lockedCoin.Denom
coinInfo.Available = "0"
coinInfo.Locked = lockedCoin.Amount.String()
m[lockedCoin.Denom] = coinInfo
}
}
for _, coinInfo := range m {
coinsInfo = append(coinsInfo, coinInfo)
}
sort.Sort(coinsInfo)
return coinsInfo
}
func GenTokenResp(token Token) TokenResp {
return TokenResp{
Description: token.Description,
Symbol: token.Symbol,
OriginalSymbol: token.OriginalSymbol,
WholeName: token.WholeName,
OriginalTotalSupply: token.OriginalTotalSupply,
Owner: token.Owner,
Type: token.Type,
Mintable: token.Mintable,
}
}