Skip to content

Commit

Permalink
fix: included only transfer amounts that are charged to the operator …
Browse files Browse the repository at this point in the history
…in getTransferAmountSumForAccount() (#3103)

Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node authored Oct 15, 2024
1 parent afc3349 commit 7796aca
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/relay/src/lib/clients/mirrorNodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/src/lib/clients/sdkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 12 additions & 1 deletion packages/relay/tests/lib/mirrorNodeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
},
],
},
],
Expand Down
13 changes: 9 additions & 4 deletions packages/relay/tests/lib/sdkClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 },
Expand All @@ -2234,7 +2239,7 @@ describe('SdkClient', async function () {
contractFunctionResult: {
gasUsed,
},
transfers: getMockedTransaction(transactionType, false).transfers,
transfers: getMockedTransaction(transactionType, toHbar).transfers,
});

const fileInfo = {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7796aca

Please sign in to comment.