3. Fetch accounts balances

Now let's focus in listing all accounts in the workspace to retrieve their balances.

Types definition ​

Export Account & Paginated types ​

Let's start by adding an Account type, that define some properties listed in Account response, and a a generic type Paginated<T> for paginated responses (see Page-based pagination).

For convenience, let's create a separated types.ts file where we'll define the types definition for the responses:

Click to show content of types.ts file
export type Account = {
  // unique identifier
  id: string;

  // account name
  name: string;

  // account currency (see GET /assets/currencies)
  currency: string;

  // account index
  index: number;

  balance: {
    // total balance on blockchain
    total: string;

    // pending balance on Vault (e.g: pending transactions)
    pending: string;
  };
};

export type Paginated<T> = {
  // the current page
  page: number;
  // the next page (or null if there is none)
  next: number | null;
  // the previous page (or null if there is none)
  prev: number | null;
  // the max count of items per page
  pageSize: number;
  // the total count of items
  total: number;
  // the page data
  results: T[];
};

Import types in the main script

From our main.ts file, let's import the newly created types:

import "dotenv/config";
import axios from "axios";

import { Account, Paginated } from "./types"; 

async function main() {
  ...

Fetch accounts ​

Call the /accounts endpoint ​

Update the main.ts script again to add a call to /accounts:

  console.log(`Successfully logged as ${user.name}`);

  const accountsResponse = await client.get<Paginated<Account>>("/accounts"); 
  for (const account of accountsResponse.data.results) { 
    console.log( 
      `${account.name} (${account.currency}#${account.index}): ${account.balance.total} (pending: ${account.balance.pending})`, 
    ); 
  } 
};

Run the script ​

npm run dev

It should list the first page of accounts:

> [email protected] dev /tmp/vault-bot
> tsx main.ts

Successfully logged as BobAPI
holesky0 (ethereum_holesky#0): 48199096638237596474 (pending: 0)
cold storage (ethereum#1): 1579746080436000 (pending: 0)

Don't hesitate to tweak the output and make it yours, depending on which account information you need.

Loop in all pages ​

Adapt the script to pagination ​

There is a little problem in our script: it only fetches the first page of results (maximum of 30 items by page).

Let's update our script so it will loop until there is no more next page:

main.ts

ts

  //delete this section//
  console.log(`Successfully logged as ${user.name}`);

  const accountsResponse = await client.get<Paginated<Account>>("/accounts"); 
  for (const account of accountsResponse.data.results) { 
    console.log( 
      `${account.name} (${account.currency}#${account.index}): ${account.balance.total} (pending: ${account.balance.pending})`, 
    ); 
  } 
  // start with page 1 //
  let page: number | null = 1; 

  while (page !== null) { 
    const endpoint: string = `/accounts?page=${page}`; 
    const accountsResponse = await client.get<Paginated<Account>>(endpoint); 
    for (const account of accountsResponse.data.results) { 
      console.log( 
        `${account.name} (${account.currency}#${account.index}): ${account.balance.total} (pending: ${account.balance.pending})`, 
      ); 
    } 
    // point to next page (or `null` if there is none) //
    page = accountsResponse.data.next; 
  } 
};

Verify that it works ​

npm run dev

It should looks in all pages (you might not see any difference if you have less than 30 accounts):

> [email protected] dev
> tsx main.ts

Successfully logged as BobAPI
holesky0 (ethereum_holesky#0): 48198749137607615474 (pending: 0)
cold storage (ethereum#1): 1579746080436000 (pending: 0)

Last updated