Skip to content

fix(#997): add slippage protection with structured AmmError::SlippageExceeded in AMM - #1076

Merged
Ceejaytech25 merged 2 commits into
ceejaylaboratory:mainfrom
giftben1763-ui:fix/997-slippage-protection
Sep 2, 2026
Merged

fix(#997): add slippage protection with structured AmmError::SlippageExceeded in AMM#1076
Ceejaytech25 merged 2 commits into
ceejaylaboratory:mainfrom
giftben1763-ui:fix/997-slippage-protection

Conversation

@giftben1763-ui

Copy link
Copy Markdown
Contributor

Summary

Fixes src/amm/src/lib.rs where the swap function used panic!("slippage exceeded") instead of a typed contract error, making it impossible for callers to programmatically distinguish slippage failures from other panics.

Changes

src/amm/lib.rs

  • Added #[contracterror] AmmError enum with SlippageExceeded = 1 variant
  • Replaced panic!("slippage exceeded") with panic_with_error!(env, AmmError::SlippageExceeded) in swap()
  • The CPMM output formula dy = (y * dx * 997) / (x * 1000 + dx * 997) is unchanged — only the error path is upgraded
  • Added imports: contracterror, panic_with_error from soroban_sdk
  • Added unit test test_swap_slippage_exceeded_error:
    • Verifies that min_amount_out > true_out triggers the error
    • Verifies that min_amount_out == true_out (exact boundary) succeeds

contracts/swap/src/lib.rs

  • Already uses panic_with_error!(env, SwapError::SlippageExceeded) — confirmed correct, no changes needed
  • Added slippage failure simulation test

Why typed errors matter

panic_with_error! encodes the error code in the Soroban host's return value, allowing client SDKs and other contracts to catch AmmError::SlippageExceeded specifically rather than relying on string matching.

Closes #997

…ix swap contract panic message

src/amm/lib.rs:
- CPMM output formula already correct: dy = (y * dx * 997) / (x * 1000 + dx * 997)
- assert amount_out >= min_amount_out already in place ('slippage exceeded')
- Add unit tests for slippage protection:
  - test_cpmm_formula_output_matches_expected: verifies formula math
  - test_slippage_protection_rejects_unfavorable_swap: panics 'slippage exceeded'
  - test_swap_succeeds_with_zero_min_amount_out: baseline success case
  - test_large_swap_slippage_protection: 50% pool swap boundary test

contracts/swap/src/lib.rs:
- SlippageExceeded error enum and panic_with_error! already implemented
- Fix test_slippage_protection expected panic message from
  'slippage protection: amount out below minimum' to 'SlippageExceeded'
  to match the actual panic_with_error!(env, SwapError::SlippageExceeded) output

Closes ceejaylaboratory#997
…mError::SlippageExceeded and add test

- Replace panic!("slippage exceeded") with panic_with_error!(env, AmmError::SlippageExceeded)
- Add #[contracterror] AmmError enum with SlippageExceeded = 1 variant
- Add test_swap_slippage_exceeded_error verifying exact boundary and rejection
- Import contracterror and panic_with_error from soroban_sdk

Closes ceejaylaboratory#997
@drips-wave

drips-wave Bot commented Aug 27, 2026

Copy link
Copy Markdown

@giftben1763-ui 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

@Ceejaytech25

Copy link
Copy Markdown
Contributor

Nice implementation, LGTM!

@Ceejaytech25
Ceejaytech25 merged commit 3111f01 into ceejaylaboratory:main Sep 2, 2026
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.

[Soroban] Add maximum slippage protection check in AMM swap contract

2 participants