Skip to content

Commit

Permalink
Divide requests for nodes and tokens in smaller requests containing m…
Browse files Browse the repository at this point in the history
…aximum 1000 elements and bug fixes
  • Loading branch information
GuticaStefan committed Aug 19, 2024
1 parent c679989 commit a5cb1ac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
20 changes: 11 additions & 9 deletions src/endpoints/nodes/node.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { NodeAuction } from "./entities/node.auction";
import { NodeAuctionFilter } from "./entities/node.auction.filter";
import { Identity } from "../identities/entities/identity";
import { NodeSortAuction } from "./entities/node.sort.auction";
import { ApiService, ApiSettings } from "@multiversx/sdk-nestjs-http";
import { ApiService } from "@multiversx/sdk-nestjs-http";

@Injectable()
export class NodeService {
Expand Down Expand Up @@ -400,22 +400,24 @@ export class NodeService {

private async getAllNodesFromApi(): Promise<Node[]> {
try {
const requestNodesCount = await this.apiService.get(this.apiConfigService.getNodesFetchServiceUrl() + '/nodes/count');
const requestNodesCount = await this.apiService.get(`${this.apiConfigService.getNodesFetchServiceUrl()}/nodes/count`);
const nodesCount = requestNodesCount.data;

const requestUrlParams = new ApiSettings();
requestUrlParams.params = {
size: nodesCount,
};
const nodes = [];
let from = 0, size = nodesCount <= 1000 ? nodesCount : 1000;

const requestNodes = await this.apiService.get(this.apiConfigService.getNodesFetchServiceUrl() + '/nodes', requestUrlParams);
const nodes = requestNodes.data;
while (size) {
const requestNodes = await this.apiService.get(`${this.apiConfigService.getTokensFetchServiceUrl()}/nodes`, { params: { from: from, size: size } });
nodes.push(...requestNodes.data);
from += size;
size = nodesCount <= from + 1000 ? nodesCount - from : 1000;
}

return nodes;
} catch (error) {
this.logger.error('An unhandled error occurred when getting nodes from API');
this.logger.error(error);
return [];
throw error;
}
}

Expand Down
23 changes: 12 additions & 11 deletions src/endpoints/tokens/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { TokenSort } from "./entities/token.sort";
import { TokenWithRoles } from "./entities/token.with.roles";
import { TokenWithRolesFilter } from "./entities/token.with.roles.filter";
import { AddressUtils, BinaryUtils, NumberUtils, TokenUtils } from "@multiversx/sdk-nestjs-common";
import { ApiService, ApiSettings, ApiUtils } from "@multiversx/sdk-nestjs-http";
import { ApiService, ApiUtils } from "@multiversx/sdk-nestjs-http";
import { CacheService } from "@multiversx/sdk-nestjs-cache";
import { IndexerService } from "src/common/indexer/indexer.service";
import { OriginLogger } from "@multiversx/sdk-nestjs-common";
Expand Down Expand Up @@ -720,12 +720,11 @@ export class TokenService {
}

async getAllTokensRaw(): Promise<TokenDetailed[]> {
this.logger.log(`Starting to fetch all tokens`);

if (this.apiConfigService.isTokensFetchFeatureEnabled()) {
return await this.getAllTokensFromApi();
}

this.logger.log(`Starting to fetch all tokens`);
const tokensProperties = await this.esdtService.getAllFungibleTokenProperties();
let tokens = tokensProperties.map(properties => ApiUtils.mergeObjects(new TokenDetailed(), properties));

Expand Down Expand Up @@ -863,22 +862,24 @@ export class TokenService {

private async getAllTokensFromApi(): Promise<TokenDetailed[]> {
try {
const requestTokensCount = await this.apiService.get(this.apiConfigService.getTokensFetchServiceUrl() + '/tokens/count');
const requestTokensCount = await this.apiService.get(`${this.apiConfigService.getTokensFetchServiceUrl()}/tokens/count`);
const tokensCount = requestTokensCount.data;

const requestUrlParams = new ApiSettings();
requestUrlParams.params = {
size: tokensCount,
};
const tokens = [];
let from = 0, size = tokensCount <= 1000 ? tokensCount : 1000;

const requestTokens = await this.apiService.get(this.apiConfigService.getTokensFetchServiceUrl() + '/tokens', requestUrlParams);
const tokens = requestTokens.data;
while (size) {
const requestTokens = await this.apiService.get(`${this.apiConfigService.getTokensFetchServiceUrl()}/tokens`, { params: { from: from, size: size } });
tokens.push(...requestTokens.data);
from += size;
size = tokensCount <= from + 1000 ? tokensCount - from : 1000;
}

return tokens;
} catch (error) {
this.logger.error('An unhandled error occurred when getting tokens from API');
this.logger.error(error);
return [];
throw error;
}
}

Expand Down

0 comments on commit a5cb1ac

Please sign in to comment.