Appearance
Retrieve Balance
Retrieve the balance and format for display. MiniPay is a stablecoin wallet. Retrieving the balance of one of our supported stablecoins is done by reading the token's smart contract.
typescript
import { useAccount, useReadContracts } from "wagmi";
import { erc20Abi, formatUnits, Hex } from "viem";
const Example = () => {
const account = useAccount();
const token = "0xcebA9300f2b948710d2653dD7B07f33A8B32118C" // USDC on Celo mainnet
const { data: balanceResult } = useReadContracts({
allowFailure: false,
contracts: [
{
address: token,
abi: erc20Abi,
functionName: 'balanceOf',
args: [account?.address as Hex]
},
{
address: token,
abi: erc20Abi,
functionName: 'decimals',
},
{
address: token,
abi: erc20Abi,
functionName: 'symbol'
},
],
})
const [balance, decimals, symbol] = balanceResult || []
const stringBalance = balance && decimals && formatUnits(balance, decimals); // 0.05
const userLocale = navigator.language || "en-US"; // Fallback to 'en-US' if not available
// Decimal formatter based on user's locale
const decimalFormatter = new Intl.NumberFormat(userLocale, {
style: "decimal",
minimumFractionDigits: 0,
maximumFractionDigits: 2
});
const formattedBalance = decimalFormatter.format(+stringBalance); // 0.05 or 0,05
return `${formattedBalance} ${symbol}`; // 0.05 USDC
};
Mainnet
Name Symbol Token Address Adapter Decimals Uses Adapter?
──────────────────── ────── ────────────────────────────────────────── ────────────────────────────────────────── ──────── ─────────────
Tether USD USD₮ 0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e 0x0E2A3e05bc9A16F5292A6170456A710cb89C6f72 6 true
PUSO PUSO 0x105d4A9306D2E55a71d2Eb95B81553AE1dC20d7B - 18 false
USDC USDC 0xcebA9300f2b948710d2653dD7B07f33A8B32118C 0x2F25deB3848C207fc8E0c34035B3Ba7fC157602B 6 true
Celo Kenyan Shilling cKES 0x456a3D042C0DbD3db53D5489e98dFb038553B0d0 - 18 false
ECO CFA eXOF 0x73F93dcc49cB8A239e2032663e9475dd5ef29A08 - 18 false
Celo Dollar cUSD 0x765DE816845861e75A25fCA122bb6898B8B1282a - 18 false
Celo Colombian Peso cCOP 0x8A567e2aE79CA692Bd748aB832081C45de4041eA - 18 false
Celo Euro cEUR 0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73 - 18 false
Celo Brazilian Real cREAL 0xe8537a3d056DA446677B9E9d6c5dB704EaAb4787 - 18 false
Celo Ghanian Cedi cGHS 0xfAeA5F3404bbA20D3cc2f8C4B0A888F55a3c7313 - 18 false
Alfajores Testnet
Name Symbol Token Address Adapter Decimals Uses Adapter?
──────────────────── ──────── ────────────────────────────────────────── ────────────────────────────────────────── ──────── ─────────────
Celo Euro cEUR 0x10c892A6EC43a53E45D0B916B4b7D383B1b78C0F - 18 false
Celo Kenyan Shilling cKES 0x1E0433C1769271ECcF4CFF9FDdD515eefE6CdF92 - 18 false
USDC USDC 0x2F25deB3848C207fc8E0c34035B3Ba7fC157602B 0x4822e58de6f5e485eF90df51C41CE01721331dC0 6 true
Celo Dollar cUSD 0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1 - 18 false
ECO CFA eXOF 0xB0FA15e002516d0301884059c0aaC0F0C72b019D - 18 false
TetherToken USD₮ 0xC4f86E9B4A588D501c1c3e25628dFd50Bc8D615e - 18 false
Celo Brazilian Real cREAL 0xE4D517785D091D3c54818832dB6094bcc2744545 - 18 false
Explanation
- useAccount: This hook retrieves the current account information, including the address.
- useReadContracts: This hook reads the function of one or multiple smart contracts.
- formatUnits: Converts the balance from its raw format to a human-readable string.
- Intl.NumberFormat: Formats the balance as a currency string based on the user's locale.
Notes
- Ensure
wagmi
is correctly configured. Follow our quickstart guide to know how. - Ensure that the account address is correctly retrieved and passed to the
useBalance
hook. - Ensure the
token
constant is correctly set to the stablecoin you want the balance of. Check theToken addresses
tab to get the list of stablecoin addresses on Celo and Celo Alfajores (testnet).
For more information, refer to the Wagmi documentation.