How to use Flashbots

In this tutorial we will review how to use Flashbots to gain Miner/Maximum Extractable Value also known as MEV. Miner/Maximum Extractable Value or MEV represents the amount of money one can extract from the blockchain by reordering, inserting or censoring transactions. Different Defi implementations leak MEV when users send transactions to their systems. As a result bot operators and miners make profits by searching for and capturing this leaked value.

This revenue extraction process on the blockchain has been occurring for years. In January of 2020 the Flashbots organization created an MEV website Explorer to track MEV transactions. Then in the summer of 2020 new Defi projects created many new arbitrage opportunities. Because of these new opportunities MEV values started to sky rocket as seen on the daily extracted value graph on the MEV website Explorer. This had a lot of negative consequences on Ethereum from high gas fee’s to reverted transactions which was the result of chain congestion.

The introduction to Flashbots

As a result of blockchain congestion the Flashbots organization came up with a process that allowed all participants (users, bot operators and miners) to benefit from MEV opportunities. Flashbots designed and implemented a system to allow bot operators to send transactions directly to miners. Then Miners would include these transactions in the next block if it is profitable for them. This process resulted in users obtaining guaranteed execution and miners receiving additional fees for including these transactions into blocks.

There are several use cases that Flashbots solves for. They are:

  1. Users and bot operators looking for fast access to block space without worrying about over priced or reverted transactions
  2. Users and bot operators looking for sniping, front running, back running or sandwich protection for their transactions
  3. Dapps and protocols that need to solve for advanced uses cases. For example sending a basket of transactions at one time for atomic execution.

Flashbots is an enhancement to the Ethereum protocol that adds a network layer for users to send private transactions to miners. These transactions are not in the mempool so they can not be seen by other participants. Users submit transactions to a private transaction pool plus a fee and miners will include these messages in the next block.

To make this auction process work the Flashbot team created a Geth client software patch called MEV-Geth. In addition there is an MEV relay which acts as a transaction bundle relayer. The combination of these two enhancements prevents gas wars, failed transactions, and eliminates front running vulnerabilities.

The diagram below is a representation of users interacting with Miners operating MEV-Geth. Keep in mind, all Miners and Searchers do not use MEV-Geth.

flashbots overview of miners and searchers

Who uses Flashbots?

Searchers and Miners participate in Flashbot auctions. In the sections below we will review how to use Flashbots as a Searcher and Miner.

Searchers

Searchers submit transaction bundles directly to relayers instead of through the traditional Ethereum p2p network. Users that submit transactions directly to the Flashbots relayer are private. These transactions can not be seen by the rest of the network. Searchers can pay for this service by increasing gas or a direct ETH transfer to the miners address (which is conditional on the transaction processing).

To access the Flashbots network searches need:

  1. A private key. Used for identification
  2. A way to interact with the Flashbots network. Flashbots runs a relay you will send bundles to relay.flashbots.net.
  3. A “bundle” for your transactions

Following below is sample Python code on how to setup a Flashbots provider, obtain a private key and send a transaction bundle in Web3.py. The code below can be found in the Flashbots documentation. This process can also be achieved by using JavaScript. For more Flashbot information visit their Github repository.

from eth_account.signers.local import LocalAccount
from web3.middleware import construct_sign_and_send_raw_middleware

from flashbots import flashbot
from flashbots.types import SignTx
from eth_account.account import Account
from web3 import Web3, HTTPProvider
from web3.types import TxParams, Wei

import os

"""
In this example we setup a transaction for 0.1 eth with a gasprice of 1
From here we will use Flashbots to pass a bundle with the needed content
"""

# ETH_ACCOUNT_SIGNATURE is an Ethereum private key that does NOT store funds and is NOT your bot's primary key.
#This is an identifying key for signing payloads to establish reputation and whitelisting
ETH_ACCOUNT_SIGNATURE: LocalAccount = Account.from_key(os.environ.get("ETH_SIGNATURE_KEY"))
ETH_ACCOUNT_FROM: LocalAccount = Account.from_key(os.environ.get("ETH_PRIVATE_FROM"))
ETH_ACCOUNT_TO: LocalAccount = Account.from_key(os.environ.get("ETH_PRIVATE_TO"))

print("Connecting to RPC")

# Create a web3 object with a standard json rpc provider, such as Infura, Alchemy, or your own node.
w3 = Web3(HTTPProvider("http://localhost:8545"))
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(ETH_ACCOUNT_FROM))

# Flashbots providers require both a standard provider and ETH_ACCOUNT_SIGNATURE (to establish reputation)
flashbot(w3, ETH_ACCOUNT_SIGNATURE)

print(f"From account {ETH_ACCOUNT_FROM.address}: {w3.eth.get_balance(ETH_ACCOUNT_FROM.address)}")
print(f"To account {ETH_ACCOUNT_TO.address}: {w3.eth.get_balance(ETH_ACCOUNT_TO.address)}")

# Setting up an transaction with 1 in gasPrice where we are trying to send
print("Sending request")
params: TxParams = {
    "from": ETH_ACCOUNT_FROM.address,
    "to": ETH_ACCOUNT_TO.address,
    "value": w3.toWei("1.0", "gwei"),
    "gasPrice": w3.toWei("1.0", "gwei"),
    "nonce": w3.eth.get_transaction_count(ETH_ACCOUNT_FROM.address),
}

try:
    tx = w3.eth.send_transaction(
        params,
    )
    print("Request sent! Waiting for receipt")
except ValueError as e:
    # Skipping if TX already is added and pending
    if "replacement transaction underpriced" in e.args[0]["message"]:
        print("Have TX in pool we can use for the example")
    else:
        raise


print("Setting up flashbots request")
nonce = w3.eth.get_transaction_count(ETH_ACCOUNT_FROM.address)
bribe = w3.toWei("0.01", "ether")

signed_tx: SignTx = {
    "to": ETH_ACCOUNT_TO.address,
    "value": bribe,
    "nonce": nonce + 1,
    "gasPrice": 0,
    "gas": 25000,
}

signed_transaction = ETH_ACCOUNT_TO.sign_transaction(signed_tx)

bundle = [
    #  some transaction
    {
        "signer": ETH_ACCOUNT_FROM,
        "transaction": {
            "to": ETH_ACCOUNT_TO.address,
            "value": Wei(123),
            "nonce": nonce,
            "gasPrice": 0,
        },
    },
    # the bribe
    {
        "signed_transaction": signed_transaction.rawTransaction,
    },
]

block = w3.eth.block_number

result = w3.flashbots.send_bundle(bundle, target_block_number=w3.eth.blockNumber + 3)
result.wait()
receipts = result.receipts()
block_number = receipts[0].blockNumber

# the miner has received the amount expected
bal_before = w3.eth.get_balance(ETH_ACCOUNT_FROM.address, block_number - 1)
bal_after = w3.eth.get_balance(ETH_ACCOUNT_FROM.address, block_number)
profit = bal_after - bal_before - w3.toWei("2", "ether")  # sub block reward
print("Balance before", bal_before)
print("Balance after", bal_after)
assert profit == bribe

# the tx is successful
print(w3.eth.get_balance(ETH_ACCOUNT_TO.address))

Additionally, a few other interesting samples in the Flashbots repository are:

Miners and Pool Operators

A miner is the party who collects all the searcher bundles and produces a block. Miners traditionally run the go-ethereum client (Geth) and order transactions by gas price. However, miners connected to the Flashbots network run a version of the mev-geth client maintained by Flashbots.

The mev-geth nodes evaluate bundles using the first price sealed bid auction. The system picks the most profitable bundles to place at the top of the block. The miner can evaluate all the bundles received and combine those which do not conflict in order to produce the most profitable block possible. The node then compares the Flashbots block with a regular block and begins mining on the most profitable.

Miners have full access to bundle content and can reorder, steal and or censor bundles sent to them by searchers and relayers. Keep in mind that all miners are not honest.

Miners and Pool operators can follow the instructions here to get started running MEV-Geth. The instructions in their quick start guide will help one learn how to build and launch MEV-Geth, implement MEV-Geth on top of a custom version of Geth, and more.

Subsequently the mev-geth-demo repository will allow you to run an MEV-Geth node, and shows how a miner may profit from it by accepting MEV bundles directly or with extra transactions.

Furthermore if you need help setting up MEV-Get visit the Flashbots Discord server.

Resources

Blockchain Networks

Below is a list of EVM compatible Mainnet and Testnet blockchain networks. Each link contains network configuration, links to multiple faucets for test ETH and tokens, bridge details, and technical resources for each blockchain. Basically everything you need to test and deploy smart contracts or decentralized applications on each chain. For a list of popular Ethereum forums and chat applications click here.

Ethereum test network configuration and test ETH faucet information
Optimistic Ethereum Mainnet and Testnet configuration, bridge details, etc.
Polygon network Mainnet and Testnet configuration, faucets for test MATIC tokens, bridge details, etc.
Binance Smart Chain Mainnet and Testnet configuration, faucets for test BNB tokens, bridge details, etc.
Fanton networt Mainnet and Testnet configuration, faucets for test FTM tokens, bridge details, etc.
Kucoin Chain Mainnet and Testnet configuration, faucets for test KCS tokens, bridge details, etc.

Web3 Software Libraries

You can use the following libraries to interact with an EVM compatible blockchain.

Nodes

Learn how to run a Geth node. Read getting started with Geth to run an Ethereum node.

Fix a transaction

How to fix a pending transaction stuck on Ethereum or EVM compatible chain

Next – How to search for MEV opportunities