MoveAndUp

Getting Your MetaMask Balance Across Multiple Networks with Web3Modal

As a Metamask user, you’re probably familiar with the convenience of being able to manage your wallet across multiple blockchain networks. However, navigating across different networks can be overwhelming, especially when it comes to displaying your balances. In this article, we’ll explore how to use Web3Modal to display your MetaMask balance across multiple 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 more.

Displaying Balance on Different Networks with Web3Modal

To display your MetaMask account balance on different networks, you will need to use Web3Modal’s getBalance method. Here are some examples of how to do this:

Displaying the Binance Smart Chain (BSC) balance.

import { ethers } from 'web3modal';

const web3Modal = await ethers.getProvider('

const metaMaskInstance = new web3modal.Web3Modal({

provider: web3Modal,

});

asynchronous 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 get the balance of your Binance Smart Chain account. The getBalance method returns an object with the balance in decimal format.

Displays the Ethereum (ETH) balance.

import { ethers } from 'web3modal';

const web3Modal = await ethers.getProvider('

const metaMaskInstance = new web3modal.Web3Modal({

provider: web3Modal,

});

asynchronous 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 Web3Modal instance to get the balance of your Ethereum account on the mainnet.

Displaying Croakos balance

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 Web3Modal instance to get the balance of your Croakos account on the riskbit.io network.

Tips and Tricks

Metamask: How to get balance on metamask all network [bsc , eth, cro, etc] using web3modal

  • Make sure to replace YOUR_PROJECT_ID with your actual Infura project ID.
  • You can also use web3modal.getBalance() instead of getBalance() if you want to display the balance in a different format (e.g. as a string or array).
  • If you use Web3Modal’s connect method, make sure to call disconnect() when you are done with the MetaMask instance.