-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
assets.go
117 lines (97 loc) · 2.91 KB
/
assets.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 assets
import (
"database/sql/driver"
"fmt"
"math/big"
"github.com/smartcontractkit/chainlink-common/pkg/assets"
"github.com/smartcontractkit/chainlink-common/pkg/utils/bytes"
"github.com/smartcontractkit/chainlink/v2/core/utils"
"github.com/shopspring/decimal"
)
// Eth contains a field to represent the smallest units of ETH
type Eth big.Int
// NewEth returns a new struct to represent ETH from it's smallest unit (wei)
func NewEth(w int64) *Eth {
return (*Eth)(big.NewInt(w))
}
// NewEthValue returns a new struct to represent ETH from it's smallest unit (wei)
func NewEthValue(w int64) Eth {
eth := NewEth(w)
return *eth
}
// NewEthValueS returns a new struct to represent ETH from a string value of Eth (not wei)
// the underlying value is still wei
func NewEthValueS(s string) (Eth, error) {
e, err := decimal.NewFromString(s)
if err != nil {
return Eth{}, err
}
w := e.Mul(decimal.RequireFromString("10").Pow(decimal.RequireFromString("18")))
return *(*Eth)(w.BigInt()), nil
}
// Cmp delegates to *big.Int.Cmp
func (e *Eth) Cmp(y *Eth) int {
return e.ToInt().Cmp(y.ToInt())
}
func (e *Eth) String() string {
if e == nil {
return "<nil>"
}
return assets.Format(e.ToInt(), 18)
}
// SetInt64 delegates to *big.Int.SetInt64
func (e *Eth) SetInt64(w int64) *Eth {
return (*Eth)(e.ToInt().SetInt64(w))
}
// SetString delegates to *big.Int.SetString
func (e *Eth) SetString(s string, base int) (*Eth, bool) {
w, ok := e.ToInt().SetString(s, base)
return (*Eth)(w), ok
}
// MarshalJSON implements the json.Marshaler interface.
func (e Eth) MarshalJSON() ([]byte, error) {
value, err := e.MarshalText()
if err != nil {
return nil, err
}
return []byte(fmt.Sprintf(`"%s"`, value)), nil
}
// MarshalText implements the encoding.TextMarshaler interface.
func (e *Eth) MarshalText() ([]byte, error) {
return e.ToInt().MarshalText()
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (e *Eth) UnmarshalJSON(data []byte) error {
if bytes.HasQuotes(data) {
return e.UnmarshalText(bytes.TrimQuotes(data))
}
return assets.ErrNoQuotesForCurrency
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (e *Eth) UnmarshalText(text []byte) error {
if _, ok := e.SetString(string(text), 10); !ok {
return fmt.Errorf("assets: cannot unmarshal %q into a *assets.Eth", text)
}
return nil
}
// IsZero returns true when the value is 0 and false otherwise
func (e *Eth) IsZero() bool {
zero := big.NewInt(0)
return e.ToInt().Cmp(zero) == 0
}
// Symbol returns ETH
func (*Eth) Symbol() string {
return "ETH"
}
// ToInt returns the Eth value as a *big.Int.
func (e *Eth) ToInt() *big.Int {
return (*big.Int)(e)
}
// Scan reads the database value and returns an instance.
func (e *Eth) Scan(value interface{}) error {
return (*utils.Big)(e).Scan(value)
}
// Value returns the Eth value for serialization to database.
func (e Eth) Value() (driver.Value, error) {
return (utils.Big)(e).Value()
}