Skip to content
Business logic Audit finding Intermediate

Biased estimator for volatility used

The volatility estimator used in the smart contract is biased and underestimates volatility for finite sample sizes. This leads to lower fees than intended, which harms users.

Lambda Follow

Originally disclosed on Code4rena

Submitted by Lambda

DataStorage.sol#L53

The system calculates the volatility over a time period like this:

$\delta(t)=\frac{1}{T} \sum_{\tau \in[t-T, t]}(P(\tau)-\bar{P}(\tau))^{2}$

However, while this estimator is consistent (it converges in probability as the number of samples goes to infinity), it is biased and the produced estimates for finite sample sizes will be generally too low. This will result in fees that are lower than they should be (because the volatility is underestimated) and therefore hurt users.

Recommended Mitigation Steps

Apply Bessel’s correction to get an unbiased estimate, i.e.:

$\delta(t)=\frac{1}{T - 1} \sum_{\tau \in[t-T, t]}(P(\tau)-\bar{P}(\tau))^{2}$

vladyan18 (QuickSwap & StellaSwap) acknowledged and commented:

I believe the current sample size which is 1 day (86 400 seconds) makes Bessel’s correction redundant.

However, it can play a role in special situations, like here: DataStorage.sol#L354.


The original report as published on Code4rena, by Lambda. 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 volatility calculation divides by the number of samples (T) instead of T-1, which is a known statistical bias when estimating variance from a sample.

Impact

Fees are lower than they should be because volatility is underestimated, which could hurt users.

What the attacker needed

None

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.

Volatility measures how much a value (like a price) jumps around. The code calculates it by averaging squared differences from the mean, but this method tends to give a number that is a bit too small when you only have a limited number of data points. That means the fees based on this number are lower than they should be, which is bad for the system.

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 Lambda

View all

64 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.