Skip to content

API ReferenceReact provider and hooksFunction

useAvailableVaultAndWalletTokenBalances()

function useAvailableVaultAndWalletTokenBalances(
params
): UseAvailableVaultAndWalletTokenBalancesResult;

Helper hook to fetch the total available balance of a token for an account. The total available balance includes both the “available” portion of an account’s Vault balance, as well as the account’s wallet balance.

Parameters

ParameterTypeDescription
paramsUseAvailableVaultAndWalletTokenBalancesParamsObject that contains the collection of tokens to fetch balances for, and optionally the account to fetch the balances for. If account is not provided, the query captures the SDK’s connected account when the hook renders so its cache key and deferred reads always describe the same user.

Returns

UseAvailableVaultAndWalletTokenBalancesResult

The query envelope. data is a collection of token balances, ordered the same as the provided tokens param in order for the caller to be able to match the results; isLoading and error report request state, and retry re-runs the latest completed or failed request.

Throws

Error when rendered outside <AnvilProvider>, by way of useAnvilSDK.

Example

export function SpendableBalances({ tokens }: { tokens: Token[] }) {
// Balances come back in the order `tokens` was given, so index by
// position rather than by address.
const query = useAvailableVaultAndWalletTokenBalances({ tokens });
const balances = query.data;
if (query.isLoading || !balances) return <p>Loading…</p>;
return (
<ul>
{tokens.map((token, index) => (
<li key={token.address}>
{token.symbol}: {balances[index]?.toString() ?? '0'}
</li>
))}
</ul>
);
}