Skip to content
Business logic Audit finding Advanced

Undercounted liquidity leads to increased volumePerLiquidityInBlock and incorrect adaptive fee

The trading volume per liquidity calculation only counts the liquidity of the final price range after a swap, ignoring the initial and intermediary ranges. This undercounts liquidity, overstating volume per liquidity and leading to lower adaptive swap fees in most cases.

Jeiwan Follow

Originally disclosed on Code4rena

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.

Proof of Concept

According to the Tech Paper, trading volume is one of the three indicators that are used to compute the adaptive swap fee:

To find the optimal commission amount, depending on the nature of the asset’s behavior, the following indicators are monitored:

  1. Volatility
  2. Liquidity
  3. Trading Volume

Further in the paper, it’s 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 ranges–this can clearly be seen on the official analytics dashboard. During swapping, the current price moves within a price range and, when there’s 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:

  1. liquidity of the initial price range;
  2. liquidity of the intermediary price ranges;
  3. liquidity of the final price range the price has stopped at.

According to the Tech Paper, it’s 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 that’s 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.

Recommended Mitigation Steps

Calculated volume per liquidity on the entire liquidity required for a swap. Use the swap fee amount calculation as a reference.

0xean (judge) commented:

Would like the sponsor to weigh in on this one prior to judging.

vladyan18 (QuickSwap & StellaSwap) acknowledged, but disagreed with severity and commented:

Thank you!

Indeed, the calculation taking into account intermediate liquidity would be more correct according to the technical paper, although more expensive in terms of gas. The main purpose of this metric when calculating the adaptive commission is to reduce the commission when there is low activity in the pool. In case of sufficient activity, i.e. the ratio of volumes to liquidity, this metric does not affect the fee.

In the current implementation, large volumes will have a stronger impact on fees after swaps ending in “thin” liquidity. And less strongly when getting into more “thick” liquidity. Imo, this behavior is consistent with the purpose of the metric.

So, in my opinion, this is an issue because it does not exactly match the technical paper, but rather a QA or a medium.

0xean (judge) decreased severity to Medium and commented:

Downgrading to M, as this is more a “leak of value” type issue.

2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.


The original report as published on Code4rena, by Jeiwan. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the volume per liquidity is calculated after the swap loop using only the final currentLiquidity, rather than summing the liquidity used across all price ranges traversed during the swap.

Impact

Trading volume is overstated in most cases, which results in lowered swap fees, causing a leak of value; the sponsor noted the metric still behaves as intended for thin liquidity.

What the attacker needed

Large swap traversing multiple price ranges; narrow liquidity positions

The fix

The volume per liquidity should be calculated on the entire liquidity required for a swap, using the swap fee amount calculation as a reference.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

In a liquidity pool, traders swap tokens and pay a fee. The fee can change based on how much trading happens compared to how much money is in the pool. This report says the code measures the pool's money incorrectly after a big trade, so the fee can be set too low.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by Jeiwan

View all

69 more disclosed reports, best first.

More from QuickSwap and StellaSwap contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.