-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
currencies.go
298 lines (251 loc) · 7.62 KB
/
currencies.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package assets
import (
"database/sql/driver"
"fmt"
"math/big"
"strings"
"github.com/smartcontractkit/chainlink/core/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
var ErrNoQuotesForCurrency = errors.New("cannot unmarshal json.Number into currency")
// getDenominator returns 10**precision.
func getDenominator(precision int) *big.Int {
x := big.NewInt(10)
return new(big.Int).Exp(x, big.NewInt(int64(precision)), nil)
}
func format(i *big.Int, precision int) string {
r := big.NewRat(1, 1).SetFrac(i, getDenominator(precision))
return fmt.Sprintf("%v", r.FloatString(precision))
}
// Link contains a field to represent the smallest units of LINK
type Link big.Int
// NewLinkFromJuels returns a new struct to represent LINK from it's smallest unit
func NewLinkFromJuels(w int64) *Link {
return (*Link)(big.NewInt(w))
}
// String returns Link formatted as a string.
func (l *Link) String() string {
if l == nil {
return "0"
}
return fmt.Sprintf("%v", (*big.Int)(l))
}
// Link returns Link formatted as a string, in LINK units
func (l *Link) Link() string {
if l == nil {
return "0"
}
return format((*big.Int)(l), 18)
}
// SetInt64 delegates to *big.Int.SetInt64
func (l *Link) SetInt64(w int64) *Link {
return (*Link)((*big.Int)(l).SetInt64(w))
}
// ToInt returns the Link value as a *big.Int.
func (l *Link) ToInt() *big.Int {
return (*big.Int)(l)
}
// ToHash returns a 32 byte representation of this value
func (l *Link) ToHash() common.Hash {
return common.BigToHash((*big.Int)(l))
}
// Set delegates to *big.Int.Set
func (l *Link) Set(x *Link) *Link {
il := (*big.Int)(l)
ix := (*big.Int)(x)
w := il.Set(ix)
return (*Link)(w)
}
// SetString delegates to *big.Int.SetString
func (l *Link) SetString(s string, base int) (*Link, bool) {
w, ok := (*big.Int)(l).SetString(s, base)
return (*Link)(w), ok
}
// Cmp defers to big.Int Cmp
func (l *Link) Cmp(y *Link) int {
return (*big.Int)(l).Cmp((*big.Int)(y))
}
// Add defers to big.Int Add
func (l *Link) Add(x, y *Link) *Link {
il := (*big.Int)(l)
ix := (*big.Int)(x)
iy := (*big.Int)(y)
return (*Link)(il.Add(ix, iy))
}
// Text defers to big.Int Text
func (l *Link) Text(base int) string {
return (*big.Int)(l).Text(base)
}
var linkFmtThreshold = (*Link)(new(big.Int).Exp(big.NewInt(10), big.NewInt(12), nil))
// MarshalText implements the encoding.TextMarshaler interface.
func (l *Link) MarshalText() ([]byte, error) {
if l.Cmp(linkFmtThreshold) >= 0 {
return []byte(fmt.Sprintf("%s link", decimal.NewFromBigInt(l.ToInt(), -18))), nil
}
return (*big.Int)(l).MarshalText()
}
// MarshalJSON implements the json.Marshaler interface.
func (l Link) MarshalJSON() ([]byte, error) {
value, err := l.MarshalText()
if err != nil {
return nil, err
}
return []byte(fmt.Sprintf(`"%s"`, value)), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (l *Link) UnmarshalJSON(data []byte) error {
if utils.IsQuoted(data) {
return l.UnmarshalText(utils.RemoveQuotes(data))
}
return ErrNoQuotesForCurrency
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (l *Link) UnmarshalText(text []byte) error {
s := string(text)
if strings.HasSuffix(s, "link") {
s = strings.TrimSuffix(s, "link")
s = strings.TrimSuffix(s, " ")
d, err := decimal.NewFromString(s)
if err != nil {
return errors.Wrapf(err, "assets: cannot unmarshal %q into a *assets.Link", text)
}
d = d.Mul(decimal.New(1, 18))
if !d.IsInteger() {
err := errors.New("maximum precision is juels")
return errors.Wrapf(err, "assets: cannot unmarshal %q into a *assets.Link", text)
}
l.Set((*Link)(d.Rat().Num()))
return nil
}
if strings.HasSuffix(s, "juels") {
s = strings.TrimSuffix(s, "juels")
s = strings.TrimSuffix(s, " ")
}
if _, ok := l.SetString(s, 10); !ok {
return errors.Errorf("assets: cannot unmarshal %q into a *assets.Link", text)
}
return nil
}
// IsZero returns true when the value is 0 and false otherwise
func (l *Link) IsZero() bool {
zero := big.NewInt(0)
return (*big.Int)(l).Cmp(zero) == 0
}
// Symbol returns LINK
func (*Link) Symbol() string {
return "LINK"
}
// Value returns the Link value for serialization to database.
func (l Link) Value() (driver.Value, error) {
b := (big.Int)(l)
return b.String(), nil
}
// Scan reads the database value and returns an instance.
func (l *Link) Scan(value interface{}) error {
switch v := value.(type) {
case string:
decoded, ok := l.SetString(v, 10)
if !ok {
return fmt.Errorf("unable to set string %v of %T to base 10 big.Int for Link", value, value)
}
*l = *decoded
case []uint8:
// The SQL library returns numeric() types as []uint8 of the string representation
decoded, ok := l.SetString(string(v), 10)
if !ok {
return fmt.Errorf("unable to set string %v of %T to base 10 big.Int for Link", value, value)
}
*l = *decoded
case int64:
return fmt.Errorf("unable to convert %v of %T to Link, is the sql type set to varchar?", value, value)
default:
return fmt.Errorf("unable to convert %v of %T to Link", value, value)
}
return nil
}
// 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 {
return 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 utils.IsQuoted(data) {
return e.UnmarshalText(utils.RemoveQuotes(data))
}
return 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()
}