Working with Tokens

Of note: When working with official Fan Tokens in your project, you must use their correct addresses in your dApp. See here:

Testnet Fan Token addressesMainnet Fan Token addresses

Prerequisite: ERC20ABI JSON file

21KB
Open

Retrieve user token balances

You can retrieve how many tokens of a specific type (ERC-20) a user holds in their wallet.

From there, you can create any scenario related to the balance. For instance, you can implement token-gating contents: giving access to certain content of your website only to holders of a certain token.

import erc20ABI from './ERC20ABI.json';

const tokenAddress = '0xYourTokenAddress'; // Replace with the actual ERC-20 contract address
const userAddress = '0xUserWalletAddress'; // Replace with the user's wallet address

async function getTokenBalance() {
  const balance = await client.readContract({
    address: tokenAddress,
    abi: erc20ABI,
    functionName: 'balanceOf', // This is standard ERC-20 function to get balance
    args: [userAddress],
  });
  return balance.toString();
}

// Example usage
getTokenBalance().then(console.log);

Retrieve user CHZ balance

You can retrieve how many tokens of a specific native token a user has held in their wallet; for instance, CHZ on Chiliz Chain.


Send ERC-20 Tokens to a wallet

You can send any token held in your wallet to any other wallet.

Last updated