-
Notifications
You must be signed in to change notification settings - Fork 1
/
fee.go
81 lines (60 loc) · 1.72 KB
/
fee.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
package ethereumWallet
import (
"context"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ranjbar-dev/ethereum-wallet/geth"
"math/big"
)
func estimateEthTransactionFee(node Node, toAddressHex string) (int64, error) {
toAddress := common.HexToAddress(toAddressHex)
client, err := geth.GetGETHClient(node.Http)
if err != nil {
return 0, err
}
blockNum, err := client.BlockNumber(context.Background())
if err != nil {
return 0, err
}
currentBlock, err := client.BlockByNumber(context.Background(), big.NewInt(int64(blockNum)))
if err != nil {
return 0, err
}
baseFee := currentBlock.BaseFee()
gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
To: &toAddress,
})
if err != nil {
return 0, err
}
gasTipCap, err := client.SuggestGasTipCap(context.Background())
if err != nil {
return 0, err
}
fee := new(big.Int).SetInt64(baseFee.Int64() + gasTipCap.Int64())
temp := new(big.Int).Mul(new(big.Int).SetInt64(int64(gasLimit)), fee)
return temp.Int64(), nil
}
func estimateErc20TransactionFee(node Node) (int64, error) {
client, err := geth.GetGETHClient(node.Http)
if err != nil {
return 0, err
}
blockNum, err := client.BlockNumber(context.Background())
if err != nil {
return 0, err
}
currentBlock, err := client.BlockByNumber(context.Background(), big.NewInt(int64(blockNum)))
if err != nil {
return 0, err
}
baseFee := currentBlock.BaseFee()
gasTipCap, err := client.SuggestGasTipCap(context.Background())
if err != nil {
return 0, err
}
gasLimit := 70000
fee := new(big.Int).SetInt64(baseFee.Int64() + gasTipCap.Int64())
temp := new(big.Int).Mul(new(big.Int).SetInt64(int64(gasLimit)), fee)
return temp.Int64(), nil
}