Skip to content

getBalance

Usage

connect() must be called and a connection must be initiated before account information will be returned.

The getBalance() function returns an account’s 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 you want the balance state to automatically be reflected in the dApp, you should use the useBalance hook.

import { Balance, getBalance } from '@puzzlehq/sdk-core';
import { useState } from 'react';
export const Page = () => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | undefined>();
const [balance, setBalances] = useState<Balance[] | undefined>();
const onClick = async () => {
setLoading(true);
setError(undefined);
try {
const response: GetBalancesResponse = await getBalance({
address: 'aleo1t5ympyhrn6rgk7d0h0ktvmvzlymsnwg6w0c0ns5nac7s8d39x5qqtlaynz',
})
setBalances(response.balances);
} catch (e) {
setError((e as Error).message);
} finally {
setLoading(false);
}
}
return (
<div>
<button
onClick={ onClick }
disabled={ loading }
>
get selected account's balance
</button>
{ balances && balances.map((balance) => (
<>
<p>private balance: {balance.values.private}</p>
<p>public balance: {balance.values.public}</p>
</>
))}
{ error && <p>error getting balance: {error}</p> }
</div>
)
}

Types

type GetBalancesRequest = {
address?: string;
network?: Network;
multisig?: boolean;
};
type GetBalancesResponse = {
balances: Balance[], // balances of currently selected account's Aleo credits
}
type Balance = {
tokenId: string; // field
name: string;
symbol?: string;
decimals: number; // u8 in metadata
supply?: number; // u128 in metadata
maxSupply?: number; // u128 in metadata
admin?: string; // address in metadata
externalAuthorizationRequired?: boolean;
externalAuthorizationParty?: string; // address in metadata
isMTSP: boolean;
programId: string;
recordName: string; // Token if isMTSP === true
displayIfZero?: boolean;
iconURL?: string;
usageCount?: number;
priority: number;
coinbaseSymbol?: string;
owner: string;
network: Network;
values: {
private: number;
public: number;
};
};