skip to content

From Uniswap v3 to crvUSD LLAMMA

By 0xmc,0xjezex,0xstan@0xreviews, paco

Origin paper: 《From Uniswap v3 to crvUSD LLAMMA.pdf》

Preface: The most difficult part of Curve stable coin is LLAMMA (AMM for continuous liquidation/deliquidation). LLAMMA refers to some of the principles in Uniswap v3. However, the price in the white paper is different from the mathematics in the Uniswap v3 white paper. We will unify these two projects and try to figure out how Curve CEO designed the algorithm.

Refer to Uniswap v3

The definitions of price in this article and Uniswap v3 are reciprocals of each other. Therefore, we have modified the formulas in the Uniswap v3 white paper to make them consistent with this article. In short, LLAMMA tries to make everything dynamic in Uniswap v3 to give a more preferable price for both crvUSD debtors and liquidators.

Compare the constant product formulas

Formula (2.2) from Uniswap v3 whitepaper:

(x+LPb)(y+LPa)=L2\begin{aligned} (x+\dfrac{L}{\sqrt{P_b}})(y+L\sqrt{P_a})=L^2 \end{aligned}

Uniswap v3 Simulation of Virtual Liquidity

Formula (1) in the Curve stablecoin whitepaper :

I=(x+f)(y+g)\begin{aligned} I = (x + f) (y + g) \end{aligned}

AMM with an external price source

Here PcdP_{cd} means Pcurrent_downP_{current\_down}, PcuP_{cu} means Pcurrent_upP_{current\_up}.
The corresponding relationship is:

Pb=1Pcd,Pa=1Pcu,L=IP_{b} = \dfrac{1}{P_{cd}}, P_{a} = \dfrac{1}{P_{cu}}, L=\sqrt{I}

The corresponding constant product formula is:

(x+Ipcd)(y+Ipcu)=I\begin{aligned} (x+\sqrt{I}\sqrt{p_{cd}})(y+\dfrac{\sqrt{I}}{\sqrt{p_{cu}}})=I \end{aligned}

Among them:

f=Ipcd,g=Ipcuf=\sqrt{I}\sqrt{p_{cd}}, g=\dfrac{\sqrt{I}}{\sqrt{p_{cu}}}

Liquidity Calculation Formula Correspondence

Formula (6.7) from Uniswap v3 whitepaper:

L=ΔyΔp\begin{aligned} L=\dfrac{\Delta{y}}{\Delta{\sqrt{p}}} \end{aligned}

Because of the reciprocal relationship between their price definitions, it corresponds to the formula:

I=ΔyΔ1p\begin{aligned} \sqrt{I} = \dfrac{\Delta{y}} {\Delta{\sqrt{\dfrac{1}{p}}}} \end{aligned}

One specific application of this formula is:

I=(y00)(1pcd1pcu)=pcdpcupcupcdy0\begin{aligned} \sqrt{I} = &\dfrac{(y_0-0)}{(\dfrac{1}{\sqrt{p_{cd}}}-\dfrac{1}{\sqrt{p_{cu}}})} \\ = &\dfrac{\sqrt{p_{cd}p_{cu}}}{\sqrt{p_{cu}}-\sqrt{p_{cd}}} y_0 \end{aligned}

Do square expansion to get:

I=pcdpcupcu+pcd2pcdpcuy02\begin{aligned} I= \dfrac{{p_{cd}p_{cu}}}{p_{cu}+p_{cd}-2\sqrt{p_{cd}p_{cu}}} y_0^2 \end{aligned}

From the above formulas, we can easily find that when y0y_0 remains constant, The closer pcdp_{cd} and pcup_{cu} are, the greater the corresponding liquidity II.

In another word:

limpcdpcuI=+\lim\limits_{p_{cd} \to p_{cu}} \sqrt{I}= +\infty

The liquidity cannot be infinite, and the corresponding minimum tick in Uniswap v3 will limit the size of LL.

From this, it can be deduced that in LLAMMA, we also need to define an indicator to measure the minimum difference between prices, and continue the analogy between Uniswap v3 and Curve.

Correspondence between the minimum price difference

pp=A1A\dfrac{p\downarrow}{p\uparrow} = \dfrac{A-1}{A}

It can be seen from the definition of AA, the closer pp\downarrow and pp\uparrow are, that is, the larger A is, the higher the liquidity concentration:

A=pppA = \dfrac{p\uparrow}{p\uparrow - p\downarrow}

In Uniswap v3, only ticks with indexes that are divisible by tickSpacing can be initialized. Thus, tickSpacing determines the minimum price range for LPs to allocate their liquidty. The smaller the tickSpacing is, the tighter and more precise the price ranges. In Uniswap v3 different fee tiers determine different tickSpacing.

However, there is no need for crvUSD LLAMMA to have so many tickSpacing. Just make every tickSpacing=100basepointtickSpacing = 100 base point since LLAMMA is just for ETH-crvUSD. Formula (6.1) from Uniswap v3:

pi=1.0001ip_i=1.0001^i

In LLAMMA, A=100, Formula (11) from Curve stablecoin whitepaper:

p(n)=(A1A)npbase\begin{aligned} p\uparrow (n) = (\dfrac{A-1}{A})^n p_{base} \end{aligned}
p(n)=(A1A)n+1pbase\begin{aligned} p\downarrow (n) = (\dfrac{A-1}{A})^{n+1} p_{base} \end{aligned}

Set n=in = -i and A=100A=100, we have:

p(i)=(1001001)ipbase    \begin{aligned} p\uparrow (-i) = (\dfrac{100}{100-1})^i p_{base}\ \ \ \ \end{aligned}

Design pcdp_{cd} and pcup_{cu}

We hope that LLAMMA has the following properties: when the price of ETH rises, the pool buys ETH. When ETH falls, the pool sells ETH. Given this, we define pcdp_{cd} and pcup_{cu} as functions of pop_o and are steeper than linear functions, so their growth rates will be faster than pop_o. At the same time, it can be seen from the figure that the two curves pcup_{cu} and pcdp_{cd} pass through two points (p,p)(p\downarrow,p\downarrow) and (p,p)(p\uparrow,p\uparrow) respectively. The pcdp_{cd} and pcup_{cu} that meet the above requirements actually have many curves. The general formula is:

pcd=pon+1pn,pcu=pom+1pmp_{cd} = \dfrac{p_o^{n+1}}{p\uparrow^n}, p_{cu} = \dfrac{p_o^{m+1}}{p\downarrow^{m}}

where m<nm<n.

Let's start from the simplest case:

pcd=po2p,pcu=po2pp_{cd} = \dfrac{p_o^2}{p\uparrow}, p_{cu} = \dfrac{p_o^2}{p\downarrow}

Substitute pcup_{cu} and pcdp_{cd} into the square expansion of II:

I=pcdpcupcu+pcd2pcdpcuy02=po2ppo2py02po2p+po2p2po2ppo2p=po2y02p+p2pp=po2y02(pp)2\begin{aligned} I= &\dfrac{{p_{cd}p_{cu}}}{p_{cu}+p_{cd}-2\sqrt{p_{cd}p_{cu}}} y_0^2 \\ = &\dfrac{\dfrac{p_o^2}{p\uparrow}\dfrac{p_o^2}{p\downarrow} y_0^2}{\dfrac{p_o^2}{p\uparrow} + \dfrac{p_o^2}{p\downarrow} - 2\sqrt{\dfrac{p_o^2}{p\uparrow}\dfrac{p_o^2}{p\downarrow}}}\\ =&\dfrac{p_o^2 y_0^2}{p\downarrow+p\uparrow - 2\sqrt{p\downarrow p\uparrow}}\\ =&\dfrac{p_o^2 y_0^2}{(\sqrt{p\uparrow}-\sqrt{p\downarrow})^2} \end{aligned}

Then f2f^2 can be calculated as:

f2=Ipcd=po2y02(pp)2po2p\begin{aligned} f^2=&Ip_{cd}\\ =&\dfrac{p_o^2 y_0^2}{(\sqrt{p\uparrow}-\sqrt{p\downarrow})^2}·\dfrac{p_o^2}{p\uparrow} \end{aligned}

It is not difficult to find that f2f^2 is hard to comprehend and calculate under this assumption. What if pcdp_{cd} and pcup_{cu} are cubic functions of pop_o:

pcd=po3p2,pcu=po3p2p_{cd} = \dfrac{p_o^3}{p\uparrow^2}, p_{cu} = \dfrac{p_o^3}{p\downarrow^2}

Substitute pcup_{cu} and pcdp_{cd} into the square expansion of II:

I=pcdpcupcu+pcd2pcdpcuy02=po3p2po3p2y02po3p2+po3p22po3p2po3p2=po3y02p2+p22pp=po3y02(pp)2\begin{aligned} I= &\dfrac{{p_{cd}p_{cu}}}{p_{cu}+p_{cd}-2\sqrt{p_{cd}p_{cu}}} y_0^2 \\ = &\dfrac{\dfrac{p_o^3}{p\uparrow^2}\dfrac{p_o^3}{p\downarrow^2} y_0^2}{\dfrac{p_o^3}{p\uparrow^2} + \dfrac{p_o^3}{p\downarrow^2} - 2\sqrt{\dfrac{p_o^3}{p\uparrow^2}\dfrac{p_o^3}{p\downarrow^2}}}\\ =&\dfrac{p_o^3 y_0^2}{p\downarrow^2+p\uparrow^2-2p\downarrow p\uparrow}\\ =&\dfrac{p_o^3 y_0^2}{(p\uparrow-p\downarrow)^2} \end{aligned}

Recalculate f2f^2 :

f2=Ipcd=po3y02(pp)2po3p2=po6y02(pp)2p2\begin{aligned} f^2=&Ip_{cd}\\ =&\dfrac{p_o^3 y_0^2}{(p\uparrow-p\downarrow)^2}·\dfrac{p_o^3}{p\uparrow^2}\\ =&\dfrac{p_o^6 y_0^2}{(p\uparrow-p\downarrow)^2 p\uparrow^2} \end{aligned}

It can be seen that when pcdp_{cd} and pcup_{cu} are cubic functions of pop_o, the whole mathematical form is much simpler. The square root term is eliminated, and the calculation is much more convenient. If a higher order is taken, the price of AMM and pop_o will differ greatly, and thus the cost of buying ETH (when the price rises) will be much higher, which leads to a greater loss of liquidation. In summary, it is a better choice to define pcdp_{cd} and pcup_{cu} as cubic functions of pop_o.

Derivation of other parameters

On the basis of assuming that pcdp_{cd} and pcup_{cu} are cubic functions about pop_o, taking the special value po=pp_o=p\uparrow, it is not difficult to obtain that y=y0y=y_0 and x=0x=0, then:

I=po3y02(pp)2=poy02po2(pp)2=poy02(ppp)2=poy02A2\begin{aligned} I=&\dfrac{p_o^3 y_0^2}{(p\uparrow-p\downarrow)^2}\\ =&p_o y_0^2 · \dfrac{p_o^2}{(p\uparrow-p\downarrow)^2}\\ =&p_o y_0^2 · (\dfrac{p\uparrow}{p\uparrow-p\downarrow})^2\\ =&p_oy_0^2A^2 \end{aligned}

Given the fomula of II, we can calculate ff and gg :

f=Ipcd=poy02A2po3p2=po2pAy0\begin{aligned} f=&\sqrt{I}\sqrt{p_{cd}} \\ =&\sqrt{p_oy_0^2A^2}\sqrt{\dfrac{p_o^3}{p\uparrow^2}}\\ =&\dfrac{p_o^2}{p\uparrow}Ay_0 \end{aligned}
g=Ipcu=poy02A2po3p2=ppoAy0=ppo(A1)y0\begin{aligned} g =& \dfrac{\sqrt{I}}{\sqrt{p_{cu}}}\\ =& \dfrac{\sqrt{p_oy_0^2A^2}}{\sqrt{\dfrac{p_o^3}{p\downarrow^2}}}\\ =&\dfrac{p\downarrow}{p_o}Ay_0\\ =&\dfrac{p\uparrow}{p_o}(A-1)y_0 \end{aligned}

From this, we finally get the complete constant product formula:

(po2pAy0+x)(ppo(A1)y0+y)=poA2y02(\dfrac{p_o^2}{p\uparrow}Ay_0 + x)(\dfrac{p\uparrow}{p_o}(A-1)y_0 + y) = p_oA^2y_0^2

Transform the above equation into a quadratic equation of y0y_0:

poAy02y0(ppo(A1)x+po2pAy)xy=0p_oAy_0^2- y_0(\dfrac{p\uparrow}{p_o}(A-1)x + \dfrac{p_o^2}{p\uparrow}Ay) - xy = 0

Use the quadratic equation of one unknown to solve y0y_0:

y0=(ppo(A1)x+po2pAy)+(ppo(A1)x+po2pAy)2+4poAxy2poAy_0 = \dfrac{(\dfrac{p\uparrow}{p_o}(A-1)x + \dfrac{p_o^2}{p\uparrow}Ay)+\sqrt{(\dfrac{p\uparrow}{p_o}(A-1)x + \dfrac{p_o^2}{p\uparrow}Ay)^2+4p_oA·xy}}{2p_oA}

If the price moves so slowly that the oracle price pop_o is fully capable to follow it, given xx and yy, using the calculation formula of Uniswap v3, it is possible to calculate how much yy\uparrow of ETH (if the price rises) or xx\downarrow of USD will eventually be in the band (if price drops):

y=y+Δy=y+I(1p1p)=y+IpIppp=y+(f+x)fpp=y+xpp\begin{aligned} y\uparrow =& y + \Delta y \\ =& y + \sqrt{I}(\dfrac{1}{\sqrt{p\uparrow}} - \dfrac{1}{\sqrt{p}}) \\ =& y + \dfrac{\sqrt{I}\sqrt{p} - \sqrt{I}\sqrt{p\uparrow}}{\sqrt{p\uparrow p}} \\ =& y + \dfrac{(f +x) - f}{\sqrt{p\uparrow p}} \\ =& y + \dfrac{x}{\sqrt{p\uparrow p}} \end{aligned}
x=x+I(pp)=x+I(1p1p)pp=x+((g+y)g)pp=x+ypp\begin{aligned} x\downarrow =& x + \sqrt{I}(\sqrt{p\downarrow}- \sqrt{p}) \\ =& x + \sqrt{I}(\dfrac{1}{\sqrt{p}} - \dfrac{1}{\sqrt{p\downarrow}})\sqrt{p\downarrow p}\\ =& x + ((g+y) - g)\sqrt{p\downarrow p}\\ =& x + y\sqrt{p\downarrow p} \end{aligned}

Reference

[1] Adams, Hayden, et al. "Uniswap v3 core." Tech. rep., Uniswap, Tech. Rep, 2021 from https://uniswap.org/whitepaper-v3.pdf

[2] Egorov, Michael, and Curve Finance. Curve stablecoin design. Technical report, Curve Finance, Tech. Rep, 2022 from
https://github.com/curvefi/curve-stablecoin/blob/master/doc/curve-stablecoin.pdf