Try   HackMD

TWAMM 集中流动性的数学原理

Uniswap V3 核心是通过盘口区间提供流动性(集中式流动性),流动性提供者将流动性添加到有可能价格波动到的区间范围,提高资金使用率。

TWAMM 做市商作为新型的 AMM 协议同样可以采用集中流动性(Concentrated Liquidity)

资金利用率

先看看 Uniswap V2 使用非集中流动性的资金利用率。

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 →

上图为资金池中的

xy 的量变化曲线。资金池中的当前价格在
c
点,并且假设会在
a
价格点和
b
价格点之间波动。从
c
点向
a
点滑动,消耗最大
yreal
,从
c
点向
b
点滑动,消耗最大为
xreal
。也就是说,当前价格
c
点,在
a
点和
b
点之间震荡的话,最大只需要消耗
xreal
yreal
。理论上只要提供
xreal
yreal
就足够了。而事实上,如上图所示,在价格
c
点,分别提供了大于
xreal
yreal
x
y
。明显可以看出,
xxreal
yyreal
的资金在这种情况下是永远用不上的,也就称为闲置资金。在这种情况下,资金利用率为
xrealx
或者
yrealy
。如果价格波动非常小的话,资金利用率是非常低的。

如何在某个区间添加流动性并提供 Swap 功能是 Uniswap V3 的重点,这需要先从虚拟资金池说起。

虚拟资金池

Uniswap 的交易采用恒定乘积模型(

xy=k)。所谓的虚拟资金池(Virtual Reserves),是指在乘积恒定曲线上,只提供某个区间流动性的资金池:

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 →

图中的蓝色的曲线就是虚拟资金池满足的乘积恒定曲线。事实上需要的资金的曲线如图中的橘黄色。橘黄色的曲线公式如下图:

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

你可以想象成虚拟资金曲线在

xy 轴进行平移,使得
a,b
点和
x,y
轴重合。也就是用一定量的资金就能达到“虚拟”的交易曲线的效果。

Uniswap V3 集中流动性

恒定乘积做市商资金池中的两种代币金额满足:

xy=k。如果设定
k=L2
的话,
xy=L2
L
就是我们说的流动性。由恒定乘积的交易模型得出如下的公式:

L=xy,p=yx

在已知

L
p
的情况下,也能推导出资金需求量
x
y
:

x=Lp,y=Lp

通过上面的公式,在流动性不变的情况下(不添加或删除流动性),流动性可以看成是单位“价格波动”的

y 资金量的变化。“价格波动”打上引号是因为事实上是
p
的变化。

L=ΔyΔp

这个是 Uniswap V3 核心公式,用相对值(资金和价格相对值)来计算流动性。所谓的流动性,就是单位“价格变化”的资金量。在一定的交易量的情况下,如果流动性好,价格变化就小,流动性不够的话,价格波动就大。

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 →

集中流动性兑换

集中流动性的引入,使用户可以把资金放到任意价格区间内提供流动性,上文为了方便解释原理,只用了一个区间的场景,实际场景中不太可能只有一个价格区间,假设有几个连续的价格区间各自有不同的流动,则如下如所示:

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 →

我们可以看到,做市商曲线不再连续了,在交易过程中,价格变化到不同区间时要分段独立计算,但是在

XY 直角坐标系中每个价格区间的边界是一条直线,
x
y
的量是同时变化的,而且还可能存在价格区间有重叠的情况,这就导致整个的计算过程复杂度非常高。

Uniswap V3 改用

PriceLiquidity 的坐标系,这样价格区间的边界就从一个二维的直线变成了
Price
轴上的一个点了,而且增加/减少流动性不会改变当前价格,Swap会导致价格变化但不会改变流动性的值,因此
Price
Liquidity
在同一时间只有一个值变化,进一步降低了计算复杂度.

如何计算 Swap 的结果是 Uniswap V3 的重点。对用户和应用侧来说,还是对

Xtoken
Ytoken
进行的操作,所以在实际计算过程中要在
(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 →

当发生一次 Swap 时,

xin
yin
其中有一个为零,我们可以将 Swap 结果写成如下对称形式:

xammEnd=yammStartxammStart+xinyammStart+yin

yammEnd=xammStartyammStart+yinxammStart+xin

xout=xammStart+xinxammEnd=yinxammStart+xinyammStart+yin

yout=yammStart+yinyammEnd=xinyammStart+yinxammStart+xin

简单起见,我们令

yin=0

xammEnd=xammStart+xin

yammEnd=xammStartyammStartxammStart+xin

xout=xammStart+xinxammEnd=0

yout=yammStartyammEnd=xinyammStartxammStart+xin

由于,

xammEnd=LpammEnd,xammStart=LpammStart

yammEnd=LpammEnd,yammStart=LpammStart

pammEnd=yammEndxammEnd,pammStart=yammStartxammStart

可以计算出

pammEnd 关于
xin
pammStart
的计算公式:

1pammEnd=1pammStart+xinL

xin=LpammEndLpammStart

yout=yammStartyammEnd=LpammStartLpammEnd

TWAMM 集中流动性

按照 TWAMM 的数学原理 一文,TWAMM 依然采用恒定乘积的交易模型,资金池中的两种代币金额满足:

xy=k=L2。由恒定乘积的交易模型依然得出如下的公式:

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

如果 TWAMM 引入集中流动性,即时兑换(Instant Swap)的

(X,Y)(Price,Liquidity) 计算结果依然和 Uniswap V3 相同,但是定期兑换(Term Swap)就不相同了。

TWAMM 长期订单到期执行结算的结果如下:

xammEnd=kxinyine2xinyink+ce2xinyinkc

xout=xammStart+xinxammEnd

yammEnd=kyinxine2xinyink+ce2xinyinkc

yout=yammStart+yinyammEnd

c=xammStartyinyammStartxinxammStartyin+yammStartxin=c

e2xinyink>|xammStartyinyammStartxinxammStartyin+yammStartxin|

同样,我们有:

xammEnd=LpammEnd,xammStart=LpammStart

yammEnd=LpammEnd,yammStart=LpammStart

pammEnd=yammEndxammEnd,pammStart=yammStartxammStart

可以计算出

pammEnd 关于
xin
yin
p=yinxin
(长期订单到期执行结算时
p
是常数)和
pammStart
的公式:

pammEnd=p(e2xinyink+ce2xinyinkc)2

=p(e2pxinL+ce2pxinLc)2

c=pammStartppammStart+p=c

再次计算:

pammEnd=pe2pxinL+ce2pxinLc

=pe2yinLp+ce2yinLpc>0

反向计算求得

xin
yin
计算公式:

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

经过分析,

pammStartpammEndpammEndp>0

上述不等式说明

pammEnd 的值介于
p
pammStart
之间(排除
p
pammStart
pammEnd
都相等的情况)。

另外,通过对

pammEnd 求导,我们可以获得
pammEnd
xin
yin
的变化情况:

dpammEnddxin=4pcL(e2pxinLc)2

dpammEnddyin=4cL(e2yinLpc)2

上面说明,如果

p>pammStart,则
pammEnd
单增,反之则单减。

最后,

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

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

我们可以得到

xout
yout
的表达式:

xout=LpammStart+xinLpammEnd

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

yout=LpammStart+yinLpammEnd

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

如果

yin,p0,xin0,通过 L'Hôpital's Rule 求极限我们可以得到即时兑换的结果:

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

=LpammEndLpammStart

yin=0

xout=0

yout=LpammStartLpammEnd

参考