How to get ERC20 token balance for an address, using ethers.js
Quick tutorial showing how to get an erc20 token balance, using a small ethers.js script
Table of Contents for How to get ERC20 token balance for an address, using ethers.js
How to get an erc20 token balance using ethers.js
If you want to get a wallet address token balance for an ERC20 token, it is very easy with ethers.js.
You need a few things:
- The contract address.
- For USDT for example, it is 0xdac17f958d2ee523a2206206994597c13d831ec7
- The ABI for the contract address.
- As we know USDT is erc20, we can just use the standard erc20 ABI. This will allow us to easily make a call and ethers will know what arguments to send, and what it will reeive.
- You can find the ABIs online, or you can easily export it from tools like remix.
- In the example below I’ve included only the data for the 2 functions needed in this demo.
- The address of a wallet of which you wnt to know the balance.
- In the code below I’ve put in 0x00000… - but you should replace this with an address you want to get the token balance of (in this case of USDT)
As we are making read only calls, we don’t need a signer and using the default provider will just work.
The Javascript code to get erc20 token balances of an address:
The following is javascript.
const ethers = require('ethers');
// this is not the full ERC20 ABI - but only the 2 functions needed for this demo.
const ERC20_ABI = [
// ABI for the decimals() function:
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
// ABI for the getBalance() function:
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
] ;
async function getFormattedBalanceOfErc20TokenHolder(contractAddress, getBalanceOfAddress) {
// take the ABI, give it to ethers.js so we can call things like `.balanceOf(address)` or `.decimals()`.
const usdtErc20Contract = new ethers.Contract(
contractAddress,
ERC20_ABI,
ethers.getDefaultProvider()
);
// magic of JS, we can now call any function defined in the ABI, and ethers.js knows what arguments to send
const balance = await usdtErc20Contract.balanceOf(getBalanceOfAddress);
const numDecimals = await usdtErc20Contract.decimals();
// balance is an ethers BigNumber, so we can use .div() on it
const formattedBalance = balance.div(10 ** numDecimals);
console.log(`balance of ${getBalanceOfAddress} for contract ${contractAddress} is ${formattedBalance}`);
return formattedBalance;
}
// you can use any erc20 token address here - this example uses usdt
const USDT_CONTRACT_ADDRESS = '0xdAC17F958D2ee523a2206206994597C13D831ec7';
const addr = '0x0000000000000000000000000000000000000000'; // replace this with a real address!
getFormattedBalanceOfErc20TokenHolder(USDT_CONTRACT_ADDRESS, addr);
If you don’t already have a package.json file in that directory, you will probably need to:
- run
yarn init -y
to add a simple package.json file. - Then you will want to run
yarn add ethers
. - You should then be able to run it with
node ./your-file.js
.
Get erc20 balance in typescript using ethers.js
If you want to get the balance of an erc20 token in typescript, you can easily take the above script and make the following changes:
Add ts-node and typescript as dev dependencies (yarn add -D typescript ts-node @types/node
)
Then run the script above with ts-node ./your-file.ts
Spotted a typo or have a suggestion to make this crypto dev article better? Please let me know!
Next post
Previous post
📙 Solidity Auditing online quiz
Learn how to audit smart contracts by looking at some example code and trying to find the bugs
⛽ Solidity Gas Optimizations Guide
How to optimize and reduce gas usage in your smart contracts in Solidity
🧪 Guide to testing with Foundry
Guide to adding testing for your Solidity contracts, using the Foundry and Forge tools
📌 Guide to UTXO
UTXO and the UTXO set (used by blockchains such as Bitcoin) explained
📐 Solidity Assembly Guide
Introduction guide to using assembly in your Solidity smart contracts
📦 Ethereum EOF format explained
Information explaining what the upcoming Ethereum EOF format is all about