Skip to main content

useERC20Balance

Use this hook to retrieve the balance of a specified ERC20 token for a user address.

const { balance, isLoading, error, getBalance } = useERC20Balance({
token: "0x...",
userAddress: "0x...",
watch: true,
});

Usage

import { useERC20Balance } from "~~/hooks/eth-mobile";

function TokenBalance() {
const { balance, isLoading, error, getBalance } = useERC20Balance({
token: "0xA0b86a33E6441b8c4C327D7d0c65Bfa0ECf0e651", // USDC
userAddress: "0x1234...",
watch: true,
});

if (isLoading) return <Text>Loading balance...</Text>;
if (error) return <Text>Error: {error}</Text>;

return (
<View>
<Text>Token Balance: {balance?.toString()}</Text>
<Button onPress={() => getBalance()} title="Refresh Balance" />
</View>
);
}

Configuration

ParameterTypeRequiredDescription
tokenAddressNoThe ERC20 token contract address.
userAddressAddressNoThe address of the user to fetch the token balance for.
watchbooleanNoWhether to watch for balance changes on new blocks (default: false).

Return Values

PropertyTypeDescription
balancebigint | nullThe current token balance of the user.
isLoadingbooleanWhether the balance is currently being fetched.
errorstring | nullAny error message that occurred during balance fetching.
getBalance(token?: Address, userAddress?: Address) => Promise<bigint | undefined>Function to manually fetch the balance for specific token/user.