Try   HackMD

Mathematical Principle of TWAMM Concentrated Liquidity

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.

Capital Utilization

Let's look at the utilization rate of Uniswap V2 using non-concentrated liquidity.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

The above graph shows the volume change curve of

xy in the pool. The current price in the pool is at
c
and is assumed to fluctuate between
a
price point and
b
price point. Sliding from point
c
to point
a
consumes the maximum
yreal
and sliding from point
c
to point
b
consumes the maximum
xreal
. That is, the current price
c
point, oscillating between
a
and
b
points, consumes only
xreal
and
yreal
at maximum. Theoretically it is enough to provide
xreal
and
yreal
. And in fact, as shown above, at the price
c
point,
x
and
y
are provided greater than
xreal
and
yreal
, respectively. It is obvious that the funds
xxreal
and
yyreal
are never used in this case, which is also called idle funds. In this case, the capital utilization rate is
xrealx
or
yrealy
. 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.

Virtual Reserves

Uniswap's trading uses the constant product model (

xy=k). The so-called Virtual Reserves are pools that provide liquidity only in a certain interval on a constant product curve.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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.

(x+Lpb)(y+Lpa)=L2

You can imagine the virtual reserves curve translating on the

xy axis so that the
a,b
points coincide with the
x,y
axes. That is, a certain amount of money can be used to achieve a "virtual" trading curve.

Uniswap V3 Centralized Liquidity

The constant product trading model with two token amounts in the liquidity pool satisfies:

xy=k. If we set
k=L2
,
xy=L2
and
L
is what we call liquidity. The trading model with constant product yields the following equation.

L=xy,p=yx

With known

L and
p
, the funding requirements
x
and
y
can also be derived:

x=Lp,y=Lp

With the above equation, liquidity can be seen as the change in the amount of

y 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
p
.

L=ΔyΔp

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.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Centralized Liquidity Swap

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.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

XY right-angle coordinate system the boundary of each price interval is a straight line, the
x
and
y
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

PriceLiquidity coordinate system, so that the price range boundary is changed from a two-dimensional straight line to a point on the
Price
axis. Moreover, increasing/decreasing liquidity does not change the current price, and Swap causes price changes but not liquidity values, so that
Price
and
Liquidity
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

Xtoken and
Ytoken
, so the actual calculation is done by converting between
(X,Y)(Price,Liquidity)
.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

When a Swap occurs and one of

xin and
yin
is zero, we can write the Swap result in the following symmetric form.

xammEnd=yammStartxammStart+xinyammStart+yin

yammEnd=xammStartyammStart+yinxammStart+xin

xout=xammStart+xinxammEnd=yinxammStart+xinyammStart+yin

yout=yammStart+yinyammEnd=xinyammStart+yinxammStart+xin

For simplicity, we make

yin=0.

xammEnd=xammStart+xin

yammEnd=xammStartyammStartxammStart+xin

xout=xammStart+xinxammEnd=0

yout=yammStartyammEnd=xinyammStartxammStart+xin

Since, the

xammEnd=LpammEnd,xammStart=LpammStart

yammEnd=LpammEnd,yammStart=LpammStart

pammEnd=yammEndxammEnd,pammStart=yammStartxammStart

It is possible to compute

pammEnd with respect to
xin
and
pammStart
:

1pammEnd=1pammStart+xinL

xin=LpammEndLpammStart

yout=yammStartyammEnd=LpammStartLpammEnd

TWAMM Concentrated Liquidity

According to the article Mathematical Principle of TWAMM, TWAMM still uses a constant product trading model with two token amounts in the pool satisfying:

xy=k=L2. The constant product trading model still yields the following equation.

L=xy,p=yx,x=Lp,y=Lp

If TWAMM use centralized liquidity, the

(X,Y)(Price,Liquidity) 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.

xammEnd=kxinyine2xinyink+ce2xinyinkc

xout=xammStart+xinxammEnd

yammEnd=kyinxine2xinyink+ce2xinyinkc

yout=yammStart+yinyammEnd

c=xammStartyinyammStartxinxammStartyin+yammStartxin=c

e2xinyink>|xammStartyinyammStartxinxammStartyin+yammStartxin|

Similarly, we have.

xammEnd=LpammEnd,xammStart=LpammStart

yammEnd=LpammEnd,yammStart=LpammStart

pammEnd=yammEndxammEnd,pammStart=yammStartxammStart

It is possible to compute

pammEnd with respect to
xin
,
yin
,
p=yinxin
(
p
is constant when the long-term order is due for settlement) and
pammStart
:

pammEnd=p(e2xinyink+ce2xinyinkc)2

=p(e2pxinL+ce2pxinLc)2

c=pammStartppammStart+p=c

Recalculation,

pammEnd=pe2pxinL+ce2pxinLc

=pe2yinLp+ce2yinLpc>0

The inverse calculation yields

xin and
yin
computed by

e2pxinL=c(pammEnd+p)pammEndp=(pammStartp)(pammEnd+p)(pammStart+p)(pammEndp)>1

After analysis, the

pammStartpammEndpammEndp>0

The above inequality shows that the value of

pammEnd is between
p
and
pammStart
(excluding the case when
p
,
pammStart
and
pammEnd
are all equal).

Also, by deriving

pammEnd, we can obtain the variation of
pammEnd
for
xin
and
yin
as follows

dpammEnddxin=4pcL(e2pxinLc)2

dpammEnddyin=4cL(e2yinLpc)2

The above shows that if

p>pammStart, then
pammEnd
monotonically increasing, and vice versa monotonically decreasing.

Finally

xin=L2pln|(pammStartp)(pammEnd+p)(pammStart+p)(pammEndp)|

yin=Lp2ln|(pammStartp)(pammEnd+p)(pammStart+p)(pammEndp)|

We can obtain the expressions for

xout and
yout
as follows

xout=LpammStart+xinLpammEnd

=LpammStartLpammEnd+L2pln|(pammStartp)(pammEnd+p)(pammStart+p)(pammEndp)|

yout=LpammStart+yinLpammEnd

=LpammStartLpammEnd+Lp2ln|(pammStartp)(pammEnd+p)(pammStart+p)(pammEndp)|

If

yin,p0,xin0, by L'Hôpital's Rule we can obtain the instant swap result of

xin=limp0(L2pln|(pammStartp)(pammEnd+p)(pammStart+p)(pammEndp)|)

=LpammEndLpammStart

yin=0

xout=0

yout=LpammStartLpammEnd

Reference