diff --git a/contracts/src/MiladyPoolOrderManager.sol b/contracts/src/MiladyPoolOrderManager.sol index 6ce29fa..5840168 100644 --- a/contracts/src/MiladyPoolOrderManager.sol +++ b/contracts/src/MiladyPoolOrderManager.sol @@ -11,6 +11,7 @@ import {BalanceDelta} from "v4-core/types/BalanceDelta.sol"; import {BeforeSwapDelta, toBeforeSwapDelta} from "v4-core/types/BeforeSwapDelta.sol"; import {PoolId, PoolIdLibrary} from "v4-core/types/PoolId.sol"; import {StateLibrary} from "v4-core/libraries/StateLibrary.sol"; +import {CurrencyLibrary, Currency} from "v4-core/types/Currency.sol"; // Eigenlayer import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol"; import "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol"; @@ -42,6 +43,7 @@ contract MiladyPoolOrderManager is using BN254 for BN254.G1Point; using PoolIdLibrary for PoolKey; using StateLibrary for IPoolManager; + using CurrencyLibrary for Currency; constructor( IRegistryCoordinator _registryCoordinator, @@ -80,7 +82,7 @@ contract MiladyPoolOrderManager is function afterSwap( address, PoolKey calldata key, - IPoolManager.SwapParams calldata params, + IPoolManager.SwapParams calldata, BalanceDelta, bytes calldata ) external override onlyByPoolManager returns (bytes4, int128) { diff --git a/contracts/src/MiladyPoolRouter.sol b/contracts/src/MiladyPoolRouter.sol index fb20a4d..417c77d 100644 --- a/contracts/src/MiladyPoolRouter.sol +++ b/contracts/src/MiladyPoolRouter.sol @@ -13,6 +13,7 @@ import {PoolId, PoolIdLibrary} from "v4-core/types/PoolId.sol"; import {StateLibrary} from "v4-core/libraries/StateLibrary.sol"; import {TransientStateLibrary} from "v4-core/libraries/TransientStateLibrary.sol"; import {CurrencyLibrary, Currency} from "v4-core/types/Currency.sol"; +import {CurrencySettler} from "@uniswap/v4-core/test/utils/CurrencySettler.sol"; import {MiladyPoolMath} from "./libraries/MiladyPoolMath.sol"; import {BalanceDelta} from "v4-core/types/BalanceDelta.sol"; @@ -26,6 +27,7 @@ contract MiladyPoolRouter is WyvernInspired { using TransientStateLibrary for IPoolManager; using PoolIdLibrary for PoolKey; using CurrencyLibrary for Currency; + using CurrencySettler for Currency; // TODO: Hardcoded for now, should update so that we pass it in address constant PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3; @@ -155,27 +157,67 @@ contract MiladyPoolRouter is WyvernInspired { permit2Signature ); - _settle( - data.params.zeroForOne // Gets the token that is swapped in - ? Currency(data.key.currency0) - : Currency(data.key.currency1), - uint128(amountIn) - ); - - // Can skip the entire amount to swap here if we just do the swap here - // At this point the first token is already in the pool so we need to call _take - _take( - data.params.zeroForOne // Gets the token that is swapped out - ? Currency(data.key.currency1) - : Currency(data.key.currency0), - uint128(amountOut), - walletAddress - ); + console.log("amountIn: %d", amountIn); + console.log("amountOut: %d", amountOut); + console.log("zeroForOne: %s", data.params.zeroForOne); // Call this with new data params - BalanceDelta delta = manager.swap(data.key, data.params, data.hookData); + BalanceDelta delta = manager.swap(data.key, data.params, ""); + int256 deltaAfter0 = delta.amount0(); + int256 deltaAfter1 = delta.amount1(); + // _settle( + // data.params.zeroForOne // Gets the token that is swapped in + // ? Currency(data.key.currency0) + // : Currency(data.key.currency1), + // uint128(delta.amount0()) + // ); + + // // Can skip the entire amount to swap here if we just do the swap here + // // At this point the first token is already in the pool so we need to call _take + // _take( + // data.params.zeroForOne // Gets the token that is swapped out + // ? Currency(data.key.currency1) + // : Currency(data.key.currency0), + // uint128(delta.amount1()), + // walletAddress + // ); + + if (deltaAfter0 < 0) { + data.key.currency0.settle( + manager, + walletAddress, + uint256(-deltaAfter0), + true + ); + } + if (deltaAfter1 < 0) { + data.key.currency1.settle( + manager, + walletAddress, + uint256(-deltaAfter1), + true + ); + } + if (deltaAfter0 > 0) { + data.key.currency0.take( + manager, + walletAddress, + uint256(deltaAfter0), + false + ); + } + if (deltaAfter1 > 0) { + data.key.currency1.take( + manager, + walletAddress, + uint256(deltaAfter1), + false + ); + } + + return abi.encode(delta); - return abi.encode(0); + // return abi.encode(toBeforeSwapDelta(0, 0)); } // TODO: Update so that the PoolKey sqrtPriceCurrentX96, liquidity are what you need diff --git a/contracts/test/MiladyPoolOrderManager.t.sol b/contracts/test/MiladyPoolOrderManager.t.sol index da37b46..845bc1e 100644 --- a/contracts/test/MiladyPoolOrderManager.t.sol +++ b/contracts/test/MiladyPoolOrderManager.t.sol @@ -199,9 +199,12 @@ contract MiladyPoolOrderManagerTest is MiladyPoolDeployer, Deployers { hooksUseable ); + // Supporting exact input, zero for one IPoolManager.SwapParams memory swapParams = IPoolManager.SwapParams({ - zeroForOne: true, - amountSpecified: 100 * 10 ** 18, + zeroForOne: Currency.unwrap(token0) < Currency.unwrap(token1) + ? true + : false, + amountSpecified: -100 * 10 ** 18, sqrtPriceLimitX96: TickMath.MIN_SQRT_PRICE + 1 });