github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/test/e2e/shared.go (about)

     1  //nolint:deadcode,unused,varcheck
     2  package e2e
     3  
     4  import (
     5  	"context"
     6  	"fmt"
     7  	"math/big"
     8  
     9  	"github.com/0xPolygon/supernets2-node/log"
    10  	"github.com/0xPolygon/supernets2-node/state"
    11  	"github.com/0xPolygon/supernets2-node/test/operations"
    12  	"github.com/ethereum/go-ethereum"
    13  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    14  	"github.com/ethereum/go-ethereum/common"
    15  	ethTypes "github.com/ethereum/go-ethereum/core/types"
    16  	"github.com/ethereum/go-ethereum/ethclient"
    17  )
    18  
    19  const (
    20  	invalidParamsErrorCode = -32602
    21  	toAddressHex           = "0x4d5Cf5032B2a844602278b01199ED191A86c93ff"
    22  	gerFinalityBlocks      = uint64(250)
    23  )
    24  
    25  var (
    26  	toAddress = common.HexToAddress(toAddressHex)
    27  )
    28  
    29  var networks = []struct {
    30  	Name         string
    31  	URL          string
    32  	WebSocketURL string
    33  	ChainID      uint64
    34  	PrivateKey   string
    35  }{
    36  	{
    37  		Name:         "Local L1",
    38  		URL:          operations.DefaultL1NetworkURL,
    39  		WebSocketURL: operations.DefaultL1NetworkWebSocketURL,
    40  		ChainID:      operations.DefaultL1ChainID,
    41  		PrivateKey:   operations.DefaultSequencerPrivateKey,
    42  	},
    43  	{
    44  		Name:         "Local L2",
    45  		URL:          operations.DefaultL2NetworkURL,
    46  		WebSocketURL: operations.DefaultL2NetworkWebSocketURL,
    47  		ChainID:      operations.DefaultL2ChainID,
    48  		PrivateKey:   operations.DefaultSequencerPrivateKey,
    49  	},
    50  }
    51  
    52  func setup() {
    53  	var err error
    54  	ctx := context.Background()
    55  	err = operations.Teardown()
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  
    60  	opsCfg := operations.GetDefaultOperationsConfig()
    61  	opsMan, err := operations.NewManager(ctx, opsCfg)
    62  	if err != nil {
    63  		panic(err)
    64  	}
    65  	err = opsMan.Setup()
    66  	if err != nil {
    67  		panic(err)
    68  	}
    69  }
    70  
    71  func teardown() {
    72  	err := operations.Teardown()
    73  	if err != nil {
    74  		panic(err)
    75  	}
    76  }
    77  
    78  func createTX(client *ethclient.Client, auth *bind.TransactOpts, to common.Address, amount *big.Int) (*ethTypes.Transaction, error) {
    79  	nonce, err := client.NonceAt(context.Background(), auth.From, nil)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{From: auth.From, To: &to, Value: amount})
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	gasPrice, err := client.SuggestGasPrice(context.Background())
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	log.Infof("\nTX details:\n\tNonce:    %d\n\tGasLimit: %d\n\tGasPrice: %d", nonce, gasLimit, gasPrice)
    94  	if gasLimit != uint64(21000) { //nolint:gomnd
    95  		return nil, fmt.Errorf("gasLimit %d != 21000", gasLimit)
    96  	}
    97  	tx := ethTypes.NewTransaction(nonce, to, amount, gasLimit, gasPrice, nil)
    98  	signedTx, err := auth.Signer(auth.From, tx)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	log.Infof("Sending Tx %v Nonce %v", signedTx.Hash(), signedTx.Nonce())
   103  	err = client.SendTransaction(context.Background(), signedTx)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	return signedTx, nil
   108  }
   109  
   110  func logTx(tx *ethTypes.Transaction) {
   111  	sender, _ := state.GetSender(*tx)
   112  	log.Debugf("********************")
   113  	log.Debugf("Hash: %v", tx.Hash())
   114  	log.Debugf("From: %v", sender)
   115  	log.Debugf("Nonce: %v", tx.Nonce())
   116  	log.Debugf("ChainId: %v", tx.ChainId())
   117  	log.Debugf("To: %v", tx.To())
   118  	log.Debugf("Gas: %v", tx.Gas())
   119  	log.Debugf("GasPrice: %v", tx.GasPrice())
   120  	log.Debugf("Cost: %v", tx.Cost())
   121  
   122  	// b, _ := tx.MarshalBinary()
   123  	//log.Debugf("RLP: ", hex.EncodeToHex(b))
   124  	log.Debugf("********************")
   125  }