Retrieve balance
USDC balance
import { tokens } from "@crackpay/miniapp-sdk";
import { erc20Abi, formatUnits } from "viem";
const raw = await publicClient.readContract({
address: tokens.USDC.address,
abi: erc20Abi,
functionName: "balanceOf",
args: [account],
});
const dollars = new Intl.NumberFormat(undefined, { style: "currency", currency: "USD" }).format(
Number(formatUnits(raw, tokens.USDC.decimals)),
);
Read USDC through its token contract (6 decimals) for display. eth_getBalance
returns the same money with 18 decimals; never show both. See
Arc network.
Several balances at once
const [usdc, eurc] = await publicClient.multicall({
contracts: [tokens.USDC, tokens.EURC].map((token) => ({
address: token.address,
abi: erc20Abi,
functionName: "balanceOf" as const,
args: [account] as const,
})),
allowFailure: false,
});
With wagmi
import { useReadContract } from "wagmi";
const { data: raw, isLoading } = useReadContract({
address: tokens.USDC.address,
abi: erc20Abi,
functionName: "balanceOf",
args: [account],
});
useBalance returns the 18-decimal native figure; prefer the token read above.
Loading and errors
- Show a placeholder (
$—) until the first read lands. - If a read fails, keep the last value and retry; do not show
$0.00. - Refresh after the user sends a payment.