/
util.go
43 lines (34 loc) · 963 Bytes
/
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
package eth
import (
"errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"math/big"
)
var BigIntEthGWei = big.NewInt(1e9)
func HexToAddress(addr string) (common.Address, error) {
if !common.IsHexAddress(addr) {
return common.Address{}, errors.New("invalid address")
}
return common.HexToAddress(addr), nil
}
func HexToHash(s string) common.Hash {
return common.HexToHash(s)
}
func WeiToGwei(v *big.Int) int64 {
return big.NewInt(0).Div(v, BigIntEthGWei).Int64()
}
func GweiToWei(v int64) *big.Int {
return big.NewInt(0).Mul(big.NewInt(v), BigIntEthGWei)
}
func CalcEthFee(gasPrice *big.Int, gas int64) int64 {
return WeiToGwei(big.NewInt(0).Mul(big.NewInt(gas), gasPrice))
}
func SerializeTransaction(tx *types.Transaction) (string, error) {
data, err := tx.MarshalBinary()
if err != nil {
return "", err
}
return hexutil.Encode(data), nil
}