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.
Hey Julien,
Your tutorial is clear and concise, making it easy for readers to follow along with the steps. I appreciate the fact that you focused on Ethereum, which is the most popular Blockchain, and used Javascript and Ethers library for the tutorial.
Your suggestion of using Infura for running a node is a great idea, especially for those who are new to Blockchain technology and find it difficult to run a node themselves. The step-by-step process of creating a free account on Infura, creating a project, and choosing "Web3 Api" was easy to follow.
Overall, your article provides valuable information for those who are interested in reading data from the Blockchain, and I look forward to reading more of your articles in the future.