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

Interview With Founder Of CryptoJobsList | How To Find A Job In Crypto / Blockchain Industry?

https://youtu.be/YYZN96g1C7g I have interviewed Raman, the founder of cryptojobslist.com. CryptoJobsList is the first job board for crypto / Blockchain jobs.…

Read Story

How I Got 2 jobs As A Blockchain Developer | Real story

There are a few other articles /videos that claim to explain how to get a job as a blockchain developer.…

Read Story

Weekly Blockchain News, 28th Feb

Ethereum (L1/L2) Ether saw a slight fluctuation in value and is currently at $2600. Ethereum community member called out Coinbase…

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