Skip to content

useBalances

A hook that provides functionality for managing and refreshing token balances in the Glyph wallet. It allows you to fetch current balances and track balance changes with callbacks.

Import

tsx
import { useBalances } from "@use-glyph/sdk-react";

Usage

tsx
function WalletTokensTab() {
	const { balances, refreshBalances } = useBalances();

	// Example of using refreshBalances with callbacks
	const handleBalanceChange = (symbol: string) => {
		refreshBalances(true, {
			[symbol]: (diffAmount) => {
				console.log(`Balance changed by ${diffAmount} for ${symbol}`);
			},
		});
	};

	return (
		<div className="tokens-list">
			{balances?.tokens?.map((token) => (
				<div key={token.symbol} className="token-item">
					<span>{token.name}</span>
					<span>
						{token.amount} {token.symbol}
					</span>
					<span>{token.value}</span>
				</div>
			))}
		</div>
	);
}

Return Type

PropertyTypeDescription
balancesGlyphWidgetBalances | nullCurrent token balances object containing information about all tokens in the wallet.
hasBalancesbooleanWhether balances have been loaded. false on first load or when the user just switched chains.
refreshBalances(force?: boolean, cbs?: Record<string, (diffAmount: number) => void>) => Promise<void>Function to refresh balances. Pass force: true to bypass cache. Optionally accepts callbacks for balance changes.
isBalancesLoadingbooleanWhether a balance fetch is currently in progress.
fetchForAllNetworksbooleanWhether balances are being fetched across all supported chains (multi-chain mode).
setFetchForAllNetworks(value: boolean) => voidToggle multi-chain balance mode. When true, balances span all supported chains.

Supported Tokens

In v2.x, supported tokens depend on the chain(s) configured by the Glyph backend. When fetchForAllNetworks is true, balances are aggregated across all supported chains. Common tokens on ApeChain include:

SymbolNameDescription
APEApeCoinThe native token of the ApeCoin ecosystem
ApeETHApe ETHEthereum token in the Ape ecosystem
ApeUSDApe USDUSD-pegged stablecoin in the Ape ecosystem
WAPEWrapped ApeCoinWrapped version of ApeCoin for DeFi compatibility

Other chains will surface their own native and supported tokens.

Best Practices

  1. Always check if balances is null before accessing its properties
  2. Implement proper error handling for the refreshBalances function
  3. Consider implementing debouncing for frequent balance refreshes
  4. Use the callbacks to implement real-time notifications for balance changes
  5. Format currency values appropriately using the provided currency code