In this section we'll learn how to get the ERC20 balance for an account using ethers.
Check a particular token balance of an EVM address using ethers.js
constethers=require('ethers');// Replace these variables with your specific valuesconstwalletAddress='0xYourWalletAddress';consttokenAddress='0xTokenContractAddress';// Ethereum JSON-RPC endpointconstprovider=newethers.providers.JsonRpcProvider('https://moonbeam.unitedbloc.com');// ERC-20 ABI (replace with the actual ABI of the token)consterc20Abi= [// Standard ERC-20 functions'function balanceOf(address) view returns (uint)',];// Create a contract instanceconsttokenContract=newethers.Contract(tokenAddress, erc20Abi, provider);// Get the balance of the specified walletasyncfunctiongetBalance() {try {constbalance=awaittokenContract.balanceOf(walletAddress);console.log(`Balance of ${walletAddress} is: ${ethers.utils.formatUnits(balance)}`); } catch (error) {console.error('Error:',error.message); }}// Call the function to get the balancegetBalance();