Get Metamask Balance in different networks with Web3modal
As a Metamask user, you are likely to know the comfort of being able to handle your wallet in multiple blockchain networks. However, navigating different networks can be huge, especially if showing balance. In this article, we examine how web3modal can be used to display the metamask balance in various networks, including the smart binance (BSC), ETHEREUM (ETH) and Croakos chain.
What is metamask?
Metamask is a popular web extension that allows users to manage their cryptocurrency portfolio in multiple blockchain networks. Use Metamask to store, send, receive and manage the tools on the road.
Web3modal: Exhaust Library to Integration of Web3
Web3modal is an excellent directory to integrate web3 API into web applications. Provides a simple and intuitive API in the various blockchain networks, including the requests of BSC, ETH, Croakos and others.
Show the balance in web3modal in different networks
To show the Metamask account balance in different networks, you need to use the Web3modal GetBalance method. Here are some examples of this:
show the balance of the smart binance chain (BSC)
`Javascript
Import {euters} by 'web3modal';
Const Web3modal = Wait Ether.getProvider ('
Const metamaskinstance = new web3modal.web3modal ({{{{{{{{{{{{{{{{
Supplier: Web3modal,
});
Async service getbalance () {
Try {
Const Balance = (waiting metamaskinstance.getbalance ()). Tonumber ();
ReturnBSC Balance: $ {Balance};
} catch (error) {
Console.Error (error);
}
}
Getbalance (). Then ((result) => console.log (result));
In this example, we use the Metamask copy provided by Web3modal to acquire a balance of Binance Intelligent Chain Account. The “GetBalance” method returns the object in a decimal balance.
Display Ethereum Balance (ETH)
`Javascript
Import {euters} by 'web3modal';
CONST Web3modal = Wait Ether.getProvider ('
Const metamaskinstance = new web3modal.web3modal ({{{{{{{{{{{{{{{{
Supplier: Web3modal,
});
Async service getbalance () {
Try {
Const Balance = (waiting metamaskinstance.getbalance ()). Tonumber ();
Returneth Balance: $ {Balance};
} catch (error) {
Console.Error (error);
}
}
Getbalance (). Then ((result) => console.log (result));
In this example, we use the same web3modal pattern to get the Ethereum account balance in MainNet.
Show the balance on Croakos
`Javascript
Import {euters} by 'web3modal';
Const web3modal = wait ether.getprovider ('
Const metamaskinstance = new web3modal.web3modal ({{{{{{{{{{{{{{{{
Supplier: Web3modal,
});
Async service getbalance () {
Try {
Const Balance = (waiting metamaskinstance.getbalance ()). Tonumber ();
ReturnCroakos Balance: $ {Balance};
} catch (error) {
Console.Error (error);
}
}
Getbalance (). Then ((result) => console.log (result));
In this example, we use the same Web3modal to get the Croakos account balance on the Riskbit.io network.
Tips and tricks
- Be sure to replace “Your_project_id” with the Infura project ID.
- You can use
Web3modal.getbalance ()
instead of `getbalance () ‘if you want to show the balance in another format (such as a string or an array).
- If you use the Web3modal Connect method, be sure to call “Inconnect ()” when you’re ready with a copy of Metamask.