Skip to content

fix: use checked arithmetic in platform fee calculation#563

Merged
greatest0fallt1me merged 7 commits into
Predictify-org:masterfrom
DTriple-p:bug/checked-fee-arithmetic
May 27, 2026
Merged

fix: use checked arithmetic in platform fee calculation#563
greatest0fallt1me merged 7 commits into
Predictify-org:masterfrom
DTriple-p:bug/checked-fee-arithmetic

Conversation

@DTriple-p

Copy link
Copy Markdown
Contributor

Closes: #545

Summary

This PR hardens platform fee arithmetic in contracts/predictify-hybrid/src/fees.rs so basis-point fee math cannot overflow before division.

What changed

  • Added checked arithmetic helpers for fee math in FeeCalculator
  • Replaced raw multiply-then-divide paths with checked operations in:
    • FeeCalculator::calculate_platform_fee
    • FeeCalculator::calculate_user_payout_after_fees
    • FeeCalculator::calculate_fee_breakdown
    • dynamic fee multiplier paths
    • fee withdrawal cap basis-point math
    • fee total accumulation in FeeTracker
  • Added a dedicated typed error: Error::FeeArithmeticOverflow
  • Documented rounding direction as floor rounding and reconciliation guarantees
  • Added focused regression tests for:
    • zero pool rejection
    • 1-bps floor rounding
    • overflow-adjacent i128::MAX stake pools
    • fee breakdown reconciliation
    • no mutation when fee collection overflows
    • fee vault accumulation overflow rejection

Security Notes

Overflow safety

All fee multiplication/division paths now use checked arithmetic and return Error::FeeArithmeticOverflow instead of wrapping.

Rounding direction

Fee calculations round down using integer division. Because inputs are non-negative, this is equivalent to floor rounding.

This ensures:

  • no accidental over-collection
  • deterministic fee behavior
  • exact reconciliation in fee breakdowns
platform_fee + user_payout_amount == total_staked

@drips-wave

drips-wave Bot commented May 26, 2026

Copy link
Copy Markdown

@Agbasimere Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@greatest0fallt1me greatest0fallt1me merged commit caa18a6 into Predictify-org:master May 27, 2026
1 check failed
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.

Add overflow-safe checked arithmetic to FeeCalculator::calculate_platform_fee and basis-point math

2 participants