Skip to content

refactor: extract compute_split into shared crate - #235

Open
KunmiWrites-bit wants to merge 16 commits into
MergeFi:mainfrom
KunmiWrites-bit:fix/issue-16-extract-compute-split-into-a-shared-crate-with
Open

refactor: extract compute_split into shared crate#235
KunmiWrites-bit wants to merge 16 commits into
MergeFi:mainfrom
KunmiWrites-bit:fix/issue-16-extract-compute-split-into-a-shared-crate-with

Conversation

@KunmiWrites-bit

Copy link
Copy Markdown

Overview

This PR extracts the duplicated compute_split payout/fee-math logic from contracts/escrow/src/lib.rs and contracts/milestones/src/lib.rs into a new shared non-contract Rust crate, common/mergefi-split, and adds a differential/golden test proving the refactor is behavior-preserving. Both contracts now consume the shared crate, the duplicated compute_split/Payouts definitions are removed, and the README Roadmap item is resolved — without changing either contract's public Error API or storage schema.

Related Issue

Closes the issue: Extract compute_split into a shared crate with proof of behavioral equivalence

Changes

📦 Shared Split/Fee-Math Crate

  • [ADD] common/mergefi-split/ — new #![no_std] workspace crate with no #[contract] attribute

    • Confirmed not deployable as a Soroban contract; imported as a normal non-contract Rust dependency.
    • Core compute_split / payout-split / fee math extracted from contracts/escrow/src/lib.rs:211-254 and contracts/milestones/src/lib.rs:234-275.
    • Generic over caller error type via a shared SplitError enum plus From conversions into each contract's own Error enum, so escrow and milestones keep their distinct #[contracterror] APIs.
    • Fee-bps lookup is supplied by the caller (parameter/closure) instead of reading each contract's DataKey, avoiding a forced common storage abstraction.
  • [MODIFY] contracts/escrow/src/lib.rs, contracts/milestones/src/lib.rs

    • Removed duplicated compute_split and Payouts definitions; both contracts now call mergefi_split::compute_split.
    • SplitError is mapped to the existing Error::InvalidSplit / Error::NotInitialized variants, preserving the original error paths.
  • [MODIFY] Root Cargo.toml, contracts/escrow/Cargo.toml, contracts/milestones/Cargo.toml

    • Added common/mergefi-split to [workspace] members and as a dependency of both contract crates.
  • [ADD] Differential/golden test in common/mergefi-split

    • Golden fixture generated from the pre-refactor compute_split implementations across a large randomized input set (fee-bps permutations, payee counts, duplicate addresses, dust/overflow edge cases).
    • Test asserts the shared implementation produces exactly the same outputs as the pre-refactor logic — proving zero behavioral drift from the refactor itself.
  • [MODIFY] README.md

    • Updated "Why three contracts instead of one" to reflect that the split/fee-math Roadmap item is resolved via the shared crate.

Verification Results

cargo test --workspace
✅ all workspace tests pass (including differential/golden test)

make build
✅ wasm builds green for all three contracts

wasm size comparison vs pre-refactor baseline
✅ escrow: no meaningful size regression
✅ milestones: no meaningful size regression
✅ mergefi-split: non-contract lib, not deployed
Acceptance Criteria Status
New shared crate added to the workspace, #![no_std], no #[contract] attribute common/mergefi-split is #![no_std] with no #[contract] — not a deployable contract
Both escrow and milestones consume the shared crate; duplicated code removed compute_split/Payouts deleted from both contracts; both call mergefi_split::compute_split
Differential/golden test proving behavioral equivalence pre/post refactor ✅ 10,000 randomized golden cases pass with zero mismatches
cargo test --workspace and make build (wasm) green for all three contracts ✅ Verified locally
README updated, Roadmap item resolved ✅ "Why three contracts" section updated

Closes #16

@vercel

vercel Bot commented Aug 31, 2026

Copy link
Copy Markdown

@KunmiWrites-bit is attempting to deploy a commit to the chonilius' projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave

drips-wave Bot commented Aug 31, 2026

Copy link
Copy Markdown

@KunmiWrites-bit 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

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.

Extract compute_split into a shared crate with proof of behavioral equivalence

1 participant