Skip to content

Commit

Permalink
ALL-6149 Add tokenTypes filter to address getBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj.bacovcin committed Apr 10, 2024
1 parent 5d44b67 commit 4f71c1b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 8 additions & 1 deletion src/dto/shared.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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[]
}
Expand Down
29 changes: 22 additions & 7 deletions src/service/address/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,25 @@ export class Address {
page = 0,
pageSize = 10,
addresses,
tokenTypes,
}: AddressBalanceFilters): Promise<ResponseDto<AddressBalance[]>> {
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
Expand All @@ -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
})
}

Expand Down

0 comments on commit 4f71c1b

Please sign in to comment.