Skip to content

Commit

Permalink
fix: rearrange provider tx param error code checks (#1352)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* feat: remove redundant import

Co-authored-by: Anderson Arboleya <[email protected]>

---------

Co-authored-by: Anderson Arboleya <[email protected]>
  • Loading branch information
danielbate and arboleya authored Oct 18, 2023
1 parent af2500b commit 077893d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-ways-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/providers": patch
---

Rearrange error messages regarding insufficient tx params
4 changes: 2 additions & 2 deletions packages/providers/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'.`
);
}
Expand Down
58 changes: 58 additions & 0 deletions packages/providers/test/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 077893d

Please sign in to comment.