FuelLabs / fuels-ts

Fuel Network Typescript SDK
https://docs.fuel.network/docs/fuels-ts/
Apache License 2.0
44.25k stars 1.34k forks source link

Return Gas costs regardless of whether `getTransactionCost` returns an error #2866

Closed maschad closed 1 month ago

maschad commented 1 month ago

Currently, when calculating gas costs for a transaction, if there is an error during the dry run it is thrown and no gas costs are returned.

For many use cases this is sufficient, but there are also scenarios where a consumer may still want the gas costs even if an error happened during the dry run, such as in a DEX, here's an example use case taken from this forum post

when swapping tokens on a DEX and specifying some slippage, the simulation might fail but the user still might want to send the transaction, which can succeed in case the state of the DEX changes.

We should refactor this method to return both the error and the gas cost to accommodate for such a scenario.

Torres-ssf commented 1 month ago

@maschad this is already possible. The receipt of type script return will be present within the receipts array that will be present within the FuelError metadata property.

However, it is important to be aware that this value won't represent the real gas used.

Since the transaction failed, the Sway project code was not entirely executed, therefore the gas consumed does not represent the gas that would be consumed if the same transaction succeeds.

I believe we can close this issue

maschad commented 1 month ago

@Torres-ssf thanks for clarifying that, and upon reconsideration I think it would probably be more appropriate for the consumer to handle errors related to low gas costs and then allow the user to proceed with the transaction, rather than forcing them to handle all errors.