Generalizing knowledge on DEXs with AMMs — Part I
This is the first part in a two-part series about our paper. Part II can be found here.
With the heated discussion on Uniswap v3, the interest in Decentralized Exchanges (DEXes) with Automated Market Making (AMM) protocols is again reaching high levels of interest. At the time of writing, AMM-based DEXes have an aggregate value locked of over 15 billion USD.
Those of you who are a bit late in the game might still wonder:
- What is an AMM?
- Who are the key actors of an AMM?
- What are the benefits and costs of participating in an AMM-based exchange?
- What are the major AMM protocols and how do they differ?
My paper on DEXes with AMMs — a joint work with Jiahua Xu and Nazariy Vavryk UCL Centre for Blockchain Technologies, and Krzysztof Paruch from the Vienna University of Economics and Business, aims to answer all those questions. We present a generalized framework of these AMMs. Let’s have a review of the existing, time-tested protocols in this summary piece.
To read more technical and in-depth explanations of the framework, please read the paper. Any feedback is welcome and much appreciated.
The Basics
Different from order-book based exchanges where the market price of an asset is determined by the last matched buy and sell orders, each AMM uses a so-called conservation function that determines asset price algorithmically, by only allowing the exchange rates to move along predefined trajectories. Liquidity in the market is provided by so-called ‘liquidity providers’ who supply funds in a ‘liquidity pool’, a big pile of assets that users can trade against.
Core Actors
In a typical AMM system, there are 3 core actors:
- A liquidity pool creator deploys a new smart contract that acts as a liquidity pool with some initial supply of assets. The process of pool creation and setting the pool hyperparameters (weights, interpolating factor, initial supply…) can be fully decentralized (as is the case for Uniswap) or can be restricted to whitelisted addresses (as is the case for Curve).
- Liquidity providers can subsequently increase the reserve of the pool by adding more of the assets that are contained in the pool. In turn, they receive shares proportionate to their liquidity contribution as a fraction of the entire pool. The act of liquidity provision or removal updates the value of the conservation function invariant(s).
- Traders submit an exchange order to a liquidity pool by specifying which asset to spend — the input asset — and which to be received — the output asset. A trader can choose to specify the quantity of either the input asset or the output asset, and the smart contract will automatically calculate the exchange rate based on the conservation function, and execute the exchange order accordingly. Exchange users must pay swap fees to liquidity providers for using their pools.
Assets
Multiple asset types are used in AMM protocols, facilitating the functionality and allowing for governance:
- Risk & Base assets are the tokens within a certain liquidity pool . Each pool contains at least one risk asset, which is the main asset type the protocol was designed for: to provide liquidity in these assets, to facilitate exchange between them and to allow liquidity providers to earn rewards in return for their contribution. Base assets are used in some protocol to create a trading pair with a risk asset. For example, in Bancor every risk asset is paired with BNT and in Uniswap v1, ETH was the base asset for every pool.
- LP tokens (or ‘LP shares’) represent ownership in the portfolio of assets within a pool, and are distributed to liquidity providers. Shareholders earn fees whenever a trade occurs. Shares can be redeemed in exchange for the liquidity initially provided.
Implicit costs
Some implicit costs are native to the use of traditional AMM structures:
- Slippage is defined as the difference between the spot price and the realized price of a trade and is caused by the price curve design feature of an AMM that dictates asset prices. Instead of matching buy and sell orders, a conservation function determines exchange rates with a continuous curve. Therefore, a trade on an AMM-based DEX will always encounter some degree of slippage, depending on the exact design of the conservation function.
- Impermanent loss is the difference between holding assets in an AMM and holding them in your wallet, which occurs when the price of tokens inside an AMM diverge in any direction. As arbitrage opportunities pop up, profit extracted by the arbitrageurs is taken from the pockets of liquidity providers. The loss is ‘impermanent’ because as asset price moves back and forth, the depreciation of the pool value disappears and reappears all the time.
Mechanisms
Overall, the functionality of an AMM can be generalized formally by a set of very few mechanisms. An overview is shown in Figure 1. A generalized mathematical framework of the described mechanisms, such as the conservation function, swap amount function, slippage function and divergence loss function can be found in the paper.
Comparison
We compare 4 of the most representative AMMs currently available: Uniswap v2, Balancer v1, Curve and Dodo. Our paper includes general mathematical notations used in the pool mechanism formulations. This ensures that any kind of discussed AMM structures is denoted with the same symbols, creating a general framework.
Uniswap v2
The Uniswap v2 protocol prescribes that a liquidity pool always consists of two assets and assumes that the reserves of those two assets have equal value. Given this equal value assumption encoded in the pool smart contract, the implied spot price of assets in a liquidity pool can be derived based on the ratio between their reserve quantities. This results of a constant-product invariant function.
Uniswap represents the most rudimentary form of the AMM protocols, but remains widely popular, with over 50% market share at the time of writing, in terms of 30-day trading volume.
Balancer v1
The Balancer protocol allows each liquidity pool to have more than two assets. Each asset reserve rₖ is assigned with a weight wₖ at pool creation, where ∑wₖ=1. Weights are pool hyperparameters, and do not change with either liquidity provision/removal or asset swap. The weight of an asset reserve represents the value of the reserve as a fraction of the pool value.
Balancer can be deemed a generalization of Uniswap v2; the latter is a special case of the former with w₁ = w₂ = 1/2.
Curve
In Curve, a liquidity pool consists of two or more assets with a common peg, for example {wBTC, renBTC} or {USDC, USDT, DAI}. This ensures that asset price changes relative to the other asset(s) in the pool is minimal, minimizing divergence loss.
As assets from the same pool are connected to the same peg by design, the ideal exchange rate between them should always equal 1. Theoretically, this could be achieved by a constant-sum invariant. Nevertheless, Curve seeks to allow an exchange rate to deviate from 1, in order to reflect the supply-demand dynamic, while simultaneously keeping the slippage low.
Curve achieves this by interpolating between two invariants, constant sum and constant product, with hyperparameter 𝒜 as the interpolating factor. When 𝒜 → 0, the conservation function boils down to a constant-product one like Uniswap; when 𝒜 → +∞, the conservation function is essentially a constant-sum one with constant exchange rate equal to 1.
Dodo
While other AMMs derive the exchange rate between two assets in a pool purely from the conservation function, Dodo does it the other way around. Resorting to external market data as a major determinant of the exchange rate, Dodo has its conservation function derived from its exchange rate formula. The exchange rate between the two assets in a Dodo pool is set by the market rate with an adjustment based on the pool composition.
Labeled as a Proactive Market Maker (PMM), Dodo uses a price oracle to guide prices or price discovery by mimicking the behavior of human market makers and efficiently allocating funds near the market price. As market prices changes, the PMM algorithm proactively shifts the price curve in the same direction to ensure that the section in the vicinity of the market price remains flat, allowing better liquidity provision at every price.
The Dodo protocol also includes a liquidity parameter 𝒜 that governs the slope of the invariant function. When 𝒜 → 1 and liquidity provided on both assets are of equal value, the conservation function boils down to a constant-product one, as with Uniswap; when 𝒜 → 0 , the conservation function is essentially a constant-sum one.
Finally, Dodo eliminates the kind of divergence loss seen in previously discussed protocols by not forcing liquidity providers to deposit tokens in predefined ratios.
DODO eliminated both impermanent loss and the price risk outlined above at root cause — it does not interfere with LPs’ assets in any way and gives LPs full liberty to deposit any combination of base and quote tokens in a trading pair. Each trading pair is actually two separate pools rather than one big 50/50 pool. This means that LPs can always get out what they put in, assuming no market risk. Solving impermanent loss is thus equivalent to unlocking single-token exposure. ~ DodoEx
Discussion
The comparison above should clarify that some AMM protocols are more fit than others in certain use cases. Uniswap’s simple structure facilitates pool creation for every token issuer that wants to start a liquidity pool for a crypto asset, making it the most popular AMM in existence. The Uniswap v2 factory has produced over 30,000 pools since it’s start in April 2020. Balancer’s feature of weighting assets within the same pool leads to more efficient liquidity providing. The Balancer Liquidity Bootstrapping pools allow for easy price discovery for early and high-demand tokens, by changing the weights of two assets in the Bootstrapping pool gradually over time.
If one needs to trade similarly priced assets such as stablecoins, then Curve will fit your needs. Curve allows for nearly constant-sum invariant functions, leading to a handy near-1:1 exchange rate which facilitates swaps between assets with the same peg. Finally, Dodo introduces zero-divergence loss liquidity providing by combining the traditional AMM concepts with more complex liquidity provision mechanisms such as single-sided exposure.
This is the first part in a two-part series. Part II can be found here.
UCL Centre for Blockchain Technologies
Official Website: http://blockchain.cs.ucl.ac.uk/
Paper on ArXiv: https://arxiv.org/abs/2103.12732
Github: https://github.com/xujiahuayz/bondingcurves
Twitter: Simon DeFi , UCL CBT