/
coin_adapter.go
65 lines (58 loc) · 1.42 KB
/
coin_adapter.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
package types
func CoinAdapterToCoin(adapter CoinAdapter) Coin {
return Coin{
Denom: adapter.Denom,
Amount: NewDecFromBigIntWithPrec(adapter.Amount.BigInt(), Precision),
}
}
func CoinToCoinAdapter(adapter Coin) CoinAdapter {
return CoinAdapter{
Denom: adapter.Denom,
Amount: NewIntFromBigInt(adapter.Amount.BigInt()),
}
}
func CoinAdaptersToCoins(adapters CoinAdapters) Coins {
var coins Coins = make([]Coin, 0, len(adapters))
for i, _ := range adapters {
coins = append(coins, CoinAdapterToCoin(adapters[i]))
}
return coins
}
func CoinsToCoinAdapters(coins Coins) CoinAdapters {
//Note:
// `var adapters CoinAdapters = make([]CoinAdapter, 0)`
// The code above if invalid.
// []CoinAdapter{} and nil are different in json format which can make different signBytes.
var adapters CoinAdapters
for i, _ := range coins {
adapters = append(adapters, CoinToCoinAdapter(coins[i]))
}
return adapters
}
func removeZeroCoinAdapters(coins CoinAdapters) CoinAdapters {
for i := 0; i < len(coins); i++ {
if coins[i].IsZero() {
break
} else if i == len(coins)-1 {
return coins
}
}
var result []CoinAdapter
if len(coins) > 0 {
result = make([]CoinAdapter, 0, len(coins)-1)
}
for _, coin := range coins {
if !coin.IsZero() {
result = append(result, coin)
}
}
return result
}
func (coins CoinAdapters) IsZero() bool {
for _, coin := range coins {
if !coin.IsZero() {
return false
}
}
return true
}