Skip to content

Commit c004a57

Browse files
authored
feat: stop fetching transactions data when its not needed (#4598)
Signed-off-by: Mariusz Jasuwienas <jasuwienas@gmail.com> Signed-off-by: Mariusz Jasuwienas <mariusz@jasuwienas.pl>
1 parent a4894d5 commit c004a57

File tree

5 files changed

+37
-43
lines changed

5 files changed

+37
-43
lines changed

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

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { MirrorNodeClientError } from '../errors/MirrorNodeClientError';
1717
import { SDKClientError } from '../errors/SDKClientError';
1818
import { CacheService } from '../services/cacheService/cacheService';
1919
import {
20+
IAccountRequestParams,
2021
IContractCallRequest,
2122
IContractCallResponse,
2223
IContractLogsResultsParams,
@@ -43,15 +44,14 @@ export class MirrorNodeClient {
4344
private static readonly GET_CONTRACT_ENDPOINT = 'contracts/';
4445
private static readonly CONTRACT_RESULT_LOGS_PROPERTY = 'logs';
4546
private static readonly CONTRACT_ID_PLACEHOLDER = '{contractId}';
46-
private static readonly ACCOUNT_TIMESTAMP_PROPERTY = 'timestamp';
47+
private static readonly ACCOUNT_TRANSACTIONS_PROPERTY = 'transactions';
4748
private static readonly CONTRACT_CALL_ENDPOINT = 'contracts/call';
4849
private static readonly GET_ACCOUNTS_BY_ID_ENDPOINT = 'accounts/';
4950
private static readonly GET_NETWORK_FEES_ENDPOINT = 'network/fees';
5051
private static readonly GET_TRANSACTIONS_ENDPOINT = 'transactions';
5152
private static readonly TRANSACTION_ID_PLACEHOLDER = '{transactionId}';
5253
private static readonly GET_CONTRACT_RESULT_ENDPOINT = 'contracts/results/';
5354
private static readonly GET_CONTRACT_RESULTS_ENDPOINT = 'contracts/results';
54-
private static readonly ACCOUNT_TRANSACTION_TYPE_PROPERTY = 'transactiontype';
5555
private static readonly GET_NETWORK_EXCHANGERATE_ENDPOINT = 'network/exchangerate';
5656
private static readonly GET_CONTRACT_RESULT_LOGS_ENDPOINT = 'contracts/results/logs';
5757
private static readonly CONTRACT_ADDRESS_STATE_ENDPOINT = `contracts/${MirrorNodeClient.ADDRESS_PLACEHOLDER}/state`;
@@ -514,13 +514,16 @@ export class MirrorNodeClient {
514514
public async getAccount(
515515
idOrAliasOrEvmAddress: string,
516516
requestDetails: RequestDetails,
517+
queryParamObject: IAccountRequestParams & ILimitOrderParams = { transactions: false },
517518
retries?: number,
518-
timestamp?: string,
519519
) {
520-
const queryParamObject = {};
521-
this.setQueryParam(queryParamObject, 'timestamp', timestamp);
522-
this.setQueryParam(queryParamObject, 'transactions', 'false');
523-
const queryParams = this.getQueryParams(queryParamObject);
520+
const queryParamsFiltered = Object.fromEntries(
521+
Object.entries(queryParamObject).filter(([key, value]) => {
522+
if (key === MirrorNodeClient.ACCOUNT_TRANSACTIONS_PROPERTY && value) return false;
523+
return value !== undefined && value !== '';
524+
}),
525+
);
526+
const queryParams = this.getQueryParams(queryParamsFiltered);
524527
return this.get(
525528
`${MirrorNodeClient.GET_ACCOUNTS_BY_ID_ENDPOINT}${idOrAliasOrEvmAddress}${queryParams}`,
526529
MirrorNodeClient.GET_ACCOUNTS_BY_ID_ENDPOINT,
@@ -535,32 +538,12 @@ export class MirrorNodeClient {
535538
requestDetails: RequestDetails,
536539
numberOfTransactions: number = 1,
537540
) {
538-
const queryParamObject = {};
539-
this.setQueryParam(
540-
queryParamObject,
541-
MirrorNodeClient.ACCOUNT_TRANSACTION_TYPE_PROPERTY,
542-
MirrorNodeClient.ETHEREUM_TRANSACTION_TYPE,
543-
);
544-
this.setQueryParam(queryParamObject, MirrorNodeClient.ACCOUNT_TIMESTAMP_PROPERTY, `lte:${timestampTo}`);
545-
this.setLimitOrderParams(
546-
queryParamObject,
547-
this.getLimitOrderQueryParam(numberOfTransactions, constants.ORDER.DESC),
548-
); // get latest 2 transactions to infer for single case
549-
const queryParams = this.getQueryParams(queryParamObject);
550-
551-
return this.get(
552-
`${MirrorNodeClient.GET_ACCOUNTS_BY_ID_ENDPOINT}${idOrAliasOrEvmAddress}${queryParams}`,
553-
MirrorNodeClient.GET_ACCOUNTS_BY_ID_ENDPOINT,
554-
requestDetails,
555-
);
556-
}
557-
558-
public async getAccountPageLimit(idOrAliasOrEvmAddress: string, requestDetails: RequestDetails) {
559-
return this.get(
560-
`${MirrorNodeClient.GET_ACCOUNTS_BY_ID_ENDPOINT}${idOrAliasOrEvmAddress}?limit=${constants.MIRROR_NODE_QUERY_LIMIT}`,
561-
MirrorNodeClient.GET_ACCOUNTS_BY_ID_ENDPOINT,
562-
requestDetails,
563-
);
541+
return this.getAccount(idOrAliasOrEvmAddress, requestDetails, {
542+
transactiontype: MirrorNodeClient.ETHEREUM_TRANSACTION_TYPE,
543+
timestamp: `lte:${timestampTo}`,
544+
transactions: true,
545+
...this.getLimitOrderQueryParam(numberOfTransactions, constants.ORDER.DESC),
546+
});
564547
}
565548

566549
/**
@@ -1409,10 +1392,11 @@ export class MirrorNodeClient {
14091392

14101393
let data;
14111394
try {
1395+
const params = { timestamp, transactions: false };
14121396
const promises = [
14131397
searchableTypes.includes(constants.TYPE_ACCOUNT)
14141398
? buildPromise(
1415-
this.getAccount(entityIdentifier, requestDetails, retries, timestamp).catch(() => {
1399+
this.getAccount(entityIdentifier, requestDetails, params, retries).catch(() => {
14161400
return null;
14171401
}),
14181402
)

packages/relay/src/lib/services/ethService/accountService/AccountService.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class AccountService implements IAccountService {
151151

152152
if (!balanceFound && !mirrorAccount) {
153153
// If no balance and no account, then we need to make a request to the mirror node for the account.
154-
mirrorAccount = await this.mirrorNodeClient.getAccountPageLimit(account, requestDetails);
154+
mirrorAccount = await this.mirrorNodeClient.getAccount(account, requestDetails);
155155
// Test if exists here
156156
if (mirrorAccount !== null && mirrorAccount !== undefined) {
157157
balanceFound = true;
@@ -265,7 +265,10 @@ export class AccountService implements IAccountService {
265265
else {
266266
let currentBalance = 0;
267267
let balanceFromTxs = 0;
268-
mirrorAccount = await this.mirrorNodeClient.getAccountPageLimit(account, requestDetails);
268+
mirrorAccount = await this.mirrorNodeClient.getAccount(account, requestDetails, {
269+
limit: constants.MIRROR_NODE_QUERY_LIMIT,
270+
transactions: true,
271+
});
269272
if (mirrorAccount) {
270273
if (mirrorAccount.balance) {
271274
currentBalance = mirrorAccount.balance.balance;

packages/relay/src/lib/types/mirrorNode.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ export interface ILimitOrderParams {
2727
order?: string;
2828
}
2929

30+
export interface IAccountRequestParams {
31+
timestamp?: string;
32+
transactions?: boolean;
33+
[key: string]: string | boolean | number | undefined; // Allow dynamic params
34+
}
35+
3036
export interface IContractResultsParams {
3137
blockHash?: string;
3238
blockNumber?: number;

packages/relay/tests/lib/eth/eth_getBalance.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('@ethGetBalance using MirrorNode', async function () {
4949

5050
it('should return balance from mirror node', async () => {
5151
restMock.onGet(BLOCKS_LIMIT_ORDER_URL).reply(200, JSON.stringify(MOCK_BLOCK_NUMBER_1000_RES));
52-
restMock.onGet(`accounts/${CONTRACT_ADDRESS_1}?limit=100`).reply(200, JSON.stringify(MOCK_BALANCE_RES));
52+
restMock.onGet(`accounts/${CONTRACT_ADDRESS_1}?transactions=false`).reply(200, JSON.stringify(MOCK_BALANCE_RES));
5353

5454
const resBalance = await ethImpl.getBalance(CONTRACT_ADDRESS_1, 'latest', requestDetails);
5555
expect(resBalance).to.equal(DEF_HEX_BALANCE);
@@ -58,7 +58,7 @@ describe('@ethGetBalance using MirrorNode', async function () {
5858
it('should return balance from mirror node with block number passed as param the same as latest', async () => {
5959
const blockNumber = '0x2710';
6060
restMock.onGet(BLOCKS_LIMIT_ORDER_URL).reply(200, JSON.stringify(MOCK_BLOCKS_FOR_BALANCE_RES));
61-
restMock.onGet(`accounts/${CONTRACT_ADDRESS_1}?limit=100`).reply(200, JSON.stringify(MOCK_BALANCE_RES));
61+
restMock.onGet(`accounts/${CONTRACT_ADDRESS_1}?transactions=false`).reply(200, JSON.stringify(MOCK_BALANCE_RES));
6262

6363
const resBalance = await ethImpl.getBalance(CONTRACT_ADDRESS_1, blockNumber, requestDetails);
6464
expect(resBalance).to.equal(DEF_HEX_BALANCE);
@@ -73,7 +73,7 @@ describe('@ethGetBalance using MirrorNode', async function () {
7373
number: 10000,
7474
}),
7575
);
76-
restMock.onGet(`accounts/${CONTRACT_ADDRESS_1}?limit=100`).reply(200, JSON.stringify(MOCK_BALANCE_RES));
76+
restMock.onGet(`accounts/${CONTRACT_ADDRESS_1}?transactions=false`).reply(200, JSON.stringify(MOCK_BALANCE_RES));
7777

7878
const resBalance = await ethImpl.getBalance(CONTRACT_ADDRESS_1, blockHash, requestDetails);
7979
expect(resBalance).to.equal(DEF_HEX_BALANCE);
@@ -82,7 +82,7 @@ describe('@ethGetBalance using MirrorNode', async function () {
8282
it('should return balance from mirror node with block number passed as param, one behind latest', async () => {
8383
const blockNumber = '0x270F';
8484
restMock.onGet(BLOCKS_LIMIT_ORDER_URL).reply(200, JSON.stringify(MOCK_BLOCKS_FOR_BALANCE_RES));
85-
restMock.onGet(`accounts/${CONTRACT_ADDRESS_1}?limit=100`).reply(200, JSON.stringify(MOCK_BALANCE_RES));
85+
restMock.onGet(`accounts/${CONTRACT_ADDRESS_1}?transactions=false`).reply(200, JSON.stringify(MOCK_BALANCE_RES));
8686

8787
const resBalance = await ethImpl.getBalance(CONTRACT_ADDRESS_1, blockNumber, requestDetails);
8888
expect(resBalance).to.equal(DEF_HEX_BALANCE);
@@ -120,7 +120,7 @@ describe('@ethGetBalance using MirrorNode', async function () {
120120
it('should return balance from consensus node', async () => {
121121
restMock.onGet(BLOCKS_LIMIT_ORDER_URL).reply(200, JSON.stringify(MOCK_BLOCK_NUMBER_1000_RES));
122122
restMock.onGet(`contracts/${CONTRACT_ADDRESS_1}`).reply(200, JSON.stringify(null));
123-
restMock.onGet(`accounts/${CONTRACT_ADDRESS_1}?limit=100`).reply(404, JSON.stringify(NOT_FOUND_RES));
123+
restMock.onGet(`accounts/${CONTRACT_ADDRESS_1}?transactions=false`).reply(404, JSON.stringify(NOT_FOUND_RES));
124124

125125
const resBalance = await ethImpl.getBalance(CONTRACT_ADDRESS_1, 'latest', requestDetails);
126126
expect(resBalance).to.equal(constants.ZERO_HEX);
@@ -218,7 +218,7 @@ describe('@ethGetBalance using MirrorNode', async function () {
218218
restMock.onGet(`blocks/2`).reply(200, JSON.stringify(recentBlock));
219219
restMock.onGet(`blocks/1`).reply(200, JSON.stringify(earlierBlock));
220220

221-
restMock.onGet(`accounts/${CONTRACT_ID_1}?limit=100`).reply(
221+
restMock.onGet(`accounts/${CONTRACT_ID_1}?transactions=false`).reply(
222222
200,
223223
JSON.stringify({
224224
account: CONTRACT_ID_1,
@@ -698,6 +698,7 @@ describe('@ethGetBalance using MirrorNode', async function () {
698698
},
699699
};
700700
restMock.onGet(`blocks/2`).reply(200, JSON.stringify(recentBlockWithinLastfifteen));
701+
restMock.onGet(`accounts/${notFoundEvmAddress}?transactions=false`).reply(404, JSON.stringify(NOT_FOUND_RES));
701702
restMock.onGet(`accounts/${notFoundEvmAddress}?limit=100`).reply(404, JSON.stringify(NOT_FOUND_RES));
702703

703704
const resBalance = await ethImpl.getBalance(notFoundEvmAddress, '2', requestDetails);

packages/relay/tests/lib/relay.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ describe('Relay', () => {
124124
beforeEach(() => {
125125
// @ts-expect-error: Property 'operatorAccountId' is private and only accessible within class 'Relay'.
126126
operatorId = relay.operatorAccountId!.toString();
127-
getAccountPageLimitStub = sinon.stub(MirrorNodeClient.prototype, 'getAccountPageLimit');
127+
getAccountPageLimitStub = sinon.stub(MirrorNodeClient.prototype, 'getAccount');
128128
});
129129

130130
afterEach(() => {

0 commit comments

Comments
 (0)