LPs earn a swap fee from traders when they perform a swap. The total swap fee $(f_s)$ will have two components: a **base fee** $(f_b)$ and a **variable fee **$(f_v)$. Total swap fee $f_s = f_b + f_v$

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.

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.

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

$100 bin - ID = 1000; $k$ = 0

$101 bin - ID = 1001; $k$ = 1

$102 bin - ID = 1002; $k$ = 2

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

The variable fee for a bin $f_v(k)$will be calculated using the **variable fee control** parameter $(A)$, **bin step** $(s)$and **volatility accumulator** $(v_a(k))$:

$f_v(k) = A(v_a(k) \cdot s)^2$

Volatility Accumulator $(v_a(k))$ captures instantaneous volatility.

Variable Fee Control Parameter $A$ is used to scale the variable fee component depending on the expected dynamics of the market.

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:

**Volatility Reference **$(v_r)$ from the previous swaps

**Introduced Volatility** $(|i_r - (activeID + k)|)$

$v_a(k) = v_r + |i_r - (activeID + k) |$

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

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}$

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\\ \mathit{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:

$fee = (\mathit{swap\
amount})_k\cdot (f_b+f_v)_k$

Volatility Accumulator: $v_a(k) = v_r + |i_r - (\mathit{activeID} + k) |$

$t$ is time elapsed since last transaction

R is the factor that decays $v_a$, the volatility accumulator $R \cdot v_a$ when $t_f≤t<t_d$

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

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

$\begin{align*}
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{align*}$

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

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

$\begin{align*}
i_r&=103\\v_r&=0.5\cdot3=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\\ & \cdots \\v_a(5) &= 1.5 + |103 - (103 + 5)| = 6.5
\end{align*}$

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

Jane makes a trade 0.3 seconds later that crosses -2 bins to 106. As $t = 0.3$, which is $< t_f$ of 1 second, $i_r$ and $v_r$ stays the same. So $-2≤k≤0$:

$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 2)| = 4.5$

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