Skip to content
Smart contracts Audit finding Intermediate

Users will lose a majority or even all of the rewards when the amount of total shares is too large, due to precision loss

The reward calculation in LiquidityFarming.sol divides by total staked shares, and when that number is very large the division rounds down to zero, so users receive no rewards. An attacker could cause this by adding a huge amount of liquidity, making total shares exceed a threshold and wiping out rewards for all stakers.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug, also found by hyh

LiquidityFarming.sol#L265-L291

function getUpdatedAccTokenPerShare(address _baseToken) public view returns (uint256) {
    uint256 accumulator = 0;
    uint256 lastUpdatedTime = poolInfo[_baseToken].lastRewardTime;
    uint256 counter = block.timestamp;
    uint256 i = rewardRateLog[_baseToken].length - 1;
    while (true) {
        if (lastUpdatedTime >= counter) {
            break;
        }
        unchecked {
            accumulator +=
                rewardRateLog[_baseToken][i].rewardsPerSecond *
                (counter - max(lastUpdatedTime, rewardRateLog[_baseToken][i].timestamp));
        }
        counter = rewardRateLog[_baseToken][i].timestamp;
        if (i == 0) {
            break;
        }
        --i;
    }

    // We know that during all the periods that were included in the current iterations,
    // the value of totalSharesStaked[_baseToken] would not have changed, as we only consider the
    // updates to the pool that happened after the lastUpdatedTime.
    accumulator = (accumulator * ACC_TOKEN_PRECISION) / totalSharesStaked[_baseToken];
    return accumulator + poolInfo[_baseToken].accTokenPerShare;
}

LiquidityProviders.sol#L286-L292

uint256 mintedSharesAmount;
// Adding liquidity in the pool for the first time
if (totalReserve[token] == 0) {
    mintedSharesAmount = BASE_DIVISOR * _amount;
} else {
    mintedSharesAmount = (_amount * totalSharesMinted[token]) / totalReserve[token];
}

In HyphenLiquidityFarming, the accTokenPerShare is calculated based on the total staked shares.

However, as the mintedSharesAmount can easily become very large on LiquidityProviders.sol, all the users can lose their rewards due to precision loss.

Proof of Concept

Given:

  • rewardsPerSecond is 10e18;
  • lastRewardTime is 24 hrs ago;

Then:

  1. Alice addTokenLiquidity() with 1e8 * 1e18 XYZ on B-Chain, totalSharesMinted == 1e44;
  2. Alice deposit() to HyphenLiquidityFarming, totalSharesStaked == 1e44;
  3. 24 hrs later, Alice tries to claim the rewards.

accumulator = rewardsPerSecond * 24 hours == 864000e18 == 8.64e23

Expected Results: As the sole staker, Alice should get all the 864000e18 rewards.

Actual Results: Alice received 0 rewards.

That’s because when totalSharesStaked > 1e36, accumulator = (accumulator * ACC_TOKEN_PRECISION) / totalSharesStaked[_baseToken]; will be round down to 0.

When the totalSharesStaked is large enough, all users will lose their rewards due to precision loss.

Recommended Mitigation Steps

  1. Consider lowering the BASE_DIVISOR so that the initial share price can be higher;
  2. Consider making ACC_TOKEN_PRECISION larger to prevent precision loss;

See also the Recommendation on Issue #139.

ankurdubey521 (Biconomy) confirmed

pauliax (judge) commented:

Great find, probably deserves a severity of high.


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 accumulator multiplies by a fixed precision constant and then divides by totalSharesStaked, which can round down to zero when totalSharesStaked exceeds the product of the reward amount and the precision constant.

What the attacker needed

Ability to add large liquidity; victim stakes in the farming pool

How it was found

  1. Alice adds a large amount of liquidity (1e8 * 1e18 XYZ) on B-Chain, resulting in totalSharesMinted of 1e44.
  2. Alice deposits her shares into HyphenLiquidityFarming, making totalSharesStaked 1e44.
  3. After 24 hours, Alice tries to claim her rewards but receives 0 instead of the expected 864000e18.

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 is a smart contract bug where rewards are calculated by dividing a total reward amount by the number of shares staked. If the number of shares is extremely large, the division result becomes zero because the system only handles whole numbers and cannot represent tiny fractions. So everyone's reward becomes zero.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Biconomy - Smart Contract Wallet contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.