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:
- My usd per gwei calc is correct
- The gasEstimate is not wei but in fact units of gas.
- 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.
Best Answer
When you sign an Ethereum transaction, you specify a
gasLimit
. Not the blockgasLimit
of 29941438 that appears in your data, but your transactiongasLimit
. 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 transactiongasLimit
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 amaxFeePerGas
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 tomaxPriorityFeePerGas
. So e.g. if you have specifiedmaxPriorityFeePerGas
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 highermaxPriorityFeePerGas
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 maximummaxFeePerGas
of 15 gwei.So the absolute maximum amount of ETH you will pay per gas unit is always
maxFeePerGas
. So if you specify a transactiongasLimit
of 21000 gas units, and specify amaxFeePerGas
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.