eth_getTransactionByBlockHashAndIndex | Avalanche

Avalanche API method that retrieves information about a specific transaction based on the block hash and the transaction index within the block. This information can be used to track transactions, debug issues, analyze data, and build decentralized applications on the Avalanche blockchain.

Parameters

  • hash β€” the hash of the block
  • quantity β€” the integer identifying the transaction index position within the block, encoded as hexadecimal

Response

  • object β€” a transaction response object, or null if no transaction is found:
    • blockHash β€” the block hash. Identifies the block in which the transaction was included. This field is null for transactions that have not yet been included in a block.
    • blockNumber β€” the number of the block in which the transaction was included. This field is null for transactions that have not yet been included in a block.
    • from β€” the address of the sender who initiated the transaction.
    • gas β€” the units of gas included in the transaction by the sender.
    • gasPrice β€” the price of gas in Wei included in the transaction by the sender.
    • maxFeePerGas β€” the maximum amount the sender of the transaction is willing to pay per unit of gas for the transaction to be executed.
    • maxPriorityFeePerGas β€” the maximum priority fee the sender of the transaction is willing to pay per unit of gas.
    • hash β€” the hash that uniquely identifies the transaction.
    • input β€” the optional input data sent with the transaction, usually used to interact with smart contracts.
    • nonce β€” a counter identifying the transaction's number sent by the sender wallet. It essentially identifies how many transactions an account has made. Used to ensure each transaction is executed only once.
    • to β€” the address of the recipient of the transaction if it was a transaction to an address. For contract creation transactions, this field is null.
    • transactionIndex β€” the index of the transaction within the block. It is null for transactions that have not yet been included in a block.
    • value β€” the value of the native token transferred along with the transaction, in Wei.
    • type β€” the type of the transaction. 0 indicates a regular transfer; 2 indicates a contract creation or smart contract function call.
    • accessList β€” a list of authorized addresses and storage keys the transaction plans to interact with.
    • v β€” the recovery parameter in the Ethereum Signature Algorithm (ECDSA).
    • r β€” the first component of the signature in the Ethereum Signature Algorithm (ECDSA).
    • s β€” the second component of the signature in the Ethereum Signature Algorithm (ECDSA).

eth_getTransactionByBlockHashAndIndex code examples

const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

async function getTransaction() {
  const transaction = await web3.eth.getTransactionFromBlock("0xe07e904936fae48b44fa09fe4410837d9b592f5561026c0827dc93238d87bb0a", 2);
  console.log(transaction);
}

getTransaction();
const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);

const getTransaction = async (blockHash, index) => {
    const transaction = await provider.send("eth_getTransactionByBlockHashAndIndex", [blockHash, index]);
    console.log(transaction);
  }
  
  getTransaction("0xe07e904936fae48b44fa09fe4410837d9b592f5561026c0827dc93238d87bb0a", "0x2");
from web3 import Web3  
node_url = "CHAINSTACK_NODE_URL" 

web3 = Web3(Web3.HTTPProvider(node_url)) 
print(web3.eth.get_transaction_by_block("0xe07e904936fae48b44fa09fe4410837d9b592f5561026c0827dc93238d87bb0a", 2))

Use case

The eth_getTransactionByBlockHashAndIndex can be used to retrieve transaction details from a block. In the following example the getSendersInBlock checks how many transactions exist in a block, then receives the sender of each (from field) and displays it.

πŸ“˜

Note that there are simpler ways to get this information, and this is only a practical example of how to use eth_getTransactionByBlockHashAndIndex, if the number of transactions in the block is already known, the first step can be skipped.

const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

async function getSendersInBlock(blockHash) {
  const block = await web3.eth.getBlock(blockHash);
  const transactions = block.transactions;
  
  console.log(`Block ${blockHash} contains ${transactions.length} transactions`);

  for (let i = 0; i < transactions.length; i++) {
    const transaction = await web3.eth.getTransactionFromBlock(blockHash, i);
    console.log(`Sender of transaction at index ${i}:`, transaction.from);
  }
}

getSendersInBlock("0xe07e904936fae48b44fa09fe4410837d9b592f5561026c0827dc93238d87bb0a");

The number of transactions in the block is determined using transactions.length and the total number of transactions logged to the console.

A for loop is then used to retrieve information about each transaction in the block. For each iteration of the loop, the code calls the web3.eth.getTransactionFromBlock method to retrieve information about the transaction at the current index. The from property of the returned transaction object is logged to the console to show the sender of the transaction.

Try the eth_getTransactionByBlockHashAndIndex RPC method yourself

Language
Click Try It! to start a request and see the response here!