Crack the math magic of Michael Egorov’s design
by 0xmc, 0xJezex, 0xstan@0xreviews.xyz, paco@perp.com
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.
1 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.
2 Compare the constant product formulas
Formula (2.2) from Uniswap v3 whitepaper:
Formula (1) in the Curve stablecoin whitepaper :
Here Pcd means Pcurrent down, Pcu means Pcurrent up.
The corresponding relationship is:
The corresponding constant product formula is:
Among them:
3 Liquidity Calculation Formula Correspondence
Formula (6.7) from Uniswap v3 whitepaper:
Because of the reciprocal relationship between their price definitions, it
corresponds to the formula:
One specific application of this formula is:
Do square expansion to get:
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:
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.
4 Correspondence between the minimum price difference
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:
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 tickSpac
ing. Just make every tickSpacing = 100basepoint since LLAMMA is just for
ETH-crvUSD. Formula (6.1) from Uniswap v3:
In LLAMMA, A=100, Formula (11) from Curve stablecoin whitepaper:
Set n = -i and A=100, we have:
5 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:
where m < n.
Let’s start from the simplest case:
Substitute pcu and pcd into the square expansion of I:
Then f 2 can be calculated as:
It is not difficult to find that f 2 is hard to comprehend and calculate under this
assumption. What if pcd and pcu are cubic functions of po:
Substitute pcu and pcd into the square expansion of I:
Recalculate f 2 :
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.
6 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:
Given the fomula of I, we can calculate f and g:
From this, we finally get the complete constant product formula:
Transform the above equation into a quadratic equation of y0:
Use the quadratic equation of one unknown to solve y0:
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)
References
[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