# Dynamic Fees

## Introduction

DLMM implements dynamic fees with surge pricing based on market fluctuations and volatility.

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.

LPs can claim these fees whenever they are available.

## Base Fee

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

Base factor is the amplification to add to the bin step, to allow for adjustment of the pool base fees.

## Variable 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:

$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))$:

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 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:

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

**Volatility Accumulater:**

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

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.

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:

### Volatility Accumulator 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 - (\mathit{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≤t<t_d$

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

**Swap 1**

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

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≤k≤5$:

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$, which is $< t_f$ of 1 second, $i_r$ and $v_r$ stays the same. So $-2≤k≤0$:

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

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

## Protocol Fees

Protocol fees will be a percentage of the total swap fees.

Last updated