FlashBots Protect | Hero or Villain?

We look at the Flashbots Protect product and test to see if we get MEV’d or if Flashbots protects us from the watchful eyes of hungry mempool sandwich bots.

Patrick Collins
Cyfrin

--

How to get MEV’d
Flashbots Protect | How good is it?

I read the Flashbots documentation, which seems to tell me two conflicting things.

How to get MEV’d
From the Flashbots doc

You get MEV back, but it also protects you from frontrunning. But yet, frontrunning is a form of MEV.

So which is it?

Does Flashbots Protect keep me hidden from hungry bots?

Or is it a mechanism to lay my transactions up to them on a silver Subway platter?

Let’s find out.

And of course, feel free to watch my video of this demonstration.

What is MEV?

What is MEV?
Image from Cointelegraph

First, let’s explain MEV for the newcomers. MEV stands for either “Miner extractable value” or “Maximal extractable value” depending on who you ask, and it’s the value that a validator or miner can generate by ordering transactions in a block.

Ordering transactions can generate miners or validators' money. For example:

  1. There is a contract with a withdrawMoney function
  2. Whoever calls that function first, wins 10 ETH
  3. If I send a withdrawMoney call, I will often send it to the public mempool, the place where transactions wait to be placed in a block. All nodes can see into this mempool
  4. Whoever builds the block in an EVM chain (like Ethereum) gets to pick the order of transactions, and they also see the transactions in this mempool
  5. They see my transaction makes money, but they want to make money, so they copy my just put their withdrawMoney transaction first
  6. They win the 10 ETH, everyone else who called withdrawMoney gets nothing and wastes gas

This is a simple example, but more egregious examples might be you doing a swap on Uniswap, someone seeing you’re buying token X, and they buy all of token X before you buy to drive the price up.

To learn more about MEV, you can read the Chainlink article on it.

Flash Bots Protect: Savior or Villain?

flashbots home page
Flashbots home page

So, I obviously don’t want to get MEV’d.

But what can I do?

Flashbots says you can use them, but everything I know about Flashbots says they make money from MEV. And reading their documentation, I’m confused as to if they are protecting me, or feeding my transactions to the lions.

To make sure I could recommend using this to developers who don’t want to get front-run I had to devise an experiment…

The Scott Bigelow Experiment Remixed

Original video of getting MEV’d LIVE

A few years ago, ETH researcher Scott Bigelow demonstrated how straightforward it can be for bots to front-run your Ethereum transaction. We are going to run the same experiment with an updated contract, and we are going to do it twice.

  1. Once without Flashbots Protect, and we expect to get front-ran
  2. Once with Flashbots Protect, and we expect to get protected

We updated the experiment with a contract similar to the original one he used.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

contract WithdrawMe {
error BadWithdraw();

bytes32 public s_secretHash;

event success();
event fail();

constructor(bytes32 secretHash) payable {
s_secretHash = secretHash;
}

function withdraw(string memory password) external payable {
if(keccak256(abi.encodePacked(password)) == s_secretHash){
(bool sent, ) = msg.sender.call{value: address(this).balance}("");
if(!sent){
revert BadWithdraw();
}
emit success();
} else {
emit fail();
}
}

function balance() external view returns(uint256){
return address(this).balance;
}
}

The key of this contract is that:

  1. We will deploy a contract with $50 worth of ETH in it, to get MEV bots to salivate
  2. We hash a password, that only we know, so hypothetically only we should be able to call the withdraw function
  3. However, when we call withdraw we will send our transaction to the public mempool, and other nodes will see the transaction, copy it, and place their transaction ahead of ours (by paying more gas, being the block creator, etc)

But what happened when we tried this?

The Results…

As expected, when we trialed without Flashbots, our transaction got front run!

  1. We deployed the Contract
  2. Funded it with $50 worth of ETH
  3. And called withdraw

Sadly, our transaction got front-ran by someone who paid (at the time) $45 to get the $50 of ETH, and they made a $5 profit!

The front-runner transaction

This was good though, as we knew our experiment was tantalizing for MEV bots to attack.

So we ran the experiment again.

  1. We deployed the Contract
  2. Funded it with $50 worth of ETH
  3. And called withdraw — this time using the Flashbots Protect RPC

And to our excitement, we did not get front ran and successfully withdrew the funds!

We even got to see our transaction on the Flashbots Explorer, seeing our funds protected from the watchful eyes of the MEV bots.

Stay protected from Flashbots protect
Flashbots Explorer

How does it work?

Flashbots Protect works by not fanning out your transaction to the public mempool, and only having the small group of nodes in the product build the block.

Why don’t we all use this moving forward?

The downside of Flashbots Protect is that your transaction may take a lot longer to go through since the validators in the Flashbots pool must be the ones to validate your transaction, and that’s a smaller pool than *checks notes* all the nodes in the world.

So if you want quicker transactions, Flashbots Protect will not give you that.

So, now you know one way to stay protected from MEV. And you’re smarter than a lot of black hats out there. Please use this power for good.

To learn more about smart contract security and smart contract audits, visit Cyfrin.io.

--

--