Pulling Real-Time Ethereum Transactions with Web3.js
Author: Alex Leventer
This is the second post in our new Astra DB Crypto Blog Series. This series covers everything you need to know about Web3. If you want an introduction to NFTs, and a walkthrough of how you can load them into DataStax Astra DB, take a look at the previous blog post here.
This week on the DataStax crypto series we’re covering RPC Node basics, how to connect to a RPC node, and viewing real-time transactions. Ethereum uses JSON-RPC (Remote Procedure Protocol) API to ensure uniform methods for applications interacting with the Node. Developers can utilize a variety of languages to interact with a RPC Node (Go, C#, Java).
Ethereum Node Basics
To start getting information from the Ethereum blockchain, you will first have to set up a node. Follow the steps below to get started:
- An Ethereum Node is a server running the Ethereum software that verifies existing blocks and stays in sync when new blocks are mined.
- You can run your own Ethereum Node or purchase a “node-as-a-service” from companies like Ankr ($), Infura ($$$), or Alchemy ($).
- The most popular setup for running your own Ethereum Node is Geth running on AWS. You can view other popular setups here.
- You can make GraphQL, Websocket, or HTTP API calls to RPC nodes to receive blockchain data.
- Client libraries like Web3.js make it super easy to make API calls to a RPC node.
Pulling transaction data
Once you have your Node set up, it’s simple to pull Ethereum data using Web3.js. You can connect to your node with this Web3.js collection of libraries.
- Set up a basic Node.js project:
- Install Web3.js:
- Add the following to the top of app.js:
- Listen for new blocks using web3.js. The second a new block is mined, the last block number will be logged:
- Now that you have the latest block number, we can make a
getBlock()call to pull data on the latest transactions:
getBlock() call will return all the transactions processed in the block, for block #123, here are the results:
This is a straightforward approach to get you started reading blocks within minutes. It’s the backbone of any application built on the blockchain.
Now that you have all the data, the building options are endless. Continue following along with this series for more fundamental knowledge on developing with the blockchain. If you have any questions or need help with any of the instructions above, ping us at email@example.com.
Follow DataStax on Medium for exclusive posts on all things open source, including Pulsar, Cassandra, streaming, Kubernetes, and more. To join a buzzing community of developers from around the world and stay in the data loop, follow DataStaxDevs on Twitter and LinkedIn.