Skip to main content

🔵 getBalances()

Concept​

The get balances method is function that returns a promise with a map of singular rune balance or multiple rune balances for an account (with the option to query a specific block or range of blocks since rune genesis, examples below).

// If no rune_id is provided, the function will return all balances of the account
const getBalances = (rune_id?: string) => Promise<Map<string, Balance>>;

The Map returned is a collection of balances indexed by the rune_id (block:tx) of the balance. Since the balances are indexed by the rune_id, you can access a specific balance by passing the rune_id as a parameter to the get method of the map.

const balances = await Account.getBalances();
const balance = balances.get("1:0"); // Get a Balance object of UNCOMMONGOODS owned by the account

Since balances is a map of Balance objects, you can access the balance object's properties like so. For example, balances are returned by the RPC unparsed and without decimals taken into account. If you want to get the balance in a human-readable format with decimals, you can use the getAmount method of the balance object.

const balances = await Account.getBalances();
const balance = balances.get("1:0"); // Get a Balance object of UNCOMMONGOODS owned by the account

console.log(balance.getAmount()); // Returns the balance in a human-readable format with decimals

Full Example Usage​

const Runes = new Runes3("https://runes.satsignal.io/v1");

const Account = await Runes.getAccount(
"bc1pdcy7dw547w8qle3ltc3efulsv2ng66pwy3fwcxpphmn8ghc5sxfsgh72la"
);

//Resolves to all the latest rune balances of "bc1pdcy7dw547w8qle3ltc3efulsv2ng66pwy3fwcxpphmn8ghc5sxfsgh72la"
const balances = await Account.getBalances();

console.log("All Balances: ");
console.log(balances.keys());
// Returns ["1:0", "840000:3", ...etc]

console.log("UNCOMMONGOODS Balance: ");
console.log(balances.get("1:0"));
//Returns a Balance object of UNCOMMONGOODS owned by the account

Note: does this syntax confuse you? Check out the Map documentation for more information on how to use Maps in JavaScript.