How to get the balance of a wallet

What is the balance of a wallet?

The balance of a wallet refers to the total amount of a specific cryptocurrency that the wallet holds. Depending on your project, you might be need to display the balance of a single cryptocurrency, or the balances of all cryptocurrencies that a wallet holds.

Likewise, wallet assets refer to all forms of cryptocurrencies, tokens, and digital assets that are stored and managed within a cryptocurrency wallet.

How to do it with Moralis?

As an extensive framework for web3 developers, Moralis provides several EVM-centric APIs, allowing you to deliver projects more quickly.

In our case, Moralis has a Wallet API that you can make use of, and they even provide a code sample for the wallet balance use-case:

To adapt their code sample, replace the reference to Ethereum with their ID for Chiliz Chain, 0x15b38:

import Moralis from "moralis";

const runApp = async() => {
  await Moralis.start({
    apiKey: "YOUR_API_KEY",
    // ...and any other configuration
  });

  const address = "0x26fcbd3afebbe28d0a8684f790c48368d21665b5";

  const chain = "0x15b38", // This is for Chiliz Chain Mainnet.
                           // For Spicy Testnet, use "0x15b32".

  const response = await Moralis.EvmApi.balance.getNativeBalance({
    address,
    chain,
  });

  console.log(response.toJSON());
};

runApp();

How to do it with Tatum?

As a unified framework for building Web3 apps, Tatum provides several EVM-centric APIs, allowing you to deliver projects more quickly.

In our case, Tatum has a comprehensive Data API that you can make use of, and they even provide a code sample for the asset-retrieval use-case:

To adapt their code sample, replace the reference to Ethereum with Chiliz Chain:

// yarn add @tatumio/tatum
import {TatumSDK, Network, Chiliz, ResponseDto, AddressBalance} from '@@tatumio/tatum'

const tatum = await TatumSDK.init<Chiliz>({network: Network.CHILIZ})

const balance: ResponseDto<AddressBalance[]> = await tatum.address.getBalance({
  addresses: ['0xF64E82131BE01618487Da5142fc9d289cbb60E9d'], // replace with your address
})

console.log(balance.data)

How to do it with thirdweb

Thirdweb is a platform offering tools and infrastructure for building decentralized applications on multiple blockchain networks. It simplifies blockchain integration with SDKs, APIs, and pre-built components.

In our case, thirdweb has several short code samples on their docs site, which we can use as an inspiration.

Here is their code, adapted to Chiliz:

const sdk = new ThirdwebSDK("https://88888.rpc.thirdweb.com");
const walletAddress = "0xYourWalletAddress";

async function getWalletBalance() {
  try {
    const balance = await sdk.getBalance(walletAddress);
    console.log(`Balance of wallet ${walletAddress}: ${balance.displayValue} ${balance.symbol}`);
  } catch (error) {
    console.error("Error fetching wallet balance:", error);
  }
}

getWalletBalance();

Last updated