# How to get a gas fee estimation for transactions that are expected to fail

**URL:** <https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372>\
**Category:** TypeScript SDK\
**Created:** [July 29, 2024, 12:30pm UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372 "2024-07-29T12:30:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpoplavkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/mpoplavkov/32/399_2.png) [@mpoplavkov](https://forum.fuel.network/u/mpoplavkov)\
**Post date:** [July 29, 2024, 12:30pm UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/1 "2024-07-29T12:30:48Z")

</div>

Is there a way to estimate transaction fees for transactions that are expected to fail? In typescript, the `provider.getTransactionCost` function will fail with an error, although it’s still possible to send the transaction and pay fees for it

---

<div class="post-metadata">

**Author:** ![naz3eh](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/naz3eh/32/2025_2.png) [@naz3eh](https://forum.fuel.network/u/naz3eh)\
**Post date:** [July 29, 2024, 3:01pm UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/2 "2024-07-29T15:01:20Z")

</div>

Hey @mpoplavkov, are you looking for [these](https://rust.fuel.network/master/calling-contracts/cost-estimation.html?highlight=estimate_transaction_cost#estimating-contract-call-cost)? Also, do you mean estimating gas fees that are bound to fail intentionally?

---

<div class="post-metadata">

**Author:** ![mpoplavkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/mpoplavkov/32/399_2.png) [@mpoplavkov](https://forum.fuel.network/u/mpoplavkov)\
**Post date:** [July 29, 2024, 4:49pm UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/3 "2024-07-29T16:49:42Z")

</div>

We’re using `provider.getTransactionCost` from typescript and it fails with an exception when trying to estimate fees of transactions that fail during simulation.

> Also, do you mean estimating gas fees that are bound to fail intentionally?

Since I’m able to send any transaction on Fuel (even the one that’s expected to fail), I want to have an estimation on how expensive it would be to send it

As a wallet, we’re performing a simulation of transactions but we still want to give users an opportunity to send the transaction even if the simulation has failed. And for that we want to show the expected fee the user’s gonna pay

---

<div class="post-metadata">

**Author:** ![naz3eh](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/naz3eh/32/2025_2.png) [@naz3eh](https://forum.fuel.network/u/naz3eh)\
**Post date:** [July 30, 2024, 6:11am UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/4 "2024-07-30T06:11:03Z")

</div>

> [@mpoplavkov](#):
>
> We’re using `provider.getTransactionCost` from typescript and it fails with an exception when trying to estimate fees of transactions that fail during simulation.

Can you please share the error message? Also what version of SDK are you using? and an output for the `fuelup show` would be appreciated

---

<div class="post-metadata">

**Author:** ![mpoplavkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/mpoplavkov/32/399_2.png) [@mpoplavkov](https://forum.fuel.network/u/mpoplavkov)\
**Post date:** [July 30, 2024, 9:50am UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/5 "2024-07-30T09:50:06Z")

</div>

The error message depends on the simulation error. Again, it can be anything, any error from a smart contract. For example, when the contract reverts based on some condition.

We’ve been encountering this problem with different SDK versions. The latest one is `0.92.1`.

This should have nothing to do with the local fuelup versions, because we’re interacting with the contracts deployed on testnet.

I think that the problem is in the fact that `provider.getTransactionCost` function executes the transaction simulation first to calculate the fee. If the transaction fails during the simulation, the calculation stops

---

<div class="post-metadata">

**Author:** ![maschad](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/maschad/32/2710_2.png) [@maschad](https://forum.fuel.network/u/maschad)\
**Post date:** [July 30, 2024, 3:16pm UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/6 "2024-07-30T15:16:44Z")

</div>

Hi @mpoplavkov you are correct in that if there is [there is an error](https://github.com/FuelLabs/fuels-ts/blob/master/packages/account/src/providers/provider.ts#L1152-L1157) in executing the dry run of the transaction, that error will be thrown and thus the cost will not be returned. At the moment I see this as correct behaviour.

> As a wallet, we’re performing a simulation of transactions but we still want to give users an opportunity to send the transaction even if the simulation has failed. And for that we want to show the expected fee the user’s gonna pay

I’m curious about what benefit there may be in allowing a user to intentionally submit a transaction that will fail, could you elaborate a bit more on this use case?

In all likelihood, correcting that transaction so that it may be successful may increase/decrease gas costs, and so even if the gas cost was returned in the event of an error, it would not be representative of what’s required to make the transaction succeed.

---

<div class="post-metadata">

**Author:** ![mpoplavkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/mpoplavkov/32/399_2.png) [@mpoplavkov](https://forum.fuel.network/u/mpoplavkov)\
**Post date:** [July 30, 2024, 5:14pm UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/7 "2024-07-30T17:14:47Z")

</div>

We cannot guarantee that the transaction will fail if the simulation failed.

An example of a use case: 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.

---

<div class="post-metadata">

**Author:** ![maschad](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/maschad/32/2710_2.png) [@maschad](https://forum.fuel.network/u/maschad)\
**Post date:** [July 30, 2024, 6:20pm UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/8 "2024-07-30T18:20:56Z")

</div>

> Thanks for the clarification @mpoplavkov
> 
> That’s a valid use case, I’ve created this [issue](https://github.com/FuelLabs/fuels-ts/issues/2866) so that we can do the necessary refactor to accommodate for your use case.
> 
> Thanks for raising this.

EDIT: Please see [comment](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/9) below

---

<div class="post-metadata">

**Author:** ![maschad](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/maschad/32/2710_2.png) [@maschad](https://forum.fuel.network/u/maschad)\
**Post date:** [July 30, 2024, 7:34pm UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/9 "2024-07-30T19:34:07Z")

</div>

Upon [discussion](https://github.com/FuelLabs/fuels-ts/issues/2866#issuecomment-2259007170) with @Torres-ssf , he enlightened me to the fact that the [receipts](https://docs.fuel.network/docs/fuels-ts/account/assembletransactionsummaryparams/#receipts) array that will be present within the `FuelError` metadata property, which can will contain the [`gasUsed`](https://github.com/FuelLabs/fuels-ts/blob/mc/chore/integrate-launch-test-node-in-remaining-packages/packages/transactions/src/coders/receipt.ts#L631)

So once you handle this error, you can use this property calculate the gas used (in a similar fashion to [this method](https://github.com/FuelLabs/fuels-ts/blob/mc/chore/integrate-launch-test-node-in-remaining-packages/packages/account/src/providers/utils/gas.ts#L19))

Please note though, as I had [mentioned](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/6) that this gas used more than likely isn’t an accurate representation of the gas that would be used should the transaction be successfully dry run.

Please let me know if this works for you @mpoplavkov

---

<div class="post-metadata">

**Author:** ![mpoplavkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/mpoplavkov/32/399_2.png) [@mpoplavkov](https://forum.fuel.network/u/mpoplavkov)\
**Post date:** [July 31, 2024, 11:13am UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/10 "2024-07-31T11:13:06Z")

</div>

Thanks, looks like that’s what I need. Do you think that functionality should be available in the SDK? Or should we calculate the gas used on our side?

---

<div class="post-metadata">

**Author:** ![maschad](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/maschad/32/2710_2.png) [@maschad](https://forum.fuel.network/u/maschad)\
**Post date:** [August 2, 2024, 1:46am UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/11 "2024-08-02T01:46:53Z")

</div>

Hey @mpoplavkov I supposed we could expose the [`gasUsedFromReceipts`](https://getGasUsedFromReceipts) method but I would want to caution users that this may not be an accurate representation of the gas that would be used should the transaction be successfully executed, and so for that reason it may not be best to encourage it’s usage in most cases. But for your specific use case it’s applicable, as long as you ensure that your user flows properly accommodate for such scenarios.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/5/57d5a345cc15a64b636e0d56e042857f8a0e80b1.png) [@system](https://forum.fuel.network/u/system)\
**Post date:** [August 22, 2024, 9:47pm UTC](https://forum.fuel.network/t/how-to-get-a-gas-fee-estimation-for-transactions-that-are-expected-to-fail/6372/12 "2024-08-22T21:47:46Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
