Skip to content

Add slippage protection parameters to buy and sell functions #392

@Chucks1093

Description

@Chucks1093

Summary

Between the moment a user sees a price quote and the moment their transaction lands on chain, other trades can move the price. Without slippage protection the user has no on-chain guarantee about what they will pay or receive. Adding a max_price param to buy and min_proceeds to sell gives users a hard limit that reverts the transaction if the market moved too far before their trade executed.

Scope

  • Add optional max_price: Option<i128> parameter to the buy function
    • If provided and the computed buy price exceeds max_price, the transaction panics with a new SlippageExceeded error variant
  • Add optional min_proceeds: Option<i128> parameter to the sell function
    • If provided and the computed sell proceeds are below min_proceeds, the transaction panics with SlippageExceeded
  • Add SlippageExceeded to the contract error enum
  • Quote view functions remain unchanged — they are read-only and do not need the parameter

Acceptance Criteria

  • Buy with max_price reverts with SlippageExceeded when price exceeds the limit
  • Buy with max_price succeeds when price is at or below the limit
  • Sell with min_proceeds reverts with SlippageExceeded when proceeds fall below the limit
  • Sell with min_proceeds succeeds when proceeds meet or exceed the limit
  • Omitting either parameter (passing None) preserves existing behavior with no revert
  • SlippageExceeded is a stable error variant with a fixed discriminant
  • Unit tests cover: buy slippage triggered, buy slippage not triggered, sell slippage triggered, sell slippage not triggered, None passthrough

Coordinate on Telegram

Metadata

Metadata

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial CampaignCampaign: Official Campaign

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions