Function.formatBalance
function formatBalance(
client: KeybanClient,
balance: Balance,
token?: KeybanToken): string;
Format a balance using the client's native/fee units or the token metadata.
Parameters
Parameter | Type | Description |
---|---|---|
client | KeybanClient | The Keyban client providing network native currency and fee units. |
balance | Balance | Raw amount and optional hints: decimals/symbol/isNative/isFees. |
token? | KeybanToken | Optional token metadata used when isNative /isFees are false. |
Returns
string
A formatted string with decimals applied and symbol when available.
Example
import { KeybanClient, KeybanNetwork, formatBalance } from "@keyban/sdk-base";
const client = new KeybanClient({ apiUrl: "https://api.prod.keyban.io", appId: "APP", network: KeybanNetwork.EthereumAnvil });
const native = { raw: 1_000_000_000_000_000_000n, isNative: true };
console.log(formatBalance(client, native)); // "1 ETH"
const token = { id: "1", type: "ERC20", name: "Tether", symbol: "USDT", decimals: 6 } as KeybanToken;
console.log(formatBalance(client, { raw: 1_000_000n }, token)); // "1 USDT"
console.log(formatBalance(client, { raw: 1_000n, isFees: true })); // e.g. "0.000001 gwei"