If dt is not updated accurately, timeWeightedWeeklyPositionInRangeConcLiquidity_ might be updated incorrectly
A logic error in the liquidity mining contract's time-weighted liquidity accrual causes the time increment variable to be set incorrectly when a tick's exit timestamp falls in the next week. This leads to fewer loop iterations, under-accruing the position's time-weighted liquidity and resulting in lower reward calculations.
Submitted by ni8mare
In the function accrueConcentratedPositionTimeWeightedLiquidity, inside the while block, dt is initialised as:
uint32 dt = uint32(
nextWeek < block.timestamp
? nextWeek - time
: block.timestamp - time
);
If tickTracking.exitTimestamp != 0 then the following else block is executed on line 117:
else {
// Tick is no longer active
if (tickTracking.exitTimestamp < nextWeek) {
// Exit was in this week, continue with next tick
tickActiveEnd = tickTracking.exitTimestamp;
tickTrackingIndex++;
dt = tickActiveEnd - tickActiveStart;
} else {
// Exit was in next week, we need to consider the current tick there (i.e. not increase the index)
tickActiveEnd = nextWeek;
}
}
dt is now updated to tickActiveEnd - tickActiveStart; when tickTracking.exitTimestamp < nextWeek as seen in the if block of the outer else block above.
But, when tickTracking.exitTimestamp > nextWeek, in the inner else block the value of dt is not updated:
else {
// Exit was in next week, we need to consider the current tick there (i.e. not increase the index)
tickActiveEnd = nextWeek;
//@audit - No update on dt value
}
Inside this else block as well, dt must equal tickActiveEnd - tickActiveStart;, where tickActiveEnd = nextWeek;. Without this update, if tickTracking.exitTimestamp > nextWeek, then dt = nextWeek - time or dt = block.timestamp - time as it was declared initially. For example, let’s say that it was the former, that is, dt = nextWeek - time (according to the initial declaration). Assume that tickActiveStart = tickTracking.enterTimestamp (this is possible when tickTracking.enterTimestamp > time). Had the else block above (check @audit tag), updated dt, then dt = tickActiveEnd - tickActiveStart; => dt = nextWeek - tickTracking.enterTimestamp
So, on comparing again, initially -> dt = nextWeek - time and had there been an update on dt at the audit tag, dt would now be -> dt = nextWeek - tickTracking.enterTimestamp. Note that here, tickTracking.enterTimestamp > time (check the above para). So, nextWeek - tickTracking.enterTimestamp < nextWeek - time. That means dt would be a smaller value had it been equal to nextWeek - tickTracking.enterTimestamp. But, since it’s not updated, dt equals nextWeek - time, which is a bigger value.
dt is used to increase the value of time (used as an iterator in while loop). Since dt is a bigger value than required, the number of iterations of the while loop would be less than what it should be. This means that fewer iterations would be used to update timeWeightedWeeklyPositionInRangeConcLiquidity_ on line 129
timeWeightedWeeklyPositionInRangeConcLiquidity_[poolIdx][posKey][currWeek][i] +=
(tickActiveEnd - tickActiveStart) * liquidity;
timeWeightedWeeklyPositionInRangeConcLiquidity_ is used to calculate the rewardsToSend value in claimConcentratedRewards function. If timeWeightedWeeklyPositionInRangeConcLiquidity_ is incorrect, then the rewards to be sent to the user will be calculated incorrectly.
uint256 overallInRangeLiquidity = timeWeightedWeeklyGlobalConcLiquidity_[poolIdx][week];
if (overallInRangeLiquidity > 0) {
uint256 inRangeLiquidityOfPosition;
for (int24 j = lowerTick + 10; j <= upperTick - 10; ++j) {
inRangeLiquidityOfPosition += timeWeightedWeeklyPositionInRangeConcLiquidity_[poolIdx][posKey][week][j];
}
// Percentage of this weeks overall in range liquidity that was provided by the user times the overall weekly rewards
rewardsToSend += inRangeLiquidityOfPosition * concRewardPerWeek_[poolIdx][week] / overallInRangeLiquidity; //@audit - rewards to send will be less
Also, due to fewer number of iterations, tickTrackingIndexAccruedUpTo_ might not be updated correctly.
if (tickTrackingIndex != origIndex) {
tickTrackingIndexAccruedUpTo_[poolIdx][posKey][i] = tickTrackingIndex;
}
Proof of Concept
/// @notice Accrues the in-range time-weighted concentrated liquidity for a position by going over the tick entry / exit history
/// @dev Needs to be called whenever a position is modified
function accrueConcentratedPositionTimeWeightedLiquidity(
address payable owner,
bytes32 poolIdx,
int24 lowerTick,
int24 upperTick
) internal {
RangePosition72 storage pos = lookupPosition(
owner,
poolIdx,
lowerTick,
upperTick
);
bytes32 posKey = encodePosKey(owner, poolIdx, lowerTick, upperTick);
uint32 lastAccrued = timeWeightedWeeklyPositionConcLiquidityLastSet_[
poolIdx
][posKey];
// Only set time on first call
if (lastAccrued != 0) {
uint256 liquidity = pos.liquidity_;
for (int24 i = lowerTick + 10; i <= upperTick - 10; ++i) {
uint32 tickTrackingIndex = tickTrackingIndexAccruedUpTo_[poolIdx][posKey][i];
uint32 origIndex = tickTrackingIndex;
uint32 numTickTracking = uint32(tickTracking_[poolIdx][i].length);
uint32 time = lastAccrued;
// Loop through all in-range time spans for the tick or up to the current time (if it is still in range)
while (time < block.timestamp && tickTrackingIndex < numTickTracking) {
TickTracking memory tickTracking = tickTracking_[poolIdx][i][tickTrackingIndex];
uint32 currWeek = uint32((time / WEEK) * WEEK);
uint32 nextWeek = uint32(((time + WEEK) / WEEK) * WEEK);
uint32 dt = uint32(
nextWeek < block.timestamp
? nextWeek - time
: block.timestamp - time
);
uint32 tickActiveStart; // Timestamp to use for the liquidity addition
uint32 tickActiveEnd;
if (tickTracking.enterTimestamp < nextWeek) {
// Tick was active before next week, need to add the liquidity
if (tickTracking.enterTimestamp < time) {
// Tick was already active when last claim happened, only accrue from last claim timestamp
tickActiveStart = time;
} else {
// Tick has become active this week
tickActiveStart = tickTracking.enterTimestamp;
}
if (tickTracking.exitTimestamp == 0) {
// Tick still active, do not increase index because we need to continue from here
tickActiveEnd = uint32(nextWeek < block.timestamp ? nextWeek : block.timestamp);
} else {
// Tick is no longer active
if (tickTracking.exitTimestamp < nextWeek) {
// Exit was in this week, continue with next tick
tickActiveEnd = tickTracking.exitTimestamp;
tickTrackingIndex++;
dt = tickActiveEnd - tickActiveStart;
} else {
// Exit was in next week, we need to consider the current tick there (i.e. not increase the index)
tickActiveEnd = nextWeek;
}
}
timeWeightedWeeklyPositionInRangeConcLiquidity_[poolIdx][posKey][currWeek][i] +=
(tickActiveEnd - tickActiveStart) * liquidity;
}
time += dt;
}
if (tickTrackingIndex != origIndex) {
tickTrackingIndexAccruedUpTo_[poolIdx][posKey][i] = tickTrackingIndex;
}
}
} else {
for (int24 i = lowerTick + 10; i <= upperTick - 10; ++i) {
uint32 numTickTracking = uint32(tickTracking_[poolIdx][i].length);
if (numTickTracking > 0) {
if (tickTracking_[poolIdx][i][numTickTracking - 1].exitTimestamp == 0) {
// Tick currently active
tickTrackingIndexAccruedUpTo_[poolIdx][posKey][i] = numTickTracking - 1;
} else {
tickTrackingIndexAccruedUpTo_[poolIdx][posKey][i] = numTickTracking;
}
}
}
}
timeWeightedWeeklyPositionConcLiquidityLastSet_[poolIdx][
posKey
] = uint32(block.timestamp);
}
Recommended Mitigation Steps
Add the line dt = tickActiveEnd - tickActiveStart in the place of the @audit tag, as shown above.
OpenCoreCH (Canto) confirmed and commented:
Note that if the mentioned
elsebranch is reached,dtis necessarily set tonextWeek - time, it cannot beblock.timestamp - time(if the tick was exited in the next week, the next week cannot be in the future and greater than the block timestamp). So the only possible difference is regardingtickActiveStart, namely whentickActiveStart = tickTracking.enterTimestamp(in the other case, whentickActiveStart = time, we havedt = nextWeek - time = tickActiveEnd - tickActiveStart).I agree with the warden that in this particular case, the logic for updating
dtis different in theifandelsebranch, which should not be the case. However, I think the logic in theifbranch is wrong, not in theelse: We want to setdtsuch that the nexttimevalue is equal totickActiveEnd(because we accrued up totickActiveEnd). To achieve this in all cases, we need to setdt = tickActiveEnd - timein theifblock. Otherwise, whentickTracking.enterTimestamp > timewe only add the time where the tick was in range totime(but not the time before that where the tick was out of range). Because we are also increasing the tick tracking index, this should not cause any problems in practice (the next enter timestamp will be greater thantimeby definition and we will only start accruing from there on again), but I think it should still be changed.
Note: for full discussion, see here.
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 code fails to update the time increment variable `dt` in the branch where a tick's exit timestamp is in the next week, causing the loop to advance time by an incorrect amount.
Impact
Rewards for affected positions could be under-calculated, and the tick tracking index may not be updated correctly.
What the attacker needed
Position with tick exit in next week
The fix
The code should be fixed by setting `dt = tickActiveEnd - time` in the `if` block (as suggested by the developer) or by updating `dt` in the `else` block as the warden proposed.
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 that tracks how much liquidity a user provides over time to calculate rewards. A small mistake in how time is counted when a price tick exits in the next week means the contract may skip some time periods, so the user's reward is calculated as lower than it should be.
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.