Skip to content

feat(settlement): add slippage protection with oracle-based conversion#39

Merged
elizabetheonoja-art merged 1 commit into
Utility-Protocol:mainfrom
Adeyemi-cmd:Missing_Slippage_Protection_in_Payment_Settlement_Conversion
Jun 25, 2026
Merged

feat(settlement): add slippage protection with oracle-based conversion#39
elizabetheonoja-art merged 1 commit into
Utility-Protocol:mainfrom
Adeyemi-cmd:Missing_Slippage_Protection_in_Payment_Settlement_Conversion

Conversation

@Adeyemi-cmd

Copy link
Copy Markdown
Contributor

Closes #6

PR Description
Summary
Adds slippage protection to the settlement contract's currency conversion flow. When finalize_settlement() converts resource token volume to settlement currency via the oracle exchange rate, the contract now enforces both a protocol-level MAX_SLIPPAGE_BPS bound (default 1%) and an optional user-defined min_expected_amount parameter.
Changes

  • contracts/settlement/src/types.rs — New SettlementArgs struct with token_address, volume, recipient, and min_expected_amount: Option. New SettlementResult struct with net_amount, fee_amount, rate_used.
  • contracts/settlement/src/constants.rs — Added MAX_SLIPPAGE_BPS: u32 = 100, MAX_VOLUME, MAX_RATE, DECIMAL_DENOMINATOR.
  • contracts/settlement/src/rate_application.rs — New module with get_rate() that fetches the current exchange rate from the PriceOracle contract via cross-contract call.
  • contracts/settlement/src/conversion.rs — New module with convert_to_settlement_currency() implementing the conversion and slippage check:
  • Computes expected_amount = volume * oracle_rate / 1e7 using checked_mul/checked_div
  • Computes slippage_bps = (expected - actual) * 10000 / expected
  • Rejects with SlippageExceeded if slippage > MAX_SLIPPAGE_BPS
  • Rejects if actual_amount < min_expected_amount (if provided)
  • Emits SlippageProtectionTriggered event on rejection
  • contracts/settlement/src/lib.rs — Added SlippageExceeded error variant, new finalize_settlement() function returning SettlementResult, and oracle cross-contract client.
  • contracts/settlement/src/test.rs — 4 new integration tests using a MockOracle:
  1. test_zero_slippage_succeeds — no min_expected_amount, succeeds
  2. test_slippage_within_tolerance_succeeds — min_expected at 99% of actual
  3. test_slippage_exceeds_tolerance_fails — min_expected > actual by 1 unit
  4. test_user_min_expected_amount_higher_than_actual_fails — min_expected at 200x
  • contracts/Cargo.toml — Added "settlement" to workspace members.
    Testing
    cargo test --package settlement
    All 17 tests pass (13 existing + 4 new).

@elizabetheonoja-art elizabetheonoja-art merged commit 6590c09 into Utility-Protocol:main Jun 25, 2026
2 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.

Missing Slippage Protection in Payment Settlement Conversion

2 participants