Skip to content

Commit 810f84a

Browse files
committed
EstimateTransferERC20Fee & EstimateTransferFee
1 parent 2039143 commit 810f84a

File tree

4 files changed

+6
-43
lines changed

4 files changed

+6
-43
lines changed

cmd/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616

1717
token := &EthereumWallet.Token{ContractAddress: enums.GOERLI_USDC}
1818
w, _ := EthereumWallet.CreateEthereumWallet(node, "a24031202755246def61140ae1bce297d0c4886b2faea5ce79001748ef97e8ec")
19-
fmt.Println(w.EstimateTransferERC20Fee(token, "0x75c07e7207Bb00Cf60c77f2506D7CE2B8d18bf0f"))
19+
fmt.Println(w.EstimateTransferERC20Fee(token))
2020
fmt.Println(w.TransferERC20(token, "0x75c07e7207Bb00Cf60c77f2506D7CE2B8d18bf0f", big.NewInt(10)))
2121
return
2222
}

fee.go

+2-36
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"github.com/ethereum/go-ethereum"
66
"github.com/ethereum/go-ethereum/common"
77
"github.com/ranjbar-dev/ethereum-wallet/geth"
8-
"golang.org/x/crypto/sha3"
98
"math/big"
109
)
1110

@@ -49,7 +48,7 @@ func estimateEthTransactionFee(node Node, toAddressHex string) (int64, error) {
4948
return temp.Int64(), nil
5049
}
5150

52-
func estimateErc20TransactionFee(node Node, toAddressHex string) (int64, error) {
51+
func estimateErc20TransactionFee(node Node) (int64, error) {
5352

5453
client, err := geth.GetGETHClient(node.Http)
5554
if err != nil {
@@ -72,44 +71,11 @@ func estimateErc20TransactionFee(node Node, toAddressHex string) (int64, error)
7271
return 0, err
7372
}
7473

75-
gasLimit, err := erc20GasLimit(node, toAddressHex)
76-
if err != nil {
77-
return 0, err
78-
}
74+
gasLimit := 70000
7975

8076
fee := new(big.Int).SetInt64(baseFee.Int64() + gasTipCap.Int64())
8177

8278
temp := new(big.Int).Mul(new(big.Int).SetInt64(int64(gasLimit)), fee)
8379

8480
return temp.Int64(), nil
8581
}
86-
87-
func erc20GasLimit(node Node, toAddressHex string) (uint64, error) {
88-
toAddress := common.HexToAddress(toAddressHex)
89-
90-
client, err := geth.GetGETHClient(node.Http)
91-
if err != nil {
92-
return 0, err
93-
}
94-
95-
transferFnSignature := []byte("transfer(address,uint256)")
96-
hash := sha3.NewLegacyKeccak256()
97-
hash.Write(transferFnSignature)
98-
methodID := hash.Sum(nil)[:4]
99-
100-
paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32)
101-
102-
amount := new(big.Int)
103-
amount.SetString("1000000000000000000000000", 10) // 1000 tokens
104-
paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
105-
106-
var data []byte
107-
data = append(data, methodID...)
108-
data = append(data, paddedAddress...)
109-
data = append(data, paddedAmount...)
110-
111-
return client.EstimateGas(context.Background(), ethereum.CallMsg{
112-
To: &toAddress,
113-
Data: data,
114-
})
115-
}

transaction.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,7 @@ func createERC20Transaction(node Node, toAddressHex string, c *ethclient.Client,
124124
return nil, err
125125
}
126126

127-
gasLimit, err := erc20GasLimit(node, toAddressHex)
128-
if err != nil {
129-
return nil, err
130-
}
127+
gasLimit := uint64(70000)
131128

132129
return &bind.TransactOpts{
133130
From: fromAddress,

wallet.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (ew *EthereumWallet) TransferERC20(t *Token, toAddressHex string, amountInT
212212
return tx.Hash().Hex(), nil
213213
}
214214

215-
func (ew *EthereumWallet) EstimateTransferERC20Fee(t *Token, toAddressHex string) (int64, error) {
215+
func (ew *EthereumWallet) EstimateTransferERC20Fee(t *Token) (int64, error) {
216216

217-
return estimateErc20TransactionFee(ew.Node, toAddressHex)
217+
return estimateErc20TransactionFee(ew.Node)
218218
}

0 commit comments

Comments
 (0)