Read Blockchain Data

How to Read data from the Blockchain

Julien Klepatch

In this article I will show you how to read data from the Blockchain.

We will use short scripts written in Javascript.

It’s beginner level, and you don’t need to know anything about Blockchain to follow

Read data from Ethereum

There are many Blockchains available: Ethereum, Polygon, Solana…

The most popular Blockchain is Ethereum.

Its technology, the EVM, is used in many other Blockchains, like Polygon.

So we will focus on Ethereum.

For our tutorial, we will use Javascript, and a library called Ethers.

Create an API key on Infura

For reading data on the Blockchain, you need to run a node.

Running a node yourself is difficult.

This is why we will a service called Infura.

  • Go on Infura
  • Create a free account
  • Create a project and chose “Web3 Api”
  • Copy the API key

Create a new npm project

  • Create a new folder
  • Create a new npm project with npm init -y
  • Install ethers with npm install ethers

Read the blockchain number

Create a new file called read-block-number.js and put:

const ethers = require('ethers');
const provider = new ethers.JsonRpcProvider('REPLACE BY API KEY');

async function init() {
  const blockNumber = await provider.getBlockNumber()
  console.log(`Current block number: ${blockNumber}`);
}

init();

Run it with:

node read-block-number.js

Read the ether balance of an address

Create a new file called read-balance-address.js and put:

const ethers = require('ethers');
const provider = new ethers.JsonRpcProvider('REPLACE BY API KEY');

async function init() {
  //ens addresses also work, like vitalik.eth
  const balance = await provider.getBalance('0xd8da6bf26964af9d7eed9e03e53415d37aa96045')
  const formattedBalance = ethers.formatEther(balance);
  console.log(`Ether balance of Vitalik Buterin: ${formattedBalance}`);
}

init();

run it with:

node read-balance-address.js

Read data from a smart contract

Create a new file called read-smart-contract.js and put:

const ethers = require('ethers');
const provider = new ethers.JsonRpcProvider('REPLACE BY API KEY');

const usdcAbi = [
  'function balanceOf(address) view returns (uint)',
  'function decimals() view returns (uint)'
];
const usdcAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'; 
const usdc = new ethers.Contract(usdcAddress, usdcAbi, provider);

async function init() {
  const balance = await usdc.balanceOf('0xd8da6bf26964af9d7eed9e03e53415d37aa96045');
  const decimals = await usdc.decimals();
  const formattedBalance = ethers.formatUnits(balance, decimals);
  console.log(`USDC balance of Vitalik Buterin: ${formattedBalance}`);
}

init();

run it with:

node read-balance-address.js

If you have followed up to here, the next step is to watch our free masterclass on how to get into Web3.

0 Comments

Leave a Reply

More great articles

Moralis explained in 2 mins

Moralis is Firebase for web3.

 With Moralis, any web developer can build a Blockchain app with a few lines of…

Read Story

Solidity explained in 2 mins

Solidity is a language for creating Blockchain smart contracts.

You can use it to create smart contracts, on more than 200…

Read Story

Decentralized Autonomous Organizations (DAOs) explained in 2 mins

ICO, DeFi, NFT, there is always a new bubble in crypto.

But there is one bubble we haven’t seen yet.

And when…

Read Story

Never miss a minute

Get great content to your inbox every week. No spam.
[contact-form-7 id="6" title="Footer CTA Subscribe Form"]
Arrow-up