-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_erc20.go
80 lines (66 loc) · 1.96 KB
/
send_erc20.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
package main
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"goweb3/config"
"goweb3/connect"
"log"
"math/big"
)
func main() {
client := connect.Connect_proxy_eth()
from := config.Get_fromaddr()
pv := config.Get_pv()
toaddr := config.Get_toaddr()
Contractaddr := config.Get_contractaddr()
chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatal(err)
}
nonce, err := client.PendingNonceAt(context.Background(), from)
if err != nil {
log.Fatal(err)
}
value := big.NewInt(0)
var data []byte
paddedAddress := common.LeftPadBytes(toaddr.Bytes(), 32)
fmt.Println(hexutil.Encode(paddedAddress))
transferFnSignature := []byte("transfer(address,uint256)")
methodID := crypto.Keccak256(transferFnSignature)[:4]
amount := new(big.Int)
amount.SetString("1000000000000000000", 10) // 1000 tokens
paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
fmt.Println(hexutil.Encode(paddedAmount))
data = append(data, methodID...)
data = append(data, paddedAddress...)
data = append(data, paddedAmount...)
gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
From: from,
To: &Contractaddr,
Data: data,
})
if err != nil {
log.Fatal(err)
}
fmt.Println(gasLimit)
gasFeeCap, err := client.SuggestGasPrice(context.Background())
gasTipCap, err := client.SuggestGasTipCap(context.Background())
tx := types.NewTx(&types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
Gas: gasLimit,
To: &Contractaddr,
Value: value,
Data: data,
})
signedTx, err := types.SignTx(tx, types.NewLondonSigner(chainID), pv)
err = client.SendTransaction(context.Background(), signedTx)
fmt.Printf("https://sepolia.etherscan.io/tx/%s", signedTx.Hash().Hex())
}