246 total views

The core idea of Uniswap V3 is liquidity concentration. Liquidity providers can provide liquidity in a certain range and increase the utilization rate of funds.

**Original title: “Uniswap-V3 Technical White Paper Guide” Written by: Star Li**

Uniswap V3 attracted widespread attention as soon as it was announced. Compared with V2, the logic and code are more complicated. The core of V3 is to provide liquidity (centralized liquidity) through the market range to solve the problem of capital utilization when LP provides liquidity. What is the capital utilization rate? How does V3 derive the formula for calculating interval liquidity? How to understand liquidity? How to calculate the swap fee? This article starts with a detailed analysis of the V3 technical white paper.

First, give some official information about V3 from Uniswap:

It is recommended to carefully review the technical white paper first, then look at the smart contract code, and finally check the official introduction. After understanding the technical white paper, the code is very easy to understand. The first chapter of the technical white paper summarizes the technical characteristics of Uniswap V3. The core concept is “Concentrated Liquidity”.

### Fund utilization

First look at the fund utilization rate of Uniswap V2:

The figure above shows the change curve of the amount of x/y in the fund pool. The current price in the fund pool is at point c, and it is assumed to fluctuate between price point a and price point b. Sliding from point c to point a consumes the maximum y_real, sliding from point c to point b, the maximum consumption is x_real. In other words, if the current price c point oscillates between point a and point b, only x_real and y_real need to be consumed at most. In theory, it is enough to provide x_real and y_real. In fact, as shown in the figure above, at the price point c, x and y that are greater than x_real and y_real are provided. Obviously, it can be seen that the funds of x-x_real and y-y_real will never be used in this case, which is also called idle funds.

In this case, the capital utilization rate is x_real/x or y_real/y. If the price fluctuation is very small, the capital utilization rate is very low. Uniswap V3 is an attempt to solve the problem of low capital utilization. The idea is relatively simple. All funds can only be added to a certain range, only to the range of possible price fluctuations. How to add liquidity in a certain interval and provide swap function is the focus of Uniswap V3. Let’s start with Virtual Reserves.

### Virtual Reserves

Uniswap’s transaction uses a product fixed model (x*y=k). The so-called virtual fund pool (Virtual Reserves) refers to a fund pool that is still on a fixed product curve and only provides a certain range of liquidity:

The dark green curve in the figure is the product fixed curve satisfied by the virtual capital pool. In fact, the curve of required funds is orange in the figure. The orange curve formula is as follows:

You can imagine that the virtual fund curve is translated on the x/y axis, so that the a/b point and the x/y axis coincide. That is, a certain amount of funds can achieve the effect of a “virtual” transaction curve.

How to calculate the provision of a virtual fund pool in a certain interval starts with a deep understanding of liquidity.

### Liquidity (Liquidity-L)

The fixed product transaction model satisfies the two token amounts in the fund pool: x *y = K. If K=L^2 is set, x* y = L^2. L is what we call liquidity. The following formula is derived from the fixed product trading model:

Given L and sqrt(P), the capital requirements x and y can also be derived.

According to Formula 6.6, when the liquidity remains unchanged (without adding or deleting liquidity), liquidity can be regarded as the change in the amount of funds per unit of “price fluctuation”. “Price fluctuation” is quoted because it is actually a change in sqrt(P).

This is the core formula of Uniswap V3 (6.7), which **uses relative values (the relative value of funds and prices) to calculate liquidity** . The so-called liquidity is the amount of funds per unit of “price change”. In the case of a certain trading volume, if the liquidity is good, the price change will be small, and if the liquidity is not enough, the price fluctuation will be large.

Special attention is paid to the fact that the liquidity in an interval is different from the universal liquidity of V2. The focus of liquidity in a range is on the “range.” The liquidity in different intervals is not comparable. The difference between the liquidity of V2 and the liquidity of the interval is as follows:

The liquidity of V2 is “universal”, with the same liquidity at all price points. The liquidity of V3 is composed of a series of liquidity in different intervals. Relatively speaking, the liquidity around the current price is relatively high. The liquidity provider LP can only obtain transaction fees if it provides liquidity for trading. In order to obtain more transaction fees and increase the utilization rate of funds, liquidity providers will provide funds within a reasonable range of price fluctuations. In other words, only the liquidity provider in this interval can obtain the handling fee incurred in a swap transaction in a certain interval. In order to calculate the handling fee obtained in each interval, the concept and calculation method of Tick are introduced.

### Tick

Providing liquidity in intervals brings many complicated situations: overlapping coverage of intervals and intervals. Because the liquidity in different intervals is not comparable, a certain transaction fee cannot be mixed into the liquidity, and it will be withdrawn when the liquidity is deleted. To solve this problem, Uniswap V3 introduced the concept of Tick. Transaction costs are settled in real time and recorded separately, and are not mixed into working capital. Although the entire range and the range of liquidity are not comparable, but at a specific price point (on a price slice), the liquidity is comparable. Uniswap V3 divides the entire price range (negative infinity to positive infinity) into ticks (price points):

The price of the latter price point is one ten thousandth of the price of the previous price point.

Each tick also has a unique serial number. The interval (Position) can be represented by two ticks. Logically, the transaction fee can be calculated one by one tick, and the transaction is allocated on each tick according to the proportion of liquidity. Let’s first look at the calculation of swap in a Tick range.

### SWAP in Tick

Suppose there is a small amount of y, and swap is x. Through the formula in 6.13, the price change caused by the change in y can be calculated.

Then use the formula in 6.15 to calculate the amount of x exchanged.

Uniswap V3 sets different transaction fees for the same trading pair: 0.05%, 0.3% and 1% (other fees can also be added). If it is exchanged from Y for X, the transaction fee shall be deducted before the exchange is carried out.

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

Next, I will explain in depth the calculation logic of adding/removing liquidity and transaction fees.

### Add/remove liquidity

The addition/removal of liquidity in V3 is the addition or removal of liquidity in a certain range under the current price situation. All liquidity addition/removal of liquidity uses the following formula:

Note that price changes do not refer to the size of the interval, but to provide liquidity in a certain interval, relative to the current price, the price change corresponding to the “corresponding capital change”.

Divided into three situations, the relationship between the liquidity range you want to add and the current price.

- The current price is in the liquidity price range (il <= ic <iu)

Because the price is within the range, if the price slides to il, you need to provide y funds. If the price slides to iu, you need to provide the corresponding x funds. So, for delta_Y, the price change is sqrt(P)-sqrt(p(il)); for delta_X, the price change is 1/sqrt(P)-1/sqrt(p(iu)).

- The current price is below the liquidity price range

Because the current price is much lower than il, even if it slides from the current price to iu, only x funds are needed, not y funds. So, in this case, delta_Y = 0. Because sliding from il to iu requires funds in the entire range of x, for delta_X, the price change is 1/sqrt(il)-1/sqrt(p(iu)).

- The current price is higher than the liquidity price range

Similar to the first case, the analysis will not be repeated.

### Swap transaction fees

The most complex logic of Uniswap is to calculate and allocate transaction fees. The corresponding transaction fees need to be withdrawn before adding and removing liquidity.

#### Total liquidity on Tick

There can be multiple intervals on a tick. When calculating transaction costs, you need to amortize these costs to all the total liquidity in multiple intervals on this tick. Record delta_L on the Tick at the boundary of each interval (the sum of the liquidity of all intervals with this Tick as the boundary).

There is a global state: liquidity, which saves the total liquidity of the current price corresponding to Tick. When the price fluctuates and crosses a certain Tick, liquidity will increase or decrease (depending on the direction of price fluctuations). For example, the price crosses the interval from left to right. When it crosses the first tick of the interval, the liquidity needs to increase. When it passes the last tick, the liquidity needs to decrease, and there is no increase in liquidity for the middle tick. And decrease (delta_L is 0).

#### Transaction fee rate on the interval (Position)

To calculate the transaction cost rate in an interval, the total cost rate is used to subtract the cost rate outside the interval. Record feeGrowthOutside on the boundary Tick of an interval. The so-called feeGrowthOutside is the total expense rate in the “other” direction. The other direction is relative to the direction passing through the current tick. When the price crosses a tick from left to right, feeGrowthOutside refers to the expense ratio of all intervals to the left of the tick. Simply put, it is the expense ratio of all intervals in the opposite direction of the price going. feeGrowthOutside is denoted by fo. Because fo is the total expense rate of the two directions of a Tick, the sum of the expense rates of the two directions must be equal to fg (the global expense rate). So when passing through a tick, the fo on the tick needs to be flipped:

When an interval is created, the fo of Tick on the boundary of the interval needs to be initialized:

If the current price is greater than the Tick price, because even if the current price is within the set range, the previous costs will not be allocated. Therefore, you can simply assume that all costs occur below the Tick price, that is, fo= fg. If the price of Tick is greater than the current price, the price has not passed through Tick, because it is assumed that all the expenses incurred before occur below the Tick price, and there is no cost above the Tick, so fo=0. On the basis of understanding these logics, in the process of swap, as the price fluctuates, in an interval, the expense rate exceeding the highest Tick and the expense rate below the lowest Tick can be calculated in the following way:

Take the calculation of the fee rate lower than the lowest Tick as an example. If ic>=i (the current price is higher than the lowest Tick), all fee rates below the Tick are fo (as defined). If ic

On the basis of the obtained expense rate, the expense rate can be calculated by multiplying the interval liquidity by the expense rate.

### to sum up

The core idea of Uniswap V3 is liquidity concentration. Liquidity providers can provide liquidity in a certain range to increase the utilization rate of funds. The transaction fee obtained in a certain interval is divided equally by all liquidity providers in that interval. Uniswap V3 designed interval granularity-Tick, and deduced the process of liquidity addition/removal and fee calculation. On the basis of this, Uniswap V3 also updated the realization of the price oracle machine.