A Peek into the Ethereum Mempool with Flashbots

“It’s no secret that the Ethereum blockchain is a highly adversarial environment… If the chain itself is a battleground, the mempool is something worse: a dark forest.”
– Dan Robinson

Etherum, MEV, and Flashbots

Over the last few weeks, I’ve been eagerly learning the inner workings of the Ethereum Virtual Machine (EVM). Reading Ethereum is a Dark Forest piqued my interest and motivated me to learn more about Maximal Extractable Value (MEV).

Beforehand, I was only somewhat aware of MEV from reading threads on crypto Twitter. At the time, most of it went over my head. Well… the time has come to buckle up and dig deep. I’m starting with Flashbots.

Searchers

Flashbots connect searchers (bots) to node validators, avoiding the public pool where most pending transactions await to be included in the next block. This is the mempool.

Searchers are users that construct “bundles” and submit them to Flashbots’ private mempool over the public mempool.

There are three types of searchers:

  • Arbitrage/Liquidation bots looking for fast and risk-free access to block space.
  • Traders looking for privacy and protection from front-running on their transactions
  • Dapps and contracts with complex transactions

Bundles

Typically, when you submit a transaction, it is broadcast to the mempool, where it waits to be included on-chain. Bundles are one or more transactions grouped and executed atomically. This can include other pending transactions from the mempool.

If a transaction within a bundle reverts, the whole bundle is canceled. This is advantageous because it creates a low-risk way to perform complex transactions and quick access to block space.

When a transaction is “pending,” a bot scanning the mempool may simulate your transaction, see if it’s profitable, then offer to pay a much higher gas fee to have their transaction included first. We call this process front-running, and it can cause much confusion for the recipient when looking at Etherscan. By submitting bundles directly to block builders instead of through the main network, transactions remain private from the rest of the network.

Searchers incentivize block builders to include their bundle by paying a high gas price or directly transferring any ERC20 token to the block builder’s address within the bundle.

The latter is the ideal strategy. That’s because it allows users to make payments conditioned on their transaction succeeding, thus reducing the risk of transactions reverting and still paying the gas fee.

Code

Upon receiving a bundle, a builder simulates the transactions to ensure it’s valid. Then it will proceed with building the rest of the block.

To construct a bundle, first, you must create a connection with the Flashbots Relay:

const flashbotsProvider = await FlashbotsBundleProvider.create(
    provider,
    Wallet.createRandom(), 
    FLASHBOTS_ENDPOINT
  );

Here, provider is the Ethereum provider you’re connecting to. Additionally, to build a “reputation” within the Flashbots searcher community, you’ll want to use the same wallet address instead of a random wallet.

Building a bundle can be as simple as creating a transaction object and specifying a target block number:

const bundle = await flashbotsProvider.sendBundle([
      {
        transaction: {
          chainId: CHAIN_ID,
          type: 2,
          value: ETHER.div(100).mul(3),
          data: "0x", //  Hex call data for function you want to call
          maxFeePerGas: GWEI.mul(3),
          maxPriorityFeePerGas: GWEI.mul(2),
          to: "0x", // Contract you are calling
        },
        signer: new Wallet(WALLET_PRIVATE_KEY, provider),
      }
    ], blockNumber + 1);

In my next post, I’ll explore calling contract functions by constructing the call data.

Resources:

Below is a list of resources I’ve found helpful in my journey so far.

  • The official Flashbots documentation
  • Using Flashbots to Mint NFTs on Ethereum – Part 1 by Scott Bigelow
  • Flashbots: Kings of the Mempool
  • Conversation

    Join the conversation

    Your email address will not be published. Required fields are marked *