Fetch ERC-20 balances using ethers.js and ChainstackProvider
Use ethers with the ChainstackProvider
to call smart contract functions.
const ethers = require("ethers");
// Create a ChainstackProvider instance for BNB mainnet
const chainstack = new ethers.ChainstackProvider("bnb");
// ERC20 Token Contract details
const tokenAddress = "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82"; // ERC20 Token contract address, CAKE in this case
const abi = [
// Minimal ABI including balanceOf and symbol functions
"function balanceOf(address owner) view returns (uint256)",
"function symbol() view returns (string)",
];
// Smart contract instance using address, ABI, and Chainstack instance
const tokenContract = new ethers.Contract(tokenAddress, abi, chainstack);
async function getBalanceAndSymbol(account) {
// Call balanceOf and symbol functions
const balance = await tokenContract.balanceOf(account);
const symbol = await tokenContract.symbol(); // Get the token symbol
console.log(`Balance: ${ethers.formatEther(balance, "ether")} ${symbol}`);
}
// Specify the account/address to check balance
const accountAddress = "0xe26600fF09AcDFB9f4930873806abea9A14f98Ea"; // The wallet address
getBalanceAndSymbol(accountAddress);
$ node index
Balance: 9989.336 Cake
This code snippet will show you how to use the ethers.js ChainstackProvider
to call smart contract functions, such as fetching balance and symbol.
- Install node.js in case it is not installed yet.
Create a new directory for your project, then install the ethers.js
library.
npm install ethers
Create a new file, index.js
, import the ethers.js library, and initialize a new ChainstackProvider
instance.
Check the docs to find what chains are supported by ChainstackProvider
.
const ethers = require("ethers");
// Create a ChainstackProvider instance for BNB mainnet
const chainstack = new ethers.ChainstackProvider("bnb");
We need to create a smart contract instance using the address and ABI.
In this case, we only use the part of the ABI describing the symbol()
and balanceOf()
functions.
This specific example retrieves the transfer logs for the APE token, but you can use any ERC-20 token smart contract address.
// ERC20 Token Contract details
const tokenAddress = "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82"; // ERC20 Token contract address, CAKE in this case
const abi = [
// Minimal ABI including balanceOf and symbol functions
"function balanceOf(address owner) view returns (uint256)",
"function symbol() view returns (string)",
];
Now, we can use the smart contract instance to fetch the data. Ethers will use eth_call
under the hood.
This function also converts the response and logs it to the console.
async function getBalanceAndSymbol(account) {
// Call balanceOf and symbol functions
const balance = await tokenContract.balanceOf(account);
const symbol = await tokenContract.symbol(); // Get the token symbol
console.log(`Balance: ${ethers.formatEther(balance, "ether")} ${symbol}`);
}
At this point, we can make a constant to hold the address of the account we want to check the balance for, then call the function.
// Specify the account/address to check balance
const accountAddress = "0xe26600fF09AcDFB9f4930873806abea9A14f98Ea"; // The wallet address
getBalanceAndSymbol(accountAddress);
Now you can run the script with node index
.
const ethers = require("ethers");
// Create a ChainstackProvider instance for BNB mainnet
const chainstack = new ethers.ChainstackProvider("bnb");
// ERC20 Token Contract details
const tokenAddress = "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82"; // ERC20 Token contract address, CAKE in this case
const abi = [
// Minimal ABI including balanceOf and symbol functions
"function balanceOf(address owner) view returns (uint256)",
"function symbol() view returns (string)",
];
// Smart contract instance using address, ABI, and Chainstack instance
const tokenContract = new ethers.Contract(tokenAddress, abi, chainstack);
async function getBalanceAndSymbol(account) {
// Call balanceOf and symbol functions
const balance = await tokenContract.balanceOf(account);
const symbol = await tokenContract.symbol(); // Get the token symbol
console.log(`Balance: ${ethers.formatEther(balance, "ether")} ${symbol}`);
}
// Specify the account/address to check balance
const accountAddress = "0xe26600fF09AcDFB9f4930873806abea9A14f98Ea"; // The wallet address
getBalanceAndSymbol(accountAddress);
$ node index
Balance: 9989.336 Cake
Was this page helpful?
const ethers = require("ethers");
// Create a ChainstackProvider instance for BNB mainnet
const chainstack = new ethers.ChainstackProvider("bnb");
// ERC20 Token Contract details
const tokenAddress = "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82"; // ERC20 Token contract address, CAKE in this case
const abi = [
// Minimal ABI including balanceOf and symbol functions
"function balanceOf(address owner) view returns (uint256)",
"function symbol() view returns (string)",
];
// Smart contract instance using address, ABI, and Chainstack instance
const tokenContract = new ethers.Contract(tokenAddress, abi, chainstack);
async function getBalanceAndSymbol(account) {
// Call balanceOf and symbol functions
const balance = await tokenContract.balanceOf(account);
const symbol = await tokenContract.symbol(); // Get the token symbol
console.log(`Balance: ${ethers.formatEther(balance, "ether")} ${symbol}`);
}
// Specify the account/address to check balance
const accountAddress = "0xe26600fF09AcDFB9f4930873806abea9A14f98Ea"; // The wallet address
getBalanceAndSymbol(accountAddress);
$ node index
Balance: 9989.336 Cake