---
# System prepended metadata

title: Pro-AMM flatten formula

---

# Pro-AMM flatten formula

## The problem arise from existing formula
Our current model is based on this formula:
1. ${L_{fnew} = \sqrt{(x_f + deltaX.fee) * y_f}}$ 
This is approximate to:
2. ${L_{approximate} = L_f + \frac{deltaX.fee.\sqrt{p}}{2}}$
or 
${deltaL = \frac{deltaX.fee.\sqrt{p}}{2}}$

However, this approximate formula is only applied when new collected liquidity is very small compared to current collected liquidity.
- This is trivial because the collected liquidity will be very small for pool creation.
- The 1st one who created the pool has to lock liquidity there.
- The code base has to account for the accurate case. It is very hard to get the amount to pass a tick

So 1. should be
${L_{fnew} + L_p = \sqrt{(x_f + x_p + deltaX.fee) * (y_f + y_p)}}$
When do 2. to approximate this formula, we will get the same result:
${L_{approximate} = L_f + L_p + \frac{deltaX.fee.\sqrt{p}}{2}}$
or
${deltaL = \frac{deltaX.fee.\sqrt{p}}{2}}$

Note that fee = 1% and swap within a range of 5% change of price so ${{deltaL \over L_f + L_p} < 0.0005 }$

Or we can always use the approximate formula when a swap within a 5% change of price
Moreover, with a > 5% change of price, we will divide this swap into multiple small swaps each with 5% change of price (similar with swapping between multiple ticks)

## Another explaination for approximate formula 
- Uniswap v2 based on the formula:
${(x + deltaX*(1-fee)) * (y - deltaY) = L^2}$
- Derived formula: 
${(x + deltaX)(y -deltaY)= (L+deltaL)^2}$ (1)
 
with *deltaL* is calculated approximately by: 
$\large {deltaL \over L}  = {deltaX * fee \over 2x}$ (2)

### Calculate back and forth
Assume that
${L_2}$ - ${L_1}$ is the total liquidity after/before the swap
${p_2}$ - ${p_1}$ is price after/before the swap
${x_2}$ - ${x_1}$ is amount of token0 after/before the swap
#### Given input amount, calculate new sqrt of price  

$L_2 = {L_1 + deltaL} = {L_1 * (1 + {deltaX * fee \over 2 * x1})}$
$= L_1 *(1 + {deltaX * fee * \sqrt{p_1} \over 2 * L_1})$ 
$= L_1 + {deltaX * fee * \sqrt{p_1} \over 2}$ (3)
Finally calculate new $\sqrt{p}$ and deltaY
$\large \sqrt{p_2} = {L_2 \over x_1 + \Delta X} = {L_2 * \sqrt{p_1}\over L_1 + deltaX * \sqrt{p_1}}$ (4)
$deltaY = L_2 * \sqrt{p_2} - L1 * \sqrt{p_1}$
#### Given next tick price, calculate amount needed to cross the tick  

From (3) and (4)
$x_1 + deltaX = ({L1 + {deltaX * fee * \sqrt{p_1} \over 2}}) / \sqrt{p_2}$
so
${deltaX * (1 - \frac{fee * \sqrt{p_1} }{2 * \sqrt{p_2}}}) = L1 *(\frac{1}{\sqrt{p_2}} - \frac{1}{\sqrt{p_1}})$ 
${=> deltaX = {2{L_1} (\sqrt{p_1} - \sqrt{p_2}) \over \sqrt{p_1} (2\sqrt{p_2} - fee\sqrt{p_1})}}$ (5)
Alternatively, calculate $L_2$ first, then $deltaX$.
From (3), ${deltaX = \frac{2(L_2-L_1)}{fee\sqrt{p_1}}}$ (6)
Also, ${deltaX = \frac{L_2}{\sqrt{p_2}} - \frac{L_1}{\sqrt{p_1}}}$ 
${=> \frac{2(L_2-L_1)}{fee\sqrt{p_1}} = \frac{L_2}{\sqrt{p_2}} - \frac{L_1}{\sqrt{p_1}}}$
${\frac{2L_2}{fee\sqrt{p_1}} - \frac{L_2}{\sqrt{p_2}} = \frac{2L_1}{fee\sqrt{p_1}} - \frac{L_1}{\sqrt{p_1}} }$
${L_2(\frac{2\sqrt{p_2}-fee\sqrt{p_1}}{fee\sqrt{p_1}\sqrt{p_2}}) = \frac{L_1(2-fee)}{fee\sqrt{p_1}}}$
${L_2 = \frac{(2-fee)L_1\sqrt{p_2}}{2\sqrt{p_2}-fee\sqrt{p_1}}}$ (7)
Use (6) to calculate ${deltaX}$. Since (7) calculates ${fee\sqrt{p_1}}$, can avoid redoing this calculation in (6) as well.

With the formula 5 or 7, we can calculate the exact amount needed to cross a tick.

${deltaY = L_2*\sqrt{p_2} - L1*\sqrt{p_1}}$
${= L1* (\sqrt{p_2} - \sqrt{p_1}) + deltaX * fee * \sqrt{p_1} * \sqrt{p_2} / 2}$
${= L1*(\sqrt{p_2} - \sqrt{p_1}) * (1 - \frac{\sqrt{p_2}}{2 * \sqrt{p_2} - fee* \sqrt{p_1}}) }$

or

${deltaY = L1*(\sqrt{p_2} - \sqrt{p_1}) * \frac{\sqrt{p_2} - fee* \sqrt{p_1}}{2 * \sqrt{p_2} - fee* \sqrt{p_1}}}$


 



