From 5f9ca7de3386ea7f48a74ca97594100e6e53847c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nedim=20Salki=C4=87?= Date: Mon, 13 Nov 2023 14:21:51 +0100 Subject: [PATCH] fix: `calculateTxChargeableBytes` when transaction has no witnesses (#1428) fix: `calculateTxChargeableBytes` when transaction has no witnesses --- .changeset/chatty-trains-smoke.md | 5 ++++ .../providers/src/utils/calculate-fee.test.ts | 25 ++++++++++--------- packages/providers/src/utils/calculate-fee.ts | 4 +-- 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 .changeset/chatty-trains-smoke.md diff --git a/.changeset/chatty-trains-smoke.md b/.changeset/chatty-trains-smoke.md new file mode 100644 index 00000000000..55c7b29974b --- /dev/null +++ b/.changeset/chatty-trains-smoke.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/providers": patch +--- + +Fixed calculation of chargeable bytes when transaction has no witnesses diff --git a/packages/providers/src/utils/calculate-fee.test.ts b/packages/providers/src/utils/calculate-fee.test.ts index eee2a652c5a..eeddb9a1c13 100644 --- a/packages/providers/src/utils/calculate-fee.test.ts +++ b/packages/providers/src/utils/calculate-fee.test.ts @@ -120,33 +120,34 @@ describe(__filename, () => { describe('calculateTxChargeableBytes', () => { it('should calculate properly transaction chargeable bytes just fine', () => { const transactionBytes = getBytesCopy(MOCK_TX_BYTES_HEX); - const transactionWitnesses: Witness[] = [{ dataLength: 0, data: '0x' }]; + + const dataLengthOffset = 8; + const witnessOffset = transactionBytes.length - dataLengthOffset; + const transactionWitnesses: Witness[] = [ + { + dataLength: 0, + data: '0x', + offset: witnessOffset, + }, + ]; const chargeableBytesFee = calculateTxChargeableBytes({ transactionBytes, transactionWitnesses, }); - expect(chargeableBytesFee.toNumber()).toEqual(0); + expect(chargeableBytesFee.toNumber()).toEqual(witnessOffset); }); it('should handle an empty witnesses array', () => { const transactionBytes = getBytesCopy(MOCK_TX_BYTES_HEX); - const offset = 520; - const transactionWitnesses: Witness[] = [ - { - dataLength: 64, - data: '0x3d8943b87436d54d7f1282ed9b01d38633f8003a37676276bbaf0560390d21f826b5f6e9ea85b1bca435af2bec4982436c20c1294876025f78aaa0143867eb7f', - offset, - }, - ]; const chargeableBytesFee = calculateTxChargeableBytes({ transactionBytes, - transactionWitnesses, + transactionWitnesses: [], }); - expect(chargeableBytesFee.toNumber()).toEqual(520); + expect(chargeableBytesFee.toNumber()).toEqual(transactionBytes.length); }); it('should round up the result', () => { diff --git a/packages/providers/src/utils/calculate-fee.ts b/packages/providers/src/utils/calculate-fee.ts index 0bf43246f34..8f31a856367 100644 --- a/packages/providers/src/utils/calculate-fee.ts +++ b/packages/providers/src/utils/calculate-fee.ts @@ -31,9 +31,9 @@ export interface CalculateTxChargeableBytesParams { /** @hidden */ export const calculateTxChargeableBytes = (params: CalculateTxChargeableBytesParams): BN => { - const { transactionWitnesses } = params; + const { transactionWitnesses, transactionBytes } = params; - const txChargeableBytes = bn(transactionWitnesses?.[0]?.offset || 0); + const txChargeableBytes = bn(transactionWitnesses?.[0]?.offset || transactionBytes.length); return txChargeableBytes; };