Skip to main content

How do I simulate a transaction that needs a token approval first?

Put the approval in preSimulationTransactions so Quoter runs it on the fork before the main call. You must still submit a real approval on-chain before the live transaction.

J
Written by Javier Diaz

Put the approval in preSimulationTransactions so Enso Quoter runs it on the fork before the main call. You must still submit a real approval on-chain before the live transaction; the simulated setup does not create a live allowance.

Definition

Many ERC-20 spends revert unless a spender is approved. On a fork, you can apply that approval first so the main simulation sees the allowance it would have after the user approves.

How it works

  • Encode the token approve (or equivalent) as a setup transaction with the correct to, data, from, and value.

  • Send it in preSimulationTransactions and put the spend or deposit in transaction.

  • You can also set the optional spender field on the main transaction to the address that needs to be approved on each tokenIn for the simulation.

  • If result.status is "Error" with an allowance-related revert, confirm the setup approval targets the right spender and amount, then simulate again.

  • On-chain, submit the real approval first when allowance is too low, then the main transaction.

How Enso does this

POST https://quoter.api.enso.build/api/v1/simulate accepts optional preSimulationTransactions. The Quoter FAQ lists token approvals as a primary use. See also Simulating Transactions. For Enso Route flows, use the approval data returned with the quote rather than inventing a spender.

FAQ

Does the simulated approval appear in the validate payload?

Validate checks the unsigned main transaction against the simulationId. It does not replay or confirm a live approval.

Can I skip preSimulationTransactions if the wallet already has allowance?

Yes. Setup transactions are only for state the fork does not already have.

Related articles

  • What are preSimulationTransactions?

  • What is an ERC-20 approval in a routing flow?

  • How do I handle token approvals in a routing flow?

  • What does CallReverted mean in a Quoter simulation?

  • Can I simulate a transaction without holding the tokens?

Sources

Did this answer your question?