Preface:
The most difficult part of Curve’s stablecoin design is LLAMMA, an AMM for continuous liquidation and deliquidation. LLAMMA builds on several ideas from Uniswap v3, but the price definitions in the Curve whitepaper differ from those used in the Uniswap v3 whitepaper. This article reconciles the two frameworks and uses that comparison to explain the intuition behind the LLAMMA algorithm.
Relation to Uniswap v3
The price definition used in this article is the reciprocal of the one used in Uniswap v3. Therefore, we rewrite the relevant formulas from the Uniswap v3 whitepaper so they are consistent with this article. In short, LLAMMA makes several Uniswap v3 parameters dynamic in order to offer better prices to both crvUSD borrowers and liquidators.
Comparing the Constant Product Formulas
Formula (2.2) from the 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 and Pcu means Pcurrent_up.
The corresponding relationships are:
Pb=Pcd1,Pa=Pcu1,L=I
The corresponding constant product formula is:
(x+Ipcd)(y+pcuI)=I
where:
f=Ipcd,g=pcuI
Correspondence in Liquidity Calculation
Formula (6.7) from the Uniswap v3 whitepaper:
L=ΔpΔy
Because the two price definitions are reciprocals, this corresponds to:
From the formulas above, when y0 remains constant, the closer pcd and pcu are to each other, the larger the corresponding liquidity I becomes.
In other words:
pcd→pculimI=+∞
Liquidity cannot be infinite. In Uniswap v3, the minimum tick spacing limits the size of L.
By analogy, LLAMMA also needs an indicator that measures the minimum price difference.
Correspondence in the Minimum Price Difference
p↑p↓=AA−1
From the definition of A, the closer p↓ and p↑ are to each other, the larger A becomes and the more concentrated the liquidity is:
A=p↑−p↓p↑
In Uniswap v3, only ticks whose indexes are divisible by tickSpacing can be initialized. Thus, tickSpacing determines the minimum price range in which LPs can allocate their liquidity. The smaller the tickSpacing, the tighter and more precise the price ranges. In Uniswap v3, different fee tiers correspond to different tickSpacing values.
However, crvUSD LLAMMA does not need many different tickSpacing values. Since LLAMMA is designed for ETH-crvUSD, each band can use a tickSpacing of 100 basis points. Formula (6.1) from Uniswap v3:
pi=1.0001i
In LLAMMA, A=100. Formula (11) from the Curve stablecoin whitepaper is:
p↑(n)=(AA−1)npbasep↓(n)=(AA−1)n+1pbase
Setting n=−i and A=100, we get:
p↑(−i)=(100−1100)ipbase
Design pcd and pcu
We want LLAMMA to have the following behavior: when the ETH price rises, the pool buys ETH; when the ETH price falls, the pool sells ETH. Given this goal, we define pcd and pcu as functions of po that are steeper than linear functions, so their growth rates are faster than that of po. At the same time, the figure shows that the two curves pcu and pcd pass through (p↓,p↓) and (p↑,p↑), respectively. Many possible curves can satisfy these requirements. The general form is:
pcd=p↑npon+1,pcu=p↓mpom+1
where m<n.
Let’s start with the simplest case:
pcd=p↑po2,pcu=p↓po2
Substituting pcu and pcd into the expanded formula for I gives:
When pcd and pcu are cubic functions of po, the mathematical form becomes much simpler. The square-root term disappears, making the calculation more convenient. If we use a higher-order function, the AMM price and po will diverge more significantly. As a result, the cost of buying ETH when the price rises becomes much higher, leading to greater liquidation losses. Therefore, defining pcd and pcu as cubic functions of po is a better choice.
Derivation of Other Parameters
Assuming that pcd and pcu are cubic functions of po, take the special case po=p↑. In this case, we get y=y0 and x=0, so:
If the price moves slowly enough for the oracle price po to follow it, then given x and y, we can use the Uniswap v3 formulas to calculate how much ETH, denoted by y↑, will eventually remain in the band if the price rises, or how much USD, denoted by x↓, will eventually remain in the band if the price falls: