-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
94 lines (80 loc) · 2.39 KB
/
main.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
91
92
93
94
package main
import (
"context"
"errors"
"fmt"
"math/big"
"time"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/ethclient"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
var (
// Create your own private key and fund it. You can find a test avalanche faucet.
key1, _ = // add private key here
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
toAddress = common.HexToAddress("0x766c47CFAB38A6A3813551b9e31BDAca455Bf262")
clientURI = "https://avalanche-fuji-c-chain.publicnode.com"
chainID = new(big.Int).SetInt64(43113) // fuji
)
func main() {
fmt.Println("Starting to send transaction")
ctx := context.Background()
client, err := ethclient.Dial(clientURI)
if err != nil {
fmt.Println("error with ethClient Dial: %w", err)
return
}
currentNonce, err := client.NonceAt(ctx, addr1, nil)
if err != nil {
fmt.Println("error in getting nonce: %w", err)
return
}
signedTx, err := createAndSignTx(ctx, &toAddress, currentNonce)
if err != nil {
fmt.Println("error in tx creation/signing: %w", err)
return
}
err = client.SendTransaction(ctx, signedTx)
if err != nil {
fmt.Println("error with sending transaction: %w", err)
return
}
sendCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
for {
select {
case <-time.After(2 * time.Second):
case <-sendCtx.Done():
fmt.Println("failed to await tx %s nonce %d: %w", signedTx.Hash(), currentNonce, ctx.Err())
}
newNonce, err := client.NonceAt(ctx, addr1, nil)
if err != nil {
fmt.Println("error in getting nonce: %w", err)
}
fmt.Println("checking if transaction got accepted")
if currentNonce < newNonce {
fmt.Println("successful transaction")
return
}
}
}
func createAndSignTx(ctx context.Context, toAddress *common.Address, nonce uint64) (*types.Transaction, error) {
signer := types.LatestSignerForChainID(chainID)
tx := types.NewTx(&types.DynamicFeeTx{
ChainID: chainID, // hardcode fuji testnet for now
Nonce: nonce,
To: toAddress,
Gas: 21000, //hardcode gas transaction for now
GasFeeCap: big.NewInt(25000000000), //hardcode base fee for now
GasTipCap: big.NewInt(0),
Data: []byte{},
Value: big.NewInt(0),
})
signedTx, err := types.SignTx(tx, signer, key1)
if err != nil {
return nil, errors.New("failed to sign transaction")
}
return signedTx, nil
}