Lesson 0215 min
SANDWICH · SLIPPAGE · USER PROTECTION

What Does a Sandwich Searcher Extract?

Place a searcher before and after the user, then compare minimum output, pool price, and extracted difference.

ObjectivesAfter this lesson, you should be able to:
  • Explain how a front-run changes the user's output
  • Use minimum output as a slippage boundary
  • Separate searcher profit, pool fees, and user loss
COURSE POSITIONStep 2 of 4
  1. PREVIOUSWho Can See a Trade Before It Executes?
  2. CURRENTWhat Does a Sandwich Searcher Extract?

    Place a searcher before and after the user, then compare minimum output, pool price, and extracted difference.

  3. NEXTWho Back-runs the Price Gap a User Creates?
BEFORE YOU READ

Think about these questions first.

Choose an answer before opening the explanation. You can add anything unexpected to your review list.

Q01If the user transaction succeeds, how can a sandwich still harm it?
SHORT ANSWER

Success only means no revert, not fair execution. The attacker moves the pool first, the user fills near the allowed worst price, and the attacker reverses; the loss is hidden in poorer output.

Compared with your prediction:

A sandwich is not simply “the pool was hacked.” It uses public intent and the AMM curve: a searcher changes reserves before the user, lets the user execute in the worse state, then sells back to turn part of the price impact into extraction.

Put three trades in one order

In a plain public scenario, a block may contain only the user swap. In a sandwich, the searcher proposes:

  1. Front-run buy: consume some TOKEN depth first;
  2. User swap: execute against the new reserves;
  3. Back-run sell: sell the TOKEN bought in front.
ORDERING SIMULATIONVirtual pool · no real MEV
SECURITY 001 · MEMPOOL / ORDERING / MEV

Who gets to choose the order?

Put one swap into a deterministic pool, then watch visibility, ordering, slippage protection, and extraction change the result.

ScenarioSandwichPhase: idle
Pool spot price120.00credits / TOKEN
User quote90TOKEN
User actual output0TOKEN
Searcher P/L0credits, after tip
Visible to searcherNO0 mempool item(s)
FOCUS EXPERIMENT · SANDWICHDoes the guard stop the sandwich?

Run the same public sequence with a loose and a tight minimum-output guard.

Baseline quote 90 TOKENMin output 84 TOKENExtraction 0 TOKEN
From intent to shared stateSandwich
  1. 01
    Mempoolintent is visible
  2. 02
    Block orderwho goes first
  3. 03
    Statereplayed by nodes
Mempool
Nothing submitted yet
Selected block order
Build a block after scanning

A mempool record is not a fill. The block order is not a payout. Only execution moves balances and pool reserves.

BALANCE LEDGER · NO VALUE FROM NOWHEREWho gained, who paid, and what moved?

Searcher profit, proposer tip, pool fees, and user output live on the same ledger.

Pool120,000 credits · 1,000 TOKEN
User30,000 credits · 200 TOKEN
Searcher20,000 credits · 200 TOKEN
Proposer0 credits · 0 TOKEN
Coach

Submit one user swap first. The key question is not whether the transaction is valid, but who can see and order it before it becomes shared state.

Ordering events1 EVENTS
  1. 01

    Public pool created: 120,000 credits + 1,000 TOKEN, any public transaction can change the next quote.

ACTION HISTORYExperiment timeline
1 state snapshots

Use the slider to return to an earlier step. Continuing from there replaces the later history with a new sequence.

Initial state
View experiment records →

Compare the user's quoted output with actual output. A loose minimum-output guard accepts a worse fill; a tight guard reverts the user trade, so the searcher cannot treat a failed transaction as a completed sale.

Expand: how this step works
front = swap(searcher, X → Y)
victim = quote(pool_after_front, X → Y)
if victim.amountOut < victim.minOut:
    revert(victim)
else:
    execute(victim)
back = swap(searcher, Y → X)

Production protocols also handle atomicity, gas auctions, private bundles, router minimums, and multi-pool routes. This lab keeps the smallest state machine that makes the asset flow visible.

KNOWLEDGE CHECK

What does a minimum-output guard protect in a sandwich?

Read one extraction on one ledger

Keep the user's missing TOKEN, the searcher's credit change, pool fees, and the proposer's possible ordering tip together. Otherwise every negative price impact looks like the same kind of attack.

LESSON RECAPComplete the exercise and knowledge check first