-
Notifications
You must be signed in to change notification settings - Fork 99
/
types.go
59 lines (48 loc) · 1.37 KB
/
types.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
package types
import (
"encoding/json"
"strconv"
)
// HumanAddress is a printable (typically bech32 encoded) address string. Just use it as a label for developers.
type HumanAddress = string
// CanonicalAddress uses standard base64 encoding, just use it as a label for developers
type CanonicalAddress = []byte
// Coin is a string representation of the sdk.Coin type (more portable than sdk.Int)
type Coin struct {
Denom string `json:"denom"` // type, eg. "ATOM"
Amount string `json:"amount"` // string encoing of decimal value, eg. "12.3456"
}
func NewCoin(amount uint64, denom string) Coin {
return Coin{
Denom: denom,
Amount: strconv.FormatUint(amount, 10),
}
}
// Coins handles properly serializing empty amounts
type Coins []Coin
// MarshalJSON ensures that we get [] for empty arrays
func (c Coins) MarshalJSON() ([]byte, error) {
if len(c) == 0 {
return []byte("[]"), nil
}
var d []Coin = c
return json.Marshal(d)
}
// UnmarshalJSON ensures that we get [] for empty arrays
func (c *Coins) UnmarshalJSON(data []byte) error {
// make sure we deserialize [] back to null
if string(data) == "[]" || string(data) == "null" {
return nil
}
var d []Coin
if err := json.Unmarshal(data, &d); err != nil {
return err
}
*c = d
return nil
}
type OutOfGasError struct{}
var _ error = OutOfGasError{}
func (o OutOfGasError) Error() string {
return "Out of gas"
}