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

Free lesson

How users defend against sandwich attacks

After this lesson: I can work out whether a slippage limit would have blocked a sandwich and what it saved the user.

70 min lesson3 commandsBlock building and MEV lab: mempool, sandwiches, auctions and defences in Python

Three ways to stop being the filling

A user can defend against sandwich attacks in three main ways: set a slippage limit, send the swap privately, or swap in a batch auction. Each one closes a different gap.

None of them is free. Today you measure what each one saves on the toy chain, and what it costs.

By the end of this lesson

Note

I can work out whether a slippage limit would have blocked a sandwich and what it saved the user.

Key words

WordWhat it means
Slippage limitthe least a user will accept back from a swap
Faila swap under its limit does nothing and changes nothing
Private submissionsending a transaction straight to a builder, not the mempool
Batch auctionswaps collected together and settled at one rate
Uniform ratethe same rate for every swap in a batch

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

A slippage limit caps the attack

A slippage limit says: give me at least this much, or do nothing. It is usually set as a percentage below the quote.

The bot must now keep Ana's result above her limit, or her swap fails and the sandwich earns nothing. With no limit the largest front the bot can use in the lab is 3000 red. With a 1% limit, it is 56.

Measure the largest attack under each limit

Command
docker exec jupyter python3 steps/slippage.py

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

Worked example: limits against the largest front

LimitLeast Ana acceptsLargest frontBot gain
None03000 red205 red
5%452271 red25 red
1%47156 red4 red

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

Try it: turn a percentage into a limit

Code: python
quote = 476
for percent in [0, 1, 5, 10]:
    least = quote * (100 - percent) // 100
    print(str(percent) + "% limit: accept no less than", least)
# Change the quote to 3333, the big swap from lesson 2, and run again.

Private submission hides the swap

A sandwich needs the bot to see the swap first. Sending it straight to a builder, instead of the public mempool, takes it out of view.

In the lab, the public view lists t2 to t5 and not Ana's swap, which pays the full 476. But trust has moved: the builder now sees the swap and could still misuse it.

Send a swap privately

Command
docker exec jupyter python3 steps/private.py

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

A batch auction makes the order worthless

In a batch auction, swaps in the same direction are added up, swapped once, and shared out. Everybody in the batch gets the same rate, so being first inside it is worth nothing.

In the lab the bot and Ana each get 86 blue per 100 red, whichever way round they are listed. Ana gets 434, better than 395 sandwiched. She still gets less than 476 alone, because the bot's big swap shares the price impact.

Put two swaps in one batch

Command
docker exec jupyter python3 steps/batch.py

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

A common mistake

Warning

Many people think private submission removes the risk. It removes the public view, but the builder still sees the swap. You are trusting that builder not to misuse it, so choose one with rules you can check.

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.