feat: Add swap external function to AMM contract
#117
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces the
swapexternal function to theTestAMMContract, enabling users to swap between token pairs within a prediction market pool. The function is built on the constant product formula (x * y = k) to preserve AMM invariants and includes slippage protection via a minimum output amount check.Changes
New Function:
swapParameters:
_marketId— unique identifier of the prediction market pool._amountIn— amount of input tokens provided by the user._amountOutMinimum— minimum acceptable output amount (slippage protection)._zeroForOne— swap direction flag (true= tokenA → tokenB,false= tokenB → tokenA).Core Logic:
_zeroForOne.(reserveIn + amountIn) * (reserveOut - amountOut) = reserveIn * reserveOut.reserveAandreserveB).TokensSwappedevent with details of the swap.Event Emission:
TokensSwappedevent for transparency and analytics.Formatting:
forge fmtfor style consistency.Motivation
_amountOutMinimumcheck to mitigate slippage attacks and guarantee minimum expected returns.addLiquidity,removeLiquidity, and nowswap, the AMM contract supports the full lifecycle of pool interactions.Next Steps
swap:amountOut < _amountOutMinimum.