HomeKnowledge BaseHow CoW Swap solves the MEV problem

How CoW Swap solves the MEV problem

7 min read

MEV or “maximal extractable value” is a hidden tax on all types of Ethereum transactions. Any time you make a DeFi trade, buy or sell an NFT, or lend tokens to a liquidity pool, opportunistic users known as “searchers” may manipulate your trades, resulting in unfavorable prices, failed transactions, and missed opportunities.

To date, MEV has been responsible for more than $1.3 billion in lost value for Ethereum users.

1-mev-cumulative-gross-profit.webp

MEV Cumulative Gross Profit

In this article, we will take a deep dive into the problem of MEV and explore some potential solutions, including those offered by CoW Swap.

Understanding MEV

When it comes to DEX trading, there are three main types of MEV attacks that searchers use to take advantage of your trading orders:

  • Frontrunning: Searchers submit a transaction similar to yours and place it before you in the block. Their transaction then executes first and they get to keep your profits for themselves, while your transaction fails
  • Backrunning: Any time you make a large trade, it leaves behind a price impact. If your trade is large enough, this price impact may move the market enough to open an arbitrage opportunity. Searchers can “backrun” your trades by placing their own trades directly after yours and profiting from this leftover arbitrage. Backrunning is generally viewed as a less-harmful type of MEV since it doesn’t affect the main transaction, it simply captures leftover arbitrage
  • Sandwich attacks: Sandwich attacks combine both frontrunning and backrunning into the worst kind of MEV attack. In a sandwich attack, searchers first ratchet up the price for your trade up to your slippage tolerance by frontrunning your transaction. Once your transaction clears, searches make a second trade in the opposite direction that cleans up any profits from the price change your trade causes

MEV is possible because validators are free to order blockchain transactions as they see fit in order to maximize their profits. Proof-of-stake (PoS) blockchains such as Ethereum use parties known as “validators” in order to ensure that the consensus rules of the blockchain are followed by all participants. Validators get randomly selected to add the next block to the blockchain. In this process, they determine what transactions are included in the block, and in what order.

When you submit a transaction on Ethereum, it does not immediately get added to the next block. Instead, it first goes into the “mempool” which is the collection of all pending transactions. Validators then pull transactions from the mempool and add them to the next block when it’s time for it to be built.

Here is where the opportunity to extract value through MEV comes in.

Validators are not required to add transactions to the block in the same order that they were submitted by users, so searchers can pay validators a fee to order transactions in a specific way. For example, they can pay to have their transactions come first if they want to frontrun someone else, or they can pay to have their transactions come right after a specific transaction if they want to perform a backrun.

2-flow-of-an-Ethereum-transaction.webp

The flow of an Ethereum transaction

Is MEV inevitable?

MEV is a complex and controversial topic in the Ethereum community.

Some say that MEV is a natural part of the Ethereum market mechanism and it helps to find the most efficient prices. Others believe that MEV can be harmful, but only if it’s concentrated in the hands of a few validators — therefore they work to “democratize” MEV and allow anyone to extract value from on-chain transactions.

At CoW Swap, we believe that all MEV is harmful to all traders, especially beginners who may not even be aware that they are getting shortchanged on their trades. This is why the protocol is designed to protect user trades from all types of MEV attacks.

Strategies for Protecting Against MEV

CoW Swap gives traders the tools they need to protect themselves from MEV, so when you’re trading through https://swap.cow.fi/ you’re protected from the majority of opportunistic searchers, regardless of the size or type of trade.

CoW Swap relies on 3 main features to keep you safe from MEV:

  1. Delegated Trade Execution
  2. Coincidence of Wants
  3. Uniform Clearing Prices

Let’s dive in!

Delegated Trade Execution

On CoW Swap, all transactions are routed through a decentralized network of “solvers” — independent algorithms that are responsible for finding the best execution price for your trades.

When you submit a trade on CoW Swap, rather than initiating a blockchain transaction like you would on a platform like Uniswap, you instead submit a signed “intent to trade” message. This means that CoW Swap can perform optimizations on your trade that would not be possible if you submitted the transaction directly to the blockchain. These optimizations include finding Coincidences of Wants, adjusting the slippage tolerance dynamically, and completing your trade at an optimal price during the time of execution.

Since it is the solvers who execute your trade on your behalf, you are never directly exposed to the public mempool, meaning that you are protected from MEV at all times.

Whatever price solvers quote you at the time you place your order is actually the worst price that your order will be executed at (it may complete at a better price if solvers find a better execution path). All risk of MEV is taken on by the solver itself.

Coincidence of Wants

CoW Swap’s delegated trade execution mechanism has another major advantage — it can match your orders off-chain.

Any time you submit a trade on CoW Swap, solvers start looking for the best way to execute it. Before checking on-chain liquidity, solvers check other incoming orders on CoW Swap’s current batch auction to see if your trades can be matched peer-to-peer.

Here’s how it works:

Let’s say you’re looking to trade ETH for USDC. You submit your order and solvers see that someone else in that batch is actually looking to trade USDC for ETH — the opposite of your trade — at the same time. These two trades will be matched with each other in a “Coincidence of Wants” (CoW) which allows traders to swap their assets with each other directly (one to one or many to many), thereby saving on gas fees, liquidity provider fees, price impact, and also bypassing MEV risks.

CoW Swap’s founders were inspired by the potential for Coincidence of Wants to provide lower prices for everyday Ethereum users (it’s in the name!). This mechanism is one of the cheapest and most secure ways to make a DeFi trade, and since you’re not using on-chain liquidity pools, your Coincidence of Wants trade cannot be subject to MEV attacks.

Uniform Clearing Prices

As a third line of defense against MEV, CoW Swap ensures all orders complete under a “Uniform Clearing Price” (UCP).

Upon submitting your trade, your order gets included in a batch auction alongside other pending orders in the queue. All of the orders within a batch trade for the same price on the blockchain, a phenomenon referred to as “Uniform Clearing Price” (UCP).

To understand how UCP works on CoW Swap, we first have to consider how DeFi trading works on traditional DEXs. On a normal decentralized exchange, if 8 people want to trade ETH for USDC, each of their trades clears at a slightly different price since DEXs process trades in a sequential manner even though they might be happening concurrently (within the same Ethereum block). This leaves room for MEV because the order in which transactions are processed heavily impacts the prices that users get. Searchers are constantly monitoring DEX trades, looking for opportunities like these where they can reorder transactions to their advantage.

On CoW Swap, if the same 8 people are making that trade, as long as they are in the same batch auction, each order will be settled for the same clearing price when it goes on-chain. This leaves no room for MEV attacks since all prices are the same, meaning that transaction ordering becomes irrelevant.

Getting Started with CoW Swap

CoW Swap was built from the ground up with MEV protection in mind.

Whether you’re a beginner or a seasoned trader, CoW Swap ensures a fair trading experience that protects you from price exploitation. Give it a shot for your next trade at https://swap.cow.fi/#/swap