Alright, let's go

A thread on leveraged (!) sandwiching using flashswaps
Let's review what a normal 🥪looks like.

A victim attempts to trade on a DEX, buying asset X with ETH

1. A bot gets in front of them, buying X & raising the price of X

2. The victim's tx lands, further raising the price of X.

3. The bot sells X at this higher price for profit ImageImageImage
Two things to note here.

First, the bot starts and ends with WETH. That's because the victim was trading from WETH to another asset.

If victims are trading from a token to WETH it is harder to sandwich because bots don't hold random tokens.
Second, the pair in the above sandwich example was relatively illiquid.

That meant the bot didn't need much WETH at all to push the price up and book a profit.
But not all trades are going to be from WETH to an illiquid token.

How might 🥪 bots deal with liquid trades in the other direction? For example, this victim's trade from SHIBU to WETH? Image
Most people know flashloans. Uniswap v2 has a similar functionality called flashswaps.

You can flash-borrow tokens in a pair provided at the end of a transaction you:

1. Return the token w/ a fee

or

2. Return enough of the other token such that the x*y=k invariant holds
Lets break down how this is used in the context of a sandwich. Recall that the victim's trade is from SHIB *o ETH.

First the bot flashswaps SHIB from the Sushiswap pool. They must return either this SHIB or ETH later.

Second it transfers SHIB to the Uniswap SHIB/ETH pool. Image
Third it trades SHIB for 140 ETH, pushing up the price

Now the bot needs to close out their SHIB flashswap. But pool needs 180 ETH to be balanced! Otherwise it will throw a revert.

In the fourth step the bot returns 180 ETH total to the Sushi pool, fronting 40 of their own ETH Image
That is where the leverage comes from.

The bot only had 40 of their ETH at risk but they moved the price of the relevant pool with 140 ETH worth of SHIB.

I wonder how leveraged you can get with a trade like this.
At this point the victim's transaction trading SHIB for ETH lands on-chain at the inflated price post-sandwich buy, and brings the price higher still.

Now the bot needs to close out their position for a profit. That works similarly to how they opened it. Image
First the bot flashswaps SHIB from the Uniswap pool, where their 40 ETH had been fronted before.

Second and third that SHIB is transferred to a Sushi pool, where it is swapped at a high price for 193 ETH.

Fourth, 151 ETH is returned to the Uniswap pool to balance it post-swap. Image
How do the economics work out here?

193 (from the swap) - 151 (returned to the pool) = 42 ETH

42 - 40 (what was fronted by the bot) = 2 ETH

2 - 0.7 (miner payment) = 1.3 ETH in profit for the bot
Note also that the bot is paying via gas price instead of coinbase transfer, which makes it more gas efficient

That's it for today.

Let's illuminate the dark forest together. Learn more about Flashbots and how to get involved on our Github repo:

github.com/flashbots/pm

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Robert Miller

Robert Miller Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @bertcmiller

8 Sep
Let’s get a few things straight.

Eden's lead investor - @multicoincap - is lying about Flashbots and Eden.

🧵
Contrary to Multicoin's claims, minimizing MEV is core to Flashbots' mission & products.

That shows from our funding of fairness and ethics research, work on MEV aware dApps, & 100s of users that have used Flashbots to skip the mempool & protect themselves from frontrunning.
More importantly, Multicoin is lying about Eden: it is not permissionless OR transparent.

Eden is a permissioned system with a multisig that has exclusive control over MEV payouts to miners. The Eden team alone decides whether miners are mining "Eden blocks" and should be paid.
Read 10 tweets
6 Sep
Dropping what is probably the most alpha packed thing I've written so far: an in-depth writeup of my strategy for a highly competitive MEV opportunity, the Synthetix Ether Collateral Liquidations, along with code

Blog post:
bertcmiller.com/2021/09/05/mev…

Repo:
github.com/bertmiller/sMEV
Blog post contains:
- My process from start the finish
- Explanation of why I made the design decisions I did
- How I sped up data collection
- A few gas optimizations
- Explanation of my economic strategy
- My novel bundle submission strategy
- Code snippets
- Many links
Repo contains:
- The only (I think?) open source example of how to backrun transactions
- Monitoring tools I made
- Contracts used for execution, including dydx flashloan
- Contract for data collection
- my somewhat embarrassing messy Hardhat testing env
Read 7 tweets
4 Aug
The beating heart of every blockchain is how space is allocated within a block.

With major changes to how Ethereum's blockspace market works in the near and medium term we'd like to share how Flashbots thinks about designing MEV marketplaces.

medium.com/flashbots/on-t…
Mining today consists of 2 jobs.

1st: create the most profitable block.

2nd: attest to this with a proof-of-work and propagate it to the network.

Prior to widespread MEV extraction, the 1st job was mainly ordering txs by their gas price, with optimization at the network lvl.
However, since MEV extraction is widespread now, the the job of miners has changed. Now to produce the most profitable block miners must find the optimal ordering of transactions within a block.

This is exponentially more difficult than sorting by gas price.
Read 12 tweets
31 Jul
Fatal flaws in a bot, a sort of on-chain virus, a trojan horse token, and arbitrage gone wrong

Join me in looking at the latest MEV bot exploit in this thread 👇🏻
The victim today tried to arbitrage the CHUM token (!) for ~0.01 ETH in profit, but in the process had 30 WETH transferred out of their wallet.

They only interacted with Uniswap v2 pools, how did this happen!?
Searchers optimize their contracts to the extreme to do very specific things very efficiently.

However, occasionally they have a need to do random things & add in functions that can be used for to execute basically any arbitrary transaction in a contract

Read 19 tweets
31 Jul
New MEV bot violence thread being written now 🔥
Writing this as quick as I can but I gotta figure this one out first. It's a hard one.
Looks like tomorrow or Monday I’ll put something out. Taking awhile to understand and express in a way that makes sense and I’ve got stuff to do today. Sorry folks.
Read 4 tweets
7 Jul
All 8 transactions here - including some from unexpected places - are a part of one sandwich!

A thread on the increasingly complex sandwich bots we're seeing
A normal 🥪 bot looks like this

🥪 frontruns a user, buying the asset they intend to and increasing the price. The user gets less tokens now.

The user's buy is then included, pushing the price up more

🥪 sells after the user's tx at the higher price, thus capturing profit
🥪 bots will watch the mempool for users trading with high slippage that they can frontrun. Until recently 🥪 bots would only do this with one trade, and with the Uniswap v2 or Sushiswap router.

But they've leveled up.
Read 13 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(