How to get the balance of an ERC-20 token
The most popular in the Ethereum network is the ERC20 specification . To comply with this ERC20 standard, a token must have many different methods and properties. You can think of it as a standard interface for smart contracts if they are going to work in the Ethereum network as a token.
Today we are going to tell you how to get the current wallet balance of a particular ERC-20 token.
Prerequisites:
  • Installing Nodejs on your system
  • Text editor
  • Terminal/Command Line
Node configuration
See our quick start guide to know how you can work with our API
Configure your project
Now that you have done all the work to get the ethereum node up and running you can connect to the node via web3js . This is a package on npm, which will allow you to easily interact with the Ethereum blockchain.
Then you should create a folder for the project. To do this you should open a command prompt and run the following commands:
  • mkdir ERC20Balance
  • cd ERC20Balance.
This should create a folder named ERC20Balance and then move the command line to that directory.
Next you will need to install the web3.js package via npm. To do this you can run — npm install web3.
This will create the package.json and package-lock.json files as well as the node_modules folder. All of these should be in your ERC20Balance folder .
Once we’re done with that, we can create a file and name it index.js . That’s all the setup you’ll need to write the JavaScript that will give you the token balance you’re looking for!
Getting an ERC20-Token wallet balance
Once our project is set up, it’s time to learn a little bit about the Ethereum blockchain. To get an ERC-20 token balance, you’ll need to do a number of things.
  1. 1.
    Connect to the Ethereum node
  2. 2.
    Write an ABI for the smart contract we want to use to interact with the blockchain.
  3. 3.
    Find the ERC20 token to get the balance
  4. 4.
    Find a wallet to get the balance
  5. 5.
    Put it all together.
Connect to the Ethereum node
At the top of your index.js file, you’ll want to import the Web3 library that you installed earlier.
This allows us to call Web3 methods that are useful for interacting and connecting to the Ethereum blockchain.
To connect to the node, we can use the HTTP provider from WatchData that we built earlier. In general, your code should look like this:
const Web3 = require('web3');
const provider = 'https: //ethereum.api.watchdata.io/node/jsonrpc?api_key=YOUR_API_KEY';
const web3 = new Web3(new Web3.providers.HttpProvider(provider));
This code will connect you to the Watchdata API, which runs the Ethereum client for you. You can now use the Web3 variable to call any web3js methods offered by the library.
Write the ABI
ABI is short for Application Binary Interface .
What the ABI does is determine what function you would like to use from a smartcontract deployed on an Ethereum virtual machine.
The ERC20 specification is actually a smart contract on Ethereum, and you can see the entire ABI for it here . You only need the `balanceOf` method. It seems a bit unnecessary to copy it all for just one function.
Fortunately for you, it is quite possible. Of this huge thing, you only need one part to use the `balanceOf` method.
const minABI = [
{
constant: true,
inputs: [
{ name: "_owner", type: "address"
}
],
name: "balanceOf",
outputs: [
{ name: "balance", type: "uint256"
}
],
type: "function",
},
];
Find an ERC20-Token to get the balance of
You will most likely interact with many different ERC20-Tokens over the course of your crypto activities. One such token is the USDC.
USDC will be the token that I showcase, but you could use any token that follows the ERC20 spec.
You will want to head over to Etherscan to find the `contract address`. This is the smart contract that governs how the token functions. In my case it’s USDC.
You will want to grab the `Contract Address` which you can find like so:
USDC token address 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
Find the wallet address to get the balance
You can use a similar process from step 3 to find the wallet address.
I randomly selected one wallet from the list of transactions, which is under all the token information. Its address is — 0x2e91728aF3a54aCDCeD7938fE9016aE2cc5948C9
Let’s write the code
We have a connection to the Ethereum node, an ABI to call, a contract address, and a wallet address. With a few calls to web3js we can get the amount of USDC that this address contains.
Your whole index.js file will look like this
//index.js
console.log('Hello, World.');curl --request POST \
const Web3 = require('web3');
const provider = 'https: //ethereum.api.watchdata.io/node/jsonrpc?api_key='YOUR_API_KEY';
const web3 = new Web3(new Web3.providers.HttpProvider(provider));
const token = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
const wallet = '0x2e91728aF3a54aCDCeD7938fE9016aE2cc5948C9';
const minABI = [
{
constant: true,
inputs: [
{ name: "_owner", type: "address"
}
],
name: "balanceOf",
outputs: [
{ name: "balance", type: "uint256"
}
],
type: "function",
},
];
const contract = new web3.eth.Contract(minABI, token);
const getBalance = async () => {
const res = await contract.methods.balanceOf(wallet).call();
const format = web3.utils.fromWei(res);
console.log(format);
}
getBalance();
Attention , the resulting wallet balance will be in wei format . This is the smallest value that Ethereum supports
The last thing you need to do is run it! The only thing you need to do at this point is to save the index.js file and then run it in the terminal:
node index Make sure you are still in the correct directory!
It should display in your console the number of USDC in wei format
Copy link