Skip to content
Business logic Audit finding Intermediate

Incorrect calculation of TWAP in OptionTokenV4.getTimeWeightedAveragePrice() function

The getTimeWeightedAveragePrice() function in OptionTokenV4 calculates the average price using only past observations from the Pair contract, ignoring the current price, so the returned price can be up to 30 minutes out of date. An attacker could exploit this stale price to exercise options at an incorrect discount, potentially causing losses for the protocol or users.

Eeyore Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-06-velocimeter-judging/issues/298

Found by

eeyore

Summary

The average price returned by OptionTokenV4.getTimeWeightedAveragePrice() can be up to 30 minutes outdated and does not reflect the current Token price.

Vulnerability Detail

In the getTimeWeightedAveragePrice() function, the average price is calculated using the last X known price observations from the Pair contract. However, this approach has a flaw because it does not consider the current price, which could be as much as 30 minutes old.

Consider a scenario where the price of the Token significantly increases during this 30-minute window. The resulting maximum discount might not adequately cover the percentage increase in price. This could lead to the protocol failing to collect proper fees when exercising the Tokens.

Impact

The use of an outdated TWAP price could result in losses for the protocol or users.

Code Snippet

https://github.com/sherlock-audit/2024-06-velocimeter/blob/main/v4-contracts/contracts/OptionTokenV4.sol#L372-L388 https://github.com/sherlock-audit/2024-06-velocimeter/blob/main/v4-contracts/contracts/Pair.sol#L222-L246

Tool used

Manual Review

Recommendation

To address this issue, incorporating also the current price retrieved from Pair.current() function:

Diff
function getTimeWeightedAveragePrice(uint256 _amount) public view returns (uint256) {
    uint256[] memory amtsOut = IPair(pair).prices(
        underlyingToken,
        _amount,
        twapPoints
    );
    uint256 len = amtsOut.length;
    uint256 summedAmount;

    for (uint256 i = 0; i < len; i++) {
        summedAmount += amtsOut[i];
    }

+    summedAmount += IPair(pair).current(underlyingToken, _amount);

-    return summedAmount / twapPoints;
+    return (summedAmount / twapPoints) + 1;
}

Discussion

nevillehuang

Invalid, user can simply call sync() in pair contract to update the latest prices before exercising options

0xklapouchy

Escalate.

sync() will not work, price can be outdated up to 30 min:

Solidity
File: Pair.sol
171:         timeElapsed = blockTimestamp - _point.timestamp; // compare the last observation with current timestamp, if greater than 30 minutes, record a new event
172:         if (timeElapsed > periodSize) {
173:             observations.push(Observation(blockTimestamp, reserve0CumulativeLast, reserve1CumulativeLast));
174:         }

Observation point will only be added when timeElapsed > periodSize and periodSize == 1800 (30min)

sherlock-admin3

Escalate.

sync() will not work, price can be outdated up to 30 min:

Solidity
File: Pair.sol
171:         timeElapsed = blockTimestamp - _point.timestamp; // compare the last observation with current timestamp, if greater than 30 minutes, record a new event
172:         if (timeElapsed > periodSize) {
173:             observations.push(Observation(blockTimestamp, reserve0CumulativeLast, reserve1CumulativeLast));
174:         }

Observation point will only be added when timeElapsed > periodSize and periodSize == 1800 (30min)

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

nevillehuang

Hi @spacegliderrrr @dawiddrzala I recall us discussing a similar/or this issue that resulted in me invalidating the issue but I can't seem to find where, could you double check this issue? I think it was related to issue #354

spacegliderrrr

@nevillehuang that’s a different issue. This issue basically means that instead of getting TWAP of the last 2 hours, it may get it up to 30min delayed (getting the TWAP of 2h30m ago to 30m ago).

I believe this should be a valid solo Medium

nevillehuang

@spacegliderrrr Got it thanks seems valid for now, I will double check again and come to a more definite conclusion

cvetanovv

I agree with the escalation and think it can be a valid Medium.

Watson has shown how the getTimeWeightedAveragePrice() function calculates an outdated price up to 30 minutes. An outdated TWAP could result in losses for the protocol or its users because the calculated price may not reflect the current market conditions.

Planning to accept the escalation and make this issue a Medium severity.

dawiddrzala

good point we are going to add the current price to the twap price

WangSecurity

Result: Medium Unique

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/Velocimeter/v4-contracts/pull/22

spacegliderrrr

Fix looks good. TWAP now includes current prices too.

sherlock-admin2

The Lead Senior Watson signed off on the fix.

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

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

The function calculates the TWAP using only historical observations from the Pair contract and does not include the current price, so the result can be up to 30 minutes stale.

Impact

The use of an outdated TWAP price could result in losses for the protocol or users.

What the attacker needed

None

The fix

The issue was fixed by including the current price from Pair.current() in the TWAP calculation.

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.

This report is about a smart contract function that calculates an average price for options. The function only looks at past price data and ignores the most recent price, so the average can be up to 30 minutes old. If the real price has changed a lot in that time, someone could use the outdated price to get a better deal than they should, which could cost the protocol money.

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 Eeyore

View all

7 more disclosed reports, best first.

More from Velocimeter

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.