-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
helpers.go
49 lines (39 loc) · 979 Bytes
/
helpers.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
package flashbotsrpc
import (
"bytes"
"fmt"
"math/big"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/core/types"
)
// ParseInt parse hex string value to int
func ParseInt(value string) (int, error) {
i, err := strconv.ParseInt(strings.TrimPrefix(value, "0x"), 16, 64)
if err != nil {
return 0, err
}
return int(i), nil
}
// ParseBigInt parse hex string value to big.Int
func ParseBigInt(value string) (big.Int, error) {
i := big.Int{}
_, err := fmt.Sscan(value, &i)
return i, err
}
// IntToHex convert int to hexadecimal representation
func IntToHex(i int) string {
return fmt.Sprintf("0x%x", i)
}
// BigToHex covert big.Int to hexadecimal representation
func BigToHex(bigInt big.Int) string {
if bigInt.BitLen() == 0 {
return "0x0"
}
return "0x" + strings.TrimPrefix(fmt.Sprintf("%x", bigInt.Bytes()), "0")
}
func TxToRlp(tx *types.Transaction) string {
var buff bytes.Buffer
tx.EncodeRLP(&buff)
return fmt.Sprintf("%x", buff.Bytes())
}