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 () will have two components: a base fee () and a variable fee (), 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 () and the bin step ():
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 bins, then the total swap fee is calculated per bin (such that ), where 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 for each bin is listed below (note: bin prices are simplified for this example):
$1 bin - ID = 1844; = 0
$1.1 bin - ID = 1845; = 1
$1.2 bin - ID = 1846; = 2
$1.3 bin - ID = 1847; = 3
can also be negative if it pushes the price downwards.
The variable fee for a bin will be calculated using the variable fee control parameter (), bin step () and the volatility accumulator ():
Volatility Accumulator
The volatility accumulator () 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 () from the previous swaps, and the introduced volatility ():
(Note: is the ID of the active bin before the swap is made.)
The volatility reference () depends on the time elapsed since the last transaction (). We define a window which has an upper and lower bound. If is smaller than the lower bound defined by filter period () (i.e. high frequency of transactions occurring), then stays the same. If is greater than the upper bound defined by decay period () (i.e. low frequency of transactions occurring), then is reset to 0. If is within the window, then it takes the previous decayed by a factor .
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 (), is introduced. In most cases, will simply be the ID of the active bin before the swap is made. But in times of high frequency transactions, 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 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 will be:
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 = 1 sec, = 5 secs, = 0.5 secs and active bin ID is 100.
Swap 1
You make a trade that crosses +3 bins to 103. So :
Swap 2
Alice makes a trade 4 secs later that crosses +5 bins to 108. So :
Swap 3
Bob makes a trade 0.3 secs later that crosses -2 bins to 106. So :
Note: and 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%.