Skip to content
Helpers

Balances

Read the private balance from the device, show one row per token with its name and logo, and refresh it after a sync.

readBalances reads the user's private balance from their device. It fetches nothing, so it returns at once. It shows what the last sync left, so sync first when you need the latest.

In this guide, Alice's app shows her USDC balance, with what is still settling beside it.

Read the balance

import { ,  } from "@umbra/nimbus-solana-web";

await (, {});
const balances = await (, {});
const balances: readonly Balance[]

Each Balance is what Alice holds of one token in one state:

  • mint is the token's address. It is absent for a token the SDK's preset does not list.
  • state says whether she can spend it, below.
  • total is the amount, in the token's smallest unit.
  • count is how many separate pieces make up the total.
  • zone is 0, unless you read with a zone option. Most apps never pass one.

A user who has not registered yet reads as an empty list.

Each state

stateWhat it meansWhat to show
availableAlice can spend it now.Her balance.
pendingShe received it, but it is not final on Solana yet, or not yet past the time it unlocks."Settling". It becomes available on its own.
reservedA payment that has not finished is holding it."In use". See Reserved funds.
expiredIt passed the time it could be spent by."Expired". It can no longer be spent.

Show available as the balance. A payment can only spend that part.

One row per token

readBalances returns one entry per token and state. Most apps show one row per token, with the spendable amount first. The example app groups them like this:

interface TokenBalance {
  : string;
  : bigint;
  : bigint;
  : bigint;
  : bigint;
}

function (: readonly Balance[]): TokenBalance[] {
  const  = new <string, TokenBalance>();
  for (const  of ) {
    if (. === ) continue;
    const  = .(.) ?? {
      : .,
      : 0n,
      : 0n,
      : 0n,
      : 0n,
    };
    [.] += .;
    .(., );
  }
  return [....()];
}

Show token names and logos

Amounts are integers in the token's smallest unit. Format them with the token's decimals, which sdk.mints lists:

const  = ..(
  () => . === "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
);

.((25_000_000n, ?. ?? 0)); // "25"

Tokens has formatAmount in full. The SDK carries no token names or logos. The example app reads them from a token list it writes ahead of time. Tokens shows how.

Refresh after a sync

Read the balance again after each of these:

  • A sync you run. See Sync for one that runs in the background.
  • A payment that returns. Each payment syncs before it starts. A Private Send or withdrawal the SDK followed until it landed has synced again since.
  • A release of reserved funds.

A deposit lands in the private balance with the next sync, not at once. Sync before you read it back.

Next

Last updated

On this page