Demystifying Arbitrageurs’ profits, LVR, and sandwich attacks: batch trading as an AMM design response
Editor’s Note:
In today’s AMMs, the difference between a trade’s average price and the AMM’s post-trade price creates opportunities for arbitrageurs to profit through MEV attacks. FM-AMM raises an insightful question: if these two prices were aligned, could this eliminate such arbitrage opportunities? This article breaks down FM-AMM’s design, exploring how its unique batching mechanism and double price impact can make AMMs MEV-resistant.
by Yuren, my friend from Perpetual Protocol 😁
0. tl;dr
1. Intro
2. MEV on a CFAMM
3. Deriving FM-AMM Using a Constant Function
1. Double Price Impact
2. Similarity to CFAMM with Added Liquidity After Each Trade
3. An Auto-rebalancing two-asset portfolio with customisable weights
4. The Necessity for Batching Trades
5. Experiment Result
1. Comparison with Uniswap V3 at the Same Fee Tier
2. The Optimal Fee Tier
6. Two Minor Limitations
7. Prospect
8. Conclusion
The announcement of CoW AMM and its implementation on Balancer has been out for over six months now, yet there’s been little discussion about its innovation, theory, and design.
Initially, I thought the lack of interest might stem from a simple truth: perhaps there wasn’t much innovation here, and it’s just another Constant Function AMM (CFAMM) implementation.
Apparently, I was wrong, which brings us to this article. 🙈
While researching into the topic and already drafting my thoughts on CoW AMM, I finally explored the key paper frequently cited by the CoW team: Arbitrageurs’ profits, LVR, and sandwich attacks: batch trading as an AMM design response, by Andrea Canidio & Robin Fritsch.
In this paper, I discovered that the Function Maximising AMM does point out a clear mathematical solution to MEV. So, let’s explore the various attributes of this intriguing new concept in DeFi: Function Maximising AMM, FM-AMM.
Btw, I love listening to music while working on math; anyone else?
The Function Maximising AMM, as its name suggests, aims to maximise a function, which, in this paper, is defined as the liquidity reserves of AMMs.
For example, if an FM-AMM is constructed initially with a constant function, similar to a Constant Function AMM (CFAMM), then the product of the two liquidity reserves, X * Y = K, becomes its target for maximisation.
This paper introduces a novel AMM mechanism designed to be MEV-resistant. As we will explore further, the curve-shifting behaviour inherent in this design is what inspired the name FM-AMM.
Before we officially get started, there are two important pieces of background knowledge you’ll need:
Now, let’s go straight into the most crucial question to help understand this paper: how do we define MEV mathematically, specifically on a CFAMM curve?
The paper defines MEV on a CFAMM as the price difference between:
To break it down, assume we trading x’ on a CFAMM X * Y = K, where current price of X is P0 = Y / X. Here’s how we analyse the prices:
Calculations
We can observe a clear price difference: the average price of a trade (P1) is lower than the new AMM pool price post-trade (P2), which creates an opportunity for MEV.
If arbitrageurs have access to more liquid exchanges, possibly centralised, where they can sell tokens at the market price with a small slippage, they can buy tokens from the AMM at the lower average price and profit from the difference.
For example, consider a scenario where the market price shifts from 1000 USDC/ETH to 2777. If arbitrageurs buy 0.4 ETH at an average price of 1666 USDC on the AMM and manage to sell it at 2777 USDC in the more liquid markets, they can secure a profit of 0.4 * (2777 - 1666) = 444.4 USDC.
For those familiar with equations, their presence might hint that there’s something interesting to uncover! 🥳
If MEV is defined as the price difference, then wouldn’t making the two prices the same effectively solve MEV?
In the paper, the key property of making FM-AMM MEV-resistant is “clearing-price consistency,” which means the average trade price matches the marginal price. Let’s break down the conditions needed to achieve this:
P1 = (Y + P1 * x') / (X − x') = P2
-> (Y + P1 * x') = P1 * (X − x')
-> Y + P1 * x' = P1 * X - P1 * x'
-> Y = P1 * (X - 2 * x')
-> P1 = Y / (X - 2 * x') = P2
Voilà! When the average trade price is Y / (X − 2 * x’), it also becomes the marginal price post-trade.
With this first condition of FM-AMM established, we can draw several interesting observations from it!
If we compare the average price of an FM-AMM Y / (X − 2 * x’) to a typical CFAMM Y / (X - x'), we’ll notice the multiplier of x’ doubles. This indicates that the price impact of an FM-AMM is twice that of a CFAMM.
Since the average price of FM-AMM is not the same as that of CFAMM, the post-trade constants of them cannot be the same either:
Y' * X' = (Y + P1 * x') * (X − x')
= (Y + Y * x' / (X − 2 * x')) * (X − x')
= Y * ( 1 + x' / (X − 2 * x')) * (X − x')
= Y * ((X − 2 * x' + x') / (X − 2 * x')) * (X − x')
= (Y * ((X - x') / (X − 2 * x'))) * (X − x') = Y' * X'
While X’, the new reserve of X, remains unchanged (X - x’), Y’ becomes Y * ((X - x’) / (X − 2 * x’)). Since the numerator of Y (X - x’) is greater than the denominator (X − 2 * x’), the multiplier is greater than 1, making Y’ greater than Y.
This implies that the curve shifts upward, increasing the Y reserve. Let’s visualise this with the example we made above, where x’ = 0.4, X = 1, Y = 1000:
By following the property of clearing-price consistency, the blue FM-AMM curve shifts upwards from the red CFAMM curve X * Y = 1000 to X * Y = 1800 = 1000 * 0.6 / 0.2 * 0.6.
Since shifting the curve upward or rightward indicates an increase in liquidity, this behaviour underscores that FM-AMM is fundamentally an optimisation/maximisation problem.¹
However, if you’re familiar with Uniswap V1 and V2², you might already be thinking: don’t the curves of Uniswap V1 and V2 also shift after each trade due to fees being added to the pool?
You’re right! FM-AMM is an approximation to CFAMM with extra liquidity added after each trade. In mathematics, an approximation means the statement isn’t exact but is close enough to be useful under certain conditions.
In other words, FM-AMM behaves similarly to CFAMM in that both experience curve shifts post-trade due to added liquidity.
¹Note that CoW AMM’s implementation emphasises maximisation by accepting batching solutions with the max surplus/curve shift, rather than enforcing that average price == marginal price.
²Uniswap V3’s range fragmentation prevents adding fees back to the pool after each trade.
If we look at the value of the new X reserve X’ = (X - x’) multiplied by its price P1, we find that both reserves have equal values:
P1 (= P2) * (X - x')
= Y / (X − 2 * x') * (X - x')
= Y * (X - x') / (X − 2 * x') = Y'
Before jumping into the obvious conclusion, let’s connect this observation to the post-trade constant of FM-AMM: (Y + P1 * x')(X − x'). Notice how the values of both assets remain consistent and contribute equally to the constant — this is because each reserve has an equal exponent in the equation:
(Y + P1 * x')(X − x') = (Y + P1 * x')^1 * (X − x')^1
From this, we can conclude that FM-AMM can represent any strategy with arbitrary weights between two assets (α, 1-α) by implementing the corresponding weighted function, (Y + P1 * x')^α * (X − x')^(1-α), which naturally covers the case where both assets share equal weight and value.
Note that this attribute is also the same as CFAMM. If we multiply the new reserve X (X - x’) by the post-trade price P2 (Y + P1 * x') / (X − x'), then it becomes (Y + P1 * x') / (X − x') * (X − x') = Y’, which is still the new reserve of Y.
This section may be the most challenging for math normies like me, but we’re almost there — this is the last bit involving math. 🥳
To understand why batching is necessary for FM-AMM, let’s also introduce these common concepts in DeFi: path dependency & independency.
In the context of AMMs, path dependency/independency means that the final outcome or state depends/does not depend on the sequence of trades.
A CFAMM is path-independent, as the final price the AMM reaches does not depend on the order of trades. For instance, in our scenario where x’ = 0.4, X = 1, Y = 1000, the final marginal price will always reach 2777, whether the trade is executed in one step or split into multiple smaller transactions.
Conversely, an FM-AMM is path-dependent, which requires slightly more intense mathematical analysis.
The above derivation shows that if trades are split into many smaller ones, an FM-AMM will degrade to a CFAMM, effectively demonstrating the need for batching trades into one tx, which is the foundation of CoW AMM.
To deepen this understanding, let’s again apply some numbers to observe batching’s effect on the final product of two reserves, calculated as (Y * ((X - x’) / (X − 2 * x’))) * (X − x’). We’ll use the same setup with X = 1, Y = 1000, and compare one trade of x’ = 0.4 with splitting it into two trades of x’ = 0.2 each:
The effect of batching is clear and significant!
*Y reserve after the first trade is (1000 * ((1 - 0.2) / (1 − 2 * 0.2))) = 1333
While the paper delves deeply into the impact of fees on FM-AMM, I’ll focus directly on the experiment results:
The paper simulated passive liquidity provision — without active liquidity management — on FM-AMMs using the same fee tiers as their corresponding Uniswap V3 pools. The results indicate that providing liquidity on FM-AMMs generally outperformed in highly traded pairs but underperformed in less-traded pairs.
In the paper, 6 pools were analysed: 4 of the 0-fee pools outperformed, while the remaining two were led by the 0.1% fee tier. As for the least-performing pools, 0-fee, 0.1%, and 0.3% tiers each accounted for 2 instances. It’s important to note that 6 samples do not provide sufficient statistical significance, so I am curious whether the authors have conducted any further analysis, as that information would be valuable to know!
There’re two minor limitations of FM-AMM that come to my mind:
Though perhaps more research needs to be done to optimise the design of FM-AMM, such as the optimal fee tier, there are some exciting and foreseeable changes that FM-AMM could possibly bring:
Can you think of more interesting and exciting ideas? Feel free to share!
Expanding all the math is exhausting 🫠🫠🫠
I hope this article helps all the patient readers better understand the intricate details of the mysterious and mostly under-the-radar FM-AMM. I was pleasantly surprised by the solid mathematical foundation of FM-AMM, which clearly paves the way toward MEV-resistant AMMs, and that inspired me to write this piece!
As always, please help point out any errors you find and feel free to leave your comments and thoughts below. I seriously need to take a nap and veg out now 🥱 🥦 🛋️
Thanks to Andrea, Yuren and Anton for their review and feedback! 🙏🙏🙏
ELI5: What Is FM-AMM, The Theory Behind CoW AMM, and How Is It MEV-Resistant? was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
【免责声明】市场有风险,投资需谨慎。本文不构成投资建议,用户应考虑本文中的任何意见、观点或结论是否符合其特定状况。据此投资,责任自负。