Skip to content

Fix/rounding error payouts#232

Closed
whitezaddy wants to merge 3 commits into
DigiNodes:mainfrom
whitezaddy:fix/rounding-error-payouts
Closed

Fix/rounding error payouts#232
whitezaddy wants to merge 3 commits into
DigiNodes:mainfrom
whitezaddy:fix/rounding-error-payouts

Conversation

@whitezaddy
Copy link
Copy Markdown
Contributor

closes #169

Title: Fix: Rounding Error in Reward Payouts (Dust Accumulation)
Internal Ref: #CO-169 Labels: contracts, stellar-wave, complexity-medium

📝 Summary
This PR addresses an audit finding where the contracts were accumulating "dust" over time due to fractional rounding errors during the settlement reward distribution. The previous implementation tracked remainders (rewardDust) improperly, leading to stranded bounty tokens in the contract.

The reward payout logic has been redesigned to precisely track the cumulative claimedWinnerWeightedStake and distributedRewards. The final participant to claim rewards now receives the exact remaining token balance (totalRewards - distributedRewards), ensuring 100% distribution of rewards and zero dust accumulation.

🛠️ Technical Changes
[TruthBountyWeighted.sol](code-assist-path:c:\Users\user\Desktop\Drips Wave Projects\truthbounty-contract\contracts\TruthBountyWeighted.sol) & [TruthBounty.sol](code-assist-path:c:\Users\user\Desktop\Drips Wave Projects\truthbounty-contract\contracts\TruthBounty.sol):
Replaced rewardDust and rewardsDistributedCount with claimedWinnerWeightedStake (or claimedWinnerStake) and distributedRewards.
Updated claimSettlementRewards to identify the last claimant and grant them the exact mathematical remainder.
Test Suite Enhancements:
Added Fuzz Testing: Created RoundingErrorPrevention.fuzz.sol which simulates 256 randomized claim scenarios to strictly verify that distributedRewards == totalRewards with 0 dust remaining.
Added Invariant Testing: Created RewardRoundingInvariant.t.sol to formally verify that the protocol's distributed rewards never exceed the total rewards and exactly match at full claim.
Testing Maintenance: Cleaned up several stale test files, resolved shadowing variables, fixed calldata/memory array bound issues in the fuzzer, and disabled SMTChecker in [foundry.toml](code-assist-path:c:\Users\user\Desktop\Drips Wave Projects\truthbounty-contract\foundry.toml) to suppress noisy environment warnings.

✅ Acceptance Criteria Met
[x] Implementation needed is functional (Rounding error tracked properly).
[x] Tests passed (Forge test suite compiles and succeeds).
[x] Verify with protocol invariants (Invariant handlers verified).
[x] No regressions (Backward compatibility with equal-weight fallback and standard staking is intact).

🧪 How to Test
Execute the test suite using Foundry:
forge test --match-contract RoundingErrorPreventionFuzzTest
forge test --match-contract RewardRoundingInvariantTest

@whitezaddy whitezaddy force-pushed the fix/rounding-error-payouts branch from 09cea67 to af62a5e Compare May 30, 2026 15:16
@dDevAhmed dDevAhmed closed this May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rounding Error in Reward Payouts

2 participants