# Fees

## Introduction

Liquidity Book introduces dynamic fees to allow for surge pricing during high market volatility.

Traders pay a swap fee to liquidity providers when performing a swap. The total swap fee ($f_s$) will have two components: a **base fee** ($f_b$) and a **variable fee** ($f_v$), which is a function of instantaneous price volatility. The fee rate will be applied to the swap amount in each liquidity bin and distributed proportionally to the liquidity providers in that bin.

Fees will be held separate from liquidity and claimable by liquidity providers.

## Base Fee

The base fee of a market is configured by the protocol owner and determined by the **base factor** ($B$) and the **bin step** ($s$):

## Variable Fee

The variable fee on the other hand depends on the volatility of the market. It will be affected by the frequency of the swaps, but doing large swaps accross many bins (on large price movements) will also increase it. Fees are calculated and distributed *per bin*, to allow a fair distribution of the fee to the liquidity providers of the bin crossed.

Fees are calculated iteratively as each bin is crossed (if the swap is large enough). In other words, if a large swap crosses $n$ bins, then the total swap fee is calculated per bin $k$ (such that $0 \leq k \leq n$), where $k$ is the difference in bin IDs from the initial bin where the swap originated and the current bin in which it is being calculated.

Example - take MAS/USDC. The active bin before swap is $1. A large buy order is made that pushes the price up to $1.3. The values of $k$ for each bin is listed below (note: bin prices are simplified for this example):

$1 bin - ID = 1844; $k$ = 0

$1.1 bin - ID = 1845; $k$ = 1

$1.2 bin - ID = 1846; $k$ = 2

$1.3 bin - ID = 1847; $k$ = 3

$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 the **volatility accumulator** ($v_a(k)$):

### Volatility Accumulator

The **volatility accumulator** ($v_a(k)$) is the witness of the current volatility of the pair. This value will be kept in memory between each calculation step. It will be calculated during a swap and will depend on two factors: the **volatility reference** ($v_r$) from the previous swaps, and the **introduced volatility** ($|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 elapsed since the last transaction ($t$). We define a window which has an upper and lower bound. If $t$ is smaller than the lower bound defined by **filter period** ($t_f$) (i.e. high frequency of transactions occurring), then $v_r$ stays the same. If $t$ is greater than the upper bound defined by **decay period** ($t_d$) (i.e. low frequency of transactions occurring), then $v_r$ is reset to 0. If $t$ is within the window, then it takes the previous $v_a$ decayed by a factor $R$.

This means that high frequency trades will stack up volatility, while low frequency trades will slowly reduce the volatility, or even reset it after a long moment has passed without any trade.

Now that the **volatility reference** is calculated, it is time to calculate the volatility introduced by the trade. Another variable, the **index reference** ($i_r$), is introduced. In most cases, $i_r$ will simply be the ID of the active bin before the swap is made. But in times of high frequency transactions, $i_r$ will keep its old value instead. This is to prevent people from manipulating fees by making small lots of small transactions that increase and decrease price.

The **volatility accumulated** 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: $f\!ee = (swap\;amount)_k \cdot (f_b + f_v)_k$

### Volatility Accumulator Example

All that was probably extremely confusing so we outline an example of how the volatility accumulator will look as swaps are made.

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

#### Swap 1

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

#### Swap 2

Alice makes a trade 4 secs later that crosses +5 bins to 108. So $0\leq k \leq 5$:

#### Swap 3

Bob makes a trade 0.3 secs later that crosses -2 bins to 106. So $-2\leq k \leq 0$:

Note: $v_r$ and $i_r$ do not update as the transaction was less than the filter period.

Notice that the volatility accumulator is decreasing despite high frequency of transactions.

## Protocol Fees

It is expressed as a variable, `protocolShare`

, which is a percentage of the total swap fee. The maximum possible is 25%.