From 96bae31c113f5521a3569a348271a41ee71fc06f Mon Sep 17 00:00:00 2001 From: tyranis0x01 Date: Fri, 3 Oct 2025 07:21:16 -0700 Subject: [PATCH 1/2] feat: Add swap External Function --- src/onchain/TestAMMContract.sol | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/onchain/TestAMMContract.sol b/src/onchain/TestAMMContract.sol index afd2b01..aaad0b1 100644 --- a/src/onchain/TestAMMContract.sol +++ b/src/onchain/TestAMMContract.sol @@ -366,4 +366,44 @@ contract Test_AMMContract is Ownable { emit LiquidityRemoved(_marketId, _user, amount0Decreased, amount1Decreased); } + + function swap(bytes32 _marketId, uint256 _amountIn, uint256 _amountOutMinimum, bool _zeroForOne) external { + PoolData storage pool = marketIdToPool[_marketId]; + require(pool.poolInitialized, "Pool not active"); + require(_amountIn > 0, "Amount in must be greater than zero"); + + // Determine input and output tokens based on swap direction + address inputToken = _zeroForOne ? pool.tokenA : pool.tokenB; + address outputToken = _zeroForOne ? pool.tokenB : pool.tokenA; + + // Get current reserves for the swap calculation + uint256 reserveIn = _zeroForOne ? pool.reserveA : pool.reserveB; + uint256 reserveOut = _zeroForOne ? pool.reserveB : pool.reserveA; + + require(reserveIn > 0 && reserveOut > 0, "Insufficient pool liquidity"); + + // Calculate output amount using constant product formula: x * y = k + // Formula: amountOut = (reserveOut * amountIn) / (reserveIn + amountIn) + // This maintains the invariant: (reserveIn + amountIn) * (reserveOut - amountOut) = reserveIn * reserveOut + uint256 amountOut = (reserveOut * _amountIn) / (reserveIn + _amountIn); + require(amountOut >= _amountOutMinimum, "Insufficient output amount"); + require(reserveOut > amountOut, "Insufficient output reserve"); + + // Transfer input tokens from user to this contract + IERC20(inputToken).transferFrom(msg.sender, address(this), _amountIn); + + // Update pool reserves + if (_zeroForOne) { + pool.reserveA += _amountIn; // Increase tokenA reserve + pool.reserveB -= amountOut; // Decrease tokenB reserve + } else { + pool.reserveB += _amountIn; // Increase tokenB reserve + pool.reserveA -= amountOut; // Decrease tokenA reserve + } + + // Transfer output tokens to user + IERC20(outputToken).transfer(msg.sender, amountOut); + + emit TokensSwapped(_marketId, inputToken, outputToken, _amountIn, amountOut); + } } From 19d34d6b71d0c24d0ee46d93e8bb3bf2c1fd4877 Mon Sep 17 00:00:00 2001 From: tyranis0x01 Date: Fri, 3 Oct 2025 07:21:27 -0700 Subject: [PATCH 2/2] feat: forge fmt --- src/onchain/TestAMMContract.sol | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/onchain/TestAMMContract.sol b/src/onchain/TestAMMContract.sol index aaad0b1..e267f03 100644 --- a/src/onchain/TestAMMContract.sol +++ b/src/onchain/TestAMMContract.sol @@ -379,7 +379,7 @@ contract Test_AMMContract is Ownable { // Get current reserves for the swap calculation uint256 reserveIn = _zeroForOne ? pool.reserveA : pool.reserveB; uint256 reserveOut = _zeroForOne ? pool.reserveB : pool.reserveA; - + require(reserveIn > 0 && reserveOut > 0, "Insufficient pool liquidity"); // Calculate output amount using constant product formula: x * y = k @@ -394,11 +394,11 @@ contract Test_AMMContract is Ownable { // Update pool reserves if (_zeroForOne) { - pool.reserveA += _amountIn; // Increase tokenA reserve - pool.reserveB -= amountOut; // Decrease tokenB reserve + pool.reserveA += _amountIn; // Increase tokenA reserve + pool.reserveB -= amountOut; // Decrease tokenB reserve } else { - pool.reserveB += _amountIn; // Increase tokenB reserve - pool.reserveA -= amountOut; // Decrease tokenA reserve + pool.reserveB += _amountIn; // Increase tokenB reserve + pool.reserveA -= amountOut; // Decrease tokenA reserve } // Transfer output tokens to user