From 077893de032903ef9d50507948ff8eb1d2cd7b84 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 18 Oct 2023 22:49:22 +0100 Subject: [PATCH] fix: rearrange provider tx param error code checks (#1352) * feat: flip error message for provider tx gas params checks * chore: changeset * feat: remove test only call in provider tests Co-authored-by: Anderson Arboleya * feat: remove redundant import Co-authored-by: Anderson Arboleya --------- Co-authored-by: Anderson Arboleya --- .changeset/twenty-ways-pretend.md | 5 ++ packages/providers/src/provider.ts | 4 +- packages/providers/test/provider.test.ts | 58 ++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 .changeset/twenty-ways-pretend.md diff --git a/.changeset/twenty-ways-pretend.md b/.changeset/twenty-ways-pretend.md new file mode 100644 index 00000000000..f406de4feeb --- /dev/null +++ b/.changeset/twenty-ways-pretend.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/providers": patch +--- + +Rearrange error messages regarding insufficient tx params diff --git a/packages/providers/src/provider.ts b/packages/providers/src/provider.ts index acd8aa46e7b..d2a9b99e5d1 100644 --- a/packages/providers/src/provider.ts +++ b/packages/providers/src/provider.ts @@ -478,12 +478,12 @@ export default class Provider { // Resulting in lost of funds on a OutOfGas situation. if (bn(gasUsed).gt(bn(transactionRequest.gasLimit))) { throw new FuelError( - ErrorCode.GAS_PRICE_TOO_LOW, + ErrorCode.GAS_LIMIT_TOO_LOW, `Gas limit '${transactionRequest.gasLimit}' is lower than the required: '${gasUsed}'.` ); } else if (bn(minGasPrice).gt(bn(transactionRequest.gasPrice))) { throw new FuelError( - ErrorCode.GAS_LIMIT_TOO_LOW, + ErrorCode.GAS_PRICE_TOO_LOW, `Gas price '${transactionRequest.gasPrice}' is lower than the required: '${minGasPrice}'.` ); } diff --git a/packages/providers/test/provider.test.ts b/packages/providers/test/provider.test.ts index e9af6b8e447..5d85a946b95 100644 --- a/packages/providers/test/provider.test.ts +++ b/packages/providers/test/provider.test.ts @@ -830,4 +830,62 @@ describe('Provider', () => { message: `Fuel client version: ${FUEL_CORE}, Supported version: ${mock.supportedVersion}`, }); }); + + it('throws when gas limit is lower than tx gas used', async () => { + const provider = await Provider.create(FUEL_NETWORK_URL); + const gasLimit = 1; + const gasUsed = bn(1000); + const transactionParams = { + minGasPrice: bn(1), + gasPrice: bn(1), + gasUsed, + fee: bn(1), + }; + + const estimateTxSpy = jest.spyOn(provider, 'estimateTxDependencies').mockImplementation(); + + const txCostSpy = jest + .spyOn(provider, 'getTransactionCost') + .mockReturnValue(Promise.resolve(transactionParams)); + + await expectToThrowFuelError( + () => provider.sendTransaction(new ScriptTransactionRequest({ gasPrice: 1, gasLimit })), + { + code: ErrorCode.GAS_LIMIT_TOO_LOW, + message: `Gas limit '${gasLimit}' is lower than the required: '${gasUsed}'.`, + } + ); + + expect(txCostSpy).toHaveBeenCalled(); + expect(estimateTxSpy).toHaveBeenCalled(); + }); + + it('throws when gas price is lower than min tx gas price', async () => { + const provider = await Provider.create(FUEL_NETWORK_URL); + const gasPrice = 1; + const minGasPrice = bn(1000); + const transactionParams = { + minGasPrice, + gasPrice: bn(1), + gasUsed: bn(1), + fee: bn(1), + }; + + const estimateTxSpy = jest.spyOn(provider, 'estimateTxDependencies').mockImplementation(); + + const txCostSpy = jest + .spyOn(provider, 'getTransactionCost') + .mockReturnValue(Promise.resolve(transactionParams)); + + await expectToThrowFuelError( + () => provider.sendTransaction(new ScriptTransactionRequest({ gasPrice, gasLimit: 1000 })), + { + code: ErrorCode.GAS_PRICE_TOO_LOW, + message: `Gas price '${gasPrice}' is lower than the required: '${minGasPrice}'.`, + } + ); + + expect(txCostSpy).toHaveBeenCalled(); + expect(estimateTxSpy).toHaveBeenCalled(); + }); });