Skip to content
Business logic Audit finding Intermediate

Wrong calculation for the new rewardRate[token] can cause some of the late users can not get their rewards

The reward rate calculation in the Gauge contract used a fixed duration that did not match the actual remaining reward period, causing rewards to be distributed too quickly. As a result, users who claimed later could not receive their rewards because the contract balance was insufficient.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

uint bribeStart = block.timestamp - (block.timestamp % (7 days)) + BRIBE_LAG;
uint adjustedTstamp = block.timestamp < bribeStart ? bribeStart : bribeStart + 7 days;
if (rewardRate[token] == 0) _writeRewardPerTokenCheckpoint(token, 0, adjustedTstamp);
(rewardPerTokenStored[token], lastUpdateTime[token]) = _updateRewardPerToken(token);
_claimFees();

if (block.timestamp >= periodFinish[token]) {
    _safeTransferFrom(token, msg.sender, address(this), amount);
    rewardRate[token] = amount / DURATION;
} else {
    uint _remaining = periodFinish[token] - block.timestamp;
    uint _left = _remaining * rewardRate[token];
    require(amount > _left);
    _safeTransferFrom(token, msg.sender, address(this), amount);
    rewardRate[token] = (amount + _left) / DURATION;
}

In Gauge.sol#notifyRewardAmount(), the updated rewardRate for the token: rewardRate[token] is calculated based on the newly added amount of tokens (amount), the remaining amount of existing rewards (_left), and the DURATION.

While the DURATION is 5 days, the period from the current time to periodFinish[token] is much longer.

rewardPerToken() is calculated based on the current time, lastUpdateTime[token], and rewardRate[token].

Gauge.sol#L375-L380

function rewardPerToken(address token) public view returns (uint) {
    if (derivedSupply == 0) {
        return rewardPerTokenStored[token];
    }
    return rewardPerTokenStored[token] + ((lastTimeRewardApplicable(token) - Math.min(lastUpdateTime[token], periodFinish[token])) * rewardRate[token] * PRECISION / derivedSupply);
}

lastUpdateTime[token] will frequently be updated to the current timestamp by _updateRewardForAllTokens().

See: Gauge.sol#L460-L469

As a result, rewardPerToken() can be much higher than expected, which makes the total amount of reward tokens less than the total amount of rewards accumulated by all the users.

This makes the users who claim the rewards later unable to retrieve their rewards as the balance can be insufficient.

Proof of Concept

  1. Alice and Bob both deposited 1,000 stake token to Gauge at 1653091200 (May 21 2022 00:00:00 GMT+0000)
  2. Admin called notifyRewardAmount() add 1,000 DAI at 1653100000 (May 21 2022 02:26:40 GMT+0000)
  3. bribeStart = 1653004800 (May 20 2022 00:00:00 GMT+0000)
  4. adjustedTstamp = 1653609600 (May 27 2022 00:00:00 GMT+0000)
  5. periodFinish[DAI] = 1654041600 (Jun 01 2022 00:00:00 GMT+0000)
  6. lastUpdateTime[DAI] = 1653100000
  7. rewardRate[DAI] = 1,000 * 1e18 / 432000 = 2314814814814815
  8. Alice withdrawn and getReward() at 1654041800 Jun 01 2022 00:03:20 GMT+0000, get ~1,000 DAI
  9. rewardPerTokenStored[DAI] = ~1e18
  10. Bob tried to getReward(), the transaction will revert due to insufficient balance.

Recommended Mitigation Steps

Consider calculating rewardRate base on timeUntilNextPeriodFinish to next period finish:

uint nextPeriodFinish = adjustedTstamp + DURATION;
uint timeUntilNextPeriodFinish = nextPeriodFinish - block.timestamp;
if (block.timestamp >= periodFinish[token]) {
    _safeTransferFrom(token, msg.sender, address(this), amount);
    rewardRate[token] = amount / timeUntilNextPeriodFinish;
} else {
    uint _remaining = periodFinish[token] - block.timestamp;
    uint _left = _remaining * rewardRate[token];
    require(amount > _left);
    _safeTransferFrom(token, msg.sender, address(this), amount);
    rewardRate[token] = (amount + _left) / timeUntilNextPeriodFinish;
}
require(rewardRate[token] > 0);
uint balance = IERC20(token).balanceOf(address(this));
require(rewardRate[token] <= balance / timeUntilNextPeriodFinish, "Provided reward too high");
periodFinish[token] = nextPeriodFinish;

pooltypes (Velodrome) acknowledged

Alex the Entreprenerd (judge) decreased severity to Medium and commented:

The warden has shown how a desynch between periodFinish and DURATION can cause rewards to be distributed faster than a reward period, leaving the last claimers unable to claim.

While the loss in this scenario, to those claimers can be viewed as total, in reality those people will be able to claim on the next rewards round (unless no more tokens will be minted to that gauge, which is highly unlikely but possible).

For that reason, I believe Medium Severity to be more appropriate, as this will be a temporary loss of yield which for some people may be permanent.


The original report as published on Code4rena, by WatchPug. 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 reward rate was calculated using a fixed DURATION instead of the actual time until the next period finish, causing a mismatch between the reward rate and the remaining reward period.

Impact

Users who claim rewards later may be unable to retrieve their rewards because the contract balance is insufficient, though the judge noted this could be a temporary loss if future rewards are added.

What the attacker needed

Users have deposited stake tokens; Admin adds rewards via notifyRewardAmount; Reward period longer than DURATION

The fix

The reward rate should be calculated based on the time until the next period finish, not a fixed duration, and the period finish should be updated accordingly.

How it was found

  1. Alice and Bob both deposit 1,000 stake tokens to the Gauge at a specific timestamp.
  2. Admin calls notifyRewardAmount() to add 1,000 DAI at a later timestamp.
  3. The contract calculates bribeStart and adjustedTstamp, setting periodFinish[DAI] to a date five days after adjustedTstamp.
  4. rewardRate[DAI] is computed as 1,000 * 1e18 / 432000.
  5. Alice withdraws and calls getReward() shortly after periodFinish, receiving approximately 1,000 DAI.
  6. Bob tries to call getReward(), but the transaction reverts due to insufficient balance.

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.

A smart contract pays out rewards over time. The code set the speed of payouts based on a fixed five-day window, but the actual reward period was longer. This made the contract pay out too fast, so the last people to claim found no money left.

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 WatchPug

View all

197 more disclosed reports, best first.

More from Velodrome Finance 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.