github.com/lmittmann/w3@v0.20.0/module/eth/call_test.go (about)

     1  package eth_test
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/ethereum/go-ethereum/common"
     8  	"github.com/ethereum/go-ethereum/core/types"
     9  	"github.com/lmittmann/w3"
    10  	"github.com/lmittmann/w3/module/eth"
    11  	"github.com/lmittmann/w3/rpctest"
    12  	"github.com/lmittmann/w3/w3types"
    13  )
    14  
    15  var funcBalanceOf = w3.MustNewFunc("balanceOf(address)", "uint256")
    16  
    17  func TestCall(t *testing.T) {
    18  	rpctest.RunTestCases(t, []rpctest.TestCase[[]byte]{
    19  		{
    20  			Golden: "call_func",
    21  			Call: eth.Call(&w3types.Message{
    22  				To:   w3.APtr("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
    23  				Func: funcBalanceOf,
    24  				Args: []any{w3.A("0x000000000000000000000000000000000000c0Fe")},
    25  			}, nil, nil),
    26  			WantRet: make([]byte, 32),
    27  		},
    28  		{
    29  			Golden: "call_func__overrides",
    30  			Call: eth.Call(&w3types.Message{
    31  				To:   w3.APtr("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
    32  				Func: funcBalanceOf,
    33  				Args: []any{w3.A("0x000000000000000000000000000000000000c0Fe")},
    34  			}, nil, w3types.State{
    35  				w3.A("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"): &w3types.Account{
    36  					Storage: w3types.Storage{
    37  						w3.H("0xf68b260b81af177c0bf1a03b5d62b15aea1b486f8df26c77f33aed7538cfeb2c"): w3.H("0x000000000000000000000000000000000000000000000000000000000000002a"),
    38  					},
    39  				},
    40  			}),
    41  			WantRet: common.BigToHash(big.NewInt(42)).Bytes(),
    42  		},
    43  	})
    44  }
    45  
    46  func TestCallFunc(t *testing.T) {
    47  	srv := rpctest.NewFileServer(t, "testdata/call_func.golden")
    48  	defer srv.Close()
    49  
    50  	client := w3.MustDial(srv.URL())
    51  	defer client.Close()
    52  
    53  	var (
    54  		balance     = new(big.Int)
    55  		wantBalance = big.NewInt(0)
    56  	)
    57  	if err := client.Call(
    58  		eth.CallFunc(w3.A("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"), funcBalanceOf, w3.A("0x000000000000000000000000000000000000c0Fe")).Returns(balance),
    59  	); err != nil {
    60  		t.Fatalf("Request failed: %v", err)
    61  	}
    62  	if wantBalance.Cmp(balance) != 0 {
    63  		t.Fatalf("want %v, got %v", wantBalance, balance)
    64  	}
    65  }
    66  
    67  func TestEstimateGas(t *testing.T) {
    68  	rpctest.RunTestCases(t, []rpctest.TestCase[uint64]{
    69  		{
    70  			Golden: "estimate_gas",
    71  			Call: eth.EstimateGas(&w3types.Message{
    72  				To:   w3.APtr("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
    73  				Func: funcBalanceOf,
    74  				Args: []any{w3.A("0x000000000000000000000000000000000000c0Fe")},
    75  			}, nil),
    76  			WantRet: 23750,
    77  		},
    78  	})
    79  }
    80  
    81  func TestAccessList(t *testing.T) {
    82  	rpctest.RunTestCases(t, []rpctest.TestCase[*eth.AccessListResponse]{
    83  		{
    84  			Golden: "create_access_list",
    85  			Call: eth.AccessList(&w3types.Message{
    86  				To:   w3.APtr("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
    87  				Func: funcBalanceOf,
    88  				Args: []any{w3.A("0x000000000000000000000000000000000000c0Fe")},
    89  			}, nil),
    90  			WantRet: &eth.AccessListResponse{
    91  				AccessList: types.AccessList{
    92  					{
    93  						Address: w3.A("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
    94  						StorageKeys: []common.Hash{
    95  							w3.H("0xf68b260b81af177c0bf1a03b5d62b15aea1b486f8df26c77f33aed7538cfeb2c"),
    96  						},
    97  					},
    98  				},
    99  				GasUsed: 26050,
   100  			},
   101  		},
   102  	})
   103  }