MEV (Maximal Extractable Value) is a term used to describe the maximum value that can be extracted from block production beyond the standard block reward and gas fees by including, excluding, and changing the order of transactions in a block.
MEV can be thought of as the economic value that arises from the ability to manipulate the order of transactions in a block. It arises from a variety of sources including:
- Arbitrage – discrepencies in pricing across decentralized exchanges opens the opportunity to purchase an asset in one location and sell it in another within the same block. This acts as a valuable service balancing asset prices across the DeFi ecosystem.
- Liquidations – many borrowing and lending protocols have an open liquidation engine that anyone can execute. The person who executes the liquidation can then sell off the collateral for a profit.
- Front-Running & Sandwich – It’s possible to monitor the mempool for incoming transactions and then front run these to take advantage of price movement caused by large orders. A sandwich attack front-runs a trade and then sells off the asset at the higher price once the tx has gone through sandwiching the users order and creating more slippage.
Arbitrage
Arbitrage is a common practice in all areas of finance, where a profit is made by taking advantage of the difference in prices in two or more markets. An arbitrage opportunity is present when there is the possibility to instantaneously buy something for a low price and sell it for a higher price.
There different types of arbitrage, the simplest is arbitrage involving two tokens between two trading venues. This type of arbitrage involves undertaking two transactions, buy on one DeX and selling on another, utilizing the difference in pricing to make a profit.
Transactions in DeFi are atomic meaning we can add a simple check to see if the trade has been profitable and if it hasn’t revert the transaction. This limits risk to just the loss of a transaction fee.
require(profit > 0, "You trade like me");
In triangular arbitrage an asset will be swapped for another asset which is then swapped for a 3rd asset which then gets swapped back to the original. For example let’s imagine we have 3 liquidity pools:
- UNISWAP USDC/WETH
- SUSHI WETH/PEPE
- UNISWAP PEPE/USDC
Our arbitrage bot would see a mispricing between all three assets and execute a round robin trade going USDC > WETH > PEPE > USDC
This type of arbitrage can go further and the more assets you trade on more venues the more potential there is for arbitrage between them. Searchers have also started bundling arb trades together in the same transaction to optimise their execution and get multiple arbitrages done in a single transaction.
There’s an overly simplified and not very profitable arbitrage trading bot here:
Liquidations
MEV liquidations occur in DeFi over-collateralized loans on protocols such as Maker, Compound and Aave, where a user will deposit $100 in ETH and then borrow $70 in stablecoins. If ETH drops in price a borrower’s collateral value may not cover their loan/debt value. If the collateral value drops below a certain threshold, a position can be liquidated and anyone on permissionless blockchains can repay the debt and claim the collateral.
There are generally two types of liquidation engine in DeFi:
- Fixed spread liquidation allows a liquidator to purchase collateral at a fixed discount when repaying debt
- Auction liquidation allows a liquidator to start an auction that lasts for a pre-configured period and competing liquidators can engage and bid on the collateral price
To reward individuals for the costs and risks incurred in liquidating under-collateralized loans and to disincentivize borrowers from letting themselves become under-collateralized in the first place, lending protocols place an additional fee on liquidations, which goes to the liquidators themselves.
Liquidators are most profitable during periods of market volatility where price is causing liquidation cascades on centralized exchanges. Searchers use similar MEV capturing techniques as arbitrageurs, monitoring the mempool and executing efficiently.
Latency plays a big part in liquidations because prices move faster than oracles can update the on-chain price. This creates a competitive environment for searchers and their trading bots.
Front-Running & Sandwich
When a user makes a trade on Uniswap they either knowingly, or in the obfuscated settings on the UI, set a maximum slippage rate. This means the transaction will fail if the price moves more than x% away from the price quoted.
They then execute this trade which sends a transaction to a public mempool to be included in the next block. These mempools are monitored and searchers have the ability to front-run the transaction.
Let’s say a token is trading at 0.1 ETH on Uniswap, when someone buys that token it moves the price. Price moves more for less liquid tokens with smaller liquidity pools creating greater slippage. A trader buys a big allocation of the token which in theory would move the price to 0.11 ETH. A searcher can execute a trade at 0.1 ETH front-running the trader. The searchers order moves the price to 0.105, the trader then gets worse execution and their order moves the price further to 0.115 ETH. The searcher then sells off the asset back-running the order and reducing the price back down to 0.110 claiming the profit from the trade.
All this takes place in a single block where the searcher places one transaction before the trade and one after the trade, sandwiching it.
Analysing Real-TIme MEV
My new favourite website for monitoring MEV in real time is https://eigenphi.io
It provides a real time stream of MEV transactions, break downs for different types of MEV and a heat map of tokens and liquidity pools doing volume.
Individual trades can be broken down into the individual transactions and tokens swaps.
This provides a way for anyone to explore the world of MEV as it happens on Ethereum.
Preventing MEV Attacks
The best way to prevent getting front-run on a transaction is to use a private RPC node “dark pool”.
Metamask connects to an RPC node which acts as it’s gateway to the decentralized p2p network. As default Metamask uses an RPC provider called Infura for this but we can add a custom node which will provide private transactions preventing front-run attacks.
The ETH2 Scourge Update
The Eth2 era is likely to have a significant impact on the MEV landscape. The Scourge update was added to the Ethereum 2.0 roadmap late last year. The goal is to “ensure reliable and credibly neutral transaction inclusion and avoid centralization and other protocol risks from MEV”
This was added as a response to MEV-Boost, the centralized relay operated by Flashbots which currently builds blocks for just under 50% of the Ethereum network. Validators earn significantly more rewards for staked Ethereum positoins if they use the relay creating a financial incentive towards centralized relays that profit from MEV.
The Scourge will aim to tackle this issue and while MEV wont go away a lot can be done to reduce it’s impact.
The Future Of MEV
There are many complex DeFi protocols that are permissionless and provide special opportunities for MEV. Whether that be a liquidation engine, some custom arbitrage from a minting process or something else entirely, there will always be opportunity in the long tail of DeFi.
The MEV space and the opportunities it presents are constantly evolving. Being competitive at the highest level is one of the hardest jobs in blockchain development. You will not find anyone willing to share profitable code, if you do it’s likely a scam. MEV is not a get rich quick opportunity, it takes a lot of work to create and sustain a competitive system.
To take advantage of MEV opportunities, searchers need to have a deep understanding of the underlying protocols and tokens. They also need to have the ability to write efficient Solidity/Yul code and be able to run up nodes and modify clients.
MEV will never go away but perhaps the Scourge update will negate some of the negative impacts of MEV on users and the network.