- Demonstrates building a stand-in for the eth_getBlockReceipts method (available natively on Erigon and newer Geth versions) using the eth_getBlockByNumber and eth_getTransactionReceipt calls on any EVM chain.
- Uses Node.js and web3.js to fetch all transaction receipts for a specified block, grouping them into a single array – effectively replicating the one-call convenience of eth_getBlockReceipts.
- Highlights the parsing of receipt logs for more user-friendly output and includes a helper function to selectively extract specific fields (e.g. gasUsed) from each receipt.
- Offers a practical solution to unify transaction + receipt data retrieval on clients that lack the native eth_getBlockReceipts endpoint.
Main article
Theeth_getBlockReceipts method is a powerful tool in the Ethereum ecosystem that offers a window into the inner workings of the network. Retrieving the receipts of all transactions within a block provides insight into the status and outcome of each transaction. Whether you’re developing a decentralized exchange, a contract auditing tool, or just curious about the Ethereum network, the eth_getBlockReceipts method is an essential resource that can help you uncover the details and outcomes of transactions on the blockchain. It’s like having an x-ray of the Ethereum network, revealing what’s happening beneath the surface and providing a clear picture of the network’s activity.
The caveat is that this method is only available by querying nodes running the Erigon client. This guide will show you how you can emulate this method in essentially any EVM-compatible network, even if the node is not running on Erigon.
Read the Erigon vs. Geth guide to get a better understanding of these two popular Ethereum clients.
This article is code-centered, and you should read the Uncovering the power of eth_getBlockReceipts guide to have more theory insights.
Prerequisites
To start with a JavaScript development project, you’ll need to installnode.js, a powerful JavaScript runtime environment that enables developers to run JavaScript code outside a web browser. For this project, it’s recommended to use at least version 16. You can download it from their website.
With node.js installed, you’re ready to start using JavaScript. However, you’ll need access to a blockchain node to query the data. Here’s where Chainstack comes in to save the day. Simply follow these steps to sign up and deploy your own blockchain node with Chainstack for free:
Disclaimer
This tool was developed and tested using an Avalanche endpoint, but you can choose any EVM-compatible network.The project
Now that you have all the tools required, you are ready to create youreth_getBlockReceipt emulator.
Initialize an npm project
An npm (Node Package Manager) project is a software project managed using thenpm platform. npm is the default package manager for the JavaScript runtime environment node.js and is needed to manage the dependencies and packages used in a project.
At the heart of an npm project is the package.json file, a blueprint that outlines the packages and dependencies your project requires. This file acts as a roadmap, guiding npm in managing your project’s dependencies.
npm makes it easy for developers to share and reuse code, and its vast library of packages can be easily installed and used in any project. This allows developers to focus on writing their own code and let npm manage external dependencies.
To initialize an npm project, open your terminal in the root directory of your project and run the following command:
package.json file. Note that these answers do not affect the functionality of your project, so don’t stress too much about what you input.
You can also run the command with the -y flag, which will skip all questions.
npm project and are ready to start development.
Install the dependencies
As previously mentioned, creating this tool requires some npm packages. Specifically thedotenv and web3 packages.
During development, we used the following versions:
- dotenv ^16.0.3
- web3 ^1.8.1
The code
Thiseth_getBlockReceipt emulator will be developed on a single JavaScript file, so go ahead and create a file named index.js in the root directory of your project.
The tool will do the following:
- Create a provider instance and import packages.
- Generate an eth_getBlockReceipt-like object with a function.
- Loop into the object and extract specific fields with a function.
- Run the program with the main function.
Import packages and create a provider instance
The first step of any tool using the web3.js library is to import the necessary packages and create a provider instance. So add the following code at the top of yourindex.js file.
web3 instance that can access a high-level API for interacting with a blockchain; essentially, it’s your connection to the blockchain node.
This tool uses an environment variable to import the node URL to keep sensitive information safe, so it is more difficult to inadvertently share it.
This is done using the dotenv package. Create a file named .env in the root directory and create a variable holding your Chainstack node URL.
It is important that the environment variable has the same name as the variable imported in the
index.js file.Generate an eth_getBlockReceipt-like object
Now, let’s start working on the bulk of the logic for this project. Create a newasync function named getBlockReceipts which takes two parameters: the provider and the target block for retrieving receipts.
Then paste the following code into the index.js file:
eth_getBlockByNumber method from the Ethereum JSON-RPC API. When calling this method, set the full transactions flag to false to ensure that only the transaction hashes are returned in an array format.
The transaction hashes retrieved in this step will be stored in the extractTransactions constant for further processing.
eth_getTransactionReceipt method on each hash, do some parsing, and store the result in an array called receipts.
(let log of txReceipt.logs) will extract the logs data and replace the logs object from the original receipt. This step aims to provide a more user-friendly representation of the logs data. Without this processing, the logs data in the transaction receipt would only be returned as an array of objects, [object, object], which may require additional steps from the end-user to extract the relevant information.
By replacing the original logs object with the logData object, the final response includes a more intuitive representation of the log data, eliminating the need for additional processing.
Once all the logs in the transaction receipt have been processed, the code proceeds to the next iteration of the loop and repeats the same process for the next transaction.
The final step pushes the unpacked transaction receipt into the receipts array, and after all of the transactions have been processed, the function returns the receipts array.
Support function to isolate fields
This script also includes a support function calledgetElement which takes the receipts array returned by getBlockReceipts and the name of a field as the input parameters.
Paste the following function into the index.js file.
receipts array. This is achieved through the map method, which iterates over each element in the receipts array and returns the specified field’s value for that element.
In essence, the getElement function provides a convenient way to extract a specific field from a collection of objects and returns the extracted values as an array.
For example:
Main function and full code
The final step of the script is themain() function, which runs the full code and demonstrates the logic.
Paste the following at the bottom of the file.
- A block number is specified as input to the function.
- The
getBlockReceiptsfunction is invoked within the function. - The results of the
getBlockReceiptsfunction are output to the console. - The second part of the function calls the
getElementfunction. - The
tofield is extracted from each receipt using thegetElementfunction.
Full code
The previous sections show the script broken down into all its components to make it easier to understand. Here, you can find the full code:Run the program
Save the file and run the program by executing the following command:25792736 on the Avalanche mainnet, the response will be the following:
Conclusion
In conclusion, this script effectively leverages the power of the Web3 library and its methods. With this function, you can now useeth_getBlockReceipts even if your node is not running the Erigon client. At the same time, you just learned that, if a specific method is not available, you can always build it yourself.
