Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'addresses' query parameter to filter accounts by a list of addresses #1374

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/gateway/entities/gateway.component.request.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ export enum GatewayComponentRequest {
networkEconomics = 'networkEconomics',
networkTotalStaked = 'networkTotalStaked',
addressDetails = 'addressDetails',
addressesBulk = 'addressesBulk',
addressEsdt = 'addressEsdt',
addressEsdtHistorical = 'addressEsdtHistorical',
addressEsdtBalance = 'addressEsdtBalance',
5 changes: 5 additions & 0 deletions src/common/gateway/gateway.service.ts
Original file line number Diff line number Diff line change
@@ -94,6 +94,11 @@ export class GatewayService {
return result;
}

async getAccountsBulk(addresses: string[]): Promise<Account[]> {
const result = await this.create('address/bulk', GatewayComponentRequest.addressesBulk, addresses);
return result.accounts;
}

async getEsdtSupply(identifier: string): Promise<EsdtSupply> {
const result = await this.get(`network/esdt/supply/${identifier}`, GatewayComponentRequest.esdtSupply);
return result;
3 changes: 3 additions & 0 deletions src/endpoints/accounts/account.controller.ts
Original file line number Diff line number Diff line change
@@ -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<Account[]> {
const queryOptions = new AccountQueryOptions(
{
ownerAddress,
addresses,
sort,
order,
isSmartContract,
20 changes: 18 additions & 2 deletions src/endpoints/accounts/account.service.ts
Original file line number Diff line number Diff line change
@@ -328,6 +328,24 @@ export class AccountService {

const verifiedAccounts = await this.cachingService.get<string[]>(CacheInfo.VerifiedAccounts.key);

if (options.addresses && options.addresses.length > 0) {
const gatewayResponse: any = await this.gatewayService.getAccountsBulk(options.addresses);
const finalAccounts: Record<string, AccountDetailed> = {};

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;
7 changes: 6 additions & 1 deletion src/endpoints/accounts/entities/account.query.options.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}