> ## 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.

# DLMM Fee Calculation

> How base fees and dynamic fees are calculated in DLMM pools, including the variable fee formula based on price volatility.

# Total Trading Fee

LPs earn a trading fee from traders when they perform a swap. The total trading fee ($f_s$) will have two components:

1. **A base fee** ($f_b$)
2. **A variable fee** ($f_v$)

The **total swap fee** is calculated as:

<div align="center">
  $f_s = f_b + f_v$
</div>

The variable fee ($f_v$) is a function of real-time price volatility. The fee rate will be applied to the swap amount in each liquidity bin and distributed proportionally to the LPs in that bin.

LPs can claim these fees whenever they are available.

## Base Fee

The minimum fee charged per swap. Lower fee gets more volume but higher fee earns more per volume. Generally if you want a higher base fee, the higher the bin step. But a lower base fee than other DEXes might give more flexibility for dynamic fees to optimize for volume or fees given the market volatility.

The base fee of a market is configured by the pool creator and determined by the base factor (B) and the bin step (s):

<div align="center">
  $f_b = B \cdot s \cdot 10 \cdot 10^{\text{base\_fee\_power\_factor}}$
</div>

Where:

* **Base Factor** ($B$): An amplification to add to the bin step, to allow for adjustment of the pool base fees.
* **Bin Step** ($s$): The price increment per bin, in basis points.
* **Base Fee Power Factor**: An exponent that scales the base fee calculation. Default is 0 for most pools. Higher values increase the base fee exponentially.

## Variable Fee (Dynamic Fee)

The variable fee depends on the volatility of the market where it is affected by swap frequencies as well as swaps that span across many bins. Fees are calculated and distributed on a **per bin** basis, to allow for a fair fee distribution to the LPs of each bin that is crossed.

For large swaps that span across bins, fees are calculated iteratively. This means that if a large swap crosses
$n$ bins, total swap fee is calculated per bin $k$ (such that $0 \leq k \leq n$), where $k$ is the difference in the bin IDs from the initial bin where the swap originated and the current bin in which it is being calculated.

For example, let's use SOL/USDC. We also assume each bin step is \$1 for simplicity. Let us assume that the active bin (ID = 1000) before swap is \$100. A large buy order was made that pushed the price up to \$102.

The value of $k$ for each bin is illustrated below:

<div align="center">
  | Bin Price | Bin ID | $k$ value |
  | :-------: | :----: | :-------: |
  |   \$100   |  1000  |     0     |
  |   \$101   |  1001  |     1     |
  |   \$102   |  1002  |     2     |
</div>

Note that $k$ can also be negative if the price moves downwards.

The variable fee for a bin $f_v(k)$ is calculated using:

* **Variable fee control parameter** ($A$)
* **Bin step** ($s$)
* **Volatility accumulator** ($v_a(k)$)

The formula is:

<div align="center">
  $f_v(k) = A(v_a(k) \cdot s)^2$
</div>

<Note>The on-chain computation uses ceiling division: it adds 99,999,999,999 before dividing by 100,000,000,000, ensuring the variable fee always rounds up.</Note>

where:

* **Volatility Accumulator** ($v_a(k)$): Captures instantaneous volatility
* **Variable Fee Control Parameter** ($A$): Used to scale the variable fee component depending on the expected dynamics of the market

### Volatility Accumulator

Volatility on the DLMM is derived from the number of bin changes that occur over time. Each bin change is a fixed price movement of the bin step. The **Volatility Accumulator** ($v_a(k)$) allows us to use bin crossovers as a measure of volatility beyond a single transaction.

Think of the **Volatility Accumulator** ($v_a(k)$) as a witness of the current volatility of the token pair. Between each calculation step, this value will be kept in memory. It is calculated during a swap and depends on two factors:

1. **Volatility Reference** ($v_r$) from the previous swaps
2. **Introduced Volatility** ($|i_r - (activeID + k)|$)

The Volatility Accumulator is calculated as:

<div align="center">
  $v_a(k) = v_r + |i_r - (activeID + k)|$
</div>

(Note: $activeID$ is the ID of the active bin **before** the swap is made.)

#### Volatility Reference

The **volatility reference** ($v_r$) depends on the time passed since the last transaction ($t$). We will define a window with an upper and lower bound. The lower bound of this window is defined as filter period ($t_f$), and upper bound as decay period ($t_d$).

If $t$ is smaller than the **filter period** ($t_f$) (this indicates a high frequency of transactions occurring), then $v_r$ stays the same.

If $t$ is greater than the **decay period** ($t_d$) (this indicates a low frequency of transactions occurring), then $v_r$ is reset to 0.

If $t$ is within the **window bounded** by $t_f$ & $t_d$, then $v_r$ takes the previous value of $v_a$ decayed by a factor $R$

$$
v_r = \begin{cases}
v_r, & t < t_f \\
R \cdot v_a, & t_f \leq t < t_d \\
0, & t_d \leq t
\end{cases}
$$

High frequency trades will stack up volatility, while low frequency trades will slowly reduce the volatility. If a long time has passed without any trade, the volatility will reset to 0.

We introduce a new variable, the **index reference** ($i_r$) to calculate the volatility introduced by the trade. In most cases, $i_r$ will be the ID of the active bin before the swap is made. In times of high frequency transactions, $i_r$ will keep its old value instead. Doing this will help prevent people from manipulating fees through making small lots of small transaction that increase and decrease price.

$$
i_r = \begin{cases}
i_r, & t < t_f \\
activeID, & t_f \leq t
\end{cases}
$$

The **volatility accumulated** $v_r$ for the bin $k$ will then be used to calculate the fees generated by swapping through this bin and be allocated to the liquidity providers of the bin.

The final fee for the bin $k$ will be:

<div align="center">
  $fee = (swap\ amount)_k \cdot (f_b + f_v)_k$
</div>

### Example

This example will show how the volatility accumulator will look as swaps are made.

**Quick Recap:**

* **Volatility Accumulator:** $v_a(k) = v_r + |i_r - (activeID + k)|$
* $t$ is time elapsed since last transaction
* $t_f$ is filter period
* $t_d$ is decay period
* $R$ is the factor that decays $v_a$, the volatility accumulator ($R \cdot v_a$) when $t_f \leq t < t_d$

Let $t_f = 1$ sec, $t_d = 5$ secs, $R = 0.5$, and active bin ID is 100.

#### **Swap 1**

You make a trade that crosses +3 bins to 103. So $0 \leq k \leq 3$:

$$
\begin{aligned}
i_r &= 100 \\
v_r &= 0 \\
v_a(0) &= 0 + |100 - (100 + 0)| = 0 \\
v_a(1) &= 0 + |100 - (100 + 1)| = 1 \\
v_a(2) &= 0 + |100 - (100 + 2)| = 2 \\
v_a(3) &= 0 + |100 - (100 + 3)| = 3
\end{aligned}
$$

At the end of swap 1, $v_a = 3$.

#### **Swap 2**

John makes a trade 4 seconds later that crosses +5 bins to 108. As $t = 4$, $v_r = R \cdot v_a$. So $0 \leq k \leq 5$:

$$
\begin{aligned}
i_r &= 103 \\
v_r &= 0.5 \cdot 3 = 1.5 \\
v_a(0) &= 1.5 + |103 - (103 + 0)| = 1.5 \\
v_a(1) &= 1.5 + |103 - (103 + 1)| = 2.5 \\
v_a(2) &= 1.5 + |103 - (103 + 2)| = 3.5 \\
\vdots \\
v_a(5) &= 1.5 + |103 - (103 + 5)| = 6.5
\end{aligned}
$$

At the end of swap 2, $v_a = 6.5$.

#### **Swap 3**

Jane makes a trade 0.3 seconds later that crosses -2 bins to 106. As $t = 0.3 < t_f = 1$ second, $i_r$ and $v_r$ are not reset. The active bin before this swap is 108. So $-2 \leq k \leq 0$:

$$
\begin{aligned}
i_r &= 103 \\
v_r &= 1.5 \\
v_a(0) &= 1.5 + |103 - (108 + 0)| = 6.5 \\
v_a(-1) &= 1.5 + |103 - (108 - 1)| = 5.5 \\
v_a(-2) &= 1.5 + |103 - (108 - 2)| = 4.5
\end{aligned}
$$

At the end of swap 3, $v_a = 4.5$.

You can see that the volatility accumulator is decreasing despite high frequency of transactions, preventing people from manipulating fees by making lots of small transactions that increase/decrease prices.

# DLMM Protocol Fees

Protocol Fee is a percentage of the total swap fee (base + variable fee).

## Market-maker (MM) positions

* **Standard Pools:** **10%** Protocol Fee, **90%** LP Fee
* **Launch Pools:** **20%** Protocol Fee, **80%** LP Fee

## Limit orders

* **50%** Protocol Fee, **50%** LO Fee

## Swaps

Swap hosts (such as Jupiter, Photon, or trading bots) can include a referral account in the swap transaction to receive a **Host Fee** equal to **20%** of the protocol fee.

Protocol fees are configured via the `protocol_share` parameter on each pool's preset parameters (measured in basis points of the total swap fee, max 2500 = 25%).

<Warning>
  **Breaking change in v0.11.0:** Bin state no longer stores `amount_x_in` and `amount_y_in` fields. The `reward_per_token_stored` fields have been renamed to `function_bytes`. Integrations that index bin state directly must update their deserialization logic.
</Warning>
