Skip to content

Commit

Permalink
accounts endpoint make es heavy fiealds optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dragos-rebegea committed Dec 19, 2024
1 parent dbec8bd commit ee0c1b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/endpoints/accounts/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,20 @@ export class AccountController {
@UseInterceptors(DeepHistoryInterceptor)
@ApiOperation({ summary: 'Account details', description: 'Returns account details for a given address' })
@ApiQuery({ name: 'withGuardianInfo', description: 'Returns guardian data for a given address', required: false })
@ApiQuery({ name: 'fields', description: 'List of fields to filter by', required: false, isArray: true, style: 'form', explode: false })
@ApiQuery({ name: 'withTxCount', description: 'Returns the count fo the transactions', required: false })
@ApiQuery({ name: 'withAccountScResults', description: 'Returns the sc results associated with the given account', required: false })
@ApiQuery({ name: 'withTimestamp', description: 'Returns the timestamp of the last activity for the account', required: false })
@ApiQuery({ name: 'timestamp', description: 'Retrieve entry from timestamp', required: false, type: Number })
@ApiOkResponse({ type: AccountDetailed })
async getAccountDetails(
@Param('address', ParseAddressPipe) address: string,
@Query('withGuardianInfo', new ParseBoolPipe) withGuardianInfo?: boolean,
@Query('fields', ParseArrayPipe) fields?: string[],
@Query('withTxCount', new ParseBoolPipe) withTxCount?: boolean,
@Query('withAccountScResults', new ParseBoolPipe) withAccountScResults?: boolean,
@Query('withTimestamp', new ParseBoolPipe) withTimestamp?: boolean,
@Query('timestamp', ParseIntPipe) _timestamp?: number,
): Promise<AccountDetailed> {
const account = await this.accountService.getAccount(address, fields, withGuardianInfo);
const account = await this.accountService.getAccount(address, withGuardianInfo, withTxCount, withAccountScResults, withTimestamp);
if (!account) {
throw new NotFoundException('Account not found');
}
Expand Down
26 changes: 10 additions & 16 deletions src/endpoints/accounts/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { AddressUtils, BinaryUtils, OriginLogger } from '@multiversx/sdk-nestjs-
import { ApiService, ApiUtils } from "@multiversx/sdk-nestjs-http";
import { GatewayService } from 'src/common/gateway/gateway.service';
import { IndexerService } from "src/common/indexer/indexer.service";
import { AccountOptionalFieldOption } from './entities/account.optional.field.options';
import { AccountAssets } from 'src/common/assets/entities/account.assets';
import { CacheInfo } from 'src/utils/cache.info';
import { UsernameService } from '../usernames/username.service';
Expand Down Expand Up @@ -74,34 +73,29 @@ export class AccountService {
return await this.indexerService.getAccountsCount(filter);
}

async getAccount(address: string, fields?: string[], withGuardianInfo?: boolean): Promise<AccountDetailed | null> {
async getAccount(address: string, withGuardianInfo?: boolean, withTxCount?: boolean, withAccountScResults?: boolean, withTimestamp?: boolean): Promise<AccountDetailed | null> {
if (!AddressUtils.isAddressValid(address)) {
return null;
}

const provider: Provider | undefined = await this.providerService.getProvider(address);

let txCount: number = 0;
let scrCount: number = 0;
const account = await this.getAccountRaw(address);

if (!fields || fields.length === 0 || fields.includes(AccountOptionalFieldOption.txCount)) {
txCount = await this.getAccountTxCount(address);
if (account && withTxCount === true) {
account.txCount = await this.getAccountTxCount(address);
}

if (!fields || fields.length === 0 || fields.includes(AccountOptionalFieldOption.scrCount)) {
scrCount = await this.getAccountScResults(address);
if (account && withAccountScResults === true) {
account.scrCount = await this.getAccountScResults(address);
}

const [account, elasticSearchAccount] = await Promise.all([
this.getAccountRaw(address, txCount, scrCount),
this.indexerService.getAccount(address),
]);

if (account && withGuardianInfo === true) {
await this.applyGuardianInfo(account);
}

if (account && elasticSearchAccount) {
if (account && withTimestamp) {
const elasticSearchAccount = await this.indexerService.getAccount(address);
account.timestamp = elasticSearchAccount.timestamp;
}

Expand Down Expand Up @@ -161,7 +155,7 @@ export class AccountService {
return await this.getAccountRaw(address);
}

async getAccountRaw(address: string, txCount: number = 0, scrCount: number = 0): Promise<AccountDetailed | null> {
async getAccountRaw(address: string): Promise<AccountDetailed | null> {
const assets = await this.assetsService.getAllAccountAssets();
try {
const {
Expand All @@ -170,7 +164,7 @@ export class AccountService {

const shardCount = await this.protocolService.getShardCount();
const shard = AddressUtils.computeShard(AddressUtils.bech32Decode(address), shardCount);
let account = new AccountDetailed({ address, nonce, balance, code, codeHash, rootHash, txCount, scrCount, shard, developerReward, ownerAddress, scamInfo: undefined, assets: assets[address], ownerAssets: assets[ownerAddress], nftCollections: undefined, nfts: undefined });
let account = new AccountDetailed({ address, nonce, balance, code, codeHash, rootHash, shard, developerReward, ownerAddress, scamInfo: undefined, assets: assets[address], ownerAssets: assets[ownerAddress], nftCollections: undefined, nfts: undefined });

const codeAttributes = AddressUtils.decodeCodeMetadata(codeMetadata);
if (codeAttributes) {
Expand Down

0 comments on commit ee0c1b0

Please sign in to comment.