github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/test/benchmarks/sequencer/eth-transfers/tx_sender.go (about)

     1  package eth_transfers
     2  
     3  import (
     4  	"errors"
     5  	"math/big"
     6  	"time"
     7  
     8  	"github.com/0xPolygon/supernets2-node/log"
     9  	"github.com/0xPolygon/supernets2-node/state"
    10  	"github.com/0xPolygon/supernets2-node/test/benchmarks/sequencer/common/params"
    11  	"github.com/0xPolygon/supernets2-node/test/contracts/bin/ERC20"
    12  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    13  	"github.com/ethereum/go-ethereum/core/types"
    14  	"github.com/ethereum/go-ethereum/ethclient"
    15  )
    16  
    17  var (
    18  	gasLimit  = 21000
    19  	ethAmount = big.NewInt(0)
    20  	sleepTime = 5 * time.Second
    21  	countTxs  = 0
    22  )
    23  
    24  // TxSender sends eth transfer to the sequencer
    25  func TxSender(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20) error {
    26  	log.Debugf("sending tx num: %d nonce: %d", countTxs, nonce)
    27  	auth.Nonce = big.NewInt(int64(nonce))
    28  	tx := types.NewTransaction(nonce, params.To, ethAmount, uint64(gasLimit), gasPrice, nil)
    29  	signedTx, err := auth.Signer(auth.From, tx)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	err = l2Client.SendTransaction(params.Ctx, signedTx)
    35  	if errors.Is(err, state.ErrStateNotSynchronized) {
    36  		for errors.Is(err, state.ErrStateNotSynchronized) {
    37  			time.Sleep(sleepTime)
    38  			err = l2Client.SendTransaction(params.Ctx, signedTx)
    39  		}
    40  	}
    41  
    42  	if err == nil {
    43  		countTxs += 1
    44  	}
    45  
    46  	return err
    47  }