Skip to content

Commit

Permalink
New account methods supported for listing, counting and checking acco…
Browse files Browse the repository at this point in the history
…unt transfers in `AccountAPI`.
  • Loading branch information
marcvelmer committed Nov 22, 2023
1 parent 58ebdf6 commit 10ee8cf
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- New account methods supported for listing, counting and checking account transfers in `AccountAPI`.

## [0.5.2] - 2023-10-16

### Fixed
Expand Down
84 changes: 79 additions & 5 deletions src/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import { API } from './api';
import { AccountMetadata } from '../types';

enum AccountAPIMethods {
INFO = '/accounts',
LIST = '/accounts/page',
NUM_ACCOUNTS = '/accounts/count',
INFO = '/accounts/{accountId}',
SET_INFO = '/accounts',
ELECTIONS = '/accounts/{accountId}/elections/page',
TRANSFERS = '/accounts/{accountId}/transfers/page',
NUM_TRANSFERS = '/accounts/{accountId}/transfers/count',
}

export type IAccountSummary = Pick<IAccountInfoResponse, 'address' | 'balance' | 'nonce'>;

interface IAccountInfoResponse {
/**
* The address of the account
Expand Down Expand Up @@ -59,7 +64,7 @@ interface IAccountSetInfoResponse {
metadataURL: number;
}

interface IAccountTransfersResponse {
interface IAccountTransfer {
amount: number;
from: string;
height: number;
Expand All @@ -68,6 +73,34 @@ interface IAccountTransfersResponse {
to: string;
}

interface IAccountTransfersResponse {
transfers: {
received: Array<IAccountTransfer>;
sent: Array<IAccountTransfer>;
};
}

export interface IAccountsListResponse {
/**
* List of accounts
*/
accounts: Array<IAccountSummary>;
}

export interface IAccountTransfersCountResponse {
/**
* Number of account's transfers
*/
count: number;
}

export interface IAccountsCountResponse {
/**
* Number of accounts
*/
count: number;
}

export abstract class AccountAPI extends API {
/**
* Cannot be constructed.
Expand All @@ -76,16 +109,43 @@ export abstract class AccountAPI extends API {
super();
}

/**
* Returns paginated list of accounts
*
* @param {string} url API endpoint URL
* @param {number} page The page number
* @returns {Promise<IAccountsListResponse>}
*/
public static list(url: string, page: number = 0): Promise<IAccountsListResponse> {
return axios
.get<IAccountsListResponse>(url + AccountAPIMethods.LIST + '/' + page)
.then((response) => response.data)
.catch(this.isApiError);
}

/**
* Returns the number of accounts
*
* @param {string} url API endpoint URL
* @returns {Promise<IAccountsCountResponse>}
*/
public static count(url: string): Promise<IAccountsCountResponse> {
return axios
.get<IAccountsCountResponse>(url + AccountAPIMethods.NUM_ACCOUNTS)
.then((response) => response.data)
.catch(this.isApiError);
}

/**
* Fetches an Account information
*
* @param {string} url API endpoint URL
* @param {string} address The one we want the info from
* @param {string} accountId The account we want the info from
* @returns {Promise<IAccountInfoResponse>}
*/
public static info(url: string, address: string): Promise<IAccountInfoResponse> {
public static info(url: string, accountId: string): Promise<IAccountInfoResponse> {
return axios
.get<IAccountInfoResponse>(url + AccountAPIMethods.INFO + '/' + address)
.get<IAccountInfoResponse>(url + AccountAPIMethods.INFO.replace('{accountId}', accountId))
.then((response) => response.data)
.catch(this.isApiError);
}
Expand Down Expand Up @@ -120,6 +180,20 @@ export abstract class AccountAPI extends API {
.catch(this.isApiError);
}

/**
* Returns the account's transfers count
*
* @param {string} url API endpoint URL
* @param {string} accountId accountId to get the transfers count
* @returns {Promise<IAccountTransfersCountResponse>}
*/
public static transfersCount(url: string, accountId: string): Promise<IAccountTransfersCountResponse> {
return axios
.get<IAccountTransfersCountResponse>(url + AccountAPIMethods.NUM_TRANSFERS.replace('{accountId}', accountId))
.then((response) => response.data)
.catch(this.isApiError);
}

/**
* Returns paginated list of elections for a specific account
*
Expand Down

0 comments on commit 10ee8cf

Please sign in to comment.