Skip to content

Commit

Permalink
fix: update calculate fee outputs (#1383)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf authored Oct 30, 2023
1 parent 37673aa commit 3f4a50d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-garlics-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/providers": minor
---

made calculateTransactionFee to return minFee and maxFee
4 changes: 2 additions & 2 deletions packages/providers/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ export default class Provider {

const gasUsed = getGasUsedFromReceipts(receipts);

const { fee } = calculateTransactionFee({
const { minFee } = calculateTransactionFee({
gasPrice,
gasPerByte,
gasPriceFactor,
Expand All @@ -691,7 +691,7 @@ export default class Provider {
minGasPrice,
gasPrice,
gasUsed,
fee,
fee: minFee,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function assembleTransactionSummary<TTransactionType = void>(
transactionWitnesses: witnesses,
});

const { fee } = calculateTransactionFee({
const { minFee: fee } = calculateTransactionFee({
gasUsed,
gasPrice,
gasLimit,
Expand Down
22 changes: 16 additions & 6 deletions packages/providers/src/utils/calculate-fee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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', () => {
Expand All @@ -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,
Expand All @@ -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());
});
});

Expand Down
12 changes: 9 additions & 3 deletions packages/providers/src/utils/calculate-fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
};

0 comments on commit 3f4a50d

Please sign in to comment.