Skip to content

Commit

Permalink
fix: calculateTxChargeableBytes when transaction has no witnesses (#…
Browse files Browse the repository at this point in the history
…1428)

fix: `calculateTxChargeableBytes` when transaction has no witnesses
  • Loading branch information
nedsalk authored Nov 13, 2023
1 parent 2ca4b3f commit 5f9ca7d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-trains-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/providers": patch
---

Fixed calculation of chargeable bytes when transaction has no witnesses
25 changes: 13 additions & 12 deletions packages/providers/src/utils/calculate-fee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/providers/src/utils/calculate-fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit 5f9ca7d

Please sign in to comment.