Submitted by Jeiwan
Undercounted liquidity leads to incorrect trading volume reporting and affects the adaptive fee calculation. In most cases (when the majority of liquidity in a pool is concentrated around the current price) trading volume is overstated, which results in lowered swap fees.
According to the Tech Paper,
trading volume is one of the three indicators that are used to compute the adaptive swap fee:
Further in the paper, its shown how trading volume per liquidity is calculated:
$I_t = \frac{\widetilde{L}\_t}{L_t}$
Where $\widetilde{L}\_t = \sqrt{V\_1 \* V\_2}$ is the average trading volume of assets 1 and 2; $L_t$ is the liquidity that was used during a trade.
In Uniswap V3 (on which Algebra Finance is based), liquidity is concentrated in discrete price rangesthis
can clearly be seen on the official analytics dashboard. During swapping, the current price moves within a price range and, when theres not enough liquidity in the price range to satisfy the swap, jumps to adjacent liquidity positions.
In the case of big swaps and narrow liquidity positions, the current price can traverse over multiple intermediary liquidity positions before the swap is fulfilled and end up in a different price range. In such situation, the liquidity required to fulfil the swap is made of:
According to the Tech Paper, its expected that $L_t$ takes into account the liquidity in the initial and final price ranges, as well as liquidity of the intermediary price ranges (since this is the liquidity required to make the swap).
However, in the code, it only counts the liquidity of the final price range (calculateVolumePerLiquidity is called after the main swap loop): AlgebraPool.sol#L880.
By the time the calculateVolumePerLiquidity function is called, currentLiquidity is the liquidity thats
available at the new price (the price after a swap), which includes only the price ranges that include the new price. During a swap, currentLiquidity is updated multiple times
whenever new liquidity is added or removed while the price is traversing over liquidity positions. The liquidity of the
initial price range and all the intermediary price ranges is not counted by calculateVolumePerLiquidity.
The idea of volume per liquidity calculation is somewhat similar to swap fees calculation: swap fees are also calculated on the liquidity engaged in a swap. In the code, swap fees are calculated in the movePriceTowardsTarget function, which is called inside the main swap loop and which receives currentLiquidity multiple times as the price moves from one price range to another and liquidity gets added and removed. Thus, swap fees are always calculated on the entire liquidity required for a swap while the trading volume computation counts only the price ranges that include the new price after a swap is done.
Calculated volume per liquidity on the entire liquidity required for a swap. Use the swap fee amount calculation as a reference.
0xean (judge) commented:
vladyan18 (QuickSwap & StellaSwap) acknowledged, but disagreed with severity and commented:
0xean (judge) decreased severity to Medium and commented:
