paint-brush
Formulas of Uniswap: A Deep Diveby@qizhou
8,172 reads
8,172 reads

Formulas of Uniswap: A Deep Dive

by Qi ZhouAugust 28th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

We propose the concentrated liquidity for multiple pooled assets • Share liquidity among multiple assets • Well suited for stablecoin swaps • Can be a competitive product in same-backed asset swap market (vs., curve) • Smart contract implementation in solidity

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coins Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Formulas of Uniswap: A Deep Dive
Qi Zhou HackerNoon profile picture


A Brief Revisit of Uniswap V2

Uniswap V2 is the most popular and successful DEX.


It became such by pricing paired assets via the curve:

Where x and y are the balance of the assets in the pool.


Given Δx, to swap Δx for Δy, Uniswap V2 will perform the following calculation:

So that xy = k invariant is still satisfied after the exchange, and the price is:

One great feature of Uniswap V2 is permissionless — anyone is able to create a trading pair of two assets by supplying the tokens.


For example, by supplying x = 1 ETH and y = 3000 USDT in a pool, the liquidity provider can create a trading pair with ETH initially being priced at 3000 USDT/ETH.


Although Uniswap V2 gains a wide adoption, the key issue of Uniswap V2 is its low capital efficiency because the liquidity is spread over price [0, +∞].


This means that if the two assets’ prices are concentrated in a relatively small range (e.g., stable coin swap), most of the assets in the pool are not effectively contributed to the swap, resulting in high slippage and the lower fee collected by LPs.

Concentrated Liquidity of Uniswap V3


Uniswap V3 improves the low capital efficiency issue by using a technique called concentrated liquidity, which uses the following curve:

Where the actual price of swap is in [p_a, p_b], p_a < p_b.


By setting p_a = 0 and p_b = +∞, V2 is essentially a special case of V3.


When supplying a liquidity, Uniswap V3 will ask the price range of the liquidity from LPs (see below graph).



This allows LPs to concentrate liquidity in the target price ranges, achieving much higher capital efficiency.


Taking stable coin swap (USDC/USDT) as an example, from the following graph, 95% liquidity is concentrated in price range[0.999, 1.001], achieving about 2000x capital efficiency over Uniswap V2.


With concentrated liquidity, the slippage of swap USDC/USDT is much lower than V2 and thus LPs can collect more fee compared to V2 for the same amount of assets provided in the pool.


Because of the concentrated liquidity feature, the TVL of Uniswap V3 progresses very well, which achieves 2.5B in about 3 months.


Concentrated Liquidity for Multiple Pooled Assets

Uniswap V3 only offers concentrated liquidity for paired assets. A natural question is:


how about concentrated liquidity for multiple assets in a pool?

By concentrating liquidity for multiple assets, we may achieve even high capital efficiency since the assets can share the liquidity in the pool. In contrast, in V3, a swap may be routed to multiple trading pairs due to insufficient liquidity of direct swap. E.g., swapping TUSD -> BUSD may be routed to TUSD -> USDT ->BUSD, which means the traders will pay more fees with higher slippage.


So the core question is:

How should the curve of concentrated liquidity for multiple assets look like?


Unfortunately, the answer is not straightforward, and even more complicated. Let us start with the stablecoin swap that benefits most from concentrated liquidity.


Assuming the price of two stablecoins is between [p, 1/p] (e.g., p = 0.999), we could simplify V3 curve as:

Where [p, 1/p] = [p_a, p_b].


The good property of the simplified curve is that it is kind of symmetric, and an intuitive extension is to add the third term for another stablecoin, which results in the following equation:

Note that couple of slight changes of 3 assets equation vs 2 assets’:


• Right-hand side is L³ instead of L²

• Instead of using the square root of p on the left-hand side, we use the cubic root of p.


Given the equation, we have a key result:


Examples

Balanced Pool

• 3 stablecoins with 6 decimals


• x, y, z = [1000,000e6, 1000,000e6, 1000,000e6], i.e., each asset has 1M in the pool


• p = 0.999, i.e., price range is [0.999, 1.001]


Since the three terms are the same, we have:


L = x / (1 — ∛0.999) = 2998.99977x


Note that compared to x y = k curve, where L = x, we have about 2000 times capital efficiency.


Swapping 10,000e6 (i.e., 10k USD) T0 for T1 will return 9999.96e6 T1 @ 0.999996 T0/T1. As a comparison, the xy=k curve will return 9375e6 T1 @ 0.9375 T0/T1, whose slippage is much higher.

Extremely Unbalanced Pool

• 3 stablecoins with 6 decimals


• x, y, z = [0, 0, 1000,000e6], i.e., each asset has 1M in the pool


• p = 0.999, i.e., price range is [0.999, 1.001]


Since the pool has only one asset T2, we are expected the price of T2 will be close to the price limit, i.e., 0.999 or, equivalently, the price of the rest of assets (T0/T1) vs T2 should be 1.001.


Following the curve, we can solve L = 999.333z. Therefore, swapping 10,000e6 T0 for T2 will return 10009.90e6 T2 @ 1.00099 T0/T2, which is almost the same as the price limit (1/0.999).

Current Status

We are implementing the swapping algorithm for stablecoin swaps in solidity with:


• High-resolution fixed-point solver • n = 3 assets (but could extended to more)

• Adjustable price range [p, 1/p]

• Uniswap V2 style interface (mint/burn/swap)

• Gas cost optimization


Once the algorithm’s implementation is well verified, we will use it as the release candidate for next version of Smoothy.finance (SMTY) — a project for the next-generation swapping protocol for same-backed asset that I am consulting for.


Future Topics


A couple of interesting topics can be further developed in the area of concentrated liquidity for pooled assets:


More assets {x_0, x_1,…,x_n}, where the curve would look like:

Multiple price ranges. E.g., suppose we have p_0 < p_1 (e.g., p_0 = 0.99, p_1 = 0.999), and an LP can choose one of the price ranges for its liquidity.


As a result, the algorithm will jointly solve the following equations for swapping.


Different price ranges for different assets, where the equation may look like:

Concluding Remarks

Concentrated liquidity is one of the hottest topic in DEX with the following benefits:


• Higher capital efficiency • Lower slippage • More fees


Uniswap V3 is the first DEX with concentrated liquidity for paired assets. For stablecoin swap, the capital efficiency can be 2000x.


We propose the concentrated liquidity for multiple pooled assets:


• Share liquidity among multiple assets.

• Well suited for stablecoin swaps.

• Can be a competitive product in the same-backed asset swap market (vs., curve).

• Smart contract implementation in solidity.


Questions? Feel free to reach me at [email protected].


Also published here