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

     1  package eth
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/ethereum/go-ethereum/common"
     7  	"github.com/ethereum/go-ethereum/common/hexutil"
     8  	"github.com/ethereum/go-ethereum/core/types"
     9  	"github.com/lmittmann/w3/internal/module"
    10  	"github.com/lmittmann/w3/w3types"
    11  )
    12  
    13  // Tx requests the transaction with the given hash.
    14  func Tx(hash common.Hash) w3types.RPCCallerFactory[*types.Transaction] {
    15  	return module.NewFactory[*types.Transaction](
    16  		"eth_getTransactionByHash",
    17  		[]any{hash},
    18  	)
    19  }
    20  
    21  // TxByBlockHashAndIndex requests the transaction in the given block with the given index.
    22  func TxByBlockHashAndIndex(blockHash common.Hash, index uint64) w3types.RPCCallerFactory[*types.Transaction] {
    23  	return module.NewFactory[*types.Transaction](
    24  		"eth_getTransactionByBlockHashAndIndex",
    25  		[]any{blockHash, hexutil.Uint64(index)},
    26  	)
    27  }
    28  
    29  // TxByBlockNumberAndIndex requests the transaction in the given block with the given index.
    30  func TxByBlockNumberAndIndex(blockNumber *big.Int, index uint64) w3types.RPCCallerFactory[*types.Transaction] {
    31  	return module.NewFactory[*types.Transaction](
    32  		"eth_getTransactionByBlockNumberAndIndex",
    33  		[]any{module.BlockNumberArg(blockNumber), hexutil.Uint64(index)},
    34  	)
    35  }
    36  
    37  // SendRawTx sends a raw transaction to the network and returns its hash.
    38  func SendRawTx(rawTx []byte) w3types.RPCCallerFactory[common.Hash] {
    39  	return module.NewFactory[common.Hash](
    40  		"eth_sendRawTransaction",
    41  		[]any{hexutil.Encode(rawTx)},
    42  	)
    43  }
    44  
    45  // SendTx sends a signed transaction to the network and returns its hash.
    46  func SendTx(tx *types.Transaction) w3types.RPCCallerFactory[common.Hash] {
    47  	return module.NewFactory(
    48  		"eth_sendRawTransaction",
    49  		[]any{tx},
    50  		module.WithArgsWrapper[common.Hash](func(args []any) ([]any, error) {
    51  			tx := (args[0]).(*types.Transaction)
    52  
    53  			rawTx, err := tx.MarshalBinary()
    54  			if err != nil {
    55  				return nil, err
    56  			}
    57  			return []any{hexutil.Encode(rawTx)}, nil
    58  		}),
    59  	)
    60  }
    61  
    62  // TxReceipt requests the receipt of the transaction with the given hash.
    63  func TxReceipt(txHash common.Hash) w3types.RPCCallerFactory[*types.Receipt] {
    64  	return module.NewFactory[*types.Receipt](
    65  		"eth_getTransactionReceipt",
    66  		[]any{txHash},
    67  	)
    68  }
    69  
    70  // BlockReceipts requests all receipts of the transactions in the given block.
    71  func BlockReceipts(number *big.Int) w3types.RPCCallerFactory[types.Receipts] {
    72  	return module.NewFactory[types.Receipts](
    73  		"eth_getBlockReceipts",
    74  		[]any{module.BlockNumberArg(number)},
    75  	)
    76  }
    77  
    78  // Nonce requests the nonce of the given common.Address addr at the given
    79  // blockNumber. If blockNumber is nil, the nonce at the latest known block is
    80  // requested.
    81  func Nonce(addr common.Address, blockNumber *big.Int) w3types.RPCCallerFactory[uint64] {
    82  	return module.NewFactory(
    83  		"eth_getTransactionCount",
    84  		[]any{addr, module.BlockNumberArg(blockNumber)},
    85  		module.WithRetWrapper(module.HexUint64RetWrapper),
    86  	)
    87  }