New Survey: Leveraging real-time data delivers higher revenue growth and increased developer productivity. Learn more.

Toggle Menu
Pulling Real-Time Ethereum Transactions with Web3.js

This is the second post in our brand 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, have 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 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.

Step 1.  Set up a basic Node.js project.

mkdir w3
cd w3
yarn init
touch app.js

Step 2.  Install Web3.js.

yarn add web3

Step 3.  Add the following to the top of app.js.

const Web3 = require('web3');

const RPC_ENDPOINT = “REPLACE_ME”;

const web3 = new Web3(RPC_ENDPOINT);
#replace RPC_ENDPOINT with the endpoint you spun up in step 1.

Step 4.  Listen for new blocks using web3.js. The second a new block is mined, the last block number will be logged. 

const subscription = web3.eth.subscribe(
"newBlockHeaders",
(err, result) => {
    const { number } = result;
    console.log(number)
});

Step 5.  Now that you have the latest block number, we can make a getBlock() call to pull data on the latest transactions.

web3.eth.getBlock('123').then(blockResults => console.log(blockResults))
The getBlock() call will return all the transactions processed in the block, for block #123, here are the results.

{
difficulty: '18118731572',
extraData: '0x476574682f4c5649562f76312e302e302f6c696e75782f676f312e342e32',
gasLimit: 5000,
gasUsed: 0,
hash: '0x37cb73b97d28b4c6530c925d669e4b0e07f16e4ff41f45d10d44f4c166d650e5',
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
miner: '0xbb7B8287f3F0a933474a79eAe42CBCa977791171',
mixHash: '0x2e4b92a11b1bac2a311f6a47006442bf1dc689e76c9c1fee90da56ff6f2df7a7',
nonce: '0x18c851620e8d6cb6',
number: 123,
parentHash: '0x701bc7632e80976d1a2c408ffa58e4f11aa3ed3c5a030d1125930a9d944e4343',
receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
size: 542,
stateRoot: '0x2d1e6407139174d74e9485ce0b9f80d31f6ec55f447708796d2582e3ffbdbb85',
timestamp: 1438270492,
totalDifficulty: '2181259381686',
transactions: [],
transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
uncles: []
}

Final notes

This post presents 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 hello@datastax.com.

Resources

  1. Ethereum
  2. JSON-RPC (Remote Procedure Protocol)
  3. Ankr.com
  4. Infura.io
  5. Alchemy.com
  6. Installing Geth | Go Ethereum
  7. Ethernodes.org
  8. web3.js - Ethereum JavaScript API Documentation
Authored by

Sign up for our Developer Newsletter

Get the latest articles on all things data delivered straight to your inbox.

Open-Source, Scale-Out, Cloud-Native NoSQL Database

Astra DB is scale-out NoSQL built on Apache Cassandra™. Handle any workload with zero downtime and zero lock-in at global scale.

Get Started For FreeSchedule Demo
Open-Source, Scale-Out, Cloud-Native NoSQL Database