diff --git a/CHANGELOG.md b/CHANGELOG.md index 7aa824023..6fcad452c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [4.2.23] - 2024.4.10 + +### Updated + +- Improved `getBalance` method in `address` module by introducing `tokenTypes` filter along with `native` option. + ## [4.2.22] - 2024.4.10 ### Fixed diff --git a/package.json b/package.json index cc8679296..e1f7accf9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tatumio/tatum", - "version": "4.2.22", + "version": "4.2.23", "description": "Tatum JS SDK", "author": "Tatum", "repository": "https://github.com/tatumio/tatum-js", diff --git a/src/dto/shared.dto.ts b/src/dto/shared.dto.ts index eb4514353..0f70f71d0 100644 --- a/src/dto/shared.dto.ts +++ b/src/dto/shared.dto.ts @@ -32,6 +32,12 @@ export interface AddressBalanceFilters extends Pagination { * List of addresses to check. */ addresses: string[] + + /** + * Optional filter for token types. If not specified, all token types are returned. + * Allowed values are `native`, `fungible`, `nft` and `multitoken`. + */ + tokenTypes?: string[] } export interface AddressBalanceFiltersTron { @@ -48,7 +54,8 @@ export interface AddressBalanceFiltersTezos extends Pagination { address: string /** - * Optional filter for token types. If not specified, all token types are returned. Allowed values are `fungible`, `nft` and `multitoken`. + * Optional filter for token types. If not specified, all token types are returned. + * Allowed values are `fungible`, `nft` and `multitoken`. */ tokenTypes?: string[] } diff --git a/src/service/address/address.ts b/src/service/address/address.ts index 5d7d9ead2..3a6496d7b 100644 --- a/src/service/address/address.ts +++ b/src/service/address/address.ts @@ -246,10 +246,25 @@ export class Address { page = 0, pageSize = 10, addresses, + tokenTypes, }: AddressBalanceFilters): Promise> { const chain = this.config.network return ErrorUtils.tryFail(async () => { - const nativeBalances = await this.getNativeBalance(addresses) + const result: AddressBalance[] = [] + + if (!tokenTypes || tokenTypes.includes('native')) { + const nativeBalances = await this.getNativeBalance(addresses) + result.push(...formatNativeBalances(nativeBalances, addresses, chain)) + + if (tokenTypes) { + tokenTypes = tokenTypes.filter((tokenType) => tokenType !== 'native') + } + } + + if (tokenTypes?.length === 0) { + return result + } + const tokenBalances = isDataApiEvmEnabledNetwork(chain) && (await this.connector @@ -261,17 +276,17 @@ export class Address { excludeMetadata: true, chain, addresses: addresses.join(','), + tokenTypes: tokenTypes?.join(','), }, }) .then((r) => r.result)) - const result = formatNativeBalances(nativeBalances, addresses, chain) - - if (!tokenBalances) { - return result + if (tokenBalances) { + const serializedTokenBalances = await this.processTokenBalanceDetails(tokenBalances, chain) + result.push(...serializedTokenBalances) } - const serializedTokenBalances = await this.processTokenBalanceDetails(tokenBalances, chain) - return [...result, ...serializedTokenBalances] + + return result }) }