Frontrunners and MEV Explained: How to Beat the Bots
How-to Guides

Frontrunners and MEV Explained: How to Beat the Bots

6m"
2 years ago

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.

Frontrunners and MEV Explained: How to Beat the Bots

Mục lục

On most blockchains, miners (on Proof-of-Work chains) or nodes (on Proof-of-Stake 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.

Join us in showcasing the cryptocurrency revolution, one newsletter at a time. Subscribe now to get daily news and market updates right to your inbox, along with our millions of other subscribers (that’s right, millions love us!) — what are you waiting for?

Memes and MEV

With the recent hysteria surrounding memecoins, frontrunners are once again taking advantage of the opportunity to generate near-risk-free profits on a large scale.
One recently created bot, operating at the address “Jaredfromsubway.eth,” has already executed more than 238,000 attacks against more than 100,000 victims. According to a recent appraisal by EigenPhi, the bot has raked in more than $6 million in profit by executing hundreds of front and back run transactions per hour.

The bot was so prolific that it drove up the average gas fee for all transactions on the Ethereum network on more than one occasion. In total, it paid more than $34 million in gas fees and placed transactions in around 60% of blocks over the last three months.

Incredibly, the bot has netted as much as $134,000 profit in a single sandwich transaction, though around half of its profits are in the $0 - $10 range per transaction.

Its most frequent targets were LADYS, BAM and MONG traders.

There are two primary reasons meme coin traders are particularly vulnerable to MEV attacks.

First, many meme coins have a transaction tax associated with them, whereby a fraction of the purchased or sold amount is automatically deducted from the trade via a smart contract function. This can range from <1% to 10% or more. Second, meme coins are notoriously volatile, often fluctuating in value by several percentage points from block to block.

Both of these force would-be traders to set their DEX slippage high to ensure the transaction completes. This makes them juicy targets for MEV searchers, who form transactions that manipulate the liquidity pool weighting such that individual traders are pushed to the edge of their slippage tolerance.

As a result, affected traders get the lowest acceptable amount for their tokens, and the bot gets away with the difference in expected price vs final price as profit (sans fees/costs).

What is Maximal Extractable Value?

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:

  1. Assuming a trader (i.e., the target) wants to purchase X amount of token A.
  2. 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).
  3. It would then order a second transaction that would then sell all the tokens they just bought.
  4. 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.

How Bad Is the MEV Problem?

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 and Aave 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).

Image: Flashbots

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:

Image: Flashbots

Beating the Bots

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.

Fair Sequencing

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.
6 people liked this article