I’ve been trying to use estimate_transaction_cost to estimate gas costs so I could set up hard limits for my applications calls.
The issue seems to be that with the tolerance set to 0.0 or None it underestimates the price of the transaction by quite a lot.
From my understanding, tolerance set to a low amount should give the exact or close to the exact price of the transaction.
I’ve resorted to using a simulate(Execution::Realistic) call instead to estimate the cost and it seems to be doing a way better job. When the estimate_transaction_cost call says the gas used is 70k the simulate call returns the gas used at 150k which is way closer to the actual target.
The only way to make the estimate_transaction_cost worthwhile is to check by how much is the estimate off, and increasing the tolerance, in my case setting it to 2.0, which seems ridiculous.
Fuels and the Fuel toolchain used are at the latest versions.
Could you please provide a minimal repro for this ?
It is hard to give a constructive answer without some code to reproduce it. Transaction estimation can heavily depend on the “state” of the tx (for example inputs, outputs, witnesses etc.). Let’s say you have a long contract call that will exit early if no inputs are available. In that case the estimation will have different results depending on whether you have inputs or not.
As this is a contract call, it could be the case that the way you get the tx out of the contract call differs from what simulate is doing. Realistic simulation will add inputs to cover for the tx fees etc.
So if I understood correctly, both estimate and simulate should provide around the same gas usage outputs if the function inputs are the exact same in both calls, and also identical to the actual call that will be performed?
Yes they should be similar. Just note that estimate uses a block_horizon to estimate gas prices, plus you can set the estimation tolerance. In addition, estimate does not do any utxo validation!