-
Notifications
You must be signed in to change notification settings - Fork 28
/
cosmoscoin.go
65 lines (54 loc) · 1.81 KB
/
cosmoscoin.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 tmcosmosutils
import (
"fmt"
"github.com/crypto-com/chain-indexing/usecase/coin"
)
// MustNewCoinFromAmountInterface returns a Coin from the amount map in the form of
// map[string]interface{}. It panics when the amount is invalid.
func MustNewCoinFromAmountInterface(amount map[string]interface{}) coin.Coin {
result, err := NewCoinFromAmountInterface(amount)
if err != nil {
panic(err)
}
return result
}
// NewCoinFromAmountInterface returns a Coin from the amount in the form of
// map[string]interface{}. It returns error when the amount is invalid.
func NewCoinFromAmountInterface(amount map[string]interface{}) (coin.Coin, error) {
result, err := coin.NewCoinFromString(amount["denom"].(string), amount["amount"].(string))
if err != nil {
return coin.Coin{}, err
}
return result, nil
}
// MustNewCoinsFromAmountInterface returns Coins from the list of amount in the
// from of []interface{}. It behaves the same as NewCoinsFromAmountInterface
// except it panics on any error.
func MustNewCoinsFromAmountInterface(amounts []interface{}) coin.Coins {
result, err := NewCoinsFromAmountInterface(amounts)
if err != nil {
panic(err)
}
return result
}
// NewCoinsFromAmountInterface returns Coins from the list of amount in the
// from of []interface{}. It returns error when any of the amount inside is
// invalid
func NewCoinsFromAmountInterface(amounts []interface{}) (coin.Coins, error) {
coins, newCoinsErr := coin.NewCoins()
if newCoinsErr != nil {
return nil, newCoinsErr
}
for _, rawAmount := range amounts {
amount, ok := rawAmount.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid coin in the amount list: %v", rawAmount)
}
coinUnit, err := NewCoinFromAmountInterface(amount)
if err != nil {
return nil, err
}
coins = coins.Add(coinUnit)
}
return coins, nil
}