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
| Property | Type | Description |
|---|---|---|
| balances | GlyphWidgetBalances | null | Current token balances object containing information about all tokens in the wallet. |
| hasBalances | boolean | Whether 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. |
| isBalancesLoading | boolean | Whether a balance fetch is currently in progress. |
| fetchForAllNetworks | boolean | Whether balances are being fetched across all supported chains (multi-chain mode). |
| setFetchForAllNetworks | (value: boolean) => void | Toggle 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:
| Symbol | Name | Description |
|---|---|---|
| APE | ApeCoin | The native token of the ApeCoin ecosystem |
| ApeETH | Ape ETH | Ethereum token in the Ape ecosystem |
| ApeUSD | Ape USD | USD-pegged stablecoin in the Ape ecosystem |
| WAPE | Wrapped ApeCoin | Wrapped version of ApeCoin for DeFi compatibility |
Other chains will surface their own native and supported tokens.
Best Practices
- Always check if
balancesis null before accessing its properties - Implement proper error handling for the
refreshBalancesfunction - Consider implementing debouncing for frequent balance refreshes
- Use the callbacks to implement real-time notifications for balance changes
- Format currency values appropriately using the provided currency code
