From 3f4a50d6e99b481f3dca729fb985062d2757f9a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Mon, 30 Oct 2023 07:47:28 -0300 Subject: [PATCH] fix: update calculate fee outputs (#1383) --- .changeset/strong-garlics-prove.md | 5 +++++ packages/providers/src/provider.ts | 4 ++-- .../assemble-transaction-summary.ts | 2 +- .../providers/src/utils/calculate-fee.test.ts | 22 ++++++++++++++----- packages/providers/src/utils/calculate-fee.ts | 12 +++++++--- 5 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 .changeset/strong-garlics-prove.md diff --git a/.changeset/strong-garlics-prove.md b/.changeset/strong-garlics-prove.md new file mode 100644 index 00000000000..7103416d04b --- /dev/null +++ b/.changeset/strong-garlics-prove.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/providers": minor +--- + +made calculateTransactionFee to return minFee and maxFee diff --git a/packages/providers/src/provider.ts b/packages/providers/src/provider.ts index 86bca204494..547b36d83aa 100644 --- a/packages/providers/src/provider.ts +++ b/packages/providers/src/provider.ts @@ -678,7 +678,7 @@ export default class Provider { const gasUsed = getGasUsedFromReceipts(receipts); - const { fee } = calculateTransactionFee({ + const { minFee } = calculateTransactionFee({ gasPrice, gasPerByte, gasPriceFactor, @@ -691,7 +691,7 @@ export default class Provider { minGasPrice, gasPrice, gasUsed, - fee, + fee: minFee, }; } diff --git a/packages/providers/src/transaction-summary/assemble-transaction-summary.ts b/packages/providers/src/transaction-summary/assemble-transaction-summary.ts index facaba21ff2..c75567149cc 100644 --- a/packages/providers/src/transaction-summary/assemble-transaction-summary.ts +++ b/packages/providers/src/transaction-summary/assemble-transaction-summary.ts @@ -58,7 +58,7 @@ export function assembleTransactionSummary( transactionWitnesses: witnesses, }); - const { fee } = calculateTransactionFee({ + const { minFee: fee } = calculateTransactionFee({ gasUsed, gasPrice, gasLimit, diff --git a/packages/providers/src/utils/calculate-fee.test.ts b/packages/providers/src/utils/calculate-fee.test.ts index 5f8a73f7662..eee2a652c5a 100644 --- a/packages/providers/src/utils/calculate-fee.test.ts +++ b/packages/providers/src/utils/calculate-fee.test.ts @@ -54,8 +54,11 @@ describe(__filename, () => { Math.ceil(maxGas.mul(gasPrice).toNumber() / gasPriceFactor.toNumber()) ); - const partialFee = Math.ceil(gasUsed.mul(gasPrice).toNumber() / gasPriceFactor.toNumber()); - const expectedFee = expectedMinGasToPay.add(partialFee); + const expectedFeeFromGasUsed = bn( + Math.ceil(gasUsed.mul(gasPrice).toNumber() / gasPriceFactor.toNumber()) + ); + const expectedMinFee = expectedMinGasToPay.add(expectedFeeFromGasUsed); + const expectedMaxFee = expectedMaxGasToPay.add(expectedFeeFromGasUsed); const result = calculateTransactionFee({ gasPrice, @@ -66,9 +69,11 @@ describe(__filename, () => { chargeableBytes, }); - expect(result.fee.toNumber()).toEqual(expectedFee.toNumber()); + expect(result.minFee.toNumber()).toEqual(expectedMinFee.toNumber()); + expect(result.maxFee.toNumber()).toEqual(expectedMaxFee.toNumber()); expect(result.minGasToPay.toNumber()).toEqual(expectedMinGasToPay.toNumber()); expect(result.maxGasToPay.toNumber()).toEqual(expectedMaxGasToPay.toNumber()); + expect(result.feeFromGasUsed.toNumber()).toEqual(expectedFeeFromGasUsed.toNumber()); }); it('should calculate transaction fee for multiple receipts', () => { @@ -89,8 +94,11 @@ describe(__filename, () => { Math.ceil(maxGas.mul(gasPrice).toNumber() / gasPriceFactor.toNumber()) ); - const partialFee = Math.ceil(gasUsed.mul(gasPrice).toNumber() / gasPriceFactor.toNumber()); - const expectedFee = expectedMinGasToPay.add(partialFee); + const expectedFeeFromGasUsed = bn( + Math.ceil(gasUsed.mul(gasPrice).toNumber() / gasPriceFactor.toNumber()) + ); + const expectedMinFee = expectedMinGasToPay.add(expectedFeeFromGasUsed); + const expectedMaxFee = expectedMaxGasToPay.add(expectedFeeFromGasUsed); const result = calculateTransactionFee({ gasPrice, @@ -101,9 +109,11 @@ describe(__filename, () => { chargeableBytes, }); - expect(result.fee.toNumber()).toEqual(expectedFee.toNumber()); + expect(result.minFee.toNumber()).toEqual(expectedMinFee.toNumber()); + expect(result.maxFee.toNumber()).toEqual(expectedMaxFee.toNumber()); expect(result.minGasToPay.toNumber()).toEqual(expectedMinGasToPay.toNumber()); expect(result.maxGasToPay.toNumber()).toEqual(expectedMaxGasToPay.toNumber()); + expect(result.feeFromGasUsed.toNumber()).toEqual(expectedFeeFromGasUsed.toNumber()); }); }); diff --git a/packages/providers/src/utils/calculate-fee.ts b/packages/providers/src/utils/calculate-fee.ts index 2efef2a3a51..0bf43246f34 100644 --- a/packages/providers/src/utils/calculate-fee.ts +++ b/packages/providers/src/utils/calculate-fee.ts @@ -64,12 +64,18 @@ export const calculateTransactionFee = ({ const minGasToPay = bn(Math.ceil(minGas.mul(gasPrice).toNumber() / gasPriceFactor.toNumber())); const maxGasToPay = bn(Math.ceil(maxGas.mul(gasPrice).toNumber() / gasPriceFactor.toNumber())); - const partialFee = Math.ceil(gasUsed.mul(gasPrice).toNumber() / gasPriceFactor.toNumber()); - const fee = minGasToPay.add(partialFee); + const feeFromGasUsed = bn( + Math.ceil(gasUsed.mul(gasPrice).toNumber() / gasPriceFactor.toNumber()) + ); + + const minFee = minGasToPay.add(feeFromGasUsed); + const maxFee = maxGasToPay.add(feeFromGasUsed); return { - fee, + minFee, + maxFee, minGasToPay, maxGasToPay, + feeFromGasUsed, }; };