forked from chenzhijie/go-web3
/
utils.go
269 lines (221 loc) · 5.47 KB
/
utils.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
package utils
import (
"bytes"
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
type EtherUnit int
const (
EtherUnitNoEther EtherUnit = iota
EtherUnitWei
EtherUnitKWei
EtherUnitMWei
EtherUnitGWei
EtherUnitSzabo
EtherUnitFinney
EtherUnitEther
)
var unitMap = map[EtherUnit]string{
EtherUnitNoEther: "0",
EtherUnitWei: "1",
EtherUnitKWei: "1000",
EtherUnitMWei: "1000000",
EtherUnitGWei: "1000000000",
EtherUnitSzabo: "1000000000000",
EtherUnitFinney: "1000000000000000",
EtherUnitEther: "1000000000000000000",
}
type Utils struct{}
func NewUtils() *Utils {
return &Utils{}
}
func (u *Utils) FromWei(wei *big.Int) *big.Float {
if wei == nil {
return big.NewFloat(0)
}
exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
expF := new(big.Float)
expF.SetInt(exp)
bigval := new(big.Float)
bigval.SetInt(wei)
ret := bigval.Quo(bigval, expF)
return ret
}
func (u *Utils) FromGWei(wei *big.Int) *big.Float {
if wei == nil {
return big.NewFloat(0)
}
exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(9), nil)
expF := new(big.Float)
expF.SetInt(exp)
bigval := new(big.Float)
bigval.SetInt(wei)
ret := bigval.Quo(bigval, expF)
return ret
}
func (u *Utils) FromWeiFloat(wei *big.Float) *big.Float {
if wei == nil {
return big.NewFloat(0)
}
exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
expF := new(big.Float)
expF.SetInt(exp)
bigval := new(big.Float)
ret := bigval.Quo(wei, expF)
return ret
}
func (u *Utils) FromDecimals(wei *big.Int, decimals int64) *big.Float {
if wei == nil {
return big.NewFloat(0)
}
exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(decimals), nil)
expF := new(big.Float)
expF.SetInt(exp)
bigval := new(big.Float)
bigval.SetInt(wei)
ret := bigval.Quo(bigval, expF)
return ret
}
func (u *Utils) ToWei(val string) *big.Int {
if !strings.Contains(val, ".") {
whole, ok := big.NewInt(0).SetString(val, 10)
if !ok {
return big.NewInt(0)
}
return big.NewInt(1).Mul(whole, big.NewInt(1e18))
}
comps := strings.Split(val, ".")
if len(comps) != 2 {
return big.NewInt(0)
}
whole := comps[0]
fraction := comps[1]
baseLength := len(unitMap[EtherUnitEther]) - 1
fractionLength := len(fraction)
if fractionLength > baseLength {
return big.NewInt(0)
}
fraction += strings.Repeat("0", baseLength-fractionLength)
wholeInt, ok := big.NewInt(0).SetString(whole, 10)
if !ok {
return big.NewInt(0)
}
fractionInt, ok := big.NewInt(0).SetString(fraction, 10)
if !ok {
return big.NewInt(0)
}
wholeMulBase := big.NewInt(1).Mul(wholeInt, big.NewInt(1e18))
wholeAddFraction := big.NewInt(1).Add(wholeMulBase, fractionInt)
return wholeAddFraction
}
func (u *Utils) ToWeiInt(val int64, denominator int64) *big.Int {
bigval := big.NewInt(val)
exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
result := bigval.Mul(bigval, exp)
result = big.NewInt(1).Div(result, big.NewInt(denominator))
return result
}
func (u *Utils) FromWeiWithUnit(wei *big.Int, unit EtherUnit) *big.Float {
if wei == nil {
return big.NewFloat(0)
}
unitInt := 0
switch unit {
case EtherUnitNoEther:
unitInt = 0
case EtherUnitWei:
unitInt = 1
case EtherUnitKWei:
unitInt = 3
case EtherUnitMWei:
unitInt = 6
case EtherUnitGWei:
unitInt = 9
case EtherUnitSzabo:
unitInt = 12
case EtherUnitFinney:
unitInt = 15
case EtherUnitEther:
unit = 18
}
exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(unitInt)), nil)
expF := new(big.Float)
expF.SetInt(exp)
bigval := new(big.Float)
bigval.SetInt(wei)
ret := bigval.Quo(bigval, expF)
return ret
}
func (u *Utils) ToGWei(val float64) *big.Int {
bigval := new(big.Float)
bigval.SetFloat64(val)
exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(9), nil)
expF := new(big.Float)
expF.SetInt(exp)
bigval.Mul(bigval, expF)
result := new(big.Int)
bigval.Int(result) // store converted number in result
return result
}
func (u *Utils) ToHex(n *big.Int) string {
return fmt.Sprintf("0x%x", n) // or %X or upper case
}
func (u *Utils) HexToUint64(str string) (uint64, error) {
return ParseUint64orHex(str)
}
func (u *Utils) ToDecimals(val uint64, decimals int64) *big.Int {
return convert(val, decimals)
}
func (u *Utils) SameAddress(a, b common.Address) bool {
return bytes.Equal(a[:], b[:])
}
func (u *Utils) DifferentAddress(a, b common.Address) bool {
return !bytes.Equal(a[:], b[:])
}
func (u *Utils) RoundNWei(wei *big.Int, n int) (*big.Int, error) {
af := u.FromWei(wei)
aff, _ := af.Float64()
roundfs := ""
if n > 6 {
return nil, fmt.Errorf("round n not support bigger than 6")
}
switch n {
case 1:
roundfs = fmt.Sprintf("%.1f", aff)
case 2:
roundfs = fmt.Sprintf("%.2f", aff)
case 3:
roundfs = fmt.Sprintf("%.3f", aff)
case 4:
roundfs = fmt.Sprintf("%.4f", aff)
case 5:
roundfs = fmt.Sprintf("%.5f", aff)
case 6:
roundfs = fmt.Sprintf("%.6f", aff)
}
r := u.ToWei(roundfs)
return r, nil
}
// Ether converts a value to the ether unit with 18 decimals
func Ether(i uint64) *big.Int {
return convert(i, 18)
}
func convert(val uint64, decimals int64) *big.Int {
v := big.NewInt(int64(val))
exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(decimals), nil)
return v.Mul(v, exp)
}
// ToBlockNumArg. Wrap blockNumber arg from big.Int to string
func ToBlockNumArg(number *big.Int) string {
if number == nil {
return "latest"
}
pending := big.NewInt(-1)
if number.Cmp(pending) == 0 {
return "pending"
}
return hexutil.EncodeBig(number)
}