MEV Bots 101: What are MEV Bots and How are They Making Millions?

The Gray Area of DeFi: How Bots Are Front-Running Crypto Investors and Squeezing Out Profits

Richard Larsson
Coinmonks
Published in
8 min readAug 30, 2023

--

On July 30, Curve Finance (the asset management protocol) lost $62 million to an exploit. The hacker had exploited an issue with the protocol’s programming language Vyper and stolen funds from several of its liquidity pools.

But that’s just half the story. The attacker never got away with the entire stash.

The savior — “coffeebabe.eth,” an MEV bot jumped amidst the DeFi chaos to exploit the exploiters.

The bot identified the incoming hack in the CRV-ETH liquidity pool and reproduced the transaction. The bot then paid a higher gas ($32 in ETH) fee to remain at the front of the line and got their transaction executed first.

The attempt secured millions of dollars in ETH which were then returned by the bot as a white hat initiative, aka ethical hacking, to Curve..

Source: Eigenphi.io

MEV Bots: Why are they hitting the headlines?

Ethereum MEV bots have captured more than $1 million in profits from 11,640 transactions executed in a single day, according to data from eigenphi.io.

MEV bots are a fascinating subject in the DeFi world. Though they operate secretively, they have gained a lot of popularity lately.

MEV searchers and bots have earned Millions of dollars in the past using various MEV strategies. In May 2023, MEV extracted on Ethereum alone stood at $686 million.

There have been a lot of debates around the ethicality of such bots. Are such bots useful for the DeFi ecosystem, or do they pose any risks?

There’s much to be unearthed about MEV bots before we arrive at any conclusion.

I can already feel the stream of questions coming my way. To ensure a clear understanding, let’s begin by breaking down this complex concept step by step.

What is MEV, and How Does It Work?

MEV, or maximal extractable value, is the maximum value validators, or traders can extract from a block production on a blockchain.

Let me simplify.

On a blockchain, transactions are clubbed as bundles called blocks and sent to validators who approve the transactions in exchange for ‘block rewards’. Once approved, the block becomes a part of the blockchain. This string of events constitutes block production.

Coming back to MEV — searchers look for profitable opportunities to extract maximum value by reordering, including, or excluding transactions in a block.

Searchers are independent network participants who look for such opportunities. They may be validators running nodes or traders submitting transactions in the mempool (waiting room) of the Ethereum blockchain..

So how do MEV bots work? They are software programs run to scan networks and identify profitable opportunities on Ethereum. These bots manipulate the sequence of transactions in a block to gain extractable value and some huge ones have been logged over the past few months.

This is done through popular MEV strategies such as arbitrage, sandwiching by utilizing frontrunning, flash loans, etc.

An arbitrage strategy takes advantage of price differences between two DEXs like Uniswap or Curve Finance.

Whereas, Flash loans leverage the temporary liquidity on DEXs to earn rewards by helping liquidate borrowers who defaulted on the minimum collateral requirements.

The most infamous one is — Sandwiching. It involves transaction reordering, where a transaction pair is bundled with the user’s transaction (whose trade is being targeted), with the user’s transaction ‘sandwiched’ in between.

For instance: A subway-themed trading bot with the Ethereum Name Service (ENS) domain “jaredfromsubway.eth” made millions of dollars in extractable value by using sandwich attacks during the memecoin trading frenzy in April.

Sandwich attacks are highly popular, but more on this later.

How Does an MEV Strategy Work?

Here comes the interesting part…Since each block of transactions is limited in size, there’s always competition among the users to get their transactions approved first. There is also no standard set of rules to decide on the sequence of transactions sent for validation on a blockchain network.

Mostly, the validators decide the order based on the gas fees. The higher the gas fees, the higher probability for your transaction to get approved first. Searchers can identify MEV opportunities and attract validators by paying a higher gas fee to approve their transactions before others or include them in the next block.

Searchers can also employ MEV bots to identify profitable bets more quickly. I have tried to break down the entire MEV process into four stages to simplify it for you.

Stage 1: Transaction Watching by Searchers

Transactions on Ethereum never get executed immediately. They line up to be selected and sent for approval as a part of a block. They are routed through public nodes on the network where searchers using MEV bots observe the transactions in the mempool to identify profitable ones.

Stage 2: Simulating Transactions for Testing

It is time to test whether the MEV strategy would extract value. To do this, searchers simulate the transactions on their privately owned nodes.

These simulations are helpful in observing the impact your chosen transactions will have on the blockchain. You can deploy tools like Revm and Arbiter to analyze and gain insights into whether or not there is profitable MEV opportunity.

Step 3: Bundling and Sending Transactions

If the simulation shows profitable results. The MEV bot can start bundling up their transaction with the one selected from the public mempool. Bundling ensures the transactions are executed in the same order as desired to ensure the maximum output from the MEV strategy, in this case a sandwich

One way to do this is through Flashbots. Searchers use private relays to bypass the mempool to get them executed through builders like Flashbots. Out of the 30 known builders, 4 hold 90% of the market share.

Source

Builders forward bundled transactions to validator nodes for approval.

MEV bots send their bundled transactions to all four builders and more such builders to increase the chances of their bundled transactions getting selected.

Source: Chainlink

If other MEV bots propose transaction bundles, it may result in bribing wars.

MEV bots were able to secure large profits. Earlier, it was a few thousand dollars per day. But as competition intensifies, the MEV market will get consolidated further. Profit margins will shrink with more bots contesting for the same opportunity and paying even higher gas fees.

Stage 4: Validator Nodes Propose New Blocks

MEV boost is a tool that allows validator nodes to receive bundled transactions created by Flashbots or other builders. Bribes in the form of higher gas fees accompany the bundles.

The validator proposes a new block to the Ethereum network ➡ The proposed block gets added to the network. Consequently, the searchers with their bots earn profits while the validators receive some of the profits as gas fees (bribes).

Source: Milkroad

Let’s understand this with the help of an example of a Sandwich Attack.

Suppose an MEV bot scans a mempool for large DEX trades. It finds a transaction that wants to buy 5,000 AAVE with USDT on Uniswap. If this trade executes, the AAVE/USDT pair will see a potential surge in price.

What does the MEV bot do?

It calculates the possible price effect of this trade. It then executes a buy order before the trade to buy AAVE for cheap. Similarly, it executes a sell order after the trade to profit from the price increase caused by the large order.

You must know that each sandwich attack is calculated before it’s executed. If it’s not profitable, it won’t execute. If the competition is high, larger bribes will be involved so that a large chunk of the profit can be wiped off, leaving the MEV traders with little to celebrate.

Do MEV Bots Make Millions of Dollars From Their Trades?

The answer is yes and no.

Look at this bar chart from Eidenphi.io.

Source: Eigenphi

The bar chart shows that most MEV trades don’t acquire profits above $10. Only a handful of trades guarantee you windfall gains above $1k. MEV traders need to acquire deep knowledge of how DeFi markets operate and program their bots to execute the best of the MEV strategies.

Data from Eigehphi shows that $10.03 million worth of trades were executed in Arbitrage, $2.18 million in Sandwich attacks, and $1.4 million in Liquidations in the last 30 days on Ethereum itself.

It’s basically a money printer without risk attached to it.

Are There Any Ethical Considerations in MEV Bot Development?

It’s worth remembering that MEV DOESN’T CONCERN SPECULATING ON TOKEN’ PRICES. It isn’t any Ponzi scheme running on a yield farming protocol.

For the layperson, MEV is to DeFi what HFT (High-Frequency trading) is to the stock market. (though both strategies differ in approach and outcome)

MEV bots aren’t illegal. They do extract value at the expense of others. But MEV bots have existed in the past and will continue to do so in the future.

Such bots exist in the equity markets and are an accepted practice. However, the impact of such trades in the equities market is more opaque, given their centralized nature. Whereas in the DeFi markets, we can witness real-time MEV transactions.

MEV bots can lend efficiency to the DeFi markets by decreasing price discrepancies, enhancing price discovery, and adding liquidity. We already discussed a Whitehat use case for an MEV bot. Similarly, these bots can foster new innovations and use cases to help develop the Ethereum ecosystem. UniswapX is a great example of this.

However, MEV bots can threaten the security and stability of the blockchain by creating incentives for malicious or dishonest behavior.

How to Code Your Own MEV Bot

When you are trading tokens you are speculating on prices and there is a big risk involved. When running MEV operations you can calculate for each transaction and make sure it is profitable before you execute.

That is why I decided to build a proof of concept of MEV bot and I made the code public for you to use. Hopefully it will get you started if you are interested to learn more about the MEV space.

https://github.com/0xRLA/sandwich-bot

MEV traders are often secretive, but some communities can be extremely helpful in your bot development journey. The 0 to 1 Guide for MEV and Flashbots can be your starting point to learn the technicalities involved in bot development.

However, studying Geth is the best way to get started with MEV.

Conclusion

MEV bots are a complex and controversial topic in the DeFi world. They can be used to extract value from the blockchain ecosystem, but they can also be used to improve efficiency and liquidity. The ethical considerations of MEV bots are still being debated, but it is clear that they are here to stay.

Only time will tell how MEV bots will ultimately impact the DeFi ecosystem. However, it is clear that they are a powerful force that will need to be carefully considered by developers, regulators, and users alike.

Some people believe that they are simply a way of maximizing profits in a free market, while others believe that they are unfair and exploitative.

What’s your take on MEV bots?

--

--

Richard Larsson
Coinmonks

I share insights on Web3, leadership and entrepreneurship from the perspective of a CTO every week.