-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_transaction.go
90 lines (81 loc) · 2.36 KB
/
send_transaction.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
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"fmt"
"log"
"math/big"
"os"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
func sendTransaction() {
var (
ctx = context.Background()
url = "https://eth-goerli.g.alchemy.com/v2/" + os.Getenv("ALCHEMY_ID")
client, err = ethclient.DialContext(ctx, url)
)
if err != nil {
log.Fatal(err)
}
privateKey, err := crypto.HexToECDSA(os.Getenv("ACCOUNT_KEY"))
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
value := big.NewInt(1) // 1 wei
gasLimit := uint64(21000)
gasFeeCap, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
gasTipCap, err := client.SuggestGasTipCap(context.Background())
if err != nil {
log.Fatal(err)
}
toAddress := fromAddress // send eth to self
var data []byte
chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatal(err)
}
tx := types.NewTx(&types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
Gas: gasLimit,
To: &toAddress,
Value: value,
Data: data,
})
signedTx, err := types.SignTx(tx, types.NewLondonSigner(chainID), privateKey)
if err != nil {
log.Fatal(err)
}
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatal(err)
}
txHash := common.HexToHash(signedTx.Hash().Hex())
tx, isPending, err := client.TransactionByHash(context.Background(), txHash)
fmt.Println("transaction hash:", tx.Hash().Hex())
fmt.Println("transaction value:", tx.Value().String())
fmt.Println("transaction gas limit:", tx.Gas())
fmt.Println("transaction fee cap per gas:", tx.GasFeeCap())
fmt.Println("transaction tip cap per gas:", tx.GasTipCap())
fmt.Println("transaction gas price:", tx.GasPrice())
fmt.Println("transaction nonce:", tx.Nonce())
fmt.Println("transaction data:", hex.EncodeToString(tx.Data()))
fmt.Println("transaction to:", tx.To().Hex())
fmt.Println("isPending:", isPending)
}