github.com/0xsequence/ethkit@v1.25.0/ethtest/helpers.go (about)

     1  package ethtest
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"math/big"
     7  	"math/rand"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/0xsequence/ethkit/ethtxn"
    12  	"github.com/0xsequence/ethkit/ethwallet"
    13  	"github.com/0xsequence/ethkit/go-ethereum/common"
    14  	"github.com/0xsequence/ethkit/go-ethereum/core/types"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func RandomSeed() uint64 {
    19  	rand.Seed(time.Now().UnixNano())
    20  	return rand.Uint64()
    21  }
    22  
    23  func ETHValue(ether float64) *big.Int {
    24  	x := big.NewInt(10)
    25  	x.Exp(x, big.NewInt(15), nil)
    26  	n := big.NewInt(int64(ether * 1000))
    27  	return n.Mul(n, x)
    28  }
    29  
    30  func ETHValueBigInt(ether *big.Int) *big.Int {
    31  	oneEth := big.NewInt(10)
    32  	oneEth.Exp(oneEth, big.NewInt(18), nil)
    33  	return ether.Mul(ether, oneEth)
    34  }
    35  
    36  func GetBalance(t *testing.T, wallet *ethwallet.Wallet) *big.Int {
    37  	balance, err := wallet.GetBalance(context.Background())
    38  	assert.NoError(t, err)
    39  	assert.NotNil(t, balance)
    40  	return balance
    41  }
    42  
    43  // DummyAddr returns a dummy address
    44  func DummyAddr() common.Address {
    45  	addr, _ := ethwallet.NewWalletFromRandomEntropy()
    46  	return addr.Address()
    47  }
    48  
    49  // DummyPrivateKey returns random private key in hex used with ethwallet
    50  func DummyPrivateKey(seed uint64) string {
    51  	return fmt.Sprintf("%064x", seed)
    52  }
    53  
    54  func WalletAddresses(wallets []*ethwallet.Wallet) []common.Address {
    55  	addrs := []common.Address{}
    56  	for _, w := range wallets {
    57  		addrs = append(addrs, w.Address())
    58  	}
    59  	return addrs
    60  }
    61  
    62  func SendTransaction(t *testing.T, wallet *ethwallet.Wallet, to common.Address, data []byte, ethValue *big.Int) (*types.Transaction, ethtxn.WaitReceipt) {
    63  	txr := &ethtxn.TransactionRequest{
    64  		To:       &to,
    65  		Data:     data,
    66  		ETHValue: ethValue,
    67  		GasLimit: 300_000,
    68  	}
    69  
    70  	txn, err := wallet.NewTransaction(context.Background(), txr)
    71  	assert.NoError(t, err)
    72  
    73  	txn, waitReceipt, err := wallet.SendTransaction(context.Background(), txn)
    74  	assert.NoError(t, err)
    75  
    76  	return txn, waitReceipt
    77  }
    78  
    79  func SendTransactionAndWaitForReceipt(t *testing.T, wallet *ethwallet.Wallet, to common.Address, data []byte, ethValue *big.Int) (*types.Transaction, *types.Receipt) {
    80  	txn, waitReceipt := SendTransaction(t, wallet, to, data, ethValue)
    81  
    82  	receipt, err := waitReceipt(context.Background())
    83  	assert.NoError(t, err)
    84  	assert.True(t, receipt.Status == types.ReceiptStatusSuccessful)
    85  
    86  	return txn, receipt
    87  }
    88  
    89  func PrepareBlastSendTransactions(ctx context.Context, fromWallets []*ethwallet.Wallet, toAddresses []common.Address, values []*big.Int) ([]*ethtxn.TransactionRequest, []*types.Transaction, error) {
    90  	if len(fromWallets) != len(values) {
    91  		return nil, nil, fmt.Errorf("arrays 'fromWallets' and 'values' must be the same length")
    92  	}
    93  
    94  	lastNonces := []uint64{}
    95  	for _, w := range fromWallets {
    96  		nonce, err := w.GetNonce(ctx)
    97  		if err != nil {
    98  			return nil, nil, err
    99  		}
   100  		lastNonces = append(lastNonces, nonce)
   101  	}
   102  
   103  	txrs := []*ethtxn.TransactionRequest{}
   104  	txns := []*types.Transaction{}
   105  
   106  	for i, w := range fromWallets {
   107  		for k, to := range toAddresses {
   108  			nonce := lastNonces[i] + uint64(k)
   109  
   110  			txr := &ethtxn.TransactionRequest{
   111  				From:     w.Address(),
   112  				To:       &to,
   113  				ETHValue: ETHValue(0.1),
   114  				GasLimit: 120_000,
   115  				Nonce:    big.NewInt(int64(nonce)),
   116  			}
   117  			txrs = append(txrs, txr)
   118  
   119  			txn, err := w.NewTransaction(ctx, txr)
   120  			if err != nil {
   121  				return nil, nil, err
   122  			}
   123  			txns = append(txns, txn)
   124  		}
   125  	}
   126  
   127  	return txrs, txns, nil
   128  }