Skip to content
Business logic Audit finding Advanced

TWA update is not correct

The time-weighted average price (TWA) update in the swap function incorrectly clips the end square root price, so it only contributes positively to the active tick. This can cause moving bins to be updated incorrectly, potentially leading to inaccurate pricing.

hansfriese Follow

Originally disclosed on Code4rena

Submitted by hansfriese

Time-warped-price is updated incorrectly and this affects moving bins.

Proof of Concept

The protocol updates twa on every swap and uses that to decide how to move bins.

But in the function swap(), the delta’s endSqrtPrice can not contribute negatively to the activeTick because it is wrapped with a clip() function.

// Pool.sol
286:         if (amountOut != 0) {
287:             if (tokenAIn) {
288:                 binBalanceA += delta.deltaInBinInternal.toUint128();
289:                 binBalanceB = Math.clip128(binBalanceB, delta.deltaOutErc.toUint128());
290:             } else {
291:                 binBalanceB += delta.deltaInBinInternal.toUint128();
292:                 binBalanceA = Math.clip128(binBalanceA, delta.deltaOutErc.toUint128());
293:             }
294:             twa.updateValue(currentState.activeTick * PRBMathSD59x18.SCALE + int256(Math.clip(delta.endSqrtPrice, delta.sqrtLowerTickPrice).div(delta.sqrtUpperTickPrice - delta.sqrtLowerTickPrice)));//@audit second part of the sum is always positive, should contribute both ways
295:         }
296:

I believe delta.endSqrtPrice should contribute both ways to the activeTick and it is quite possible for the endSqrtPrice to be out of the range (delta.sqrtLowerTickPrice, delta.sqrtUpperTickPrice). (In another report, I mentioned an issue of accuracy loss in the calculation of endSqrtPrice).

Recommended Mitigation Steps

I recommend changing the relevant line as below without using clip() so that the endSqrtPrice can contribute to the twa reasonably.

294:             twa.updateValue(currentState.activeTick * PRBMathSD59x18.SCALE + int256((delta.endSqrtPrice, delta.sqrtLowerTickPrice).div(delta.sqrtUpperTickPrice - delta.sqrtLowerTickPrice)));

gte620v (Maverick) confirmed and commented:

The suggested mitigation is not correct, but this did alert us to a related issue in calculating the fractional part of the log TWAP when swap to sqrtPriceLimit is used for tokenAIn=false and when there is a gap between bins. In that case, the fractional part of the twap is not accurate. We will implement a fix.


The original report as published on Code4rena, by hansfriese. 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 use of a clip function on the end square root price when updating the TWA, which prevents it from contributing negatively to the active tick.

What the attacker needed

None

The fix

The project acknowledged the issue and stated they will implement a fix for a related inaccuracy in the fractional part of the log TWAP when swapping to the price limit with tokenAIn=false and when there is a gap between bins.

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 decentralised exchange (a smart contract that lets people trade tokens). The contract keeps a running average price (called TWA) to decide how to adjust its pricing bins. The bug is that when updating this average, a value is forced to be positive, so the average can be wrong, which might make the exchange move its bins incorrectly.

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 hansfriese

View all

65 more disclosed reports, best first.

More from Maverick 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.