From 64e24745239e91b53e0740e3b323ccee33ab100a Mon Sep 17 00:00:00 2001 From: cfaur09 Date: Thu, 7 Nov 2024 11:15:43 +0200 Subject: [PATCH 1/4] Add 'addresses' query parameter to filter accounts by a list of addresses --- src/endpoints/accounts/account.controller.ts | 3 +++ src/endpoints/accounts/entities/account.query.options.ts | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/endpoints/accounts/account.controller.ts b/src/endpoints/accounts/account.controller.ts index 8d77f4879..f5172eb25 100644 --- a/src/endpoints/accounts/account.controller.ts +++ b/src/endpoints/accounts/account.controller.ts @@ -96,6 +96,7 @@ export class AccountController { @ApiQuery({ name: 'excludeTags', description: 'Exclude specific tags from result', required: false }) @ApiQuery({ name: 'hasAssets', description: 'Returns a list of accounts that have assets', required: false }) @ApiQuery({ name: 'search', description: 'Search by account address', required: false }) + @ApiQuery({ name: 'addresses', description: 'A comma-separated list of addresses to filter by', required: false, type: String }) getAccounts( @Query('from', new DefaultValuePipe(0), ParseIntPipe) from: number, @Query("size", new DefaultValuePipe(25), ParseIntPipe) size: number, @@ -112,10 +113,12 @@ export class AccountController { @Query("excludeTags", new ParseArrayPipe) excludeTags?: string[], @Query("hasAssets", new ParseBoolPipe) hasAssets?: boolean, @Query("search") search?: string, + @Query("addresses", ParseAddressArrayPipe) addresses?: string[], ): Promise { const queryOptions = new AccountQueryOptions( { ownerAddress, + addresses, sort, order, isSmartContract, diff --git a/src/endpoints/accounts/entities/account.query.options.ts b/src/endpoints/accounts/entities/account.query.options.ts index 35b32bca7..91649d3c1 100644 --- a/src/endpoints/accounts/entities/account.query.options.ts +++ b/src/endpoints/accounts/entities/account.query.options.ts @@ -35,6 +35,10 @@ export class AccountQueryOptions { if (this.withScrCount && size > 25) { throw new BadRequestException('Size must be less than or equal to 25 when withScrCount is set'); } + + if (this.addresses && this.addresses.length > 25) { + throw new BadRequestException('Addresses array must contain 25 or fewer elements'); + } } isSet(): boolean { @@ -48,6 +52,7 @@ export class AccountQueryOptions { this.tags !== undefined || this.excludeTags !== undefined || this.hasAssets !== undefined || - this.search !== undefined; + this.search !== undefined || + this.addresses !== undefined; } } From b9e75c4e73ba6a8dd67ae75187575f65a8cd44fc Mon Sep 17 00:00:00 2001 From: cfaur09 Date: Thu, 7 Nov 2024 12:13:08 +0200 Subject: [PATCH 2/4] Add bulk account retrieval method and integrate with account service --- src/common/gateway/gateway.service.ts | 5 +++++ src/endpoints/accounts/account.service.ts | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/common/gateway/gateway.service.ts b/src/common/gateway/gateway.service.ts index e0bbb8cc3..5e63b42ae 100644 --- a/src/common/gateway/gateway.service.ts +++ b/src/common/gateway/gateway.service.ts @@ -94,6 +94,11 @@ export class GatewayService { return result; } + async getAccountsBulk(addresses: string[]): Promise { + const result = await this.create('address/bulk', GatewayComponentRequest.addressDetails, addresses); + return result.accounts; + } + async getEsdtSupply(identifier: string): Promise { const result = await this.get(`network/esdt/supply/${identifier}`, GatewayComponentRequest.esdtSupply); return result; diff --git a/src/endpoints/accounts/account.service.ts b/src/endpoints/accounts/account.service.ts index 26b3e7bad..b2fcd6462 100644 --- a/src/endpoints/accounts/account.service.ts +++ b/src/endpoints/accounts/account.service.ts @@ -328,6 +328,24 @@ export class AccountService { const verifiedAccounts = await this.cachingService.get(CacheInfo.VerifiedAccounts.key); + if (options.addresses && options.addresses.length > 0) { + const gatewayResponse: any = await this.gatewayService.getAccountsBulk(options.addresses); + const finalAccounts: Record = {}; + + for (const address in gatewayResponse) { + if (gatewayResponse.hasOwnProperty(address)) { + finalAccounts[address] = gatewayResponse[address] as AccountDetailed; + } + } + + for (const account of accounts) { + const gatewayAccount = finalAccounts[account.address]; + if (gatewayAccount) { + account.balance = gatewayAccount.balance; + } + } + } + for (const account of accounts) { account.shard = AddressUtils.computeShard(AddressUtils.bech32Decode(account.address), shardCount); account.assets = assets[account.address]; @@ -357,8 +375,6 @@ export class AccountService { if (verifiedAccounts && verifiedAccounts.includes(account.address)) { account.isVerified = true; } - - } return accounts; From 265adb8645ba09f769af394defff4552f34c84dc Mon Sep 17 00:00:00 2001 From: cfaur09 Date: Thu, 7 Nov 2024 12:31:45 +0200 Subject: [PATCH 3/4] Update request type in getAccountsBulk method to use addressesBulk --- src/common/gateway/gateway.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/gateway/gateway.service.ts b/src/common/gateway/gateway.service.ts index 5e63b42ae..4ff84a727 100644 --- a/src/common/gateway/gateway.service.ts +++ b/src/common/gateway/gateway.service.ts @@ -95,7 +95,7 @@ export class GatewayService { } async getAccountsBulk(addresses: string[]): Promise { - const result = await this.create('address/bulk', GatewayComponentRequest.addressDetails, addresses); + const result = await this.create('address/bulk', GatewayComponentRequest.addressesBulk, addresses); return result.accounts; } From e74172c88e1185656fe97fe9750d37c7229b645c Mon Sep 17 00:00:00 2001 From: cfaur09 Date: Thu, 7 Nov 2024 12:34:00 +0200 Subject: [PATCH 4/4] Add 'addressesBulk' request type to GatewayComponentRequest enum --- src/common/gateway/entities/gateway.component.request.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common/gateway/entities/gateway.component.request.ts b/src/common/gateway/entities/gateway.component.request.ts index a3821082c..066b06904 100644 --- a/src/common/gateway/entities/gateway.component.request.ts +++ b/src/common/gateway/entities/gateway.component.request.ts @@ -5,6 +5,7 @@ export enum GatewayComponentRequest { networkEconomics = 'networkEconomics', networkTotalStaked = 'networkTotalStaked', addressDetails = 'addressDetails', + addressesBulk = 'addressesBulk', addressEsdt = 'addressEsdt', addressEsdtHistorical = 'addressEsdtHistorical', addressEsdtBalance = 'addressEsdtBalance',