Constant Function Market Makers
CFMMs are the dominant design architecture of decentralized exchanges (DEXs). CFMMs are commonly implemented as a standalone piece of software that acts as a trusted third party. Trades are automatically facilitated against the reserves of tokens held in a CFMM. Any actor can add or remove liquidity to the reserves during the lifetime of a pool to participate in making the market. Actors are incentivized to add liquidity to the reserves because they earn swap fees denoted by gamma laying within the interval zero (exclusive) to one (inclusive). The available actions to interact with a CFMM are determined by its trading function. Formally, a CFMM is an -token pool of positive real numbers and a trading function such that
Given any , the value the trading function evaluates to is called the invariant. The quantity of reserves of an of an -asset pool is denoted where for every where specifies a specific asset. Reserves change when trades are executed. For example in the ETH/USDC liquidity pool ETH, and USDC, denotes the quantity of ETH in the pool and denotes the quantity of USDC respectfully.
Recall that a trading function is a function of asset reserves. The trading function determines the exchange rate of assets in a CFMM. Since trades change the reserves, and the exchange rate is in terms of the trading function, trades can also impact the price. The price denotes the value of one asset in terms of another. Because you can’t ensure that your trade is executed at an exact price and that there may be some trade before you in the current block, you must allow some price tolerance such that as long as that price deviates within this bound, your trade will still go through. This tolerance is called slippage and is proportional to the price impact of your trade. Given a CFMM the price vector is
and the reported price of is
Recall that the gradient is a vector of derivatives. The price is the derivative of the trading function with respect to over the derivative of the trading function with respect to . The exchange rate graphically corresponds to the tangent line at a point on the trading function. The slope of this tangent line is the additive inverse of the exchange rate and can be seen for two different measures of reserves in the graph below.
Price Impact and Arbitrage
Recall that all trades on a CFMM have a price impact, thought of as the change in price for a given trading function . If there is a high demand for the reserves of in the CFMM will diminish, and the trading function will price this token higher. How can we ensure that the price is aligned with the greater market? Arbitrage is when an actor is aware of a price discrepancy between marketplaces and executes a series of trades to exploit this discrepancy. All markets for a given asset pair tend to converge on an expected price due to arbitrage. To perform this arbitrage optimally (i.e., maximizing profit), the arbitrageur needs to know the exact size of their arbitrage trade that will align the price. A given price impact implies a distinct trade size on CFMMs and thus an optimal arbitrage trade quantity exists. Consider a two-asset liquidity pool with assets 1 and 2. The geometric representation of price impact can be seen below as the difference in slope of the tangent lines where represents the number of assets tendered to the liquidity pool, represents the number of assets received from the liquidity pool, and is the swap fee.
To calculate the required trades for arbitrage, arbitrageurs seek to answer the question: How large does trade have to be to change the price by a certain amount? This question can be thought of as: What is the optimal size of the arbitrage trade such that after the trade, the price is aligned with an external reference market thus, maximizing revenue from the arbitrage opportunity? This was shown for UniswapV2's trading function in the appendix of the Analysis of Uniswap markets paper. In the remainder of this article, we will show this for the trading function of Primitive's RMM-01 trading function.
RMM-01 is a constant function automated market maker (CFMM) deployed by Primitive. RMM-01 approximates the payoff of a covered call to liquidity providers through the aggregation of swap fees. To do this, the curvature of the level sets of the trading function used by the RMM-01 trading function changes over time. RMM-01 follows Black-Scholes option pricing, which requires setting , , and parameters at pool creation. For an arbitrageur to maximize their profits and achieve a price equilibrium between two markets, they need to know the exact size of their arbitrage trade corresponding to the optimal arbitrage. Here we refer to this as . To understand the derivation of the optimal arbitrage for an RMM-01 pool, it is necessary to first examine the RMM-01 trading function. Unlike the static trading function of Uniswap, RMM-01 is a dynamic curve and thus changes over time. The trading function is
Where is the zero-mean unit-variance Gaussian cumulative distribution function (CDF), is the reserve of the risky asset (ETH), is the reserves of numeraire (USDC), and σ and τ here are the volatility and time to expiry. All the pools in RMM-01 have these parameters and, all the results here can be calculated from onchain information. RMM-01 is normalized per-liquidity provider tokens(LPTs) token, and all solutions here are per LPT. This is not a problem in practice because the quantity of LPTs is also kept onchain and is publicly available.
A key property of the trading function is that it is dynamic with respect to and . Notice at expiry, when , the trading function has a constant price at the strike price , which is graphically interpreted as a straight line with no curvature. We can see the curvature of the RMM-01 trading function in the graph below, where is the strike price.
Optimal Arbitrage Problem
Consider an RMM-01 ETH-USDC pool and an external reference market where the price of ETH is where is the reported price of RMM-01. What is the quantity of ETH we need to tender to RMM-01 for it to match the reference price? Or in other words, what is the precise size of our arbitrage trade that maximizes our profit? We will first calculate the exchange rate of USDC/ETH on RMM-01 using the methods mentioned in the section on the exchange rate. Then we will calculate a new price in terms of some tendered quantity of assets and then invert that function to get the tendered assets in terms of some market price.
Recall that for CFMMs the price of asset in terms of asset is where is the gradient vector of the trading function. Taking the derivative of the trading function with respect to gives us
and the derivative of the trading function with respect to is and thus the price of ETH with respect to USDC on RMM-01 is from above.
Let and denote the price impacts due to tendering and , respectively. Then if is tendered the price impact is
and similarly, if is tendered then the price impact is
Solving for inverse and substituting the desired price change gives the following results. If you want to tender ETH, the optimal size of the arbitrage opportunity is
Interestingly, RMM-01 is not symmetric. This means that if you want to tender USDC, the derivation is different from the solution above and is
Note that these solutions are per LPT. So say that the market price of an external market for ETH is $2000, and the primitive market price is $1800. Then and with known values of , , and and reserves and , both deltas can be computed trivially and then multiplied by the quantity of LPT tokens.
In this piece, we briefly summarized CFMMs, introduced the dynamic trading function of RMM-01, and presented the optimal arbitrage derivation per LPT value. Searchers are free to compete to capture these arbitrage opportunities. Since the RMM-01 trading function is dynamic concerning στ it is a strong assumption that these arbitrage opportunities will always arise. Thus there will continue to be profitable arbitrage in RMM-01 liquidity pools. The arbitrage not only aligns the price with external marketplaces but also contributes to the payoff of the liquidity providers via the swap fees.