DEV Community

Cover image for Getting Your Coin Balances with JavaScript
Mr. G
Mr. G

Posted on

Getting Your Coin Balances with JavaScript

Alice, a proactive investor, ventured into the volatile yet exciting world of cryptocurrencies. She meticulously spread her investments across various networks to minimize risk and maximize potential gains. Aware of the importance of security, Alice backed up her private key, ensuring a layer of safety for her digital assets.

However, upon transitioning to a new computer, Alice faced a minor setback. Her trusty MetaMask, which once offered a seamless view of her portfolio, was not yet installed. The initial sense of panic was quickly subdued by her technical acumen; she realized it was an opportunity to create a personalized solution.

Alice decided to develop a small application to monitor her diverse coin holdings across different networks. She envisioned a tool that would not only display her balances in real time but also provide alerts for significant market movements.

The Code

function getBalances() {
    const walletAddress = "<WALLET_ADDRESS>";
    const networks = [
        {
            name: "Polygon Mumbai",
            rpc: "https://polygon-testnet.public.blastapi.io/",
        },
        {
            name: "Fantom Testnet",
            rpc: "https://rpc.ankr.com/fantom_testnet/",
        },
        {
            name: "Binance Smart Chain Testnet",
            rpc: "https://data-seed-prebsc-1-s1.binance.org:8545/",
        },
    ];

    networks.map(async (network) => {
        const web3 = new Web3(network.rpc);
        const balance = await web3.eth.getBalance(walletAddress);
        console.log(network.name, "balance", Web3.utils.fromWei(balance.toString(), "ether"));
    });
}
Enter fullscreen mode Exit fullscreen mode

If you are not sure about the networks where you have coins, you can go to ankrand add all of the RPCs to check.

You can try DevWeb3.co and see what they offer as a tool for checking wallet balances across multiple networks. This utility is part of a broader toolkit aimed at assisting Web 2.0 developers in exploring and integrating Web3 technologies into their workflows.

Top comments (0)