Skip to content

Latest commit

 

History

History
151 lines (109 loc) · 5.49 KB

File metadata and controls

151 lines (109 loc) · 5.49 KB

Co-Creator Fee Split Invariant Test Implementation

Summary

Implemented comprehensive unit tests to verify that co-creator fee splits preserve the full creator fee with no XLM lost, validating the invariant:

co_creator_amount + creator_recipient_amount == total_creator_fee

Test File

Location: creator-keys/tests/co_creator_fee_split_invariant.rs

Test Coverage

Core Invariant Tests (Acceptance Criteria)

  1. 30% Split Test (test_co_creator_fee_split_30_percent_no_xlm_lost)

    • Registers creator with 30% co-creator share (3000 bps)
    • Executes a buy operation
    • Verifies sum of both parties equals total creator fee
    • ✅ Acceptance: Co-creator + creator recipient = total creator fee
  2. 50% Split Test (test_co_creator_fee_split_50_percent_no_xlm_lost)

    • Registers creator with 50% co-creator share (5000 bps)
    • Executes a buy operation
    • Verifies sum of both parties equals total creator fee
    • ✅ Acceptance: No XLM lost at 50% share value
  3. 10% Split Test (test_co_creator_fee_split_10_percent_no_xlm_lost)

    • Registers creator with 10% co-creator share (1000 bps)
    • Executes a buy operation
    • Verifies sum of both parties equals total creator fee
    • ✅ Acceptance: No XLM lost at 10% share value

Additional Comprehensive Tests

  1. Multiple Trades Test (test_co_creator_fee_split_invariant_across_multiple_trades)

    • Verifies invariant holds across 5 consecutive buy operations
    • Validates both per-trade and cumulative totals
    • Ensures consistency over time
  2. Sell Operation Test (test_co_creator_fee_split_invariant_on_sell)

    • Verifies invariant applies to sell operations
    • Tests sell fee distribution matches buy fee distribution logic
    • Confirms individual shares are correctly calculated
  3. Boundary Cases Test (test_co_creator_fee_split_boundary_cases)

    • Tests minimum valid share (1 bps = 0.01%)
    • Tests maximum valid share (9999 bps = 99.99%)
    • Validates extreme boundary conditions
  4. Share Validation Test (test_no_party_receives_more_than_their_share)

    • Tests 10%, 30%, 50%, 70%, and 90% splits
    • Verifies neither party receives more than their allocated share
    • Confirms exact amounts match expected calculations
    • ✅ Acceptance: Test fails if either party receives more than their share
  5. Rounding Edge Cases Test (test_co_creator_fee_split_with_odd_price_amounts)

    • Uses odd key price (997) with fractional percentage (33.33%)
    • Verifies no XLM lost despite integer division rounding
    • Ensures remainder logic works correctly

Test Implementation Details

Helper Functions

  • register_creator_with_co_creator(): Sets up test creators with co-creator configuration
  • verify_fee_split_invariant(): Reusable verification logic for different split percentages

Test Constants

const KEY_PRICE: i128 = 1000;
const CREATOR_BPS: u32 = 9000;  // 90% creator fee
const PROTOCOL_BPS: u32 = 1000;  // 10% protocol fee

Verification Approach

Each test:

  1. Captures balances before trade
  2. Executes buy or sell operation
  3. Captures balances after trade
  4. Calculates balance increases for both parties
  5. Asserts sum equals total creator fee from quote
  6. Validates individual shares match expected amounts

Key Findings

Fee Split Implementation

The contract uses checked_split_bps_amount() from the fee module:

pub fn checked_split_bps_amount(total: i128, share_bps: u32) -> Option<(i128, i128)> {
    if total <= 0 {
        return Some((0, 0));
    }
    let shared_amount = apply_percentage_fee(total, share_bps)?;
    let remainder = checked_sub_i128(total, shared_amount)?;
    Some((remainder, shared_amount))
}

Key Property: Remainder from integer division stays with the primary recipient (creator fee recipient), ensuring the sum always equals the total.

Distribution Flow

  1. Trade price split: (creator_fee, protocol_fee) = compute_fee_split(price, creator_bps, protocol_bps)
  2. Creator fee split: (creator_recipient_amount, co_creator_amount) = checked_split_bps_amount(creator_fee, share_bps)
  3. Balances updated via credit_creator_fee_recipient_balance() and credit_co_creator_fee_balance()

Running the Tests

# Run all co-creator fee split invariant tests
cargo test --test co_creator_fee_split_invariant

# Run specific test
cargo test --test co_creator_fee_split_invariant test_co_creator_fee_split_30_percent_no_xlm_lost

# Run with output
cargo test --test co_creator_fee_split_invariant -- --nocapture

Acceptance Criteria Status

  • ✅ Co-creator amount plus creator recipient amount equals total creator fee
  • ✅ No XLM lost in the split at 30%, 50%, and 10% share values
  • ✅ Test fails if either party receives more than their share

Commits

  1. 30bc12e - Add co-creator fee split invariant test with 30%, 50%, and 10% shares
  2. c141f59 - Add edge case test for odd price amounts with fractional percentages

Related Files

  • Contract Implementation: creator-keys/src/lib.rs (lines 743-825)
  • Fee Module: creator-keys/src/lib.rs (lines 130-230)
  • Existing Co-Creator Tests: creator-keys/tests/co_creator_revenue_split.rs
  • Test Helpers: creator-keys/tests/contract_test_env/mod.rs

Notes

  • All tests follow existing project patterns and conventions
  • Tests use mocked authorization via test_env_with_auths()
  • Balance tracking uses persistent storage with checked arithmetic
  • Integer division remainder always assigned to creator fee recipient to preserve total