How to get the balance of a wallet

What is the CHZ 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

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, we need to 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 = "0xYourWalletAddress";

  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

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, we need to replace the reference to Ethereum with Chiliz Chain:

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: ['0xYourWalletAddress'], // replace with your address
})

console.log(balance.data)

How to do it with thirdweb

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();

How to do it using Nodit

Nodit provides the following code sample in their doc:

const url = 'https://web3.nodit.io/v1/chiliz/mainnet/native/getNativeBalanceByAccount';
const options = {
  method: 'POST',
  headers: {
    accept: 'application/json',
    'content-type': 'application/json',
    'X-API-KEY': 'nodit-demo'
  },
  body: JSON.stringify({accountAddress: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'})
};

fetch(url, options)
  .then(res => res.json())
  .then(json => console.log(json))
  .catch(err => console.error(err));

Last updated

Was this helpful?