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
| Word | What it means |
|---|---|
| Swap pool | a program holding two tokens that swaps one for the other |
| Reserve | how much of one token the pool holds |
| Constant product | the rule that one reserve times the other stays the same |
| Price impact | how much a swap worsens its own rate |
| Sandwich | a 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
docker exec jupyter python3 steps/swap.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: what the lab printed
| Swap | Red in | Blue out | Blue per 100 red |
|---|---|---|---|
| Small, on a fresh pool | 500 | 476 | 95 |
| Big, on a fresh pool | 5000 | 3333 | 66 |
| The same 500, after the first | 500 | 432 | 86 |
The cells students fill in are left blank in this preview.
Try it: rates on a smaller pool
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.
- Front-run: the bot swaps 1000 red, so the blue side shrinks.
- The victim: Ana swaps at the worse rate.
- 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
docker exec jupyter python3 steps/sandwich.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: the harm to the user
| What | Value |
|---|---|
| Ana alone gets | 476 blue |
| Ana sandwiched gets | 395 blue |
| Ana loses | 81 blue, about one sixth |
| The bot ends with | 88 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.