An analysis of Uniswap markets

TLDR:

  • Constant Product Markets (CPMs) have a number of helpful properties (such as optimal arbitrage and bounded deviation from reference price) that can be represented by convex equations.
  • The researchers’ analytical modeling (agent-based simulation) reinforces these mathematical properties, implying that under certain conditions CPMs can be robust price oracles.
  • Constant Mean Markets share a majority of these properties of CPMs.

Core Research Question

  • Are Constant Product Markets stable and can the mathematical properties of these markets be represented with convex equations?

Citation

Background

  • Constant Product Markets (CPMs) are markets where the product of the reserves of the two assets in a liquidity pool remains relatively constant.
  • Constant Mean Markets (CMMs) are a generalization of CPMs where the geometric mean of the reserves is kept constant.
  • Automated Market Makers (AMMs) (eg. Uniswap) are generally constant product markets that implement pools of asset pairs with which users can trade or add/remove liquidity.
  • A Liquidity Provider (LPs) is a user that deposits proportional amounts of two tokens into a pool of liquidity in an AMM.
  • An Arbitrage Opportunity is defined here as a situation where trading some amount of a loaned asset for some amount of a second asset and then returning the original loaned amount will yield a net positive result for the trader.
  • The Optimal Arbitrage problem is the problem of finding a mathematical solution for maximal profit from these arbitrage opportunities.
  • Trading Risk Functions are cost functions which could cause a trader to fail to close an arbitrage opportunity, and are composed of multiple factors: noisy information, front-running, trading delays, etc.
  • Cost of Manipulation represents risk, transaction, and other costs incurred by an actor seeking to manipulate prices on the system.
  • Convex Optimization Problems are problems where the solutions are subject to the constraints of convex functions. In particular, there is one globally optimal solution and if a local optimum is found, it is also the global optimum. This means solutions to these problems are generally simple or easy to implement.

Summary

  • For Uniswap and CPMs more broadly, the researchers present formulas for optimal arbitrage and bounds on deviation from reference market price and on manipulation costs using simple mathematical equations.
  • They also present formulas showing how many of these properties generalize to CMMs.
  • They run a multi-agent simulation of users interacting with the Uniswap contract while varying market parameters to show that the system can be stable under a variety of conditions and that, in practice, the properties presented should hold.
  • The researchers conclude that these properties do hold, can be represented by convex formulas, and, assuming some liquidity conditions, Uniswap can be a robust price oracle for smart contracts.

Method

  • This research paper outlines a study of Constant Product Markets (Uniswap) and Constant Mean Markets using two approaches: 1) formal mathematical analysis and 2) an analytical simulation to confirm the results.
  • Formal Analysis
    • Optimal Arbitrage
      • The optimal arbitrage problem in the infinitely liquid market case is not obviously convex, but the researchers re-wrote it such that it is more obviously convex, and then used that to derive an analytical form for optimal trade amounts.
    • No-Arbitrage Conditions
      • The researchers differentiated the constant-product market formula and applied basic algebra to discover bounds on the deviation of Uniswap market price versus external reference price.
  • Risk Models
    • The researchers wrote out the optimal arbitrage formula and then added in an example risk function based on marginal price after a trade, which happens to be convex in order to show how risk functions can be convex.
    • Other Properties of CPMs
      • Using simple transformations of the aforementioned formulas as well as some basic calculus and algebra, the researchers demonstrate a few other mathematical properties of CPMs via formal analysis.
    • Extensions to Constant Mean Markets
      • The researchers relax equality constraints and perform algebraic substitutions in a number of the Constant Product Market formulas to produce a new set that apply to Constant Mean Markets, in order to show which properties carry over to CMMs.
  • Simulation
    • To verify the results of these mathematical formulas, the team constructed an agent-based simulation using the Gauntlet DSL on a simulated ETH blockchain using Python bindings.
    • The simulation was run for a predetermined number of time steps. At each step, the environment state variables were updated. The researchers also included policies on when new agents were added to the environment.
    • They then calculated the utility of actions for those agents to determine what actions they would execute (i.e. their transactions).
    • They defined three classes of agents used in the simulation: 1) arbitrageurs, 2) Liquidity Providers, and 3) traders.
    • Arbitrageurs in this model try to maximize their profit by trading between Uniswap and an external reference market using the modified marginal pricing function with ‘cost of risk’ taken into account.

Results

  • Formal Results
    • Optimal Arbitrage problems for CPMs are convex problems.
    • Assuming No-Arbitrage, Uniswap prices should deviate by at most a factor of (1 - transaction fee), suggesting that the larger the trade fees, the larger the gap between the reference price and the Uniswap market price.
    • Risk Models
      • There are many factors which could cause an agent to fail to close an arbitrage opportunity, including noisy information, front-running, and trading delays.
      • An agent may not always want to perform a trade for the exact solution the researchers identify for the optimal arbitrage problem, and this discrepancy can be represented by a ‘cost of risk’ function (which is convex and non-decreasing)
    • Other Properties of CPMs:
      1. Increasing product constant: for every trade the product constant is non-decreasing (and if transaction fee is > 0 then it is strictly increasing)
      2. Splitting trades is more expensive: When transaction fee > 0, it will be more profitable to trade between two tokens in a single trade than it is in two subsequent trades that add up to the same amount. This implies that, when a reference market is infinitely liquid, an arbitrageur cannot do better by visualizing several trading steps ahead than by simply solving the Optimal Arbitrage Problem and executing that trade.
      3. No depletion property: It is impossible to completely deplete the reserves of two tokens in Uniswap by just trading those tokens. The researchers show that the total reserve is always bounded from below by applying the arithmetic-geometric mean inequality.
      4. Increasing liquidity with increasing reserves: the more reserves there are, the less any one trade will cost. The marginal cost of a Uniswap trade is strictly decreasing as reserves are increasing, assuming the Uniswap market price stays constant.
      5. Cost of manipulations: In the no-fee case with an infinitely liquid reference market, there is a lower bound to the cost of manipulating the price of a CPM for each time period (e.g. the time for a block to be mined). This extends to the case with fees as well. Further, this cost of manipulation scales linearly with reserves, which is another reason why large reserved pools are important.
      6. LP Returns: In the no fee case, the researchers produce a formula for computing the portfolio value of the Uniswap contract. Since this return depends only on the relative ratio of the sizes of the reserves of the two tokens, the result holds even when LPs add or remove tokens from the reserves, although in practice, the price is likely to be more unstable if reserves are smaller.
    • Extension to Constant Mean Markets
      • Constant Mean Markets have their own optimal arbitrage problem solution.
      • Nearly all the conditions and properties of Constant Product Markets also hold for Constant Mean Markets in a similar form.
    • However, it is not clear that there exists a closed-form solution for the no-arbitrage conditions outlined for CPMs that also applies to CMMs.
  • Simulation Results
    • Agent-Based Simulation of Uniswap Markets
      • The researchers’ results suggest the theoretical results above hold in practice under a wide variety of market conditions.
      • Arbitrage Bounds
        • The researchers show that even in the presence of outside noise (e.g. random noise) or changes in reserves due to rational LPs, the marginal Uniswap price stays within predicted no-arbitrage bounds.
    • Initial LP Returns
      • The researchers recorded the performance of initial LPs relative to a portfolio comprised of coins A and B, and found that in almost all simulations, initial LPs end up having negative utility over most varying conditions. They only have positive expected value when the mean market return is close to zero.

Discussion & Key Takeaways

  • Constant Product Markets have properties that can be solved with convex formulas such as:
    • a calculable optimal arbitrage opportunity
    • bounds on difference between internal and external asset price
    • convex trade risk formulas, increasing product constant
    • splitting trades is less efficient
    • reserves are largely non-depletive
    • they have lower-bounded costs of manipulation
    • returns can be theoretically computed.
  • The researchers conclude that their analytical modeling (agent-based simulations) confirms these theoretical properties, implying that CPMs (under certain conditions) are robust price oracles.

Implications & Follow-ups

  • Many of the properties applicable to CPMs are transferable to Constant Mean Markets via generalization. The researcher identified this as an interesting avenue of exploration for future work.

Applicability

  • CPMs are implemented in a number of live Decentralized Finance applications, most notably Uniswap.
  • Other products like Celo are mentioned by the researchers as they use the CPM as a DEX oracle.
  • The researchers later built on this work in future papers, including “Why stake when you can borrow?”
4 Likes

This is an exciting analysis due to the nature of the researchers putting mathematic proofs to the theoretical framework that liquidity pools can be resistant to being drained as long as the trading pairs are constrained. Is there any indication that this understanding will be used to inform restrictions on trading trinaries under certain market conditions, or do you know if is this still purely in the theoretical exploration stage of LPs between restricted trading pairs?

5 Likes

As far as I understand, this is largely theoretical as no material trading restrictions have been applied to protocols like Uniswap, but the math is not farfetched and they did an agent-based simulation to confirm, so I’m fairly confident the conclusion holds.

4 Likes

That is fantastic! I was honestly hoping this was the answer, because if that is the case then this truly represents a breakthrough in how to approach liquidity markets to protect value long-term. I know a lot of people had suspected this framework would be the best way to approach establishing liquidity pools, but it’s great that someone took the time to do the math!

3 Likes

So reading through this I take away two main things:

  1. Convex equations allow for DEXs to operate using relatively simple math, and by utilizing these computationally efficient equations they can increase the security of their liquidity pools by ensuring the LPs cannot be fully depleted with just one trading pair.
  2. Prices may be highly impacted by small liquidity pools/reserves when CPMs are used.

The second takeaway is the one I want to focus on. The authors state in the paper:

…the price is likely to be less stable in the case of smaller reserves (pg8)

I recently posted another research summary here discussing AMMs where the author made a similar statement regarding constant product markets when reserves are low:

…the token price could range from 0 to ∞ in the constant product cost model, when the price for one token is close to infinity, any meaningful trade in the market is infeasible.

Given both researchers, and many others, have concluded constant product makers may be sensitive to low reserves (when one asset’s reserves approach 0 the price should increase towards ∞ given the CPM) has Uniswap, or other DEXs, put in mechanisms to ensure or encourage relatively large reserves?

4 Likes

I think this is a correct and important takeaway. Reserve sizes are important. Liquidity improves as reserve sizes improve. As liquidity improves, price impact is reduced. As price impact is reduced, many of these security assumptions of a CPM as a price oracle are improved.

4 Likes

This paper was written in the early days of Uniswap, when there was a mere $8mm in weekly volume. Could you help me put this into context? Why was this such a pivotal moment / discovery for the DeFi community? What did this open the doors to?

3 Likes

I think it’s helpful to contextualize this paper. This was released in the early days of Uniswap but 1) there were also a lot of questions at the time about security and efficiency of AMM-based DEX’s and 2) this paper has allowed for future analysis that hinges on the idea that a number of CFMM equations are convex (e.g. Research Summary - Why Stake When You Can Borrow?)

The idea of using convex optimization problems to approach AMMs and curvature has allowed for a deeper understanding of the nature of CFMMs and GMMMs. These types implementations have proliferated in DeFi since the time of this paper ( Uniswap, Balancer, Curve, etc.).

2 Likes