Ethers.js Token Balance – Displaying Token Balance of Test Tokens using ethers.js

ethereum-wallet-dappethers.jsetherscanreactsepolia

I am working on a React-Native crypto application for fun, and I am trying to show the test tokens that I have inside my metamask wallet to display on my app to display the token balances.

I have created a Wallet Screen, and I am using the @walletconnect/modal-react-native library to connect to the metamask wallet on the Android emulator, and this works as I am able to see my wallet address.

I have to use either web3 or ethers.js to display the token balance, and the problem is since I am a newbie, I cannot find out the tokenAddress for test tokens like SepoliaEth or GoerliEth.

const tokenContract = new web3Instance.eth.Contract(tokenABI, tokenAddress);
            const balance = await tokenContract.methods.balanceOf(address).call();

Upon doing some research, I found this guide that shows how to find the token contract address from metamask:

https://support.metamask.io/hc/en-us/articles/360059683451-How-to-find-a-token-contract-address#:~:text=On%20the%20homepage%20of%20MetaMask,address%20on%20the%20active%20network.

I have looked into this guide, but when I go to etherscan and look at the token in my wallet address, I cannot seem to find the contract address for the test token like SepoliaEth which I have received from a faucet like Alchemy.

I then did some further reading and heard that tokens like Ethereum are native, and that they do not have a address of their own, and instead a ERC-20 token like WETH is the closest there is. I assume that the test tokens like SepoliaEth, GoerliEth, etc are native tokens too on their own respective test networks?

This should not be hard to figure out, but I Googled a lot and I cannot find an answer. It should be something very straightforward. I would appreciate an answer and some lecturing. Thanks for the help.

Best Answer

The balance you are looking for is the native token of a respective chain.

The approach you are going with is for the ERC20 tokens, there's a difference. Native tokens don't have any contracts deployed, they are directly recorded on the blockchain.

The code snippet you mentioned above is for ERC20 tokens like USDT, USDC, DAI, etc.

For the native tokens, this is how you get the balance.

let balance = await ethers.provider.getBalance(address);

Related Topic