Skip to main content

How do I estimate gas for a DeFi transaction?

Simulate the already-built transaction with Enso Quoter and read result.gas. The figure comes from actual execution on a forked state at simulation time.

J
Written by Javier Diaz

Simulate the already-built transaction with Enso Quoter and read result.gas. The figure comes from actual EVM execution on a forked state, so it is highly accurate at simulation time.

Definition

A Quoter gas estimate is the gas consumed during the simulated execution of your calldata, not a static lookup. Use it on the review screen as the predicted cost of submitting that same transaction.

How it works

  • Send the transaction, chainId, and token-flow fields to the simulate endpoint.

  • When you need a realistic path, include any required setup in preSimulationTransactions (for example an approval).

  • Read result.gas only in combination with result.status. An "Error" result may still report gas, but you should fix the revert first.

  • Re-simulate close to signing if state may have changed, then validate the new simulationId.

  • Live consumption can differ if other transactions change storage before inclusion.

How Enso does this

POST https://quoter.api.enso.build/api/v1/simulate returns result.gas as a string. See the Quoter FAQ and Simulating Transactions. The Route API also returns a gas estimate from its internal simulation when Enso builds the path; use that when you are submitting a Route tx, not a separate Quoter call.

FAQ

Does this estimate include the approval transaction?

Only if you are simulating the approval itself, or you are looking at a setup transaction you ran as preSimulationTransactions. The main result.gas is for the main transaction.

Should I add a buffer to result.gas?

Public Quoter docs do not prescribe a buffer. They do warn that consumption can change if state changes before execution.

Related articles

  • How accurate is the Quoter gas estimate?

  • How do I preview the output of a transaction?

  • What is a forked EVM simulation?

  • How do I simulate a DeFi transaction before sending it?

  • What is transaction simulation in DeFi?

Sources

Did this answer your question?