# Loss of incentives if total weight in an epoch is zero

In Olas Dispenser, when an epoch's total staking weight is zero, calculateStakingIncentives returns zero refund, so that epoch's staking incentives are lost instead of returned to tokenomics inflation.

- Payout: Audit contest
- Severity: medium
- Program: Olas
- Found by: fyamf
- Disclosed: 2024-08-06
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-05-olas-findings/issues/61
- Library page: https://bugbountylibrary.com/r/c4%3A2024-05-olas%3AM-04
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:solidity technique:source-review vuln:accounting-error vuln:reward-distribution

**Target:** Dispenser.calculateStakingIncentives / claimStakingIncentives (Solidity tokenomics)
**Root cause:** The refund branch computes returnAmount = ((stakingDiff + availableStakingAmount) * stakingWeight) / 1e18; when totalWeightSum and stakingWeight are both zero, returnAmount is zero, so the epoch's stakingIncentive is neither distributed nor refunded.
**Steps:**
1. Deploy tokenomics/dispenser; set staking fraction to 100% via `changeIncentiveFractions(0, 0, 0, 0, 0, 100)` and `changeStakingParams(50,10)`.
2. Advance one epoch and `checkpoint()`; add a nominee via `vw.addNomineeEVM` but cast no votes so total weight for the epoch is zero.
3. Advance another epoch and `checkpoint()` so the zero-weight epoch is claimable.
4. Call `dispenser.callStatic.calculateStakingIncentives(numClaimedEpochs, chainId, stakingTarget, bridgingDecimals)`.
5. Observe totalStakingIncentive=0 and totalReturnAmount=0 while `tokenomics.mapEpochStakingPoints(2).stakingIncentive` is nonzero (e.g. 259644210229512049587306).
**How found:** Code reading of the refund formula: noticed returnAmount is multiplied by stakingWeight, so a zero total weight forces zero refund despite nonzero available staking incentive; confirmed with two Hardhat PoC tests (no votes, and votes dropped to zero).
**Insight:** When auditing proportional distribution/refund math, check the zero-denominator/zero-weight path: a refund computed as amount*weight/total silently becomes zero when weight is zero, stranding funds rather than reverting.

## Proof of concept (copied exactly from the original report)

**Test showing zero total weight loses incentives**

```
it("loss of incentives if the total weight sum is zero", async () => {
            // Take a snapshot of the current state of the blockchain
            const snapshot = await helpers.takeSnapshot();

            // Set staking fraction to 100%
            await tokenomics.changeIncentiveFractions(0, 0, 0, 0, 0, 100);
            // Changing staking parameters
            await tokenomics.changeStakingParams(50, 10);

            // Checkpoint to apply changes
            await helpers.time.increase(epochLen);
            await tokenomics.checkpoint();

            // Unpause the dispenser
            await dispenser.setPauseState(0);

            // Add a staking instance as a nominee
            await vw.addNominee(stakingInstance.address, chainId);

            // Checkpoint to apply changes
            await helpers.time.increase(epochLen);
            await tokenomics.checkpoint();

            const stakingTarget = convertAddressToBytes32(stakingInstance.address);
            // Calculate staking incentives
            const stakingIncentives = await dispenser.callStatic.calculateStakingIncentives(numClaimedEpochs, chainId,
                stakingTarget, bridgingDecimals);

            // this shows the total staking incentives and return amount in epoch 2
            console.log("total staking incentive: ", await stakingIncentives.totalStakingIncentive);
            console.log("total return amount: ", await stakingIncentives.totalReturnAmount);

            // 2 is the epoch that the total weight sum is zero
            console.log("available staking amount: ", (await tokenomics.mapEpochStakingPoints(2)).stakingIncentive);

            // Restore to the state of the snapshot
            await snapshot.restore();
        });
```

