# How to calculate how much of gas fee would transaction use?

**URL:** <https://forum.fuel.network/t/how-to-calculate-how-much-of-gas-fee-would-transaction-use/3871>\
**Category:** General\
**Created:** [December 16, 2023, 11:51am UTC](https://forum.fuel.network/t/how-to-calculate-how-much-of-gas-fee-would-transaction-use/3871 "2023-12-16T11:51:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sway](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sway/32/103_2.png) [@sway](https://forum.fuel.network/u/sway)\
**Post date:** [December 16, 2023, 11:51am UTC](https://forum.fuel.network/t/how-to-calculate-how-much-of-gas-fee-would-transaction-use/3871/1 "2023-12-16T11:51:14Z")

</div>

Does ts sdk have a method where i can calculate how much gas fee the transaction uses?

---

<div class="post-metadata">

**Author:** ![Torres-ssf](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/torres-ssf/32/1380_2.png) [@Torres-ssf](https://forum.fuel.network/u/Torres-ssf)\
**Post date:** [December 18, 2023, 5:10pm UTC](https://forum.fuel.network/t/how-to-calculate-how-much-of-gas-fee-would-transaction-use/3871/2 "2023-12-18T17:10:08Z")

</div>

@sway You can use `getTransactionCost` from the Provider class:

From an instantiated contract:

```ts
const { maxFee } = await myContract.functions
      .increment_count(1337)
      .txParams({ gasPrice })
      .getTransactionCost();

```

Or from a Transaction request:

```ts
const transactionRequest = new ScriptTransactionRequest({
  gasPrice: 1,
  gasLimit: 1000
})

const resources = await wallet.getResourcesToSpend([[1000, BaseAssetId]]);

request.addResources(resources);

const { maxFee } = await provider.getTransactionCost()

```

The `maxFee` value is the estimated max fee that this transaction will cost, and it needs to carry enough funds to cover it.

UPDATE:

@sway, be aware of the `gasLimit` value that you are using, this drastically impacts on the estimated `maxFee`. A high `gasLimt` value, means a higher `maxFee`.

One approach that you can use to avoid this problem is to get the `gasUsed` by the transaction and set it to the `gasLimit`:

```ts
const transactionRequest = new ScriptTransactionRequest({
  gasPrice: 1,
  gasLimit: 1000
})

const resources = await wallet.getResourcesToSpend([[1000, BaseAssetId]]);

request.addResources(resources);

const { maxFee, gasUsed } = await provider.getTransactionCost()

transactionRequest.gasLimit = gasUsed

```

---

<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:** [December 19, 2023, 3:49pm UTC](https://forum.fuel.network/t/how-to-calculate-how-much-of-gas-fee-would-transaction-use/3871/3 "2023-12-19T15:49:41Z")

</div>

How does it work, why `gasLimit` affects the `maxFee`, isn’t it controversial? Is it a bug?
