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.