Bottom line: You can reproduce a reverted or sandwiched Uniswap V3 swap against a forked block with eth_call and state overrides, and read back the exact revert reason, gas consumed, and output amount without spending a wei or touching the mempool.

The mechanism is straightforward once you separate the two things eth_call gives you: a defaultBlock parameter (which block’s state to execute against) and a stateOverride set (which accounts and slots to patch before execution). Fork the chain at a block, override the pool’s slot0 or a holder’s balance, call the router’s exactInputSingle, and decode the result. EIP-1898 is relevant here because it lets you pin the call to a specific block hash rather than a number, which matters when you are comparing runs across a reorg or a re-org-prone testnet.

Not investment advice. This describes how the mechanism works and what the operational risks are. It does not tell you what to buy. See the editorial policy.

What a Uniswap V3 swap actually does on-chain

A V3 swap is not a single contract call to a pool. The periphery SwapRouter (and its successor SwapRouter02) wraps the core UniswapV3Pool.swap() call with a callback that moves tokens in and out. For a single-hop exactInputSingle, the flow is:

  1. The router pulls amountIn of tokenIn from the caller via transferFrom.
  2. It calls pool.swap(recipient, zeroForOne, amountSpecified, sqrtPriceLimitX96, abi.encode(...)).
  3. The pool computes the new price and output amount, calls back into the router’s uniswapV3SwapCallback, which pays tokenIn to the pool.
  4. The pool sends tokenOut to the recipient.

The revert conditions you care about live in three places: the router (insufficient allowance, Too little received from the slippage check), the pool (SPL for a price limit breach, LOK for reentrancy lock), and the token (STF for transfer failure, or a custom error from a fee-on-transfer token). Being able to see which one fired is the whole point of simulating.

The sqrtPriceLimitX96 argument is the one people forget. If you set it to zero the pool uses its default limit; if you set it to a value that the swap would cross, you get SPL and no swap. A sandwich is, mechanically, a swap whose realized price moved past your limit before your transaction landed.

Forking a block and pinning it with a block hash

You need an archive node or a fork provider that serves historical state. eth_call against latest on a public RPC will not reproduce a sandwich that happened at block N; you need the state at N.

Start anvil (Foundry) or a Hardhat node with a fork URL:

1
2
3
4
5
anvil \
--fork-url "$RPC_URL" \
--fork-block-number 19000000 \
--chain-id 1 \
--port 8545

Now every eth_call you make against localhost:8545 executes against the state at block 19000000. If you want to be unambiguous about which block, use the EIP-1898 object form. The spec allows {"blockNumber": "0x..."} or {"blockHash": "0x...", "requireCanonical": true} in place of the plain defaultBlock string for eth_call, eth_getBalance, eth_getStorageAt, eth_getTransactionCount, eth_getCode, and eth_getProof. Pinning by hash means a reorg cannot silently swap the state under you between two calls; if the block is not found the node returns -32001, and if requireCanonical is true and the block is orphaned you get -32000. Distinguishing those two errors is exactly why the EIP exists.

For a one-shot simulation you can also skip the local node and hit a fork provider directly with cast:

1
2
3
4
5
6
cast call 0xE592427A0AEce92De3Edee1F18E0157C05861564 \
"exactInputSingle((address,address,uint24,address,uint256,uint256,uint256,uint160))(uint256)" \
0xTokenIn 0xTokenOut 3000 0xYourAddress 1750000000000000000 0 0 0 \
--from 0xYourAddress \
--block 19000000 \
--rpc-url "$RPC_URL"

That is the happy path. It tells you the output amount at that block. It does not tell you what happens when someone else’s swap lands first, which is the case you actually want to model.

State overrides: patching the pool before you call it

State overrides are the part of eth_call that most people never use. The stateOverride parameter is a map from address to an object with optional balance, nonce, code, and state (a map of storage slot to value). The node applies these to a copy of the state, executes the call, and throws the copy away.

Two override patterns are useful for swap simulation.

Patching a balance. If your test address does not hold the token, give it some:

1
2
3
4
5
6
7
cast call 0xE592427A0AEce92De3Edee1F18E0157C05861564 \
"exactInputSingle((address,address,uint24,address,uint256,uint256,uint256,uint160))(uint256)" \
0xTokenIn 0xTokenOut 3000 0xYourAddress 1750000000000000000 0 0 0 \
--from 0xYourAddress \
--block 19000000 \
--override-balance 0xYourAddress:100000000000000000000 \
--rpc-url "$RPC_URL"

Patching the pool’s slot0. The V3 pool stores sqrtPriceX96, tick, and protocol fee state in slot0. Moving the price before your call simulates the state a sandwich leaves behind. You need the storage layout; slot0 is slot 0 in UniswapV3Pool, packed as uint160 sqrtPriceX96 | int24 tick | uint16 observationIndex | uint16 observationCardinality | uint16 observationCardinalityNext | uint8 feeProtocol | bool unlocked. Writing a raw word is fiddly, so the practical approach is to fork, impersonate a large holder, execute the sandwich swap for real on the fork, then call your swap. That gets you the exact post-sandwich state without hand-packing bits.

If you do want the raw override, the shape is:

1
2
3
4
5
6
7
{
"0xPoolAddress": {
"stateDiff": {
"0x0000000000000000000000000000000000000000000000000000000000000000": "0x..."
}
}
}

Note that some clients accept stateDiff and some accept state; state replaces the whole storage, stateDiff merges. Use stateDiff unless you are deliberately wiping the contract’s storage.

Reading the revert reason and gas

A reverted eth_call returns a JSON-RPC error with a data field containing the ABI-encoded revert payload. cast decodes it for you if you pass the right signature; otherwise decode the first four bytes against the selector table. The common ones you will see:

Selector Error Where it comes from
0x08c379a0 Error(string) Generic require with a message
0x4e487b71 Panic(uint256) Arithmetic overflow, division by zero
0x1a2d80f6 Too little received Router slippage check on amountOutMinimum
0x1f9c1a4e Too much requested Router check on amountInMaximum for exact-output
0x1c9d7eb3 SPL Pool price limit breached
0x1f2a2005 STF Token transfer failed (balance or allowance)

Gas is the other number worth extracting. eth_call does not return gas used directly in the standard response, but most clients accept a gas field in the call object and will fail with an out-of-gas error if you set it too low. Binary-search the gas value until the call succeeds; the threshold is a close upper bound on what the transaction will consume. On mainnet V3 single-hop swaps this typically lands in the 120k–180k range depending on whether the tick crosses initialized liquidity, and a multi-hop route through two pools roughly doubles it. Treat those as measured ranges from your own fork, not constants.

For a proper measurement, use eth_estimateGas with the same call object and the same block parameter. It runs the same execution and returns the estimate; combine it with the override set and you get the gas a sandwiched swap would have cost.

A worked sandwich reproduction

The sequence, against a fork at block N:

  1. Identify a victim transaction in block N. Pull it with eth_getTransactionByHash and note the router call and its amountOutMinimum.
  2. Fork at block N-1.
  3. Impersonate the attacker’s address (or any address with the input token) and execute the front-run swap on the fork. This moves slot0.
  4. Call the victim’s exact calldata from the victim’s address with the victim’s balance overridden in.
  5. Observe the revert (Too little received) or the reduced output.
  6. Execute the back-run swap and measure the attacker’s net position.

Step 4 is the one that answers the question “would this have reverted.” If it reverts with Too little received, the victim’s slippage protection worked and the sandwich failed to extract. If it succeeds with a worse price, the victim’s amountOutMinimum was set too loose.

You can script the whole thing in a Foundry test with vm.createSelectFork, vm.prank, and deal. That is usually faster than shelling out to cast six times, and it gives you assertions instead of eyeballing hex.

Pitfalls and how to diagnose them

Forking at the wrong block. If you fork at N instead of N-1, the victim’s transaction is already in state and your replay will behave differently. Symptom: the call succeeds when you expected a revert, or the nonce is already consumed. Fix: fork one block earlier and check eth_getTransactionCount for the victim address matches what the pre-state should be.

Missing archive state. Public RPCs prune. A fork at a block older than the node’s prune window returns empty code for contracts that should exist. Symptom: eth_call reverts with an empty data field, or eth_getCode returns 0x. Fix: use an archive endpoint or a fork provider.

Override applied to the wrong address. Overriding the router’s balance instead of the caller’s does nothing useful. Symptom: STF on the first transfer. Fix: log the from address in the call object and override that.

Allowance not overridden. A balance override does not grant allowance. If the router has not been approved, you get STF regardless of balance. Fix: override the token’s allowance slot for (caller, router), or impersonate an address that already approved.

Block hash pinning with requireCanonical on a testnet. Testnets reorg more than mainnet. If you pin by hash with requireCanonical: true and the block gets orphaned mid-run, you get -32000 and your script dies. Symptom: intermittent -32000 on a call that worked a minute ago. Fix: drop requireCanonical for testnet work, or retry with a fresh hash.

Fee-on-transfer and rebasing tokens. State overrides do not change token semantics. A token that takes a cut on transfer will still take a cut, and your simulated output will not match a naive amountIn * price calculation. Symptom: output is consistently a few basis points low. Fix: measure against the token’s actual transfer behaviour, not the pool math alone.

Gas limit too low in the call object. eth_call with an explicit gas field below the real cost returns an out-of-gas error that looks like a revert. Symptom: data is empty and the error message mentions gas. Fix: omit gas and let the node use its default cap, or binary-search upward.

FAQ

Why use eth_call instead of just sending a transaction on a fork?

eth_call does not require a signed transaction, does not consume a nonce, and returns the revert payload directly. On a local fork you can send transactions, but eth_call is faster for iterating on calldata and overrides, and it works against a remote archive node without spinning up a node at all.

What does EIP-1898 actually change for this workflow?

It lets you pass {"blockHash": "0x...", "requireCanonical": true} as the defaultBlock parameter to eth_call and the other state-query methods, instead of a block number. That pins the call to one specific block even across a reorg, and the two distinct error codes (-32001 not found, -32000 not canonical) let your script tell the difference between “wrong block” and “block got orphaned.”

Can I simulate a multi-hop swap the same way?

Yes. exactInput on the router takes a path of packed token and fee-tier bytes. The override mechanics are identical; you just need to override balances for the first token and, if any intermediate pool needs a price nudge, execute the front-run against the relevant pool before the call.

How do I get the exact revert reason if cast shows only hex?

Decode the data field. The first four bytes are the selector; for Error(string) (0x08c379a0) the rest is an ABI-encoded string. cast decode-error <data> handles the common cases, and cast 4byte <selector> looks up unknown selectors against the public signature database.

Does a state override persist between calls?

No. Each eth_call gets a fresh copy of state with the overrides applied. If you need a sequence of calls to see each other’s effects, either send real transactions on a local fork or batch the logic into a single call against a helper contract you deploy on the fork.

What is the difference between state and stateDiff in the override object?

state replaces the entire storage of the contract with the map you provide, so any slot you omit reads as zero. stateDiff merges your values into the existing storage, leaving everything else intact. For simulating a swap you almost always want stateDiff, because wiping a pool’s storage will break the call in ways that have nothing to do with the swap.