0

I must be missing something. Maybe someone can help me see where i'm misunderstanding.

Forgive me if this question has been asked already. I'm seeing some adjacent questions but haven't been able to answer my question.

I'm trying to calculate the max cost in USD a tx might be. Rather than using the oracle or an api, i wanted to see how this might be done with raw rpc / provider data. Here's an example of what i'm looking at

  usdPerEth: 1514.5428940543754, // current avg ETH in USD (accurate enough)
  usdPerGwei: 0.0000015145428940543755, // = (usdPerEth / 1000000000)
  blockData: { // alchemy raw block data
    gasLimit: '29941438', // max amount of gas units (not wei?) allowed in block
    baseFeePerGas: '24169920433' // base fee that everyone pays - adjusts by 12.5% depending on amount of txs processed previously.
  },
  feeData: { // alchemy fee data
    gasEstimate: 21000, // # of gas units? not wei? for my estimated tx
    maxFeePerGas: '49839840866', // total amount in wei i might pay per unit above (including priority)?
    maxPriorityFeePerGas: '1500000000', // "tip" / priority / miner incentive in wei
    gasPrice: '25169920433', // is this is the pre-london price? wei

    // converted above fee data to gwei
    maxFeePerGasGwei: 49.839840866,
    maxPriorityGwei: 1.5,
    gasPriceGwei: '25.169920433'
  }

Looking at the ethereum dev docs it says

Calculating the total transaction fee works as follows: Gas units (limit) * (Base fee + Tip)

With the above data that becomes.

total cost (in wei) = gasLimit * (baseFeePerGas + maxPriorityFeePerGas)

becomes 29941438 * (24.169920433 + 1500000000) = 7.68594331e17 (wei) convert to gwei to make it easier 29941438 * (24.169920433 + 1.5) = 768594331.1096027 Gwei?! no. must be wrong. so maybe i have my gas units wrong and it is a price in wei? so that becomes: 0.029941438 * (24.169920433 + 1.5) = 0.76859433 Gwei!? no. still obviously wrong.

Ok, lets forget manually figuring out and use the feeData straight from alchemy.

maxFeePerGas = 49839840866 or ~49.8 Gwei
gasEstimate = 21000

so...
49.8 * 21000 = 1045800 (Gwei!?) this is not right either.

What i know is that:

  1. My usd per gwei calc is correct
  2. The gasEstimate is not wei but in fact units of gas.
  3. It seems as though the maxFeePerGasGwei is the total gwei cost that i'm seeing on the oracles, but, multiplying that with my estimate is obviously not right...

Thanks for reading this far. I've been struggling with this the last couple of days and i want to understand.

1 Answer 1

1

When you sign an Ethereum transaction, you specify a gasLimit. Not the block gasLimit of 29941438 that appears in your data, but your transaction gasLimit. This is the maximum number of gas units that you will ever pay for. A plain ETH transfer to an EOA always consumes 21000 gas units, so for such a transaction you would set the transaction gasLimit to 21000.

Suppose that your transaction ends up getting mined in a block with a baseFeePerGas of 10 gwei. This means that for each 1 of the 21000 gas units that your transaction will consume, you will pay a 10 gwei “block inclusion fee”. This fee is a “protocol fee” and it is burned, it doesn’t go to the miner. If you have offered a maxFeePerGas of more than 10 gwei, e.g. 15 gwei, then there is a 5 gwei surplus (per gas unit). The miner will try to claim as much as possible of those 5 gwei as “priority fee” (tips)… but only up to maxPriorityFeePerGas. So e.g. if you have specified maxPriorityFeePerGas of 2 gwei, then the miner will take 2 gwei and you will be refunded the extra 3 gwei you had paid upfront (when you offered 15 gwei per gas unit). If on the other hand you had specified a higher maxPriorityFeePerGas of e.g. 9 gwei, then the miner will take the entire 5 gwei as tips, but not any more that that, because you specified an absolute maximum maxFeePerGas of 15 gwei.

So the absolute maximum amount of ETH you will pay per gas unit is always maxFeePerGas. So if you specify a transaction gasLimit of 21000 gas units, and specify a maxFeePerGas of 15 gwei per gas unit, then the absolute maximum ETH that you could ever possibly be charged is 21000 × 15 = 315000 gwei, or 0.000315 ETH.

The USD value of those 0.000315 ETH will depend on the current market rate.

3
  • 1
    Ahhhh.... I see. Thank you for explaining that. So just to be clear, if i wanted to calculate the maxFeePerGas myself i would use the calc mentioned in the eth docs with my estimated # of units (limit?) like so 21000 * (baseFeePerGas + tip) Am i understanding how that is calculated correctly? I appreciate the breakdown. best one yet. Jun 12 at 19:14
  • Yes. There are different ways to look at the same thing. One way is to say “I want my tx to be included only when the base fee goes down to 10 gwei, and when that happens, I am ready to pay up to 2 gwei tip over and above the base fee”. In that case you would set maxFeePerGas to 12 gwei and maxPriorityFeePerGas to 2 gwei.
    – dwardu
    Jun 12 at 19:43
  • 1
    Thanks so much for your help. This has been super educational. Jun 12 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.