-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
big.go
187 lines (157 loc) · 4.16 KB
/
big.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
package utils
import (
"database/sql/driver"
"encoding/json"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
bigmath "github.com/smartcontractkit/chainlink/core/utils/big_math"
)
const base10 = 10
// BigFloat accepts both string and float JSON values.
type BigFloat big.Float
// MarshalJSON implements the json.Marshaler interface.
func (b BigFloat) MarshalJSON() ([]byte, error) {
var j = big.Float(b)
return json.Marshal(&j)
}
// UnmarshalJSON implements the json.Unmarshal interface.
func (b *BigFloat) UnmarshalJSON(buf []byte) error {
var n json.Number
if err := json.Unmarshal(buf, &n); err == nil {
f, _, err := new(big.Float).Parse(n.String(), 0)
if err != nil {
return err
}
*b = BigFloat(*f)
return nil
}
var bf big.Float
if err := json.Unmarshal(buf, &bf); err != nil {
return err
}
*b = BigFloat(bf)
return nil
}
// Value returns the big.Float value.
func (b *BigFloat) Value() *big.Float {
return (*big.Float)(b)
}
// Big stores large integers and can deserialize a variety of inputs.
type Big big.Int
// NewBig constructs a Big from *big.Int.
func NewBig(i *big.Int) *Big {
if i != nil {
var b big.Int
b.Set(i)
return (*Big)(&b)
}
return nil
}
// NewBigI constructs a Big from int64.
func NewBigI(i int64) *Big {
return NewBig(big.NewInt(i))
}
// MarshalText marshals this instance to base 10 number as string.
func (b Big) MarshalText() ([]byte, error) {
return []byte((*big.Int)(&b).Text(base10)), nil
}
// MarshalJSON marshals this instance to base 10 number as string.
func (b Big) MarshalJSON() ([]byte, error) {
text, err := b.MarshalText()
if err != nil {
return nil, err
}
return json.Marshal(string(text))
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *Big) UnmarshalText(input []byte) error {
input = RemoveQuotes(input)
str := string(input)
if HasHexPrefix(str) {
decoded, err := hexutil.DecodeBig(str)
if err != nil {
return err
}
*b = Big(*decoded)
return nil
}
_, ok := b.setString(str, 10)
if !ok {
return fmt.Errorf("unable to convert %s to Big", str)
}
return nil
}
func (b *Big) setString(s string, base int) (*Big, bool) {
w, ok := (*big.Int)(b).SetString(s, base)
return (*Big)(w), ok
}
// UnmarshalJSON implements encoding.JSONUnmarshaler.
func (b *Big) UnmarshalJSON(input []byte) error {
return b.UnmarshalText(input)
}
// Value returns this instance serialized for database storage.
func (b Big) Value() (driver.Value, error) {
return b.String(), nil
}
// Scan reads the database value and returns an instance.
func (b *Big) Scan(value interface{}) error {
switch v := value.(type) {
case string:
decoded, ok := b.setString(v, 10)
if !ok {
return fmt.Errorf("unable to set string %v of %T to base 10 big.Int for Big", value, value)
}
*b = *decoded
case []uint8:
// The SQL library returns numeric() types as []uint8 of the string representation
decoded, ok := b.setString(string(v), 10)
if !ok {
return fmt.Errorf("unable to set string %v of %T to base 10 big.Int for Big", value, value)
}
*b = *decoded
default:
return fmt.Errorf("unable to convert %v of %T to Big", value, value)
}
return nil
}
// ToInt converts b to a big.Int.
func (b *Big) ToInt() *big.Int {
return (*big.Int)(b)
}
// String returns the base 10 encoding of b.
func (b *Big) String() string {
return b.ToInt().String()
}
// Bytes returns the absolute value of b as a big-endian byte slice.
func (b *Big) Hex() string {
return hexutil.EncodeBig(b.ToInt())
}
// Bytes returns the
func (b *Big) Bytes() []byte {
return b.ToInt().Bytes()
}
// Cmp compares b and c as big.Ints.
func (b *Big) Cmp(c *Big) int {
return b.ToInt().Cmp(c.ToInt())
}
// Equal returns true if c is equal according to Cmp.
func (b *Big) Equal(c *Big) bool {
return b.Cmp(c) == 0
}
// Int64 casts b as an int64 type
func (b *Big) Int64() int64 {
return b.ToInt().Int64()
}
// Add returns the sum of b and c
func (b *Big) Add(c interface{}) *Big {
return NewBig(bigmath.Add(b, c))
}
// Sub returns the differencs between b and c
func (b *Big) Sub(c interface{}) *Big {
return NewBig(bigmath.Sub(b, c))
}
// Sub returns b % c
func (b *Big) Mod(c interface{}) *Big {
return NewBig(bigmath.Mod(b, c))
}