ELI5: What Is FM-AMM, The Theory Behind CoW AMM, and How Is It MEV-Resistant?
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 😁
Outline
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
0. tl;dr
- FM-AMM solves MEV through clearing-price consistency, where the average price of a trade equals the marginal price.
- The constant of liquidity reserves is the function that FM-AMM aims to maximise.
- The major difference between FM-AMM & CFAMM is batching.
- FM-AMM’s price impact is double that of CFAMM.
- FM-AMM serves as an auto-rebalancing two-asset portfolio with customisable weights, similar to CFAMM.
- Experiments show that FM-AMM outperformed highly traded pairs and underperformed less-traded ones on average. While the 0-fee tier seems promising, further research is likely needed to determine the optimal fee tier for FM-AMM.
1. Intro
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?
2. FM-AMM and Its MEV-Resistance
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:
- CFAMM: A CFAMM facilitates trades that always happen on the curve
X × Y= K
, whereX
andY
represent the amounts of two different tokens, andK
is a constant. Read more: Uniswap V3 Features Explained in Depth#1. Uniswap & AMM recap. - MEV: Maximal Extractable Value is the profit that miners or validators can make by arranging transactions in a block to their advantage, such as placing their own transactions ahead of others to get better prices.
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:
- The average price of a trade, and
- The price to trade an arbitrarily small (almost zero) amount after the trade. This is defined as the “marginal price” in the paper
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:
Average price P1 for trading x’
Y * X = (Y + y')(X − x')
, wherey’
is the change in Y- Since
y’ = p1 * x'
,(Y + y')(X − x')
becomes(Y + P1 * x')(X − x')
- Now we have
Y * X = (Y + P1 * x')(X − x')
, solving for P1 is:P1 = Y / (X - x')
Marginal price P2 after trading x’
- After the trade, the new constant is
(Y + P1 * x')(X − x')
- Assuming trading a very small amount has negligible impact, the price P2 becomes:
P2 = (Y + P1 * x') / (X − x')
Example
- Let
X = 1
(1 ETH) Y = 1000
(1000 USDC)x’ = 0.4
(trade 0.4 ETH; trading 40% of the liquidity of a reserve is unwise in reality)
Calculations
P0 = Y / X = 1000 / 1 = 1000
, the price of 1 ETH is 1000 USDCP1 = Y / (X - x') = 1000 / (1 - 0.4) ~= 1666
P2 = (Y + P1 * x') / (X − x') = (1000 + 1666 * 0.4) / (0.6) = 2777
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.
3. Deriving FM-AMM Using a Constant Function
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!
1. Double Price Impact
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.
2. Similarity to CFAMM with Added Liquidity After Each Trade
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.
3. An Auto-rebalancing two-asset portfolio with customisable weights
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.
4. The Necessity for Batching Trades
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.
- Recall that the post-trade constant of
X’
andY’
isX − x’
andY * ((X - x’) / (X − 2 * x’))
, respectively. - If we divide the trade into
n
smaller parts such that∑(i=1~n)xi = x'
, the reserves for an FM-AMM will becomeX - ∑(i=1~n)xi
andY * ∏(i=1~n)( (X - ∑(j=1~n-1)xj) - xi) / (X - ∑(j=1~n-1)xj) - 2 * xi) )
, where ∑ is the sum of all the values specified in the range, and ∏ is the product of all values multiplied together. - Assume
xi = 1/n * x'
, meaning eachxi
is an equal1/n
portion ofx'
. This step effectively eliminates the summation variablei
onx'/n
:∑(i=1~n) x'/n
, which means the result is merelyx/n
multiplied by the number of iterations of the summationn
:x'/n * n = x'
. Thus, the reserve for X remains asX - x'
. - Let’s also apply
xi = x'/n
to the Y reserve to get rid ofi
&j
:Y * ∏(i=1~n)( (X - x'/n * (i-1)) - x'/n) / (X - x'/n * (i-1)) - 2 * x'/n) )
. Since(X - x'/n * (i-1)) - x'/n) = (X - i*x'/n)
and(X - x'/n * (i-1)) - 2 * x'/n) = (X - (i+1)*x'/n)
, the expression becomesY * ∏(i=1~n)((X - i*x'/n) / (X - (i+1)*x'/n))
- The next step, which may seem tricky, involves recognising how the product sequence behaves:
∏
represents the product of each value in the sequence. By expanding the product, we can see that the products cancel out each other:∏(i=1~n)((X - i*x'/n) / (X - (i+1)*x'/n)) = (X - 1*x'/n) / (X - 2*x'/n)) * (X - 2*x'/n) / (X - 3*x'/n)) * (X - 3*x'/n) / (X - 4*x'/n)) ... * (X - n*x'/n) / (X - (n+1)*x'/n))
, eventually leaving(X - 1*x'/n) / (X - (n+1)*x'/n))
only - Now, let’s imagine we split the trade into infinitely small parts. This process, denoted as
lim n→∞
, simulates the extreme outcome asn
approaches infinity:lim n→∞ Y * (X - 1*x'/n) / (X - (n+1)*x'/n))
. Sincen
appears in the denominator, asn→∞
,x'/n
approaches 0. Meanwhile, the term(n+1)/n
approaches 1. This ultimately simplifies the expression toY * X / (X - x')
, which matches the Y post-trade reserve of an CFAMM!
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:
- One trade (batching):
(1000 * ((1 - 0.4) / (1 − 2 * 0.4))) * (1 − 0.4) = 1800
- Tw trades (no batching). The first trade:
(1000 * ((1 - 0.2) / (1 − 2 * 0.2))) * (1 − 0.2) ~= 1066
, and the second trade:(1333 * ((0.8 - 0.2) / (0.8 − 2 * 0.2))) * (0.8 − 0.2) = 1199
*
The effect of batching is clear and significant!
*Y reserve after the first trade is (1000 * ((1 - 0.2) / (1 − 2 * 0.2))) = 1333
5. Experiment Result
While the paper delves deeply into the impact of fees on FM-AMM, I’ll focus directly on the experiment results:
1. Comparison with Uniswap V3 at the Same Fee Tier
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.
2. The Optimal Fee Tier
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!
6. Two Minor Limitations
There’re two minor limitations of FM-AMM that come to my mind:
- As noted earlier, Uniswap V3’s range fragmentation prevents adding fees back to the pool, making it unlikely to construct FM-AMM with Uniswap V3-like AMMs. However, given the ongoing discussion about the profitability of LPs on Uniswap V3, this isn’t necessarily a drawback 😹
- While the benefits of batching can be unstable, they still yield a positive sum. For example, if multiple trades of both directions net out, those trades can be settled P2P, which won’t require the AMM to adjust its position on the curve or maximise its liquidity reserve function — unless, fees from P2P settlements are still added to the pool.
7. Prospect
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:
- FM-AMM’s auto-rebalancing nature, its MEV-resistance and customisable weights, seem promising for financial products with the need for rebalancing, which has the potential to impact many aspects of finance, such as ETFs.
- If batching is essentially the major difference between FM-AMM and CFAMM, e.g. Uniswap V1 & V2, and UniswapX is already a liquidity aggregator… 🤔
- FM-AMM can be implemented for two weighted assets. What about multi-weighted-assets? Can FM-AMM be expanded into multi-asset constant function? Is this why CoW AMM is built on Balancer? 🤩
Can you think of more interesting and exciting ideas? Feel free to share!
8. Conclusion
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! 🙏🙏🙏