Gets the current balance of the SparkWallet, including Bitcoin balance and token balances.
Method Signature
async getBalance(): Promise<{
balance: bigint;
tokenBalances: TokenBalanceMap;
}>
// TokenBalanceMap = Map<Bech32mTokenIdentifier, { balance: bigint, tokenMetadata: UserTokenMetadata }>
interface UserTokenMetadata {
rawTokenIdentifier: Uint8Array; // Binary token identifier used to encode the bech32m identifier
tokenPublicKey: string; // Issuer's public key
tokenName: string;
tokenTicker: string;
decimals: number; // Number of decimal places
maxSupply: bigint;
extraMetadata?: Uint8Array; // Arbitrary bytes set by the issuer
}
Returns
The wallet’s current balance in satoshis as a bigint. Use Number(balance) for display or arithmetic with regular numbers.
Map of Bech32m token identifiers to token balance and metadata objects
Example
const { balance, tokenBalances } = await wallet.getBalance();
console.log("Balance:", balance);
// Iterate over token balances
for (const [tokenId, info] of tokenBalances) {
console.log(`Token ${tokenId}: ${info.balance}`);
console.log(` Name: ${info.tokenMetadata.tokenName}`);
console.log(` Ticker: ${info.tokenMetadata.tokenTicker}`);
console.log(` Decimals: ${info.tokenMetadata.decimals}`);
// Check for extra metadata
if (info.tokenMetadata.extraMetadata) {
console.log(` Extra metadata: ${info.tokenMetadata.extraMetadata.length} bytes`);
}
}