# How to get transaction fee?

**URL:** <https://forum.fuel.network/t/how-to-get-transaction-fee/3884>\
**Category:** General\
**Created:** [December 19, 2023, 3:46pm UTC](https://forum.fuel.network/t/how-to-get-transaction-fee/3884 "2023-12-19T15:46:07Z")\
**Posts on this page:** 8\
**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:** [December 19, 2023, 3:46pm UTC](https://forum.fuel.network/t/how-to-get-transaction-fee/3884/1 "2023-12-19T15:46:07Z")

</div>

Given the transaction model from the [GraphQL schema](https://github.com/FuelLabs/fuel-core/blob/7e02c25116a5ce7ef5d494229d6236fc270c56c6/crates/client/assets/schema.sdl#L888-L920), how to extract the transaction fee from it?

---

<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 21, 2023, 1:47pm UTC](https://forum.fuel.network/t/how-to-get-transaction-fee/3884/2 "2023-12-21T13:47:26Z")

</div>

I see that fee calculation in the TS SDK is done in this method:

> <https://github.com/FuelLabs/fuels-ts/blob/53dafb1c6e5e02e47f805c111a8f348278101a5f/packages/providers/src/transaction-summary/get-transaction-summary.ts#L25>

But it looks like this method gets consensus parameters such as `gasPriceFactor` and `gasCosts` from the node at the time of calling the method. Those parameters could change over time, is that right? That makes this method to be inaccurate in terms of fee calculation for old transactions that were executed on a different chain configuration

---

<div class="post-metadata">

**Author:** ![calldelegation](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/calldelegation/32/1484_2.png) [@calldelegation](https://forum.fuel.network/u/calldelegation)\
**Post date:** [December 23, 2023, 4:44pm UTC](https://forum.fuel.network/t/how-to-get-transaction-fee/3884/3 "2023-12-23T16:44:30Z")

</div>

Hey @mpoplavkov, you’re correct that the fee calculation is based on individual gas costs as outlined in the chain specification here: [Fuel Core Chain Spec](https://github.com/FuelLabs/fuel-core/blob/master/deployment/scripts/chainspec/beta_chainspec.json#L77-#L288). If you’re interested in examining the gas consumption of a transaction at the time it was executed, you can easily do so by querying the transaction receipt field. This approach eliminates the need for any recalculation.

For example, consider the following query on the Fuel Network Playground:  
[Beta-4 Fuel Network Playground](https://beta-4.fuel.network/playground)

```auto
query {
  transaction(id: "0x7802615abe94a67d5fc7b5d1620d5c2b5cca32b1fb149bc39190d08f62a3bf8e") {
    receipts {
      gasUsed
    }
  }
}

```

This query will provide you with the gas used for the specified transaction.

---

<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 27, 2023, 11:20am UTC](https://forum.fuel.network/t/how-to-get-transaction-fee/3884/4 "2023-12-27T11:20:35Z")

</div>

Hey @calldelegation. Thank you for the response, but it looks like `gasUsed` is not the only parameter you should consider for gas calculation. Take this tx as an example: [Fuel Explorer](https://fuel-explorer.vercel.app/tx/0xad763d2aacad87b0995d75c41a08b8d77b1c25bbb686fc7ef88d321adee47b54/simple). It has a `gasUsed` value of 13, but the explorer shows the fee of 0.00000004 ETH.

Or this one: [Fuel Explorer](https://fuel-explorer.vercel.app/tx/0xa9d297c480999734a198319bf403d90293d9987740955f43e8403324ecbadb08/simple). Gas used is 0, but the fee is 0.000000046 ETH.

---

<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 27, 2023, 11:28am UTC](https://forum.fuel.network/t/how-to-get-transaction-fee/3884/5 "2023-12-27T11:28:46Z")

</div>

I’ve implemented a rough solution for that: just taking all inputs and outputs of ETH and calculating the difference, i.e. looking for an amount of ETH that is lost within the transaction. Not sure if that’s a correct approach though

I just take all ETH that was inputted to the transaction and subtract the amount of outputted ETH

---

<div class="post-metadata">

**Author:** ![calldelegation](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/calldelegation/32/1484_2.png) [@calldelegation](https://forum.fuel.network/u/calldelegation)\
**Post date:** [December 27, 2023, 6:56pm UTC](https://forum.fuel.network/t/how-to-get-transaction-fee/3884/6 "2023-12-27T18:56:38Z")

</div>

> [@mpoplavkov](#):
>
> taking all inputs and outputs of ETH and calculating the difference, i.e. looking for an amount of ETH that is lost within the transaction

hmm yea you’re right this is the way I would do it then. Let me confirm with the client team if there is a better way to do it with our current schema.

---

<div class="post-metadata">

**Author:** ![calldelegation](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/calldelegation/32/1484_2.png) [@calldelegation](https://forum.fuel.network/u/calldelegation)\
**Post date:** [January 2, 2024, 2:59am UTC](https://forum.fuel.network/t/how-to-get-transaction-fee/3884/7 "2024-01-02T02:59:42Z")

</div>

This might be of interest for you @mpoplavkov. Using `max_fee` and `used_gas` you should be able to use the formula below to calculate the final fee charged from the user. Both of which can be derived from the TS SDK or Rust SDK.

`MaxGas = MinGas + (WitnessBytesLimit - ActualWitnessBytes) * GasPerByte + GasLimit`

> <https://github.com/FuelLabs/fuel-specs/pull/529#discussion_r1422640555>
>
> You are looking into \`max\_gas\` implementation for \`Create\` transaction. The \`Scr…ipt\` transaction implements this method in another place.
> 
> \<img width="752" alt="image" src="https://github.com/FuelLabs/fuel-specs/assets/18346821/ac0ed9b2-d63f-403a-8f0b-c1253d47ec46"\>
> 
> The final formula looks like:
> 
> $\`MaxGas = MinGas + (WitnessBytesLimit - ActualWitnessBytes) \* GasPerByte + GasLimit\`$

---

<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:** [January 2, 2024, 9:44pm UTC](https://forum.fuel.network/t/how-to-get-transaction-fee/3884/8 "2024-01-02T21:44:31Z")

</div>

`MinGas` and `MaxGas` in this context are minimum and maximum possible fees that you need to pay for the transaction, right? So, it’s useful for user to know before confirming the transaction. I don’t get how to derive the actual fee of a confirmed transaction from these values
