Getting MetaMask balance on different networks with Web3Modal
As a Metamask user, you are probably familiar with the convenience of managing your wallet across multiple blockchain networks. However, navigating through different networks can be tedious, especially when it comes to displaying balances. In this article, we will look at how to use Web3Modal to display the balance for MetaMask on various networks, including Binance Smart Chain (BSC), Ethereum (ETH), and Croakos.
What is MetaMask?
MetaMask is a popular web extension that allows users to manage their cryptocurrency wallets across multiple blockchain networks. With MetaMask, you can store, send, receive and manage your assets on the go, all from one convenient interface.
Web3Modal: a comprehensive library for Web3 integration
Web3Modal is a great library for integrating Web3 APIs into your web applications. It provides a simple and intuitive API for making requests to various blockchain networks, including BSC, ETH, Croakos and others.
Display balance in different networks with Web3Modal
To display the balance of your MetaMask account in different networks, you will need to use the getBalance
method of Web3Modal. Here are some examples of how to do it:
Binance Smart Chain (BSC) balance display
import { ethers } from 'web3modal';
const web3Modal = await ethers.getProvider('
const metaMaskInstance = new web3modal.Web3Modal({
provider: web3Modal,
});
async function getBalance() {
try {
const balance = (await metaMaskInstance.getBalance()).toNumber();
return BSC Balance: ${balance}
;
} catch (error) {
console.error(error);
}
}
getBalance().then((result) => console.log(result));
In this example, we use the MetaMask instance provided by Web3Modal to retrieve your Binance Smart Chain account balance. The getBalance
method returns an object with a balance in decimal format.
Ethereum (ETH) balance display
import { ethers } from 'web3modal';
const web3Modal = await ethers.getProvider('
const metaMaskInstance = new web3modal.Web3Modal({
provider: web3Modal,
});
async function getBalance() {
try {
const balance = (await metaMaskInstance.getBalance()).toNumber();
return ETH Balance: ${balance}
;
} catch (error) {
console.error(error);
}
}
getBalance().then((result) => console.log(result));
In this example, we use the same instance of Web3Modal to retrieve your mainnet Ethereum account balance.
Croakos balance display
import { ethers } from 'web3modal';
const web3Modal = await ethers.getProvider('
const metaMaskInstance = new web3modal.Web3Modal({
provider: web3Modal,
});
async function getBalance() {
try {
const balance = (await metaMaskInstance.getBalance()).toNumber();
return Croakos balance: ${balance}
;
} catch (error) {
console.error(error);
}
}
getBalance().then((result) => console.log(result));
In this example, we use the same instance of Web3Modal to retrieve the balance of your Croakos account on the riskbit.io network.
Tips and Recommendations
- Be sure to replace
YOUR_PROJECT_ID
with your actual Infura project ID.
- You can also use
web3modal.getBalance()
instead ofgetBalance()
if you want to display the balance in a different format (for example, as a string or an array).
- If you use Web3Modal’s
connect
method, be sure to calldisconnect()
when you are done with the MetaMask instance.