Skip to content

useBalance

Usage

connect() must be called and WalletConnect session must be initiated before account balance will be returned.

The useBalance hook returns an accounts’ balance. If an address is provided, the wallet will return that address’ balance if that address is in the wallet. Otherwise, it will return the currently selected account’s balance. If the multisig flag is true, the sdk will not fetch until an address is present. If you create or import a new account after connection, you’ll have to reconnect your dApp to the wallet.

import { useBalance } from '@puzzlehq/sdk';
const { balances, loading, error } = useBalance({
address: 'aleo1t5ympyhrn6rgk7d0h0ktvmvzlymsnwg6w0c0ns5nac7s8d39x5qqtlaynz',
multisig: true
});
if (error) {
return (<p>error loading your balances: {error}</p>)
}
if (loading) {
return (<p>loading your balances...</p>)
}
return (
<div>
<p>public balance: { balances.publicBalance.toFixed(4) }</p>
<p>private balance: { balances.privateBalance.toFixed(4) }</p>
</div>
)

Return Value

{
balances: Balance[], // balances of currently selected account's Aleo credits
loading: boolean,
error?: string, // WalletConnect error or Puzzle wallet error
}
type Balance = {
privateBalance: number;
publicBalance: number;
};