> ## Documentation Index
> Fetch the complete documentation index at: https://docs.meteora.ag/llms.txt
> Use this file to discover all available pages before exploring further.

# DAMM v2 Formulas

> Mathematical formulas for liquidity addition/removal, pricing, and LP token calculation in DAMM v2 concentrated liquidity pools.

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:

```math theme={"system"}
\Delta a = \Delta L \times \left(\frac{1}{\sqrt{P}} - \frac{1}{\sqrt{P_{\text{max}}}}\right)
```

```math theme={"system"}
\Delta b = \Delta L \times (\sqrt{P} - \sqrt{P_{\text{min}}})
```

# LP Token Amount

Example from token B:

```math theme={"system"}
\text{Token B Amount} = \frac{\text{LP Token} × (\text{currentSqrtPrice} - \text{minSqrtPrice})}{2^{128}}
```

so you can reverse:

```math theme={"system"}
\text{LP Token} = \frac{\text{Token B Amount} × 2^{128}}{\text{currentSqrtPrice} - \text{minSqrtPrice}}
```

# Farming Rewards

The farming rewards are calculated as:

<div style={{ fontSize: "0.85em" }}>
  ```math theme={"system"}
  \text{Reward per token} = \frac{\text{Elapsed Time} \times \text{Reward Rate} \times 2^{128}}{\text{Total Liquidity}}
  ```
</div>

<div style={{ fontSize: "0.85em" }}>
  ```math theme={"system"}
  \text{User reward} = \text{Position Liquidity} \times (\text{Current Reward per Token} - \text{User Last Reward per Token}) \gg 192
  ```
</div>

# Compounding Fee Mode

When a pool uses Compounding Fee Mode (`collect_fee_mode = 2`), it operates as a standard constant-product AMM with no concentrated price range. The pool reserves grow over time as a portion of trading fees are automatically reinvested.

## Constant-Product Formula

The pool maintains the invariant:

```math theme={"system"}
\text{token\_a\_amount} \times \text{token\_b\_amount} = k
```

where $k$ is a constant. Unlike the standard DAMM v2 concentrated liquidity formula, there is no `sqrt_min_price` or `sqrt_max_price` bound — liquidity spans the full price range.

<Note>Pools with `layout_version == 1` track `token_a_amount` and `token_b_amount` directly in the pool state, enabling accurate reserve accounting for the compounding mechanism.</Note>

## Fee Split Formulas

The protocol fee is first subtracted from the total trading fee. The compounding/claiming split then applies to the LP fee (after protocol fee is removed):

```math theme={"system"}
\text{protocol\_fee} = f_s \times \frac{\text{protocol\_fee\_percent}}{100}
```

```math theme={"system"}
\text{lp\_fee} = f_s - \text{protocol\_fee}
```

```math theme={"system"}
\text{compounding\_fee} = \text{lp\_fee} \times \frac{\text{compounding\_fee\_bps}}{10000}
```

```math theme={"system"}
\text{claiming\_fee} = \text{lp\_fee} - \text{compounding\_fee}
```

The compounding fee is added directly to the pool reserves, increasing $k$ over time and benefiting all LPs proportionally.

# Price Impact

The price impact for a swap can be calculated as:

```math theme={"system"}
\text{Price Impact} = \frac{|\text{New Sqrt Price} - \text{Old Sqrt Price}|}{\text{Old Sqrt Price}}
```
