github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/test/scripts/send_transfers/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "math/big" 7 8 "github.com/0xPolygon/supernets2-node/hex" 9 "github.com/0xPolygon/supernets2-node/log" 10 "github.com/0xPolygon/supernets2-node/test/operations" 11 "github.com/ethereum/go-ethereum" 12 "github.com/ethereum/go-ethereum/accounts/abi/bind" 13 "github.com/ethereum/go-ethereum/common" 14 "github.com/ethereum/go-ethereum/core/types" 15 "github.com/ethereum/go-ethereum/ethclient" 16 ) 17 18 func main() { 19 var networks = []struct { 20 Name string 21 URL string 22 ChainID uint64 23 PrivateKey string 24 }{ 25 //{Name: "Local L1", URL: operations.DefaultL1NetworkURL, ChainID: operations.DefaultL1ChainID, PrivateKey: operations.DefaultSequencerPrivateKey}, 26 {Name: "Local L2", URL: operations.DefaultL2NetworkURL, ChainID: operations.DefaultL2ChainID, PrivateKey: operations.DefaultSequencerPrivateKey}, 27 } 28 29 for _, network := range networks { 30 ctx := context.Background() 31 32 log.Infof("connecting to %v: %v", network.Name, network.URL) 33 client, err := ethclient.Dial(network.URL) 34 chkErr(err) 35 log.Infof("connected") 36 37 auth := operations.MustGetAuth(network.PrivateKey, network.ChainID) 38 chkErr(err) 39 40 const receiverAddr = "0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D" 41 42 balance, err := client.BalanceAt(ctx, auth.From, nil) 43 chkErr(err) 44 log.Debugf("ETH Balance for %v: %v", auth.From, balance) 45 46 // Valid ETH Transfer 47 balance, err = client.BalanceAt(ctx, auth.From, nil) 48 log.Debugf("ETH Balance for %v: %v", auth.From, balance) 49 chkErr(err) 50 transferAmount := big.NewInt(1) 51 log.Debugf("Transfer Amount: %v", transferAmount) 52 53 nonce, err := client.NonceAt(ctx, auth.From, nil) 54 chkErr(err) 55 // var lastTxHash common.Hash 56 for i := 0; i < 1000; i++ { 57 nonce := nonce + uint64(i) 58 log.Debugf("Sending TX to transfer ETH") 59 to := common.HexToAddress(receiverAddr) 60 tx := ethTransfer(ctx, client, auth, to, transferAmount, &nonce) 61 fmt.Println("tx sent: ", tx.Hash().String()) 62 // lastTxHash = tx.Hash() 63 } 64 65 // err = operations.WaitTxToBeMined(client, lastTxHash, txTimeout) 66 // chkErr(err) 67 } 68 } 69 70 func ethTransfer(ctx context.Context, client *ethclient.Client, auth *bind.TransactOpts, to common.Address, amount *big.Int, nonce *uint64) *types.Transaction { 71 if nonce == nil { 72 log.Infof("reading nonce for account: %v", auth.From.Hex()) 73 var err error 74 n, err := client.NonceAt(ctx, auth.From, nil) 75 log.Infof("nonce: %v", n) 76 chkErr(err) 77 nonce = &n 78 } 79 80 gasPrice, err := client.SuggestGasPrice(context.Background()) 81 chkErr(err) 82 83 gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{To: &to}) 84 chkErr(err) 85 86 tx := types.NewTransaction(*nonce, to, amount, gasLimit, gasPrice, nil) 87 88 signedTx, err := auth.Signer(auth.From, tx) 89 chkErr(err) 90 91 log.Infof("sending transfer tx") 92 err = client.SendTransaction(ctx, signedTx) 93 chkErr(err) 94 log.Infof("tx sent: %v", signedTx.Hash().Hex()) 95 96 rlp, err := signedTx.MarshalBinary() 97 chkErr(err) 98 99 log.Infof("tx rlp: %v", hex.EncodeToHex(rlp)) 100 101 return signedTx 102 } 103 104 func chkErr(err error) { 105 if err != nil { 106 log.Fatal(err) 107 } 108 }