The core of Uniswap V3 is to provide liquidity through intervals (Concentrated Liquidity), where liquidity providers add liquidity to a range that has the potential for price fluctuations to improve capital utilization.
TWAMM automated market makers as a new type of AMM protocol can also use Concentrated Liquidity.
Let's look at the utilization rate of Uniswap V2 using non-concentrated liquidity.
The above graph shows the volume change curve of in the pool. The current price in the pool is at and is assumed to fluctuate between price point and price point. Sliding from point to point consumes the maximum and sliding from point to point consumes the maximum . That is, the current price point, oscillating between and points, consumes only and at maximum. Theoretically it is enough to provide and . And in fact, as shown above, at the price point, and are provided greater than and , respectively. It is obvious that the funds and are never used in this case, which is also called idle funds. In this case, the capital utilization rate is or . If the price fluctuation is very small, the capital utilization is very low.
How to add liquidity in a certain price interval and provide Swap function is the focus point of Uniswap V3, which needs to start with the virtual Reserves.
Uniswap's trading uses the constant product model (). The so-called Virtual Reserves are pools that provide liquidity only in a certain interval on a constant product curve.
The blue curve in the graph is the constant-product curve satisfied by the virtual Reserves. The curve of the funds that are actually needed is shown in orange in the figure. The formula for the orange curve is as follows.
You can imagine the virtual reserves curve translating on the axis so that the points coincide with the axes. That is, a certain amount of money can be used to achieve a "virtual" trading curve.
The constant product trading model with two token amounts in the liquidity pool satisfies: . If we set , and is what we call liquidity. The trading model with constant product yields the following equation.
With known and , the funding requirements and can also be derived:
With the above equation, liquidity can be seen as the change in the amount of funds per unit of "price fluctuation", with no change in liquidity (without adding or removing liquidity). "Price fluctuation" is in quotes because it is in fact a change in .
This is the core Uniswap V3 formula that calculates liquidity in terms of relative values (relative values of funds and prices). Liquidity is the amount of money per unit of "price change". With a certain volume of trading, if the liquidity is enough, the price will change less, and if it is not liquid enough, the price will fluctuate more.
Centralized liquidity allows users to put funds into any price range to provide liquidity. Above, for the convenience of explaining the principle, only one interval scenario is used, it is unlikely that there is only one price range in the actual scenario, assuming that there are several consecutive price ranges each with different flows, as shown below.
As we can see, the market maker curve is no longer continuous, and during the trading process, price changes to different intervals have to be calculated independently in segments.But in the right-angle coordinate system the boundary of each price interval is a straight line, the and quantities are changing simultaneously, and there may be overlapping price intervals, which leads to a very high complexity of the whole calculation process.
Uniswap V3 uses the coordinate system, so that the price range boundary is changed from a two-dimensional straight line to a point on the axis. Moreover, increasing/decreasing liquidity does not change the current price, and Swap causes price changes but not liquidity values, so that and have only one value change at the same time, further reducing the computational complexity.
How to calculate the result of a Swap is the focus of Uniswap V3. For both the user and the application side, it is still an operation on and , so the actual calculation is done by converting between .
When a Swap occurs and one of and is zero, we can write the Swap result in the following symmetric form.
For simplicity, we make .
Since, the
It is possible to compute with respect to and :
According to the article Mathematical Principle of TWAMM, TWAMM still uses a constant product trading model with two token amounts in the pool satisfying: . The constant product trading model still yields the following equation.
If TWAMM use centralized liquidity, the calculation for Instant Swap is still the same as Uniswap V3, but not for Term Swap.
The result of the settlement of TWAMM long-term orders at expiration is as follows.
Similarly, we have.
It is possible to compute with respect to , , ( is constant when the long-term order is due for settlement) and :
Recalculation,
The inverse calculation yields and computed by
After analysis, the
The above inequality shows that the value of is between and (excluding the case when , and are all equal).
Also, by deriving , we can obtain the variation of for and as follows
The above shows that if , then monotonically increasing, and vice versa monotonically decreasing.
Finally
We can obtain the expressions for and as follows
If , by L'Hôpital's Rule we can obtain the instant swap result of