eth_getBlockReceipts | Polygon

Recipes
πŸ•΅οΈ
Identify if a block has been included in the main chain or was forked
Open Recipe

Polygon API method that retrieves all transaction receipts for a given block. Transaction receipts contain information about the execution status of a transaction and can be useful for monitoring the status of transfers or contract execution on the blockchain. This method is available on Erigon only.

Enable the Erigon client on your elastic Polygon node

πŸ“˜

Available on the Business subscription plan and higher.

Once you have joined a public network, do the following:

  1. Select the project with the network.
  2. Select the network.
  3. Click Add node.
  4. Provide a node name.
  5. Under Type, select Elastic.
  6. Under Mode, select Archive. With an archive node, you will be able to query historical states for the entire chain.
  7. Under Debug and trace APIs, select On.
  8. Under Hosting, select Chainstack.
  9. For Chainstack hosting, select a cloud provider and a region.
  10. Review your changes and click Add node.

Once deployed, the node status will change from Pending to Running. You will also see the Debug and trace tag next to the node name.

Enable the Erigon client on your dedicated Polygon node

πŸ“˜

Available on the Growth subscription plan and higher.

Once you have joined a public network, do the following:

  1. Select the project with the network.
  2. Select the network.
  3. Click Add node.
  4. Provide a node name.
  5. Under Type, select Dedicated.
  6. Under Mode, select Archive. With an archive node, you will be able to query historical states for the entire chain.
  7. Under Hosting, select Chainstack. See Supported hosting options.
  8. For Chainstack hosting, select a cloud provider and a region.
  9. Under Client, select Erigon.
  10. Review your changes and click Send request.

The Chainstack Sales team will reach out to your shortly.

Parameters

  • quantity or tag β€” the block hash, block number encoded as hexadecimal, or the string with:

    • latest β€” the most recent block in the blockchain and the current state of the blockchain at the most recent block

    • earliest β€” the earliest available or genesis block.

    • pending β€” the pending state and transactions block. The current state of transactions that have been broadcast to the network but have not yet been included in a block.

      πŸ“˜

      See the default block parameter.

Response

  • result β€” an array of objects with the following fields:
    • Transaction receipt β€” the object with:
      • 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.
      • contractAddress β€” the address of the contract created by the transaction if it was a contract creation transaction. Otherwise, the value is null.
      • cumulativeGasUsed β€” the total amount of gas used in the block until this transaction was executed.
      • effectiveGasPrice β€” the actual value deducted from the sender's account for this transaction.
      • from β€” the address of the sender who initiated the transaction.
      • gasUsed β€” the amount of gas used by this specific transaction alone.
      • logs β€” an array of log objects generated by this transaction, if any. Logs are generated by smart contracts.
      • logsBloom β€” the bloom filter used by light clients to quickly retrieve logs related to the transaction.
      • status β€” the success status of the transaction, represented as 1 for success or 0 for failure.
      • 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.
      • transactionHash β€” the hash that uniquely identifies the transaction.
      • transactionIndex β€” the index of the transaction within the block.
      • type β€” the type of the transaction. 0 indicates a regular transfer; 2 indicates a contract creation or smart contract function call.

eth_getBlockReceipts code examples

/* 
eth_getBlockReceipts is not available in Web3.js
Create a custom class using web3.extend to implement it
*/

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

web3.extend({
    property: 'eth',
    methods: [{
      name: 'getBlockReceipts',
      call: 'eth_getBlockReceipts',
      params: 1,
      inputFormatter: [web3.extend.formatters.inputDefaultBlockNumberFormatter],
      outputFormatter: null
    }]
  });

async function getBlockReceipts(blockId) {
  const receipts = await web3.eth.getBlockReceipts(blockId)
  console.log(receipts)
}
  
getBlockReceipts("latest")
const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);

const eth_getBlockReceipts = async () => {
    const BlockReceipts = await provider.send("eth_getBlockReceipts", ["latest"]);
     console.log(BlockReceipts);
   };

eth_getBlockReceipts();
from web3 import Web3
node_url = "CHAINSTACK_NODE_URL"
web3 = Web3.HTTPProvider(node_url)

block_receipts = web3.provider.make_request('eth_getBlockReceipts', ['latest'])
print(block_receipts)

Use case

The eth_getBlockReceipts allows you to retrieve the receipts for all transactions in a specific block. A practical use case can be to retrieve all of the logs emitted from the transactions in a block. The method returns an array of transaction receipts, where each contains information about the transaction and any logs generated as a result of the transaction execution, so the logs can be isolated.

Here is an example of how to use eth_getBlockReceipts in ethers.js to extract all of the logs in a specific block and return them in an array:

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

async function retrieveTransactionLogs(blockNumber) {
  
    // Call the eth_getBlockReceipts method to retrieve the receipts for all transactions in the block
    let receipts = await provider.send('eth_getBlockReceipts', [blockNumber]);

    // Flatten the receipts array into a single array of logs
    let logs = receipts.flatMap(receipt => receipt.logs);

    // Return the logs
    return logs;
}

// call retrieveTransactionLogs on the latest block of the chain
async function main() {
    const blockNumber = 'lates';
    const logs = await retrieveTransactionLogs(blockNumber);
    console.log(logs);
}

main()

In this example, the retrieveTransactionLogs function takes a block identifier as an input and returns an array of logs. The function first calls the eth_getBlockReceipts method using the provider.send method and passes in the block identifier as an argument. The result is an array of receipts, then flattened into a single array of logs using the flatMap function.

Try the eth_getBlockReceipts RPC method yourself

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