A look at how maximal extractable value (MEV) searchers are able to profit through "sandwich attacks," and how you can avoid them in future transactions.
On most blockchains
chains) or nodes
chains) typically generate revenue from two main sources — block rewards
and transaction fees. But with the recent advent of decentralized finance
(DeFi), a new revenue stream has been unlocked — which comes from the way transactions are ordered within a newly mined/validated block
Given that miners/nodes have a great deal of control over transaction ordering, they also have a range of opportunities to extract additional profits through the clever formation and arrangement of transactions.
Here’s how it works.
Formerly known as Miner Extractable Value
, and usually going by the abbreviation, MEV. In most cases, transactions are simply organized by transaction fee, such that the highest fee/byte transactions are included first, whereas the lowest fee transactions above the threshold for inclusion are included last to ensure the block is filled. But thanks to DeFi, and decentralized exchanges
(DEXs) in particular, it can be possible to order transactions in such a way as to guarantee an additional profit through what is known as a “sandwich attack”.
Check out our guide on what sandwich attacks are
and how you can avoid them.
This sees an individual or pool-based miner/node scan the mempool
for pending large DEX transactions which can be forced to suffer slippage
through a sandwich attack.
How Would a MEV Attack Work?
Here's how this would work:
- Assuming a trader (i.e., the target) wants to purchase X amount of token A.
- The miner/node will then form a transaction that would buy a large sum of token A from that liquidity pool first (pushing up the price).
- It would then order a second transaction that would then sell all the tokens they just bought.
- The victim’s transaction is then sandwiched between these two, allowing the miner to extract value from the victim by changing the pool weight just before he/she purchases.
Sniping, Front-Running and MEV "Searchers"
Miners/nodes are also able to front-run as a way to increase their yields. This occurs when a miner detects a potentially profitable transaction in the mempool, e.g. a flash loan or large transaction from a well-known wallet (such as a hedge fund), and executes a similar transaction first — albeit with a higher fee. This would then be mined first, allowing them to essentially steal the victim’s alpha and gains. This process is also sometimes termed ‘sniping’
It should be noted that both sandwich attacks and front-running can be performed by essentially anybody with the tools to scan the mempool and automatically form a value-extracting transaction — in this case, these users are known as “searchers”
. But working as (or in partnership with) a miner/node can allow for the execution of more complex transactions, increase success rates, and even eliminate the need to outbid the victim in terms of gas. This advantage is further exacerbated by the advent of Flashbot auctions, which are communication channels that allow miners and searchers to work together to execute MEV opportunities without revealing opportunities to the mempool.
Without working in partnership with a miner, searchers usually need to resort to spamming the transaction to the network to maximize the chances of it being picked up by the successful miner/node.
According to data from Flashbots
, a total of $7.4 million was secured through MEV on the Ethereum
blockchain in the last 30 days alone. Since Ethereum suffers from relatively long block times, high fees and frequent congestion, but still has an incredibly comprehensive and popular DeFi ecosystem, it provides by far the most opportunities for MEV.
The vast majority of MEV events are associated with Uniswap V2
and Uniswap V3
, where miners typically use sandwich attacks to siphon profits from high volume traders, but other platforms like Compound
also suffer from liquidation front-runners — whereby liquidators can use front-running to find and execute liquidation transactions, securing the liquidator fee (or a discount on the collateral).
To put this into perspective, with an average of 6,395 blocks mined per day, and a reward of 2 ETH per block, Ethereum miners currently earn around $1.29 billion per 30 days in simple block reward revenue. As such, MEV still represents just a tiny fraction of all miner income (<1%), but since only a tiny fraction of miners participate in the process, the actual percentage increase is likely significantly higher than 1%.
Given that the pay-off from a single MEV attack can be worth tens to hundreds of thousands, or even millions of dollars, the incentives to participate are high. As seen by some of the highest-grossing MEV transactions below:
Unfortunately, as an individual trader or DeFi user, you’ve only got a handful of potential defenses against bots
— most of which involve limiting their potential profit by understanding how they work and forming transactions that are less likely to be targeted.
But from a developer or project’s perspective, there is a range of potential solutions that are worth considering to help minimize the impact of MEV on your users. Here, we break down some of the methods that have been used to dupe the bots or completely eliminate them altogether
The Salmonella Contract
The Salmonella contract
can be used to bait and dupe frontrunners through the use of decoy ERC-20 token and deceptive transfer function — which only transfers a fraction of the bought amount to the frontrunner if the transaction meets certain criteria (e.g., not being part of a whitelist or the contract owner itself). Similar smart contracts exist which apply a transaction tax only to known frontrunners.
Liquidity Sniper Trap
The token contract includes code that automatically detects when a backrunner snipes the initial liquidity from a new DEX pair containing its associated token. This backrunner is then automatically added to a blacklist and is unable to sell its tokens. This restriction may be released later.
MEV relies on miners/nodes having the final say over transaction ordering, allowing them to prioritize transactions based on their profitability rather than their natural sequence. A number of platforms have proposed ways to improve order fairness, including Chainlink’s Fair Sequencing Service
, Arbitrum's distributed sequencer plan and Automata's Conveyor
service. These vary in their implementation, but all work to eliminate MEV.
Bear in mind, some of these may be legally dubious — particularly if they involve blacklisting specific token holders. We recommend consulting with your legal team or a relevant legal authority on the matter before implementing any of these.
This article contains links to third-party websites or other content for information purposes only (“Third-Party Sites”). The Third-Party Sites are not under the control of CoinMarketCap, and CoinMarketCap is not responsible for the content of any Third-Party Site, including without limitation any link contained in a Third-Party Site, or any changes or updates to a Third-Party Site. CoinMarketCap is providing these links to you only as a convenience, and the inclusion of any link does not imply endorsement, approval or recommendation by CoinMarketCap of the site or any association with its operators.
This article is intended to be used and must be used for informational purposes only. It is important to do your own research and analysis before making any material decisions related to any of the products or services described. This article is not intended as, and shall not be construed as, financial advice.
The views and opinions expressed in this article are the author’s [company’s] own and do not necessarily reflect those of CoinMarketCap.