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
| Word | What it means |
|---|---|
| Slippage limit | the least a user will accept back from a swap |
| Fail | a swap under its limit does nothing and changes nothing |
| Private submission | sending a transaction straight to a builder, not the mempool |
| Batch auction | swaps collected together and settled at one rate |
| Uniform rate | the 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
docker exec jupyter python3 steps/slippage.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: limits against the largest front
| Limit | Least Ana accepts | Largest front | Bot gain |
|---|---|---|---|
| None | 0 | 3000 red | 205 red |
| 5% | 452 | 271 red | 25 red |
| 1% | 471 | 56 red | 4 red |
The cells students fill in are left blank in this preview.
Try it: turn a percentage into a limit
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
docker exec jupyter python3 steps/private.pyShown 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
docker exec jupyter python3 steps/batch.pyShown 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.