Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Front-running and sandwich attacks on a swap pool

After this lesson: I can calculate what a sandwich around a swap costs the user on a constant-product pool.

65 min lesson2 commandsBlock building and MEV lab: mempool, sandwiches, auctions and defences in Python

Paying more because somebody saw you coming

A sandwich attack is a swap placed just before yours and another just after it. You get a worse rate and the attacker keeps the difference. It is the best-known kind of MEV on a swap pool.

To defend users you first need to measure the harm. Today you work out a swap by hand and then measure a sandwich on the toy chain.

By the end of this lesson

Note

I can calculate what a sandwich around a swap costs the user on a constant-product pool.

Key words

WordWhat it means
Swap poola program holding two tokens that swaps one for the other
Reservehow much of one token the pool holds
Constant productthe rule that one reserve times the other stays the same
Price impacthow much a swap worsens its own rate
Sandwicha front-run and a back-run around the same swap

The cells students fill in are left blank in this preview.

A constant-product pool sets its own rate

The toy pool holds 10000 red and 10000 blue. It keeps red times blue the same after every swap.

So if you put in an amount of red, you get out: blue reserve times amount, divided by red reserve plus amount, rounded down. A small swap gets nearly one blue per red. A big swap gets much less per red, because it empties the blue side. That loss is price impact.

Swap on the toy pool

Command
docker exec jupyter python3 steps/swap.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: what the lab printed

SwapRed inBlue outBlue per 100 red
Small, on a fresh pool50047695
Big, on a fresh pool5000333366
The same 500, after the first50043286

The cells students fill in are left blank in this preview.

Try it: rates on a smaller pool

Code: python
reserve_red = 2000
reserve_blue = 2000
for amount in [10, 100, 1000]:
    out = reserve_blue * amount // (reserve_red + amount)
    print(amount, "red gets", out, "blue")
# Change 2000 to 20000 for both. Does a bigger pool give a fairer rate?

A sandwich has three slices

Ana swaps 500 red and sets no limit. A bot reads her swap in the mempool and gets three transactions placed in this order.

  1. Front-run: the bot swaps 1000 red, so the blue side shrinks.
  2. The victim: Ana swaps at the worse rate.
  3. Back-run: the bot swaps all its blue back to red, at the better rate Ana's swap left behind.

Measure a sandwich around Ana

Command
docker exec jupyter python3 steps/sandwich.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: the harm to the user

WhatValue
Ana alone gets476 blue
Ana sandwiched gets395 blue
Ana loses81 blue, about one sixth
The bot ends with88 more red than it started with

The cells students fill in are left blank in this preview.

The loss falls on the user

The pool still keeps its product rule, so the pool is not robbed. The user is: Ana handed over the same 500 red and received 81 fewer blue.

She never sees the bot. Her wallet simply shows a worse result than the quote she was given. That is why the defences in later lessons matter: users cannot spot this harm on their own.

A common mistake

Warning

Many people think a sandwich steals from the pool. The pool keeps its rule after every swap and is not harmed. The loss is the user's, who receives less than the quote because two swaps were placed around theirs.

About this lab and its source: Block building and MEV lab: mempool, sandwiches, auctions and defences in Python

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.