This is a question about Gas fees for transactions. The bottom line is I can't understand the price (in gas units) of transaction - And I will walk you through a specific transaction to show you my calculations, so you can point out what I'm missing.
So, the example I used is for this transaction which is on fantom, but fantom is EVM compatible (from the ISTANBUL hardfork). Also, I'd like to point that I've read this great article about gas calculation and I will use it throughout this question.
So lets start: The gas provided for this transaction was 1,000,000 units.
Initial fee is always 21,000
Now for the Input data fee - we can take the input data from the ftmscan, and with a short python script calculate the fee:
s = "0xcac637c8000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000021be370d5312f44cb42ce377bc9b8a0cef1a4c83000000000000000000000000aa621d2002b5a6275ef62d7a065a865167914801000000000000000000000000000000000000000000000000000000000000000673706f6f6b790000000000000000000000000000000000000000000000000000"
s = s[2:]
chunks = [s[i:i+2] for i in range(0, len(s), 2)]
sum = 0
for c in chunks:
if c == '00':
sum += 4
else:
sum += 16
print(sum)
the result of this is 1,560. So the gas we paid so far, before any opcode, is 1,560+21,00=22,560
Ok, Now, I've looked at the traces for this transaction (by doing what it says here
curl -H "Content-Type: application/json" -X POST --data '{"method":"debug_traceTransaction","params":["0x0bef4e97cc39b98431c81c4573572905a735d324724e83619001853e344e1092"],"id":1,"jsonrpc":"2.0"}' https://rpcapi-tracing.fantom.network
So far, we've spent 22560 gas units, so we have 1,000,000-22,560=977440, which is great, because that is exactly the gas
value in the traces before the first opcode. So far so good.
Now I'm going to the bottom of the trace: when the STOP
opcode (which has a gasCost
of 0) was executed, the amount of gas left is 833,994. So the amount of gas used in total is 1,000,000-833,994=166,006 gas units
But this is weird - ftmscan says that the gas usage was 226,050, which is waaaaaay higher. Now, I know I didn't take into account refunds. Buy here the deal - refunds are gas units returned to the user, so any refunds (and there is some refund here) will actually decrease the gas cost even more.
So - here is finally the question - how come this transaction gasCost was 226,050 gas units on ftmscan, and much less when running with their own debug trace, and on my local simulator (which I built using Foundry)??