July 14, 2023

How to Protect Your Protocol from MEV

Practical information on MEV is surprisingly rare. Usually, you either get the sensational (hacks), the political (the moral of MEV), or the theoretical (MEV research).

So this article clarifies some pointers for protocol designers:

  1. What is MEV?
  2. Is MEV a problem for your protocol?
  3. Where does your protocol leak MEV?
  4. What can you do about it?

First, let’s cover a simple definition.

A definition of MEV

MEV is the risk-free profit that bots, block builders, or block producers can make by selectively placing, and optionally re-ordering transactions in a block.

Note: Bots don't need to earn the profit in a single block for it to count as MEV.

A bots profit is your loss

All profit comes from somewhere. So unless it’s a service you consented to pay for, someone else’s risk-free profit is your loss.

In other words, every time your protocol is part of a MEV tx, it’s very likely that you – or your users, lps, or treasury – are one party paying for the bot’s profit.

This means that someone lost the over 600m in extracted MEV. And this is a conservative estimate. Perhaps the largest portion of MEV (non-atomic and long-tail MEV), isn’t even included in these metrics.

Some say part of this "loss" is rather fees paid for a useful service, performed by bots, and isn’t actually a direct loss. But if you look closer, even this smaller portion of MEV is priced at least 10-times higher than is reasonable.

So if it’s such a big problem, why don’t protocols do more about it yet? Because MEV is hard to see and the tools and heuristics to measure it aren’t yet widespread.

But there are simple approaches to recognize the MEV in your protocol.

How to find your MEV

Here's a straightforward way to see many ways you're exposed to MEV:

  1. Look at where your protocol allows swapping one asset with another.
  2. Is there any other way (on- or off-chain) to swap the same assets?
  3. When the exchange rate you offer differs from the other, there's likely MEV.

This perspective uncovers more than just obvious arbitrage.

Simple examples

Cross AMM: Someone gets an asset cheaper on your exchange than on another exchange. → They buy the asset from you below market and sell it on another pool above market. One or both pools make a loss and the bot profits.

Liquidations: Someone buys discounted collateral from your protocol and sells it at market rate. Your lender makes a loss.

Advanced examples

Sandwich: Slippage in a user transaction allows a bot to create two swap rates: One before the user’s transaction and one after. The bot buys low, then sells high. The profit comes entirely from the user.

Closing a lending position: A lending protocol allows you to redeem an asset at a better rate than the current rate on Uniswap. The lending protocol and Uniswap both "pay" for the bot’s  profit.

Expert examples

Cross token: ETH just jumped in price due to a large buy on Binance. This means any token currently paired with ETH (e.g., wBTC on Uniswap) is currently valued more highly than it should be, all other things being equal. So Uniswap allows swapping wBTC for too much ETH at this moment. A bot can profit – again at Uniswap LPs’ expense.

Cross time: A user approves a token for sale on Uniswap. A bot sees the approval and sells the token first, expecting to rebuy it more cheaply after the user executes their sale. The user gets a lower price and pays the profit for the bot.

Different swap rates are a surprisingly effective way to see your MEV, but they’re not the only way.

Three more important perspectives can reveal your MEV

  • The window between a tx being committed and finalized;
  • The disadvantage for protocols of being innate and passive, vs bots being active;
  • The disadvantage for protocols of seeing incomplete and low-resolution data on anything off-chain, compared to bots’ full and real-time information.

You can dive deeper into these others perspectives to find your MEV in How to find MEV.

How to protect your MEV

MEV should belong to those that create it

If you or your users are the source of all MEV, you should have some power to capture it. It turns out that you do.

These are 4 ways you can protect your MEV today.

A. Hiding transactions

One MEV source we listed above is that transactions are visible in the public mempool before they are mined. Bots can use this information to  profitably front-run a transaction, for example.

You can protect your protocol against this by sending the transaction into a private mempool, like Flashbots. Only the block proposers/builders, who will mine the transaction, can see it here – and they’re incentivised to use the information responsibly.

But this just protects you against frontrunning and sandwiching.

This solves a good part of the problem. But…

  • Private mempools are currently only available on Ethereum L1.
  • They are tough to get going (a significant portion of block builders need to join).
  • This doesn’t protect you against backrunning.

B. Using an off-chain relay to find the best route

Smart contracts only have a limited view of all relevant data (e.g. prices) and on-chain computation is costly.. So you can’t expect a smart contract to always know the best market rate for an asset swap.

But if you include off-chain computation, it becomes realistic to find near-optimal solutions. Software that runs off-chain in a data center can have real-time access to all markets, including on-chain. So it can find the best exchange rate for any asset at any given time, and the best way to route transactions.

Together, on-chain smart contracts and off-chain algorithms can construct more informed trades that are better for your user and your protocol. This means they implicitly capture and eliminate lots of the backrunning MEV your users would otherwise lose.

This is also called "off-chain relay, on-chain settlement" and is how 1inch and 0x work. 

What’s more, the usefulness of off-chain computation to find the best transaction goes far beyond limit orders.

C. Auctioning off the transaction

Instead of outsourcing the transaction’s construction to your relay, you can also open up the right to post your transaction – and extract its MEV – for auction.

But this introduces friction and risk, since you pay both the relay and the bots that work for it. It's also difficult to ensure nobody abuses the information. And finally, you introduce a dependency on the relay. This can fail and threaten to block your smart contract.

D. Using your own bots

Instead of making your protocol dependent on a centralized relay or auction, a better way is to fight bots with your own bots. By itself, your protocol is disadvantaged against other bots. But together with your own bot, you can protect most of your MEV.

There are several ways to build a bot and ensure it wins over other bots. The correct solution depends on your protocol and the chain you are on.

Reach out to us, ifyou want to dive deeper on how to make sure MEV bots don't drain your protocol and users. We can show you where you are exposed to MEV, and run bots that protect you.

You can meet us at Devcon in Bogota. Or reach us directly on:

Keep Reading

How to find your MEV

Three useful perspectives to notice where your protocol produces MEV.
Read More

The first MEV Protection on zkSync!

StabilityKeepers are now available on zkSync testnet.
Read More