DEV Community

metadevdigital
metadevdigital

Posted on

MEV (Maximal Extractable Value): What It Is and Why It Matters

MEV (Maximal Extractable Value): What It Is and Why It Matters

cover

When I first deployed a liquidity pool contract on Mainnet, I watched a $50k swap get sandwiched in the same block and couldn't figure out where the slippage went. Took me way too long to realize someone had inserted a transaction ahead of mine, moved the price, and I paid for it. That was my introduction to MEV, and honestly, it broke my brain for like two weeks.

MEV is the value that can be extracted from a blockchain by reordering, inserting, or censoring transactions within a block. It's profit that goes to whoever controls block ordering—miners on PoW, validators on PoS, or more commonly these days, searchers and builders. The kicker: this value comes out of users' pockets.

A user sends a swap on Uniswap. A searcher sees it in the mempool, runs a simulation, realizes they can buy the token before the user's transaction lands (driving the price up), then sell after. They pocket the difference. The user gets worse execution than they expected. Everyone knows this as sandwich attacks, but that's just one flavor.

How does MEV actually work?

You broadcast a transaction to the mempool with gasPrice: 50 gwei. Validators (or builders, in modern MEV-Boost architecture) see it. A searcher also sees it and thinks: "if I frontrun this with a different transaction, I can profit." They submit a higher gasPrice or rely on a builder to include them. Builders prefer higher fees, so the searcher's transaction lands first in the block, yours lands second. That's it.

Here's a simplified sandwich attack in code:

pragma solidity ^0.8.0;

interface IUniswapV2Router {
    function swapExactTokensForTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external returns (uint256[] memory amounts);
}

contract Sandwich {
    IUniswapV2Router router = IUniswapV2Router(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);

    function frontrun(address tokenIn, address tokenOut, uint256 amount) external {
        address[] memory path = new address[](2);
        path[0] = tokenIn;
        path[1] = tokenOut;
        router.swapExactTokensForTokens(amount, 0, path, address(this), block.timestamp);
    }

    function backrun() external {
        // After victim's tx lands (price moved up from our buy)
        // We sell and pocket the difference minus gas costs
    }
}
Enter fullscreen mode Exit fullscreen mode

In reality, searchers use private relays like Flashbots to bundle transactions before broadcasting to the network, avoiding mempool visibility entirely.

Why should you care?

If you've shipped a DEX, an AMM, or anything that touches token swaps, MEV is eating your users' returns. Uniswap V2 on Ethereum alone sees millions in MEV extracted daily. Since PoS merged on Ethereum, validators have captured something like $20M+ per month in MEV through PBS (Proposer-Builder Separation). That money isn't disappearing—it's being transferred from users to sophisticated operators.

But here's the thing that gets me: MEV isn't inherently bad. Price discovery, arbitrage, liquidations—all of that is valuable and necessary. The problem is information asymmetry. If you're a user sending a swap, you have no idea that five other transactions are also targeting the same pool in your block. You're blind, and they're not.

The specific gotchas I've hit

Slippage parameters don't protect you from MEV. You set amountOutMin to enforce maximum price impact, but MEV extraction happens inside your block. By the time your tx executes, the price has already moved against you. You eat it anyway. Some protocols offer MEV-resistant liquidity, but you pay for that privilege and it's not available everywhere.

Gas price bidding is part of the attack surface now. A searcher can outbid you on gas alone, guaranteeing ordering. If you're trying to execute a time-sensitive trade, remember that gasPrice is a variable in their optimization function. They'll just spend more gas than you to get in front. Block builders control ordering now, not miners. After MEV-Boost went mainstream, understanding the builder-validator relationship matters more than understanding Flashbots. A builder could theoretically censor your transaction or reorder it for profit, and you'd never know why.

Orderflow auctions (OFA) are emerging as a solution, but they're still experimental. Protocols like MEV-Burn try to make MEV economically useless, but adoption is slow and the game theory is still being worked out.

So what's the defense?

If you're building, use encrypted mempools, batch auctions, threshold encryption, or accept that MEV is the cost of doing business on transparent blockchains. If you're a user, set tight slippage limits, use MEV-resistant chains like Solana if you need to, or accept worse pricing as the tax for using Ethereum. You're not getting a third option.

The real answer is that MEV is a feature of transparent consensus, not a bug. You can't remove it without fundamentally changing how blocks are constructed. What you can do is redistribute who captures it and make sure the value doesn't leak entirely to robots optimizing faster than everyone else.

Top comments (0)