DAMM v2 is a constant product AMM that operates between a sqrt_min_price and a sqrt_max_price.

Liquidity Addition / Removal

When adding or removing liquidity, for a given liquidity delta ΔL, the required token amounts are calculated as:

Δa=ΔL×(1P1Pmax)\Delta a = \Delta L \times \left(\frac{1}{\sqrt{P}} - \frac{1}{\sqrt{P_{\text{max}}}}\right) Δb=ΔL×(PPmin)\Delta b = \Delta L \times (\sqrt{P} - \sqrt{P_{\text{min}}})

LP Token Amount

Example from token B:

Token B Amount=LP Token×(currentSqrtPriceminSqrtPrice)2128\text{Token B Amount} = \frac{\text{LP Token} × (\text{currentSqrtPrice} - \text{minSqrtPrice})}{2^{128}}

so you can reverse:

LP Token=Token B Amount×2128currentSqrtPriceminSqrtPrice\text{LP Token} = \frac{\text{Token B Amount} × 2^{128}}{\text{currentSqrtPrice} - \text{minSqrtPrice}}

Farming Rewards

The farming rewards are calculated as:

Reward per token=Elapsed Time×Reward Rate×264Total Liquidity\text{Reward per token} = \frac{\text{Elapsed Time} \times \text{Reward Rate} \times 2^{64}}{\text{Total Liquidity}}
User reward=Position Liquidity×(Current Reward per TokenUser Last Reward per Token)64\text{User reward} = \text{Position Liquidity} \times (\text{Current Reward per Token} - \text{User Last Reward per Token}) \gg 64

Price Impact

The price impact for a swap can be calculated as:

Price Impact=New Sqrt PriceOld Sqrt PriceOld Sqrt Price\text{Price Impact} = \frac{|\text{New Sqrt Price} - \text{Old Sqrt Price}|}{\text{Old Sqrt Price}}