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+PbL)(y+LPa)=L2
Uniswap v3 Simulation of Virtual Liquidity
Formula (1) in the Curve stablecoin whitepaper :
I=(x+f)(y+g)
AMM with an external price source
Here Pcd means Pcurrent_down, Pcu means Pcurrent_up.
The corresponding relationship is:
Pb=Pcd1,Pa=Pcu1,L=I
The corresponding constant product formula is:
(x+Ipcd)(y+pcuI)=I
Among them:
f=Ipcd,g=pcuI
Liquidity Calculation Formula Correspondence
Formula (6.7) from Uniswap v3 whitepaper:
L=ΔpΔy
Because of the reciprocal relationship between their price definitions, it corresponds to the formula:
From the above formulas, we can easily find that when y0 remains constant, The closer pcd and pcu are, the greater the corresponding liquidity I.
In another word:
pcd→pculimI=+∞
The liquidity cannot be infinite, and the corresponding minimum tick in Uniswap v3 will limit the size of L.
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
p↑p↓=AA−1
It can be seen from the definition of A, the closer p↓ and p↑ are, that is, the larger A is, the higher the liquidity concentration:
A=p↑−p↓p↑
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=100basepoint since LLAMMA is just for ETH-crvUSD. Formula (6.1) from Uniswap v3:
pi=1.0001i
In LLAMMA, A=100, Formula (11) from Curve stablecoin whitepaper:
p↑(n)=(AA−1)npbase
p↓(n)=(AA−1)n+1pbase
Set n=−i and A=100, we have:
p↑(−i)=(100−1100)ipbase
Design pcd and pcu
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 pcd and pcu as functions of po and are steeper than linear functions, so their growth rates will be faster than po. At the same time, it can be seen from the figure that the two curves pcu and pcd pass through two points (p↓,p↓) and (p↑,p↑) respectively. The pcd and pcu that meet the above requirements actually have many curves. The general formula is:
pcd=p↑npon+1,pcu=p↓mpom+1
where m<n.
Let's start from the simplest case:
pcd=p↑po2,pcu=p↓po2
Substitute pcu and pcd into the square expansion of I:
It can be seen that when pcd and pcu are cubic functions of po, 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 po 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 pcd and pcu as cubic functions of po.
Derivation of other parameters
On the basis of assuming that pcd and pcu are cubic functions about po, taking the special value po=p↑, it is not difficult to obtain that y=y0 and x=0, then:
If the price moves so slowly that the oracle price po is fully capable to follow it, given x and y, using the calculation formula of Uniswap v3, it is possible to calculate how much y↑ of ETH (if the price rises) or x↓ of USD will eventually be in the band (if price drops):