Skip to content

feat(math): shared 1e7 fixed-point and basis-point math library (#235) - #506

Merged
techisigu merged 5 commits into
Refract-Protocol:mainfrom
umpireboulianne-design:feat/issue-235-shared-fixed-point-math
Oct 24, 2026
Merged

techisigu merged 5 commits into
Refract-Protocol:mainfrom
umpireboulianne-design:feat/issue-235-shared-fixed-point-math

Conversation

@umpireboulianne-design

Copy link
Copy Markdown
Contributor

Summary

Implements a shared #![no_std] fixed-point (1e7) and basis-point (10_000) checked-math library in refract-math (math/) and migrates the four governance-adjacent contracts (backstop, bounty, grants, and treasury) onto it.

Key Changes

  • Workspace & Coverage Integration (Cargo.toml, coverage-floor.toml):
    • Moves math from exclude into workspace members so cargo test --workspace and CI coverage gates execute refract-math's unit and proptest suites on every PR.
    • Adds [math] floor (lines = 95.00, branches = 90.00) to coverage-floor.toml.
  • Fixed-Point (1e7), Basis-Point (10_000), and Checked-Math Primitives (math/src/lib.rs, math/src/tests.rs):
    • Adds SCALE_1E7: i128 = 10_000_000, MAX_BPS: u32 = 10_000, and Rounding::{Floor, Ceil}.
    • Adds mul_div(a, b, c, rounding) and mul_div_ceil(a, b, c) backed by 256-bit mul_wide / div_wide intermediates so intermediate products never overflow when the quotient fits in i128.
    • Adds apply_bps, apply_bps_floor, apply_bps_ceil, and ratio_bps with explicit validation (bps <= 10_000, 0 <= part <= whole).
    • Adds to_1e7 and from_1e7 scale conversion helpers plus checked_add, checked_sub, checked_mul, saturating_add, and saturating_sub.
    • Expands unit and proptest coverage for floor/ceil invariants, apply_bps_floor(x, bps) + apply_bps_ceil(x, 10_000 - bps) == x, and to_1e7 / from_1e7 round-trips across [0, i128::MAX].
  • Contract Migrations:
    • backstop: Uses refract_math::{mul_div_floor, split_bps, saturating_add, saturating_sub, BPS_DENOM, SCALE_1E7} for share minting/redemption (calc_shares, quote_withdrawal), tranche_share_price, senior_coverage_ratio, and receive_premium.
    • bounty: Uses refract_math::{checked_add, checked_sub} for balance and reservation accounting in fund, award, create_payout, claim, sweep_expired, and available_balance_inner.
    • grants: Uses refract_math::{checked_add, checked_sub, saturating_add} for milestone sum checks, claimable balance accumulation, liability accounting, and rescue excess checks.
    • treasury: Uses refract_math::{apply_bps_floor, saturating_add, saturating_sub, MAX_BPS} in Reserve target calculation and inflow routing.

Closes #235

…act-Protocol#235)

Extend refract-math with SCALE_1E7, MAX_BPS, Rounding::{Floor, Ceil}, mul_div_ceil, apply_bps, ratio_bps, to_1e7/from_1e7, and checked/saturating arithmetic helpers; include math in workspace members and migrate backstop, bounty, grants, and treasury.\n\nCloses Refract-Protocol#235
…licts (Simple agent)

- backstop/src/lib.rs: merged [markers] — model resolved the conflict markers

Co-authored-by: Simple Agent <agent@simple.dev>
…licts (Simple agent)

- backstop/src/lib.rs: merged [markers] — model resolved the conflict markers

Co-authored-by: Simple Agent <agent@simple.dev>
…licts (Simple agent)

- backstop/src/lib.rs: merged [markers] — model resolved the conflict markers

Co-authored-by: Simple Agent <agent@simple.dev>
…licts (Simple agent)

- backstop/src/lib.rs: merged [markers] — model resolved the conflict markers
- bounty/src/lib.rs: merged [markers] — model resolved the conflict markers

Co-authored-by: Simple Agent <agent@simple.dev>
@techisigu
techisigu merged commit 9aa314a into Refract-Protocol:main Oct 24, 2026
0 of 4 checks passed
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 a Shared Fixed-Point (1e7) Checked-Math Library for the Governance-Adjacent Contracts

2 participants