Oracle Miscalculation During Zero Liquidity Periods Leads to Inaccurate AMM Data #41
Labels
2 (Med Risk)
Assets not at direct risk, but function/availability of the protocol could be impacted or leak value
bug
Something isn't working
🤖_primary
AI based primary recommendation
🤖_44_group
AI based duplicate group recommendation
sufficient quality report
This report is of sufficient quality
Lines of code
https://github.com/code-423n4/2024-10-ramses-exchange/blob/main/contracts/CL/core/libraries/Oracle.sol#L24
Vulnerability details
Description
The transform function is a core component of the Oracle system. It's used to update observations of the pool state over time, which are critical for accurate price feeds, liquidity calculations, and potentially reward distributions.
The issue, however, lies in the handling of the liquidity parameter when it's zero. The current implementation uses a ternary operator to avoid division by zero, but this approach can lead to inaccurate accumulation of secondsPerLiquidityCumulativeX128 when the pool has no liquidity.
Code
The issue is in this line:
When liquidity is zero, we're dividing by 1 instead of zero to avoid a division-by-zero error. While this prevents the function from reverting, it introduces a logical error in the calculations.
Impact
Inaccurate Liquidity Tracking: During periods of zero liquidity, the function is still accumulating time as if there was 1 unit of liquidity. This leads to an overestimation of the time-weighted average liquidity.
Misleading Oracle Data: Any system relying on this oracle for liquidity data will receive inaccurate information. This could affect trading decisions, risk assessments, or any other mechanisms that depend on accurate liquidity reporting.
Scenario
Imagine a pool that alternates between having liquidity and being empty:
With our current implementation:
This results in an overestimation of the average liquidity over this period.
Fix
Modify the function to skip accumulation when liquidity is zero.
This change ensures that we only accumulate
secondsPerLiquidityCumulativeX128
when there's actual liquidity in the pool.Assessed type
Oracle
The text was updated successfully, but these errors were encountered: