Skip to content

Commit

Permalink
Accounts addresses assets (#119)
Browse files Browse the repository at this point in the history
* feat: accountsAddressesAssets endpoints

* fix: tests
  • Loading branch information
vladimirvolek authored Sep 3, 2021
1 parent 717c439 commit da025c4
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 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).

## [1.2.0]

### Added

- `accountsAddressesAssets` and `accountsAddressesAssetsAll` endpoints

## [1.1.0]

### Added
Expand Down
2 changes: 1 addition & 1 deletion docs/badge-coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@blockfrost/blockfrost-js",
"version": "1.1.0",
"version": "1.2.0",
"description": "A JavaScript/TypeScript SDK for interacting with the https://blockfrost.io API",
"keywords": [
"blockfrost",
Expand Down
20 changes: 20 additions & 0 deletions src/BlockFrostAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
accountsMirsAll,
accountsAddresses,
accountsAddressesAll,
accountsAddressesAssets,
accountsAddressesAssetsAll,
} from './endpoints/api/accounts';

import {
Expand Down Expand Up @@ -279,6 +281,24 @@ class BlockFrostAPI {
*/
accountsAddressesAll = accountsAddressesAll;

/**
* accountsAddressesAssets - Obtain information about assets associated with addresses of a specific account.
*
* @param stakeAddress - Bech32 stake address
* @returns Assets associated with the account addresses
*
*/
accountsAddressesAssets = accountsAddressesAssets;

/**
* accountsAddressesAssets - Obtain information about assets associated with addresses of a specific account.
*
* @param stakeAddress - Bech32 stake address
* @returns Assets associated with the account addresses
*
*/
accountsAddressesAssetsAll = accountsAddressesAssetsAll;

/**
* assets - List of assets.
*
Expand Down
60 changes: 60 additions & 0 deletions src/endpoints/api/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,63 @@ export async function accountsAddressesAll(
}
}
}

export async function accountsAddressesAssets(
this: BlockFrostAPI,
stakeAddress: string,
pagination?: PaginationOptions,
): Promise<components['schemas']['account_addresses_assets']> {
const paginationOptions = getPaginationOptions(pagination);

return new Promise((resolve, reject) => {
this.axiosInstance(
`${this.apiUrl}/accounts/${stakeAddress}/addresses/assets`,
{
params: {
page: paginationOptions.page,
count: paginationOptions.count,
order: paginationOptions.order,
},
},
)
.then(resp => {
resolve(resp.data);
})
.catch(err => reject(handleError(err)));
});
}

export async function accountsAddressesAssetsAll(
this: BlockFrostAPI,
stakeAddress: string,
allMethodOptions?: AllMethodOptions,
): Promise<components['schemas']['account_addresses_assets']> {
let page = 1;
const count = DEFAULT_PAGINATION_PAGE_ITEMS_COUNT;
const res: components['schemas']['account_addresses_assets'] = [];
const options = getAllMethodOptions(allMethodOptions);

const getPromiseBundle = () => {
const promises = [...Array(options.batchSize).keys()].map(i =>
this.accountsAddressesAssets(stakeAddress, {
page: page + i,
count,
order: options.order,
}),
);
page += options.batchSize;
return promises;
};

// eslint-disable-next-line no-constant-condition
while (true) {
const promiseBundle = getPromiseBundle();
const pages = await Promise.all(promiseBundle);
for (const page of pages) {
res.push(...page);
if (page.length < DEFAULT_PAGINATION_PAGE_ITEMS_COUNT) {
return res;
}
}
}
}
24 changes: 24 additions & 0 deletions test/fixtures/endpoints/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,28 @@ export default [
}),
]),
},
{
command: (SDK: BlockFrostAPI) =>
SDK.accountsAddressesAssets(
'stake1u9e45fvvd4ujpc0kka0pnx9zqdvh9wl96nsg6sje0f5hmfq45lrja',
),
response: expect.arrayContaining([
expect.objectContaining({
unit: expect.any(String),
quantity: expect.any(String),
}),
]),
},
{
command: (SDK: BlockFrostAPI) =>
SDK.accountsAddressesAssetsAll(
'stake1u9e45fvvd4ujpc0kka0pnx9zqdvh9wl96nsg6sje0f5hmfq45lrja',
),
response: expect.arrayContaining([
expect.objectContaining({
unit: expect.any(String),
quantity: expect.any(String),
}),
]),
},
] as const;

0 comments on commit da025c4

Please sign in to comment.