From 7796aca3d8028e9732c58fb6855bef4520c4b3cc Mon Sep 17 00:00:00 2001 From: Logan Nguyen Date: Tue, 15 Oct 2024 15:30:57 -0400 Subject: [PATCH] fix: included only transfer amounts that are charged to the operator in getTransferAmountSumForAccount() (#3103) Signed-off-by: Logan Nguyen --- packages/relay/src/lib/clients/mirrorNodeClient.ts | 2 +- packages/relay/src/lib/clients/sdkClient.ts | 2 +- packages/relay/tests/lib/mirrorNodeClient.spec.ts | 13 ++++++++++++- packages/relay/tests/lib/sdkClient.spec.ts | 13 +++++++++---- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/relay/src/lib/clients/mirrorNodeClient.ts b/packages/relay/src/lib/clients/mirrorNodeClient.ts index 3ff0f0b9f..9a45a9128 100644 --- a/packages/relay/src/lib/clients/mirrorNodeClient.ts +++ b/packages/relay/src/lib/clients/mirrorNodeClient.ts @@ -1351,7 +1351,7 @@ export class MirrorNodeClient { */ public getTransferAmountSumForAccount(transactionRecord: MirrorNodeTransactionRecord, accountId: string): number { return transactionRecord.transfers - .filter((transfer) => transfer.account === accountId) + .filter((transfer) => transfer.account === accountId && transfer.amount < 0) .reduce((acc, transfer) => { return acc - transfer.amount; }, 0); diff --git a/packages/relay/src/lib/clients/sdkClient.ts b/packages/relay/src/lib/clients/sdkClient.ts index 6a6e9f20f..84b7c240f 100644 --- a/packages/relay/src/lib/clients/sdkClient.ts +++ b/packages/relay/src/lib/clients/sdkClient.ts @@ -1039,7 +1039,7 @@ export class SDKClient { */ public getTransferAmountSumForAccount(transactionRecord: TransactionRecord, accountId: string): number { return transactionRecord.transfers - .filter((transfer) => transfer.accountId.toString() === accountId) + .filter((transfer) => transfer.accountId.toString() === accountId && transfer.amount.isNegative()) .reduce((acc, transfer) => { return acc - transfer.amount.toTinybars().toNumber(); }, 0); diff --git a/packages/relay/tests/lib/mirrorNodeClient.spec.ts b/packages/relay/tests/lib/mirrorNodeClient.spec.ts index 1efe61b1d..2d7a81e06 100644 --- a/packages/relay/tests/lib/mirrorNodeClient.spec.ts +++ b/packages/relay/tests/lib/mirrorNodeClient.spec.ts @@ -1295,11 +1295,12 @@ describe('MirrorNodeClient', async function () { }); describe('getTransactionRecordMetrics', () => { - it('Should execute getTransferAmountSumForAccount() to calculate transactionFee of the specify accountId', () => { + it('Should execute getTransferAmountSumForAccount() to calculate transactionFee by only transfers that are paid by the specify accountId', () => { const accountIdA = `0.0.1022`; const accountIdB = `0.0.1023`; const mockedTxFeeA = 300; const mockedTxFeeB = 600; + const mockedTxFeeC = 900; const expectedTxFeeForAccountIdA = mockedTxFeeA + mockedTxFeeB; @@ -1325,6 +1326,16 @@ describe('MirrorNodeClient', async function () { amount: -1 * mockedTxFeeB, is_approval: false, }, + { + account: accountIdA, + amount: mockedTxFeeC, + is_approval: false, + }, + { + account: accountIdA, + amount: mockedTxFeeB, + is_approval: false, + }, ], }, ], diff --git a/packages/relay/tests/lib/sdkClient.spec.ts b/packages/relay/tests/lib/sdkClient.spec.ts index 74d74cda5..a350bd5e5 100644 --- a/packages/relay/tests/lib/sdkClient.spec.ts +++ b/packages/relay/tests/lib/sdkClient.spec.ts @@ -2194,6 +2194,11 @@ describe('SdkClient', async function () { amount: Hbar.fromTinybars(-1 * defaultTransactionFee), is_approval: false, }, + { + accountId: process.env.OPERATOR_ID_MAIN, + amount: Hbar.fromTinybars(defaultTransactionFee), + is_approval: false, + }, { accountId: accountId.toString(), amount: Hbar.fromTinybars(-1 * defaultTransactionFee), @@ -2225,7 +2230,7 @@ describe('SdkClient', async function () { }, }) as unknown as TransactionResponse; - const getMockedTransactionRecord: any = (transactionType: string) => ({ + const getMockedTransactionRecord: any = (transactionType: string, toHbar: boolean = false) => ({ receipt: { status: Status.Success, exchangeRate: { exchangeRateInCents: 12 }, @@ -2234,7 +2239,7 @@ describe('SdkClient', async function () { contractFunctionResult: { gasUsed, }, - transfers: getMockedTransaction(transactionType, false).transfers, + transfers: getMockedTransaction(transactionType, toHbar).transfers, }); const fileInfo = { @@ -2710,9 +2715,9 @@ describe('SdkClient', async function () { } }); - it('Should execute getTransferAmountSumForAccount() to calculate transactionFee of the specify accountId', () => { + it('Should execute getTransferAmountSumForAccount() to calculate transactionFee by only transfers that are paid by the specify accountId', () => { const accountId = process.env.OPERATOR_ID_MAIN || ''; - const mockedTxRecord = getMockedTransactionRecord(); + const mockedTxRecord = getMockedTransactionRecord(EthereumTransaction.name, true); const transactionFee = sdkClient.getTransferAmountSumForAccount(mockedTxRecord, accountId); expect(transactionFee).to.eq(defaultTransactionFee);