Skip to content

Commit 3d799cc

Browse files
AlfredoG87ebadiere
andauthored
Cherry-Pick PR 1115 (#1140)
Removed the unnecessary call to the mirrornode for the account transa… (#1115) * Removed the unnecessary call to the mirrornode for the account transactions. * Implemented a pagination fix and updated mocks. * Applied feedback from PR review. * Updated test to include corrent next linl in account pagination. --------- Signed-off-by: ebadiere <[email protected]> Signed-off-by: Alfredo Gutierrez <[email protected]> Co-authored-by: Eric Badiere <[email protected]>
1 parent ad4384d commit 3d799cc

File tree

4 files changed

+175
-58
lines changed

4 files changed

+175
-58
lines changed

packages/relay/src/lib/clients/mirrorNodeClient.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export class MirrorNodeClient {
8282
private static CONTRACT_STATE_PROPERTY = 'state';
8383

8484

85-
8685
private static ORDER = {
8786
ASC: 'asc',
8887
DESC: 'desc'
@@ -322,6 +321,13 @@ export class MirrorNodeClient {
322321
requestId);
323322
}
324323

324+
public async getAccountPageLimit(idOrAliasOrEvmAddress: string, requestId?: string) {
325+
return this.get(`${MirrorNodeClient.GET_ACCOUNTS_ENDPOINT}${idOrAliasOrEvmAddress}?limit=${constants.MIRROR_NODE_QUERY_LIMIT}`,
326+
MirrorNodeClient.GET_ACCOUNTS_ENDPOINT,
327+
[400, 404],
328+
requestId);
329+
}
330+
325331
public async getTransactionsForAccount(accountId: string, timestampFrom: string, timestampTo: string, requestId?: string) {
326332
const queryParamObject = {};
327333
this.setQueryParam(queryParamObject, 'account.id', accountId);

packages/relay/src/lib/eth.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ export class EthImpl implements Eth {
642642
let blockNumber = null;
643643
let balanceFound = false;
644644
let weibars: BigInt = BigInt(0);
645-
const mirrorAccount = await this.mirrorNodeClient.getAccount(account, requestId);
645+
const mirrorAccount = await this.mirrorNodeClient.getAccountPageLimit(account, requestId);
646646

647647
try {
648648
if (!EthImpl.blockTagIsLatestOrPending(blockNumberOrTag)) {
@@ -669,12 +669,38 @@ export class EthImpl implements Eth {
669669
currentTimestamp = mirrorAccount.balance.timestamp;
670670
}
671671

672-
let transactionsInTimeWindow = await this.mirrorNodeClient.getTransactionsForAccount(
673-
mirrorAccount.account,
674-
block.timestamp.to,
675-
currentTimestamp,
676-
requestId
677-
);
672+
// Need to check if there are any transactions before the block.timestamp.to in the current account set returned from the inital
673+
// call to getAccountPageLimit. If there are we may need to paginate.
674+
let lastTransactionOnPageTimestamp;
675+
if(mirrorAccount.links.next !== null) {
676+
// Get the end of the page of transactions timestamp
677+
const params = new URLSearchParams(mirrorAccount.links.next.split('?')[1]);
678+
if((params === null) || (params === undefined)) {
679+
this.logger.debug(`${requestIdPrefix} Unable to find expected search parameters in account next page link ${mirrorAccount.links.next}), returning 0x0 balance`);
680+
return EthImpl.zeroHex;
681+
}
682+
683+
const timestampParameters = params.getAll('timestamp');
684+
lastTransactionOnPageTimestamp = timestampParameters[0].split(':')[1];
685+
if((lastTransactionOnPageTimestamp === null) || (lastTransactionOnPageTimestamp === undefined)) {
686+
this.logger.debug(`${requestIdPrefix} Unable to find expected beginning (gte:) timestamp in account next page link ${mirrorAccount.links.next}), returning 0x0 balance`);
687+
return EthImpl.zeroHex;
688+
}
689+
}
690+
691+
let transactionsInTimeWindow: any = [];
692+
if((typeof lastTransactionOnPageTimestamp !== "undefined") && (mirrorAccount.transactions[mirrorAccount.transactions.length -1].consensus_timestamp >= lastTransactionOnPageTimestamp)) {
693+
transactionsInTimeWindow = await this.mirrorNodeClient.getTransactionsForAccount(
694+
mirrorAccount.account,
695+
block.timestamp.to,
696+
currentTimestamp,
697+
requestId
698+
);
699+
} else {
700+
transactionsInTimeWindow = mirrorAccount.transactions.filter((tx: any) => {
701+
return tx.consensus_timestamp >= block.timestamp.to && tx.consensus_timestamp <= currentTimestamp;
702+
});
703+
}
678704

679705
for(const tx of transactionsInTimeWindow) {
680706
for (const transfer of tx.transfers) {
@@ -1611,7 +1637,7 @@ export class EthImpl implements Eth {
16111637
const logs = logResults.flatMap(logResult => logResult ? logResult : [] );
16121638
logs.sort((a: any, b: any) => {
16131639
return a.timestamp >= b.timestamp ? 1 : -1;
1614-
})
1640+
});
16151641

16161642
return logs;
16171643
}

0 commit comments

Comments
 (0)