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 (fs)(f_s) will have two components: a base fee (fb)(f_b) and a variable fee (fv)(f_v). Total swap fee fs=fb+fvf_s = f_b + f_v

The variable fee (fv)(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):

fb=Bsf_b=B \cdot 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 nn bins, total swap fee is calculated per bin kk (such that 0kn0\leq k\leq n), where kk 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 kk for each bin is illustrated below:

$100 bin - ID = 1000; kk = 0

$101 bin - ID = 1001; kk = 1

$102 bin - ID = 1002; kk = 2

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

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

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

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

Variable Fee Control Parameter AA 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 (va(k))(v_a(k)) allows us to use bin crossovers as a measure of volatility beyond a single transaction.

Think of the Volatility Accumulator (va(k))(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 (vr)(v_r) from the previous swaps

  • Introduced Volatility (ir(activeID+k))(|i_r - (activeID + k)|)

Volatility Accumulater:

va(k)=vr+ir(activeID+k)v_a(k) = v_r + |i_r - (activeID + k) |

(Note: activeIDactiveID is the ID of the active bin before the swap is made.)

Volatility Reference

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

If tt is smaller than the filter period(tf)(t_f) (this indicates a high frequency of transactions occurring), then vrv_rstays the same.

If tt is greater than the decay period(td)(t_d) (this indicates a low frequency of transactions occurring), then vrv_ris reset to 0.

If tt is within the window bounded by tft_f & tdt_d, then vrv_r takes the previous value of vav_a decayed by a factor RR

vr={vr,t<tfRva,tft<td0,tdtv_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 (ir)(i_r) to calculate the volatility introduced by the trade. In most cases, iri_r will be the ID of the active bin before the swap is made. In times of high frequency transactions, iri_rwill 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.

ir={ir,t<tfactiveID,tfti_r=\begin{cases}i_r,&t<t_f\\ \mathit{activeID},&t_f\leq t\end{cases}

The volatility accumulated vrv_r for the bin kk 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 kk will be:

fee=(swap amount)k(fb+fv)kfee = (\mathit{swap\ amount})_k\cdot (f_b+f_v)_k

Volatility Accumulator Example

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

Quick Recap:

  • Volatility Accumulator: va(k)=vr+ir(activeID+k)v_a(k) = v_r + |i_r - (\mathit{activeID} + k) |

  • tt is time elapsed since last transaction

  • tft_f is filter period

  • tdt_d is decay period

  • R is the factor that decays vav_a, the volatility accumulator RvaR \cdot v_a when tft<tdt_f≤t<t_d

Let tft_f = 1 sec, tdt_d = 5 secs, RR = 0.5 sec, and active bin ID is 100.

Swap 1

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

ir=100vr=0va(0)=0+100(100+0)=0va(1)=0+100(100+1)=1va(2)=0+100(100+2)=2va(3)=0+100(100+3)=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, va=3v_a = 3.

Swap 2

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

ir=103vr=0.53=1.5va(0)=1.5+103(103+0)=1.5va(1)=1.5+103(103+1)=2.5va(2)=1.5+103(103+2)=3.5va(5)=1.5+103(103+5)=6.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, va=6.5v_a = 6.5.

Swap 3

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

ir=103vr=1.5va(0)=1.5+103(108+0)=6.5va(1)=1.5+103(1081)=5.5va(2)=1.5+103(10822)=4.5i_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, va=4.5v_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