How to get transaction fee?

Given the transaction model from the GraphQL schema, how to extract the transaction fee from it?

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

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

1 Like

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. 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

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

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

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. It has a gasUsed value of 13, but the explorer shows the fee of 0.00000004 ETH.

Or this one: Fuel Explorer. Gas used is 0, but the fee is 0.000000046 ETH.

1 Like

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

1 Like

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.

1 Like

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

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