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
Location: creator-keys/tests/co_creator_fee_split_invariant.rs
-
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
-
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
-
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
-
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
-
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
-
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
-
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
-
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
register_creator_with_co_creator(): Sets up test creators with co-creator configurationverify_fee_split_invariant(): Reusable verification logic for different split percentages
const KEY_PRICE: i128 = 1000;
const CREATOR_BPS: u32 = 9000; // 90% creator fee
const PROTOCOL_BPS: u32 = 1000; // 10% protocol feeEach test:
- Captures balances before trade
- Executes buy or sell operation
- Captures balances after trade
- Calculates balance increases for both parties
- Asserts sum equals total creator fee from quote
- Validates individual shares match expected amounts
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.
- Trade price split:
(creator_fee, protocol_fee) = compute_fee_split(price, creator_bps, protocol_bps) - Creator fee split:
(creator_recipient_amount, co_creator_amount) = checked_split_bps_amount(creator_fee, share_bps) - Balances updated via
credit_creator_fee_recipient_balance()andcredit_co_creator_fee_balance()
# 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- ✅ 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
30bc12e- Add co-creator fee split invariant test with 30%, 50%, and 10% sharesc141f59- Add edge case test for odd price amounts with fractional percentages
- 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
- 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