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

     1  package eth_test
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"sync/atomic"
     7  	"testing"
     8  
     9  	"github.com/ethereum/go-ethereum/common"
    10  	"github.com/ethereum/go-ethereum/core/types"
    11  	"github.com/google/go-cmp/cmp"
    12  	"github.com/google/go-cmp/cmp/cmpopts"
    13  	"github.com/lmittmann/w3"
    14  	"github.com/lmittmann/w3/module/eth"
    15  	"github.com/lmittmann/w3/rpctest"
    16  )
    17  
    18  var type2Tx = types.NewTx(&types.DynamicFeeTx{
    19  	ChainID:   big.NewInt(1),
    20  	Nonce:     261,
    21  	GasTipCap: w3.I("30.38 gwei"),
    22  	GasFeeCap: w3.I("32.38 gwei"),
    23  	Gas:       47238,
    24  	To:        w3.APtr("0x491D6b7D6822d5d4BC88a1264E1b47791Fd8E904"),
    25  	Data:      w3.B("0x095ea7b30000000000000000000000007645eec8bb51862a5aa855c40971b2877dae81afffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
    26  	V:         w3.I("0x1"),
    27  	R:         w3.I("0x416470241b7db89c67526881b6fd8e145416b294a35bf4280d3079f6308c2d11"),
    28  	S:         w3.I("0x2c0af1cc55c22c0bab79ec083801da63253453156356fcd4291f50d0f425a0ee"),
    29  })
    30  
    31  func TestTx(t *testing.T) {
    32  	rpctest.RunTestCases(t,
    33  		[]rpctest.TestCase[*types.Transaction]{
    34  			{
    35  				Golden: "get_transaction_by_hash__type0",
    36  				Call:   eth.Tx(w3.H("0x2ecd08e86079f08cfc27c326aa01b1c8d62f288d5961118056bac7da315f94d9")),
    37  				WantRet: types.NewTx(&types.LegacyTx{
    38  					Nonce:    3292,
    39  					GasPrice: w3.I("1559 gwei"),
    40  					Gas:      21000,
    41  					To:       w3.APtr("0x46499275b5c4d67dfa46B92D89aADA3158ea392e"),
    42  					V:        w3.I("0x26"),
    43  					R:        w3.I("0xcfaab0b753c1d71f695029e5b5da2f2f619370f5f224a42e1c19dcdcb9e814da"),
    44  					S:        w3.I("0x606961e8b1dce9439df856ef1d1243f81f45938bac647568253260473efe7cc1"),
    45  				}),
    46  			},
    47  			{
    48  				Golden:  "get_transaction_by_hash__type2",
    49  				Call:    eth.Tx(w3.H("0xed382cb554ad10e94921d263a56c670669d6c380bbdacdbf96fed625b7132a1d")),
    50  				WantRet: type2Tx,
    51  			},
    52  			{
    53  				Golden:  "get_transaction_by_hash__0x00",
    54  				Call:    eth.Tx(common.Hash{}),
    55  				WantErr: fmt.Errorf("w3: call failed: not found"),
    56  			},
    57  		},
    58  		cmp.AllowUnexported(types.Transaction{}, atomic.Value{}),
    59  		cmpopts.IgnoreFields(types.Transaction{}, "time"),
    60  	)
    61  }
    62  
    63  func TestTxByBlockNumberAndIndex(t *testing.T) {
    64  	rpctest.RunTestCases(t,
    65  		[]rpctest.TestCase[*types.Transaction]{
    66  			{
    67  				Golden:  "get_transaction_by_block_number_and_index",
    68  				Call:    eth.TxByBlockNumberAndIndex(big.NewInt(12965001), 98),
    69  				WantRet: type2Tx,
    70  			},
    71  		},
    72  		cmp.AllowUnexported(types.Transaction{}, atomic.Value{}),
    73  		cmpopts.IgnoreFields(types.Transaction{}, "time"),
    74  	)
    75  }
    76  
    77  func TestSendTx(t *testing.T) {
    78  	rpctest.RunTestCases(t, []rpctest.TestCase[common.Hash]{
    79  		{
    80  			Golden:  "send_raw_transaction",
    81  			Call:    eth.SendTx(type2Tx),
    82  			WantRet: w3.H("0xed382cb554ad10e94921d263a56c670669d6c380bbdacdbf96fed625b7132a1d"),
    83  		},
    84  	})
    85  }
    86  
    87  func TestTxReceipt(t *testing.T) {
    88  	rpctest.RunTestCases(t, []rpctest.TestCase[*types.Receipt]{
    89  		{
    90  			Golden: "get_transaction_receipt",
    91  			Call:   eth.TxReceipt(w3.H("0xed382cb554ad10e94921d263a56c670669d6c380bbdacdbf96fed625b7132a1d")),
    92  			WantRet: &types.Receipt{
    93  				Type:              2,
    94  				Status:            types.ReceiptStatusSuccessful,
    95  				CumulativeGasUsed: 8726063,
    96  				Bloom:             types.BytesToBloom(w3.B("0x00000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000010000000000000000000000006000000000000002000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000010000000000000000000000000000000000000000000000000040000000000")),
    97  				Logs: []*types.Log{
    98  					{
    99  						Address: w3.A("0x491D6b7D6822d5d4BC88a1264E1b47791Fd8E904"),
   100  						Topics: []common.Hash{
   101  							w3.H("0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"),
   102  							w3.H("0x0000000000000000000000002e419a06feb47d5f640636a55a814757fa10edf9"),
   103  							w3.H("0x0000000000000000000000007645eec8bb51862a5aa855c40971b2877dae81af"),
   104  						},
   105  						Data:        w3.B("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
   106  						BlockNumber: 12965001,
   107  						TxHash:      w3.H("0xed382cb554ad10e94921d263a56c670669d6c380bbdacdbf96fed625b7132a1d"),
   108  						TxIndex:     98,
   109  						BlockHash:   w3.H("0xa32d159805750cbe428b799a49b85dcb2300f61d806786f317260e721727d162"),
   110  						Index:       187,
   111  					},
   112  				},
   113  				TxHash:            w3.H("0xed382cb554ad10e94921d263a56c670669d6c380bbdacdbf96fed625b7132a1d"),
   114  				GasUsed:           47238,
   115  				EffectiveGasPrice: w3.I("31504967822"),
   116  				BlockHash:         w3.H("0xa32d159805750cbe428b799a49b85dcb2300f61d806786f317260e721727d162"),
   117  				BlockNumber:       big.NewInt(12965001),
   118  				TransactionIndex:  98,
   119  			},
   120  		},
   121  		{
   122  			Golden:  "get_transaction_receipt_0x00",
   123  			Call:    eth.TxReceipt(common.Hash{}),
   124  			WantErr: fmt.Errorf("w3: call failed: not found"),
   125  		},
   126  	})
   127  }
   128  
   129  func TestBlockReceipts(t *testing.T) {
   130  	rpctest.RunTestCases(t, []rpctest.TestCase[types.Receipts]{
   131  		{
   132  			Golden: "get_block_receipts",
   133  			Call:   eth.BlockReceipts(big.NewInt(0xc0fe)),
   134  			WantRet: types.Receipts{
   135  				{
   136  					Type:              2,
   137  					Status:            types.ReceiptStatusSuccessful,
   138  					CumulativeGasUsed: 8726063,
   139  					Bloom:             types.BytesToBloom(w3.B("0x00000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000010000000000000000000000006000000000000002000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000010000000000000000000000000000000000000000000000000040000000000")),
   140  					Logs: []*types.Log{
   141  						{
   142  							Address: w3.A("0x491D6b7D6822d5d4BC88a1264E1b47791Fd8E904"),
   143  							Topics: []common.Hash{
   144  								w3.H("0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"),
   145  								w3.H("0x0000000000000000000000002e419a06feb47d5f640636a55a814757fa10edf9"),
   146  								w3.H("0x0000000000000000000000007645eec8bb51862a5aa855c40971b2877dae81af"),
   147  							},
   148  							Data:        w3.B("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
   149  							BlockNumber: 12965001,
   150  							TxHash:      w3.H("0xed382cb554ad10e94921d263a56c670669d6c380bbdacdbf96fed625b7132a1d"),
   151  							TxIndex:     98,
   152  							BlockHash:   w3.H("0xa32d159805750cbe428b799a49b85dcb2300f61d806786f317260e721727d162"),
   153  							Index:       187,
   154  						},
   155  					},
   156  					TxHash:            w3.H("0xed382cb554ad10e94921d263a56c670669d6c380bbdacdbf96fed625b7132a1d"),
   157  					GasUsed:           47238,
   158  					EffectiveGasPrice: w3.I("31504967822"),
   159  					BlockHash:         w3.H("0xa32d159805750cbe428b799a49b85dcb2300f61d806786f317260e721727d162"),
   160  					BlockNumber:       big.NewInt(12965001),
   161  					TransactionIndex:  98,
   162  				},
   163  			},
   164  		},
   165  	})
   166  }
   167  
   168  func TestNonce(t *testing.T) {
   169  	rpctest.RunTestCases(t, []rpctest.TestCase[uint64]{
   170  		{
   171  			Golden:  "get_transaction_count",
   172  			Call:    eth.Nonce(w3.A("0x000000000000000000000000000000000000c0Fe"), nil),
   173  			WantRet: 1,
   174  		},
   175  	})
   176  }