Skip to content

Commit 7796aca

Browse files
authored
fix: included only transfer amounts that are charged to the operator in getTransferAmountSumForAccount() (#3103)
Signed-off-by: Logan Nguyen <[email protected]>
1 parent afc3349 commit 7796aca

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ export class MirrorNodeClient {
13511351
*/
13521352
public getTransferAmountSumForAccount(transactionRecord: MirrorNodeTransactionRecord, accountId: string): number {
13531353
return transactionRecord.transfers
1354-
.filter((transfer) => transfer.account === accountId)
1354+
.filter((transfer) => transfer.account === accountId && transfer.amount < 0)
13551355
.reduce((acc, transfer) => {
13561356
return acc - transfer.amount;
13571357
}, 0);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ export class SDKClient {
10391039
*/
10401040
public getTransferAmountSumForAccount(transactionRecord: TransactionRecord, accountId: string): number {
10411041
return transactionRecord.transfers
1042-
.filter((transfer) => transfer.accountId.toString() === accountId)
1042+
.filter((transfer) => transfer.accountId.toString() === accountId && transfer.amount.isNegative())
10431043
.reduce((acc, transfer) => {
10441044
return acc - transfer.amount.toTinybars().toNumber();
10451045
}, 0);

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,11 +1295,12 @@ describe('MirrorNodeClient', async function () {
12951295
});
12961296

12971297
describe('getTransactionRecordMetrics', () => {
1298-
it('Should execute getTransferAmountSumForAccount() to calculate transactionFee of the specify accountId', () => {
1298+
it('Should execute getTransferAmountSumForAccount() to calculate transactionFee by only transfers that are paid by the specify accountId', () => {
12991299
const accountIdA = `0.0.1022`;
13001300
const accountIdB = `0.0.1023`;
13011301
const mockedTxFeeA = 300;
13021302
const mockedTxFeeB = 600;
1303+
const mockedTxFeeC = 900;
13031304

13041305
const expectedTxFeeForAccountIdA = mockedTxFeeA + mockedTxFeeB;
13051306

@@ -1325,6 +1326,16 @@ describe('MirrorNodeClient', async function () {
13251326
amount: -1 * mockedTxFeeB,
13261327
is_approval: false,
13271328
},
1329+
{
1330+
account: accountIdA,
1331+
amount: mockedTxFeeC,
1332+
is_approval: false,
1333+
},
1334+
{
1335+
account: accountIdA,
1336+
amount: mockedTxFeeB,
1337+
is_approval: false,
1338+
},
13281339
],
13291340
},
13301341
],

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,11 @@ describe('SdkClient', async function () {
21942194
amount: Hbar.fromTinybars(-1 * defaultTransactionFee),
21952195
is_approval: false,
21962196
},
2197+
{
2198+
accountId: process.env.OPERATOR_ID_MAIN,
2199+
amount: Hbar.fromTinybars(defaultTransactionFee),
2200+
is_approval: false,
2201+
},
21972202
{
21982203
accountId: accountId.toString(),
21992204
amount: Hbar.fromTinybars(-1 * defaultTransactionFee),
@@ -2225,7 +2230,7 @@ describe('SdkClient', async function () {
22252230
},
22262231
}) as unknown as TransactionResponse;
22272232

2228-
const getMockedTransactionRecord: any = (transactionType: string) => ({
2233+
const getMockedTransactionRecord: any = (transactionType: string, toHbar: boolean = false) => ({
22292234
receipt: {
22302235
status: Status.Success,
22312236
exchangeRate: { exchangeRateInCents: 12 },
@@ -2234,7 +2239,7 @@ describe('SdkClient', async function () {
22342239
contractFunctionResult: {
22352240
gasUsed,
22362241
},
2237-
transfers: getMockedTransaction(transactionType, false).transfers,
2242+
transfers: getMockedTransaction(transactionType, toHbar).transfers,
22382243
});
22392244

22402245
const fileInfo = {
@@ -2710,9 +2715,9 @@ describe('SdkClient', async function () {
27102715
}
27112716
});
27122717

2713-
it('Should execute getTransferAmountSumForAccount() to calculate transactionFee of the specify accountId', () => {
2718+
it('Should execute getTransferAmountSumForAccount() to calculate transactionFee by only transfers that are paid by the specify accountId', () => {
27142719
const accountId = process.env.OPERATOR_ID_MAIN || '';
2715-
const mockedTxRecord = getMockedTransactionRecord();
2720+
const mockedTxRecord = getMockedTransactionRecord(EthereumTransaction.name, true);
27162721

27172722
const transactionFee = sdkClient.getTransferAmountSumForAccount(mockedTxRecord, accountId);
27182723
expect(transactionFee).to.eq(defaultTransactionFee);

0 commit comments

Comments
 (0)