LPs earn a swap fee from traders when they perform a swap. The total swap fee (fs) will have two components:
A base fee (fb)
A variable fee (fv)
The total swap fee is calculated as:
fs=fb+fv
The variable fee (fv) 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.
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):
fb=B⋅s
Base factor is the amplification to add to the bin step, to allow for adjustment of the pool base fees.
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≤k≤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:
Bin Price
Bin ID
k value
$100
1000
0
$101
1001
1
$102
1002
2
Note that k can also be negative if the price moves downwards.The variable fee for a bin fv(k) is calculated using:
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)) allows us to use bin crossovers as a measure of volatility beyond a single transaction.Think of the Volatility Accumulator (va(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) from the previous swaps
Introduced Volatility (∣ir−(activeID+k)∣)
The Volatility Accumulator is calculated as:
va(k)=vr+∣ir−(activeID+k)∣
(Note: activeID is the ID of the active bin before the swap is made.)
The volatility reference (vr) 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 (tf), and upper bound as decay period (td).If t is smaller than the filter period (tf) (this indicates a high frequency of transactions occurring), then vr stays the same.If t is greater than the decay period (td) (this indicates a low frequency of transactions occurring), then vr is reset to 0.If t is within the window bounded by tf & td, then vr takes the previous value of va decayed by a factor Rvr=⎩⎨⎧vr,R⋅va,0,t<tftf≤t<tdtd≤tHigh 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) to calculate the volatility introduced by the trade. In most cases, ir will be the ID of the active bin before the swap is made. In times of high frequency transactions, ir 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.ir={ir,activeID,t<tftf≤tThe volatility accumulatedvr 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:
R is the factor that decays va, the volatility accumulator (R⋅va) when tf≤t<td
Let tf=1 sec, td=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≤k≤3:irvrva(0)va(1)va(2)va(3)=100=0=0+∣100−(100+0)∣=0=0+∣100−(100+1)∣=1=0+∣100−(100+2)∣=2=0+∣100−(100+3)∣=3At the end of swap 1, va=3.
Swap 2
John makes a trade 4 seconds later that crosses +5 bins to 108. As t=4, vr=R⋅va. So 0≤k≤5:irvrva(0)va(1)va(2)⋮va(5)=103=0.5⋅3=1.5=1.5+∣103−(103+0)∣=1.5=1.5+∣103−(103+1)∣=2.5=1.5+∣103−(103+2)∣=3.5=1.5+∣103−(103+5)∣=6.5At the end of swap 2, va=6.5.
Swap 3
Jane makes a trade 0.3 seconds later that crosses -2 bins to 106. As t=0.3<tf=1 second, ir and vr are not reset. The active bin before this swap is 108. So −2≤k≤0:irvrva(0)va(−1)va(−2)=103=1.5=1.5+∣103−(108+0)∣=6.5=1.5+∣103−(108−1)∣=5.5=1.5+∣103−(108−2)∣=4.5At the end of swap 3, va=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.