Unsiwap and liquidity provision
This blog post is the first of our blog series on Uniswap. In this article we give a short introduction to the functioning of Uniswap and lay down the definition of basic economic terms in connection with its mechanism.
27.02.2024, Thunj Chantramonklasri
For those of us who are familiar with blockchain and cryptocurrencies, a list of the most recognised projects would include not just Bitcoin and Ethereum, but likely also Uniswap. With its token amongst the top 25 largest by market capitalisation [1] and top five by total value locked (TVL), the decentralised exchange operator is a titan of DeFi. On 15th January 2024, Uniswap opened the submission window for proposals to develop the front-end of its fourth iteration (v4), with USD 300K allocated for the two-year initiative. [2]
But what is Uniswap V4? What are the preceding versions, for that matter? And what economic considerations and implications should would-be developers consider? This article, the first of three parts, provides a technical review of Uniswap basics, extending previous analyses with stylised computations and empirical research. Parts two and three will cover Uniswap V3 and V4, respectively.
What is Uniswap: The Constant Product Formula
Uniswap is a decentralised exchange (DEX) protocol operating on Ethereum, whereby smart contracts facilitate the exchange of cryptoassets without the need for an intermediary. [3] In comparison to a centralized exchange, traders exchange against a basket of assets managed by smart contracts (the liquidity pool). The assets in the basket are provided by so called liquidity providers who each receive a fee in return.
Users can interact with the Uniswap protocol in several ways: pool creation, liquidity provision, token swap for exchange or arbitrage, and governance through the native UNI token (paid out as rewards to LPs). [4] Uniswap’s role is to connect these users.
In the following section, we provide an overview of the constant product formula powering Uniswap, the mathematics of slippage and of impermanent loss, and finally, how the Center’s proprietary research sheds light on considerations regarding arbitrageurs.
Uniswap launched in November 2018 [5], and in May 2020 [6] this initial version (V1) was upgraded (V2) to allow for direct non-ETH ERC-20 token swaps, flash swaps and a new price oracle system. [7] Crucially, the pools allow anyone to withdraw tokens from them, but only in accordance with very specific rules, and for Uniswap V2, this rule is as follows:
\[ R_A * R_B = k\]
Any individual can trade against a V2 pool; to withdraw an amount of token A, an individual must deposit an amount of token B such that k is unchanged. That is to say:
\[ (R_A-\Delta_A )*(R_B+\Delta_B )=R_A*R_B \]
The above may be solved for \(\Delta_A\) to yield:
\[\Delta_A =\frac{R_A*\Delta_B}{R_B+\Delta_B}\]
which rearranges to:
\[\epsilon_{A} = \frac{\Delta_A}{\Delta_B} =\frac{R_A}{R_B+\Delta_B}\]
Rather than the direct exchange rate of A \(\epsilon_A\) for B – the amount of A received for one unit of B [8] – a more intuitive rate is given by its inverse, the indirect rate \(p_A\) – the amount of B received from depositing one unit of A. This is the price of A expressed in terms of B:
\[p_{A}=\frac{ \Delta_B}{\Delta_A} = \frac{R_B+\Delta_B}{R_A}\]
Price Impact
The above equation states that the price of token A increases as the amount of B to be exchanged \(\Delta_B\) increases – if one tries to exchange a larger amount of B for A, one automatically must pay more B per unit of A. While intuitive enough, this feature lies in stark contrast to the typical consumer experience of currency exchanges and means an individual trading via Uniswap pools needs to consider the price impact of her trades [9]– the difference between the market price she sees entering the trade and the price at which her trade executes.
Taking the derivative of \(p_A\) with respect to \(R_A\) to get:
\[\frac{\partial p_A}{\partial R_A} = -\frac{(R_B + \Delta_B)}{R_A^2}\]
Thus, the price of A in terms of B gets exponentially worse the smaller the initial reserve of A.
Moreover, the size of an order relative to the pool provides a rough guide to the price impact of the trade. In fact, it can be shown that for small orders, the price impact of an order is roughly twice the relative size of the order to the total value of the reserves in the pool[9]. However, this price impact multiplier is higher for larger trades. Using figures from the ETH/USDT pool on Uniswap as of 7th February 2024, it can be shown that orders constituting 1.49% and 2.98% of the pool value would incur price impacts of 3.07%, and 6.33% respectively. Thus, the price impact multiplier increases from 2.06 to 2.13 within this range. [10]
Figure 1 - Price impact multiplier increases as the relative size of order increases
Source: Swiss Economics
So why would an individual ever trade against a liquidity pool, the inquisitive reader may ask? For one, centralised exchanges are not immune to slippage, and afford less decentralisation to their users. Another point worth noting is that not everyone who trades with liquidity pools is a trader wanting to exchange a token with another. In fact, many are arbitrageurs who, by the protocol’s design, swoop in to take advantage of discrepancies in the price of the token between different markets’ prices.
If the implied price of token A in a pool is below the market price prevailing across other markets, arbitrageurs can profit by swapping for A in the pool and selling their proceeds outside the AMM. Similarly, if token B’s implied price in the pool is higher than the market price, an arbitrageur can buy B from external sources and swap (i.e., sell) B to the pool. Depictions of such scenarios are likely why arbitrageurs are often associated unfavourably with impermanent loss in blogs [11] and preceding academic studies [12].
Impermanent Loss
If price impact is the thorn in the sides of those trading against AMMs, the spectre haunting liquidity providers (LPs) supplying their tokens to AMMs is Impermanent Loss. Impermanent loss is the economic loss a liquidity provider incurs when he provides liquidity to a pool and the relative prices of his deposited assets change. While it is often presented as temporary [13], there are two noteworthy scenarios under which an LP could experience long-term losses: 1) if he deposits a token pair and is forced to sell them before their relative prices settle back to the original ratio and 2) if a token he deposits shoots up in price and he wants to capture the windfall.
Impermanent loss can be formally described as [14]:
\[IL = \frac{V_1 - \text{Hold}}{\text{Hold}} = \frac{2 \sqrt{d_{BA}}}{d_{BA} + 1} - 1\]
where \(d_{BA}\) is the ratio of B’s percentage price multiplier \(\delta_B\) to A’s percentage price multiplier \(\delta_A\). or their relative price elasticities. [15]
\[d_{BA} = \frac{\delta_B}{\delta_A}\]
\[p_{A1} = p_A * \delta_A\]
\[p_{B1} = p_B * \delta_B\]
If relative prices remain the same (\(d_{AB}\) = 1) the equation reduces to 0 – no impermanent loss is incurred provided relative prices stay the same. For any non-zero value of \(d_{AB}\), however, due to the convexity of \(d_{AB}\) (i.e., with two tokens, to get one we always have to deposit another, effectively moving along a downward-sloping curve), we can refer to mathematical properties of convex functions to inform us that the quotient is always less than or equal to one – without fees, there is always impermanent loss with changes in relative prices.
This would suggest that it never makes sense for anyone to become an LP! However, LPs are paid a fee for providing liquidity, and the prevalence of AMMs would suggest that these fees are viewed as offsetting the effect of impermanence loss.
A most pertinent question then, would be how various parameters affect the fees earned by LPs? The equations above lend themselves well to linear programming methods to quantify said parameters, and indeed, a recent paper co-authored by Matthias Hafner from the Center of Cryptoeconomics does precisely this. [16]
The paper conducts a series of agent-based simulations in Python, comprising of the steps below, with the loop repeated iteratively for a period of one year.
Figure 2 – Process flowchart for agent-based modelling
Source: Hafner, M and H. Dietl (2024) Impermanent Loss Conditions: An Analysis of Decentralized Exchange Platforms
Under assumptions consistent with historical Uniswap data (e.g., annual trading volume and liquidity, the ratio of which affects LP fees), the simulations found that taking liquidity provision fees into account, and provided that market prices do not fall by more than 75% or surge by more than 300% in a year, LPs would make a profit relative to a holding strategy. [17]
Figure 3 – Agent-based simulations of LPs’ relative profits with fees & ETH price growth
Source: Hafner, M and H. Dietl (2024) Impermanent Loss Conditions: An Analysis of Decentralized Exchange Platforms
But two effects are at work here. Firstly, when the (relative) market price for a token changes, arbitrageurs trade against the liquidity pool, causing a change in the asset composition of the pool, bringing about rebalancing-induced impermanent loss. On the other hand, more trades mean more fees. The simulations showed that barring exceptionally strong price movements, the latter effect prevails. [17]
Extending the analysis further, my colleague examined the effect increased arbitrage activity has on LP profits. Through varying transaction costs incurred by arbitrageurs from 0 to 5%, he found that as transaction costs for arbitrageurs fall and arbitrage activity increases, LP profitability actually increases! Again, this is because arbitrageurs reduce LP profits through rebalancing losses but contribute to LP profits through inducing fee gains. While the former is largely independent of the number of arbitrage trades – whether you have 10 arbitrageurs or one, it only takes the one to move the price back to market price – the gains from fees increase with arbitrage activity.
Figure 4 – LPs’ profits for different transaction costs of arbitrageurs
Source: Hafner, M and H. Dietl (2024) Impermanent Loss Conditions: An Analysis of Decentralized Exchange Platforms
This leads to the somewhat counterintuitive finding that LP profits increase as arbitrageurs can interact more easily with the AMM. The dynamic nature of the simulations allowed for this phenomenon, as it allows for increased arbitrage activity to translate to higher trading fees, as opposed to previous static studies that modelled this as a single trade. [17] An important implication of this result is that developers of AMMs wanting to attract LPs to promote the adoption of their protocol by traders would do well to focus on facilitating arbitrageur access by lowering their transaction costs.
Toward V3 & beyond
Armed with a thorough understanding and some uncommon insight of the economics of AMMs, the interested reader is well-placed to examine how these considerations relate to the relatively lacklustre reception for V3 and the possibilities afforded by V4 in the forthcoming articles in this series.
Appendix
Price impact computations
Using real Uniswap V2 data for the ETH-USDT pair as of 7th February 2024 12:10PM EST [18], with 33,618 ETH and 81,066,090 USDT, the implied price multipliers are as follows, increasing in the relative size of the order to the total value of the pool.
Table 1: Price multipliers
Source: Swiss Economics
Figure 5: ETH - USDT Pair Statistics
Source: Uniswap v2 dashboard
It is important to note however, that in fact, the invariant k varies depending on the ratio of reserves when the liquidity pool is created and increases due to the addition of trading fees to reserves (in the case of V2, unlike with V3 where fees generated are separated from pool reserves). [19]
Impermanent Loss derivation
How could we formalise the concept of impermanent loss as a function of prices? Let us start with the value of any initial position \(V_0\) is [20]:
\[V_0 = p_A * R_A + p_B * R_B\]
Providing liquidity to the pool requires depositing equal value of both tokens:
\[p_A * R_A = p_B * R_B\]
Or:
\[p_A = \frac{p_B * R_B}{R_A}\]
\[p_B = \frac{p_A * R_A}{R_B}\]
From the constant product rule:
\[R_A * R_B = k\]
Making the substitutions we have:
\[p_A = \frac {p_B * k}{R_A^2}\]
\[p_B = \frac{p_A * k}{R_B^2}\]
Solving for \(R_A\) and \(R_B\) yields:
\[R_A = \sqrt{\frac{k}{p_A} * p_B}\]
\[R_B = \sqrt{\frac{k}{p_B} * p_A}\]
Rewriting the value of a position in terms of just the invariant k and the prices:
\[V_0 = p_A*\sqrt{\frac{k}{p_A}*p_B} + p_B*\sqrt{\frac{k}{p_B}*p_A}\]
\[V_0 = \sqrt{k*p_B*p_A} + \sqrt{k*p_A *p_B}\]
\[V_0 = 2 \sqrt{k*p_A*p_B}\]
Following a change in prices, the new value of the LP’s deposited assets is:
\[V_1 = 2 \sqrt{k*p_{A1}*p_{B1}}\]
What about if one were to hold instead of providing liquidity to the pool? Well, in this case the quantities do not change even as prices change, thus:
\[\text{Hold} = p_{A1} * H_A + p_{B1} * H_B\]
To compare the loss versus a scenario where the LP simply held onto the same amounts of tokens instead of supplying them to the pool, substitute in the equations for \(R_A\) and \(R_B\):
\[\text{Hold} = p_{A1} * \sqrt{\frac{k}{p_A} *p_B} + p_{B1} * \sqrt{\frac{k}{p_B}*p_A}\]
Impermanent loss is just the difference between \(V_1\) and Hold:
\[V_1 - \text{Hold} = 2 \sqrt{k * p_{A1} * p_{B1}} - \left(p_{A1} * \sqrt{\frac{k}{p_A} * p_B} + p_{B1} * \sqrt{\frac{k}{p_B} *p_A}\right)\]
Of course, the new prices are just the old prices with price changes applied to them:
\[p_{A1} = p_A *\delta_A\]
\[p_{B1} = p_B*\delta_B\]
\[V_1 - \text{Hold} = 2 \sqrt{k *p_A *\delta_A *p_B *\delta_B} - \left(\sqrt{\frac{k}{p_A} * p_B} *p_A *\delta_A + \sqrt{\frac{k}{p_B}*p_A} *p_B *\delta_B\right)\]
\[V_1 - \text{Hold} = 2 \sqrt{k * p_A * \delta_A * p_B * \delta_B} - \left(\sqrt{k * p_B *p_A} * \delta_A + \sqrt{k * p_A * p_B} *\delta_B\right)\]
Expressing the above as a percentage, the value from a HODL strategy is:
\[\frac{V_1 - \text{Hold}}{\text{Hold}} = \frac{2\sqrt{k * p_A * \delta_A * p_B * \delta_B} - \left(\sqrt{k * p_B * p_A} * \delta_A + \sqrt{k * p_A * p_B} * \delta_B\right)}{\sqrt{k * p_B * p_A} * \delta_A + \sqrt{k * p_A * p_B} * \delta_B}\]
Factorising out \(\sqrt{k*p_A *p_B}\)
\[\frac{V_1 - \text{Hold}}{\text{Hold}} = \frac{\sqrt{k * p_A * p_B} * \left(2\sqrt{\delta_A * \delta_B} - (\delta_A + \delta_B)\right)}{\sqrt{k * p_A * p_B} * (\delta_A + \delta_B)}\]
The \(\sqrt{k*p_A *p_B}\) above and below the quotient cancels out:
\[\frac{V_1 - \text{Hold}}{\text{Hold}} = \frac{2 * \sqrt{\delta_A * \delta_B} - (\delta_A + \delta_B)}{\delta_A + \delta_B}\]
\[\frac{V_1 - \text{Hold}}{\text{Hold}} = \frac{2 * \sqrt{\delta_A * \delta_B}}{\delta_A + \delta_B} - 1\]
Letting \(d{BA}\) be the relative change of the price of B with respect to the change in A’s price:
\[d_{BA} = \frac{\delta_B}{\delta_A}\]
\[\delta_B = d_{BA} * \delta_A\]
\[\frac{V_1 - \text{Hold}}{\text{Hold}} = \frac{2 * \sqrt{\delta_A * d_{BA} * \delta_A}}{d_{BA} * \delta_A + \delta_A} - 1\]
\[\frac{V_1 - \text{Hold}}{\text{Hold}} = \frac{2 * \delta_A * \sqrt{d_{BA}}}{\delta_A * (d_{BA} + 1)} - 1\]
This leaves the following:
\[\text{IL}=\frac{V_1 - \text{Hold}}{\text{Hold}} = \frac{2 * \sqrt{d_{BA}}}{d_{BA} + 1} - 1\]
References
Grau, E. F. (2022) Impermanent Loss: Step by Step Demonstration (Math) EN:02. Available at: https://medium.com/coinmonks/impermanent-loss-step-by-step-demonstration-math-95b1f66fa704
Hafner, M. and Dietl H. (2024) Impermanent Loss Conditions: An Analysis of Decentralized Exchange Platforms. Available at: https://arxiv.org/pdf/2401.07689.pdf
Hasu (2021) Understanding Automated Market-Makers, Part 1: Price Impact. Available at: https://www.paradigm.xyz/2021/04/understanding-automated-market-makers-part-1-price-impact
Milionis, J., Moallemi, C. C. and T. Roughgarden (2023) Automated Market Making and Arbitrage Profits in the Presence of Fees. Available at: https://arxiv.org/pdf/2305.14604.pdf
Uniswap (2024) Uniswap v2 dashboard for ETH-USDT pair. Available at: https://v2.info.uniswap.org/pair/0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852
Uniswap (2024) How Uniswap works. Available at: https://docs.uniswap.org/contracts/v2/concepts/protocol-overview/how-uniswap-works
Uniswap (2020) Uniswap v2 Mainnet Launch. Available at: https://blog.uniswap.org/launch-uniswap-v2
Uniswap (2024) RFP: Uniswap V4 Front-End for Pool Launchers and LPs. Available at: https://uniswap.notion.site/RFP-Uniswap-V4-Front-End-for-Pool-Launchers-and-LPs-bb97f1deefdf45e6ab1bea252487e3d4
Footnotes
[1] https://www.coingecko.com; https://defillama.com/chain/Ethereum?volume=false
[3] https://docs.uniswap.org/contracts/v2/concepts/protocol-overview/how-uniswap-works
[4] https://docs.uniswap.org/contracts/v2/concepts/protocol-overview/ecosystem-participants
[6] https://blog.uniswap.org/launch-uniswap-v2
[7] https://blog.uniswap.org/uniswap-v2
[8] https://www.investopedia.com/terms/d/directquote.asp
[9] https://www.paradigm.xyz/2021/04/understanding-automated-market-makers-part-1-price-impact
[10] See Appendix.
[11] https://learn.bybit.com/defi/what-is-impermanent-loss/
[12] https://arxiv.org/abs/2305.14604
[14] See Appendix.
[15] Strictly speaking, the elasticity would be the conventionally defined as the ratio of percentage price changes (i.e., each price multiplier minus 100%).
[16] https://arxiv.org/pdf/2401.07689.pdf The paper uses an equivalent variation of the IL equation above, specifically: \(\text{IL} = \frac{2 * \sqrt{{p_m^T}/{p_m^1}}}{1 + {p_m^T}/{p_m^1}} - 1\)
[17] https://arxiv.org/pdf/2401.07689.pdf
[18] https://v2.info.uniswap.org/pair/0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852
[19] https://docs.uniswap.org/contracts/v2/concepts/protocol-overview/how-uniswap-works; https://hackmd.io/@NotBored/Sy7iuAfUc
[20] https://medium.com/coinmonks/impermanent-loss-step-by-step-demonstration-math-95b1f66fa704