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 *P**cd *means *P**current down*, *P**cu *means *P**current 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 *y*0 remains constant,

The closer *p**cd *and *p**cu *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 *= 100*basepoint *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 ***p**cd ***and ***p**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 *p**cd *and *p**cu *as functions of *p**o *and are steeper than linear functions, so

their growth rates will be faster than *p**o*. At the same time, it can be seen from

the figure that the two curves *p**cu *and *p**cd *pass through two points (*p **↓**, p **↓*)

and (*p **↑**, p **↑*) respectively. The *p**cd *and *p**cu *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 *p**cd *and *p**cu *are cubic functions of *p**o*:

Substitute *pcu *and *pcd *into the square expansion of *I*:

Recalculate *f *2 :

It can be seen that when *p**cd *and *p**cu *are cubic functions of *p**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 *p**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 *p**cd *and *p**cu *as cubic functions of *p**o*.

**6 Derivation of other parameters**

On the basis of assuming that *p**cd *and *p**cu *are cubic functions about *p**o*, taking

the special value *p**o *= *p **↑*, it is not difficult to obtain that *y *= *y*0 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 *y*0:

Use the quadratic equation of one unknown to solve *y*0:

If the price moves so slowly that the oracle price *p**o *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