header-langage
简体中文
繁體中文
English
Tiếng Việt
Scan to Download the APP
Partnered With:

Read the Uniswap V3 technology whitepaper closely, and what do we find?

Read this article in 29 Minutes
How to understand the core concept of Uniswap V3 -- centralized mobility -- from a code perspective.
Title: Introduction to the UnisWAP-V3 Technology White Paper
Original source: Star Ideas


Uniswap V3 attracted a lot of attention when it was announced. The logic and code are more complex than V2. The core of V3 is to provide liquidity (centralized liquidity) through the market range, to solve the problem of fund utilization when LP provides liquidity. What is the utilization of funds? V3 How to deduce the calculation formula of interval liquidity? How to understand liquidity? How to calculate the cost of SWAP? This article starts with a detailed analysis of the V3 technology white paper.


Here are some official Uniswap information about V3:


V3 official introduction

https://uniswap.org/blog/uniswap-v3/


Technical White Paper

https://uniswap.org/whitepaper-v3.pdf


Smart Contract Code

https://github.com/Uniswap/uniswap-v3-core

https://github.com/Uniswap/uniswap-v3-periphery


I recommend looking at the technical white paper first, then the smart contract code, and then the official introduction. Once you understand the technical whitepapers, the code is easy to understand. The first chapter of the Technical White Paper summarizes the technical features of Uniswap V3. The core concept is "Concentrated Liquidity".

 

1. Fund Utilization Rate


Let's take a look at the capital utilization of Uniswap V2:


 

The graph above shows the X/Y quantity curve in the capital pool. The current price in the pool is at point C and is assumed to fluctuate between price points A and B. When sliding from point C to point A, the maximum consumption is Y_real; when sliding from point C to point B, the maximum consumption is X_real. In other words, if the current price at point C fluctuates between point A and point B, the maximum consumption is only X_real and Y_real. In theory, just supplying x_real and y_real should suffice. In fact, as shown in the figure above, at price C, X and Y are provided greater than X_real and Y_real, respectively. It is obvious that the funds of X-X_REAL and Y-Y_REAL will never be used in this case, which is called idle funds.


In this case, the utilization of funds is either X_real/X or Y_real/Y. If the price fluctuations are very small, the utilization of funds is very low. Uniswap V3 is an attempt to solve the problem of low capital utilization. The idea is simple. All money can be added to a certain range, to a range where prices are likely to move. How to add liquidity to an interval and provide swap functionality is the focus of Uniswap V3. Let's start with Virtual Reserves.

 

2. Virtual capital pool


Uniswap trades using the product fixed model (x*y=k). A Virtual pool is one that sits on a product fixed curve and provides only an interval of liquidity:


 

The dark green curve in the figure is the product fixed curve that the virtual capital pool satisfies. The actual amount of money needed is shown in orange. The formula for the orange curve is as follows:


 

You can imagine the virtual money curve shifting along the x/y axis, so that point A/B and the x/y axis reconnect. That is, with a certain amount of money can achieve the effect of a "virtual" trading curve.

Calculating how to provide a virtual pool of money within a certain range starts with a deep understanding of liquidity.

 

3. Liquidity


The product fixed transaction model satisfies the two kinds of token amounts in the capital pool: X * Y = K. If I set K=L^2, x times y is equal to L^2. L is what we call liquidity. The following formula can be obtained from the product fixed transaction model:


 

Given L and SQRT (P), capital requirements X and Y can also be deduced.


 

According to Formula 6.6, under the condition of constant liquidity (without adding or deleting liquidity), liquidity can be regarded as the change in the amount of Y capital per unit of "price fluctuation". Price fluctuation "is quoted because it is actually a change in SQRT (P).


 

This is the Uniswap V3 Core Formula (6.7), which uses relative values (relative values of funds and prices) to calculate liquidity. Liquidity is the amount of money per "change in price". In the case of a certain trading volume, if liquidity is good, price changes will be small, if liquidity is not enough, price fluctuations will be large.


 

In particular, the liquidity on an interval is different from the universal liquidity on V2. A range of liquidity, the emphasis on the "range". There is no comparability between the ranges of liquidity. The difference between the liquidity of V2 and the liquidity on the interval is shown in the following figure:


 

V2's liquidity is "universal" and it is the same at all price points. The liquidity of V3 is composed of a series of different intervals of liquidity. Liquidity around current prices is relatively high. The liquidity provider LP receives transaction fees only if it provides tradable liquidity. In order to obtain more transaction fees and improve the utilization of funds, liquidity providers will provide funds within a reasonable price fluctuation range. That is to say, only the liquidity provider within a certain interval can obtain the fees generated by swap transactions. In order to calculate the processing charge for each interval, the concept of Tick and its calculation method are introduced.

 

4Tick


Providing liquidity on intervals creates a lot of complications: intervals and overlapping coverage of intervals. Because different ranges of liquidity are not comparable, a transaction fee cannot be added to the liquidity and withdrawn when the liquidity is removed. To solve this problem, Uniswap V3 introduces the concept of Tick. Transaction fees are settled in real time and recorded separately and are not mixed with liquidity. While there is no comparability between the liquidity of the whole range and the liquidity of the range, liquidity is comparable at a specific price point (on a price slice). Uniswap V3 breaks the entire price range (minus infinity to infinity) into Tick points:


 

The price at the latter price point is a 1/10,000th fluctuation from the price at the previous price point.


 

Each Tick also has a unique serial number. Intervals can be represented by two ticks. Logically, transaction fees can be calculated on a Tick by Tick basis and allocated to each Tick according to the proportion of liquidity. First look at the calculation of swap for a Tick range.

 

5 tick SWAP


Let's say I have a small amount of y, and I want swap to be x. Through the formula of 6.13, the price change caused by the change of Y can be calculated.


 

The formula of 6.15 can be used to calculate the amount of x exchanged.


 

Uniswap V3 sets different transaction fees for the same transaction pair: 0.05%, 0.3%, and 1% (other fees can be added). If the exchange is from Y to X, deduct the handling fee before making the exchange.


Only one rate is supported in the same trading pool. That is, different price ranges are supported in a trading pool, but all at the same rate. If you need to add a trading pool with different transaction fees for the same trading pair, you must create a new trading pool.


Next, take a closer look at the logic of adding/removing liquidity and transaction costs.

 

Add/Remove Liquidity


The addition/removal of liquidity of V3 is the addition or removal of liquidity in a certain range at the current price. All liquidity additions/deletions are made by the following formula:


 

Note that the price change does not refer to the size of the interval, but rather the change in price that "requires the corresponding change in funds" relative to the current price, providing liquidity at a certain interval.


There are three cases, the relationship between the liquidity range you want to add and the current price.


The current price is in the liquid price range. = ic < iu)


 

Because the price is in the range, if the price slides to IL, you need to provide Y capital. If the price slides to IU, the corresponding X funds need to be provided. So, for delta_Y, the change in price is SQRT (P) - SQRT (P (il)); For delta_X, the change in price is 1/ SQRT (P) - 1/ SQRT (P (iu)).


The current price is below the liquid price range


 

Since the current price is far lower than that of IL, even if sliding from the current price to IU, only X's capital is needed, not Y's. So, in this case, delta_Y = 0. Since it takes the entire interval of X to slide from IL to IU, for delta_X, the change in price is 1/ SQRT (IL) - 1/ SQRT (P (IU)).


The current price is above the liquid price range


Similar to the first case, I'm not going to repeat the analysis.

 

7SWAP transaction fees


The most complex logic of Uniswap is to calculate transaction costs and allocate them. Transaction costs need to be extracted before adding and removing liquidity.


Total liquidity on Tick


There can be multiple intervals on a single Tick. When calculating trading costs, you need to amortize those costs over the total liquidity of all the ranges on that Tick. Delta_l (the sum of the liquidity of all the intervals bounded by this Tick) is recorded on the Tick at the boundary of each interval.


There is an overarching state: Liquidity, which holds the sum of the current price and the liquidity of the ticks. When the price moves through a Tick, it increases or decreases liquidity (depending on the direction of the price move). For example, if prices move from left to right through a range, liquidity needs to increase as they move through the first Tick of the range, and liquidity needs to decrease as they move out of the last Tick, with no increase or decrease in liquidity in any of the intermediate ticks (delta_L = 0).  


 

Transaction Expense Ratio on Position


Calculate the transaction expense ratio over a range by using the total expense ratio minus the expense ratio outside the range. Record feeGrowthOutside on Tick at the boundary of an interval. Feegrowthoutside is the total expense rate in the "other" direction. The other direction is relative to the direction through the current Tick. When the price moves from left to right through a Tick, feeGrowthOutside refers to the expense rate for all ranges to the left of the Tick. Simply put, it's the cost ratio of all the intervals in the direction in which the price is going to go. Feegrowthoutside is denoted by fo. Since FO is the total expense rate of two directions of a Tick, the sum of the two directions must be equal to FG (the global expense rate). So when passing through a Tick, the FO on that Tick will flip:


 

When a range is created, the FO of the Tick on the range boundary needs to be initialized:


 

If the current price is greater than the price of Tick, since even if the current price is within the set range, the previous cost will not be distributed, it can be simply assumed that all the costs occur below the price of Tick, i.e. FO = FG. If the price of Tick is greater than the current price, the price has not passed through the Tick, because it is assumed that all previous expenses have occurred below the price of Tick, and there is no cost above Tick, so fo=0. On the basis of understanding these logics.In the process of SWAP, with the fluctuation of price, the expense ratio of the highest Tick and the lowest Tick in an interval can be calculated in the following way:


 

Take, for example, the calculation of an expense rate below the minimum Tick, if IC> = I (the current price is above the minimum Tick), and all expense ratios below Tick are fo (as defined). If ic< In the case of I, FO records all expense ratios higher than Tick, so it needs to be flipped, i.e. FG-FO. When all expenses outside a range are captured, the expense ratio of this range is calculated:


 

Based on the obtained expense ratio, the fees charged for the period can be calculated by multiplying the expense ratio by the liquidity of the period.

 

Conclusion:


The core idea of Uniswap V3 is liquidity centralization. Liquidity providers can provide liquidity in a certain range, increasing the utilization of funds. Transaction fees earned within a range are split equally among all liquidity providers within that range. Uniswap V3 designs the interval granularity -Tick and deduces the process of liquidity addition/removal and expense calculation. On top of this, Uniswap V3 also updates the implementation of the price predictor.


The original link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit