forked from tranvictor/ethutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
134 lines (116 loc) · 3.08 KB
/
util.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
package ethutils
import (
"fmt"
"io/ioutil"
"math"
"math/big"
"path"
"runtime"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
func getABIFromFile(filename string) (*abi.ABI, error) {
_, current, _, ok := runtime.Caller(0)
if !ok {
return nil, fmt.Errorf("couldn't get filepath of the caller")
}
content, err := ioutil.ReadFile(path.Join(path.Dir(current), filename))
if err != nil {
return nil, err
}
result, err := abi.JSON(strings.NewReader(string(content)))
if err != nil {
return nil, err
}
return &result, nil
}
func GetMultiCallABI() *abi.ABI {
result, _ := abi.JSON(strings.NewReader(multicallabi))
return &result
}
func GetERC20ABI() *abi.ABI {
result, _ := abi.JSON(strings.NewReader(erc20abi))
return &result
}
func PackERC20Data(function string, params ...interface{}) ([]byte, error) {
return GetERC20ABI().Pack(function, params...)
}
func FloatToInt(amount float64) int64 {
s := fmt.Sprintf("%.0f", amount)
if i, err := strconv.Atoi(s); err == nil {
return int64(i)
} else {
panic(err)
}
}
// FloatToBigInt converts a float to a big int with specific decimal
// Example:
// - FloatToBigInt(1, 4) = 10000
// - FloatToBigInt(1.234, 4) = 12340
func FloatToBigInt(amount float64, decimal int64) *big.Int {
// 6 is our smallest precision
if decimal < 6 {
return big.NewInt(FloatToInt(amount * math.Pow10(int(decimal))))
}
result := big.NewInt(FloatToInt(amount * math.Pow10(6)))
return result.Mul(result, big.NewInt(0).Exp(big.NewInt(10), big.NewInt(decimal-6), nil))
}
// BigToFloat converts a big int to float according to its number of decimal digits
// Example:
// - BigToFloat(1100, 3) = 1.1
// - BigToFloat(1100, 2) = 11
// - BigToFloat(1100, 5) = 0.11
func BigToFloat(b *big.Int, decimal int64) float64 {
f := new(big.Float).SetInt(b)
power := new(big.Float).SetInt(new(big.Int).Exp(
big.NewInt(10), big.NewInt(decimal), nil,
))
res := new(big.Float).Quo(f, power)
result, _ := res.Float64()
return result
}
func StringToBig(input string) *big.Int {
resultBig, ok := big.NewInt(0).SetString(input, 10)
if !ok {
return big.NewInt(0)
}
return resultBig
}
func StringToFloat(input string, decimal int64) float64 {
resultBig, ok := big.NewInt(0).SetString(input, 10)
if !ok {
return 0.0
}
return BigToFloat(resultBig, decimal)
}
// GweiToWei converts Gwei as a float to Wei as a big int
func GweiToWei(n float64) *big.Int {
return FloatToBigInt(n, 9)
}
// EthToWei converts Gwei as a float to Wei as a big int
func EthToWei(n float64) *big.Int {
return FloatToBigInt(n, 18)
}
func HexToHash(hex string) common.Hash {
return common.HexToHash(hex)
}
func HexToBig(hex string) *big.Int {
result, err := hexutil.DecodeBig(hex)
if err != nil {
panic(err)
}
return result
}
func HexToAddress(hex string) common.Address {
return common.HexToAddress(hex)
}
func HexToAddresses(hexes []string) []common.Address {
result := []common.Address{}
for _, h := range hexes {
result = append(result, common.HexToAddress(h))
}
return result
}