Skip to main content

Function.formatBalance

function formatBalance(
client: KeybanClient,
balance: Balance,
token?: KeybanToken): string

Formats the balance into a human-readable string with the appropriate decimals and symbol.

Parameters

ParameterTypeDescription
clientKeybanClientThe KeybanClient instance which provides information about fees and native currency.
balanceBalanceThe balance object containing raw balance, decimals, symbol, and flags indicating if it's fees or native currency.
token?KeybanToken(Optional) The KeybanToken object which provides token-specific decimals and symbol.

Returns

string

A formatted string representing the balance with the appropriate decimals and symbol.

test

Example

const client = new KeybanClient(...);
const balance = { raw: 1000000000000000000n, decimals: 18, isNative: true };
console.log(formatBalance(client, balance)); // "1 ETH"

const balanceWithToken = { raw: 1000000n };
const token = {
id: '1',
type: 'ERC20',
name: 'Tether',
symbol: 'USDT',
decimals: 6,
iconUrl: 'https://example.com/usdt-icon.png'
};
console.log(formatBalance(client, balanceWithToken, token)); // "1 USDT"

const feeBalance = { raw: 1000n, isFees: true };
console.log(formatBalance(client, feeBalance)); // "0.000001 gwei"