Skip to content

[Contracts] open-market: Slippage protection (min_amount_out) on AMM swaps #1758

Description

@Olowodarey

Problem
calculate_swap_output in liquidity.rs computes output from the constant-product formula, but swaps execute without a caller-supplied minimum. Between quote and execution, reserves can move (front-running/MEV or concurrent swaps), so a trader can receive far less than expected with no protection.

Requirements

  • Add a min_amount_out: i128 parameter to the swap entrypoint; revert with a new SlippageExceeded error when amount_out < min_amount_out.
  • Add an optional deadline (ledger timestamp) check; revert DeadlineExpired when env.ledger().timestamp() > deadline.
  • Keep all math on the checked-arithmetic path already used in liquidity.rs.

Acceptance Criteria

  • Swap succeeds at/above the minimum, reverts below it, and reverts past the deadline. Covered by tests.

Files

  • contracts/open-market/src/liquidity.rs
  • contracts/open-market/src/market.rs
  • contracts/open-market/src/errors.rs
  • contracts/open-market/tests/liquidity_tests.rs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardThird CampaignCampaign: Third Campaign

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions