Sirwin
Sirwin
MEVs and pure trade

MEV and On-chain Arbitrage: Simplified


What is MEV?

MEV stands for "Miner Extractable Value" though it is often refered to as "Maximum Extractable Value". Blockchain transactions are stored in the memory pool, or mempool  until they are picked up by the miners who verify valid transactions and pack them into a block. One can then deploy bots to looks for transactions in this mempool which 'predicts' a future increase or decrease in the price of an asset of a token in an AMM-governed pool. If executed well, these tactics are pure revenue trades (trades when the bot makes money on every asset being traded); such bots profiting from MEV opportunities are referred to as MEV bots. MEV bots relies on miners/nodes having the final word in transaction ordering, allowing them to prioritize transactions based on their profitability instead of their natural sequence. Many systems have put up ideas to enhance order fairness though with little success. 

man in gray t-shirt using macbook pro

Inception of MEV

A long-time algorithmic trader and analyst who goes by the alias Pmcgoohan initially noticed an issue in Ethereum in 2014, a year before the platform officially launched, which later can be categorised into MEV opportunities. When the network became online, Pmcgoohan realised that miners had complete control over the transaction inclusion & ordering process and could take advantage of this authority to profit from unaware users of the protocol. Unfortunately, Pmcgoohan was ahead of his time, and this concern came to light when a publication titled Flash Boys 2.0 that was published in 2019, where a team of academics brought attention to the same problem. In this paper, the word "MEV" was used to characterise the issue that Pmcgoohan had discovered years previously.

man holding black smartphone with flat screen monitor in front

After that, the books Ethereum is a Dark Forest by Georgios Konstantopoulos and Dan Robinson and Escaping the Dark Forest by Samczsun, which were released in August and September 2020, respectively, solidified MEV as a key idea in crypto-economics and emphasised its significance as one of the most difficult and pressing problems the Ethereum research community is currently dealing with. These articles demonstrated that MEV wasn't just a hypothetical problem; it was actually a real occurrence that was already having concerning effects on Ethereum users. MEV is sometimes referred to as an “invisible tax” that miners can collect from users – essentially, the maximum value a miner can extract from moving around transactions when producing a block on a blockchain network.

Arbitrage Opportunities on DeFi

This is how traditional arbitrage opportunities(buy from somewhere where the price is low and selling somewhere else where the price is high) work:
There are thousands of Uniswap-like pools with fragmented liquidity (liquidity spread across these pools) that don't communicate with one another but offer quotes for exchanging assets in real time on chain. Opportunities to purchase low and sell high across different pools are created by fragmented liquidity. Example: Say a user sells $5m of WETH in the Uniswap WETH/USDC pool in a single tx. Following this sale, as the number of WETH in the pool increases, the Uniswap pool will allow users to purchase WETH at a cheaper price than the corresponding Sushiswap pool, though the difference in price be miniscule. So, a clever onlooker could purchase WETH from Uniswap & sell on Sushiswap. A general arbitrage operates on this tenet, but on-chain De-Fi adds another dimension: Atomicity. Because the blockchain's state updates on a block-by-block basis, a transaction (tx) can carry out many operations as long as its final state is accurate. Users can take a "flash loan," for instance, from the Aave like AMM lending pool, which allows borrowing of huge sums (say, $5 million USDC) for free  as long as they return exactly $5 million USDC to the lending pool at the end of the transaction. Due to this, people with limited financial resources can generate large-scale arbitrages on-chain and profit from them for nothing.

photo-1642790551116-18e150f248e3?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1000&q=80

Yes, free money isn't easy money, of course and the introduction of these arbitrage concepts sparked a "space race" among actors to be the first to engage in such arbitrage trades. Few addresses, who win the majority of successful arbitrages, control the majority of the atomic arbitrage space. This market is arguably the most competitive of all, and success depends on a combination of cunning, low latency, and sound infrastructure. Profits have expanded quickly as a result of DeFi's quick expansion and the increasing liquidity on decentralised exchanges. Mev-explore, a public dashboard of metrics related to the Extracted MEV in the DeFi world, estimates that $611,839,862 USD worth of mev has been documented in total. The graph that follows shows that MEV bots have been trending and have grown significantly during the past few months. But this trend indicate more than just "number go up" and "huge profits"; they represent creative applications of technology and information asymmetry. Regular users wouldn't be aware that predatory bots are hiding in the mempool of the blockchain, waiting to capitalise on their errors. The average user does not ultimately benefit from this. Some of these tactics will become more lucrative and reliable when more trade volume enters the DeFi & crypto markets in general. But as we explore the DeFi space, it is our responsibility to remain vigilant and make informed decisions, as there will be undesired consequences otherwise.

Hidden grains of gold 

Let's understand the theory behind this whole operation. A pool that works on the AMM (automated market maker) principle lets users trade WETH and USDT within the pool and has X WETH and Y USDT. The "Uniswap invariant," which is the pool's guiding concept, states that the product of X and Y is always constant.
Therefore, by solving the following equation, [x*y = z], we may determine precisely how much USDC we expect to receive for selling X WETH into the pool for a pool with reserves of (X, Y).
In actuality, the pool also charges a cost of 30bps (0.3%), which you can account for in the calculation above by pre-multiplying X by 0.997; however, we remove this here for convenience.
We may broaden and rearrange this equation to obtain an expression for the USDC output Y in terms of the WETH input X: [dY = (dX*Y)/x+dX]. Bingo! Now we can determine the precise output we should anticipate for a specific input amount in any Uniswap V2 pool!

body of water

In short, we can say that the general definition of arbitrage is "buy low, sell high."
Here, we want to identify Pool A that will let us purchase WETH for less money than Pool B will let us sell it for, so we buy from Pool A and sell into Pool B. If Pool A has reserves (X, Y) and Pool B has reserves (X, Y), we can compute how many tokens we acquire by swapping X into Pool A and receiving Y in Pool B, and if we get more tokens than we started with, then we have an arbitrage.

Now that we have the data, we can calculate what will happen when we swap X for Y in the first swap and X for Y in the second swap.
After first swap, [dY = (dX*Y)/x+dX].
After second swap, [dX+e = [dY*(X~)]/[(Y~)+dY].
Our goal is to increase e>0. In this case, e stands for the difference between input dX and the results of applying both swaps. If we can maximise e > 0, we have discovered a profitable arbitrage. As a result, we may simplify the second equation by substituting our expression for Y in terms of X, giving us an equation that only considers X:

Image

Let's take an example of a bot twitter user Jake XBT has built. The backlog of pending transactions awaiting acceptance onto the chain is known as the mempool, and his/her bot is connected directly to it. The bot would listen to rumours about recently submitted transactions and wait till one that presented an opportunity came. Once an opportunity was found, it would determine the best method to capitalise on it, combine these actions into a single transaction, and submit this transaction directly to the mempool for inclusion in the same block as the opportunity transaction. The bot was searching especially for Uniswap-like arbitrages that it could execute following the opportunity trades to benefit from these liquidity discrepancies between pools.

An arbitrage bot's duties include maximising the arbitrage's profitability, which in our equations is similar to maximising e. Fortunately, the final equation always has a single optimal solution whenever A > B, and there is no arbitrage when A B. And how does a bot profit from an opportunity? The bot has to acquire dX of the initial token to do these swaps, and it can get these by taking out a flash loan from Aave, for example, once we've figured out how to perform swaps to maximise profit. The bot must then combine the flash loan with both swaps and the transfer of flash loan cash back to the Aave pool into a single transaction. A custom smart contract that has already been pre-deployed onto the chain can be used for this. This transaction is created, signed, and then sent to the mempool for mining.

Image

However, we want to make sure that our transaction is mined first because there are other other bots looking for the same possibilities. This indicates that our arbitrage transaction ought to come after the original transaction in the same block and be processed right away. This is referred to as a "back run," and in this case, the order of the transactions in the mined block would resemble the one shown in the image. The bot must first comprehend the gas price that the original transaction offered before precisely replicating these parameters in the arbitrage transaction in order to achieve a back run. Finally, the transaction is validated by the bot after being signed and transmitted directly to the mempool.
In other words, the better the bot algorithm, the better the result with MEV, as it gets first dibs on pushing its transactions before other bots do the same.

Concerns with MEVs

Should you start looking into MEVs? It is an interesting area for sure but do keep in mind that the people who introduced the MEV concepts are some of the smartest people in the world. DeFi is like a wild west now. Many black hat hackers have noticed how MEVs trade and started to set traps to trap MEV bots to lure them pretending there are profits to be made and then bleed them dry. Here is an example how such an MEV lure works. There are more ways to dupe bots, but only expertly skilled bot makers can deploy such strategical bots.

480px-Roberto.jpg

MEVs can often trap unassuming traders. Consider the "sandwich" trap, a tactic used by MEV bots on automated market makers like Uniswap. An MEV bot uses little price variations when an order is placed to its advantage in this procedure. These bots makes huge trade artificially inflating the price of a token in a pool, wait for an unsuspecting buyer, dumps the tokens on to this buyer thereby making huge profits. You only have a small number of potential defences against bots as a single trader or DeFi user, most of which include minimising their potential profit by comprehending how they operate and structuring deals that are less likely to be targeted.

MEV bots make up significant part of DeFi trades. Data from Flashbots shows that in only the last 30 days, a total of $7.4 million was secured on the Ethereum blockchain through MEV. Ethereum offers by far the most potential for MEV due to its rather lengthy block times, high fees, and frequent congestion, but it also features a highly extensive and well-liked DeFi ecosystem. However, other platforms like Compound and Aave also experience liquidation front-runners, where liquidators can use front-running to find and execute liquidation transactions, securing the liquidator fee. The vast majority of MEV events are related to Uniswap V2 and V3, where miners frequently use sandwich attacks to steal profits from high volume traders (or a discount on the collateral).

 

Reference

 

How do you rate this article?

42


Mishal Alexander
Mishal Alexander

Crypto Enthusiast | Budding Blockchain Developer |


What You Should Know
What You Should Know

Welcome to my blog, people.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.