From 93a85fb996e815b64cc38bcaf357e3c91ccf9944 Mon Sep 17 00:00:00 2001 From: ChefMist <133624774+ChefMist@users.noreply.github.com> Date: Thu, 5 Sep 2024 17:22:54 +0800 Subject: [PATCH] feat: updated testUtil and counter hook tests --- test/pool-bin/BinCounterHook.t.sol | 4 +- test/pool-bin/utils/BinTestUtils.sol | 23 +++++++++-- test/pool-cl/CLCounterHook.t.sol | 4 +- test/pool-cl/utils/CLTestUtils.sol | 59 +++++++++++++++++++++++++--- 4 files changed, 76 insertions(+), 14 deletions(-) diff --git a/test/pool-bin/BinCounterHook.t.sol b/test/pool-bin/BinCounterHook.t.sol index f024dcb..2f9c005 100644 --- a/test/pool-bin/BinCounterHook.t.sol +++ b/test/pool-bin/BinCounterHook.t.sol @@ -46,7 +46,7 @@ contract BinCounterHookTest is Test, BinTestUtils { MockERC20(Currency.unwrap(currency0)).mint(address(this), 1 ether); MockERC20(Currency.unwrap(currency1)).mint(address(this), 1 ether); - addLiquidity(key, 1 ether, 1 ether, ACTIVE_ID, 3); + addLiquidity(key, 1 ether, 1 ether, ACTIVE_ID, 3, address(this)); assertEq(counterHook.beforeMintCount(key.toId()), 1); assertEq(counterHook.afterMintCount(key.toId()), 1); @@ -55,7 +55,7 @@ contract BinCounterHookTest is Test, BinTestUtils { function testSwapCallback() public { MockERC20(Currency.unwrap(currency0)).mint(address(this), 1 ether); MockERC20(Currency.unwrap(currency1)).mint(address(this), 1 ether); - addLiquidity(key, 1 ether, 1 ether, ACTIVE_ID, 3); + addLiquidity(key, 1 ether, 1 ether, ACTIVE_ID, 3, address(this)); assertEq(counterHook.beforeSwapCount(key.toId()), 0); assertEq(counterHook.afterSwapCount(key.toId()), 0); diff --git a/test/pool-bin/utils/BinTestUtils.sol b/test/pool-bin/utils/BinTestUtils.sol index 9f10f1c..d8e004d 100644 --- a/test/pool-bin/utils/BinTestUtils.sol +++ b/test/pool-bin/utils/BinTestUtils.sol @@ -80,7 +80,8 @@ contract BinTestUtils is DeployPermit2 { uint128 amountX, uint128 amountY, uint24 currentActiveId, - uint24 numOfBins + uint24 numOfBins, + address recipient ) internal { uint24[] memory binIds = new uint24[](numOfBins); uint24 startId = currentActiveId - (numOfBins / 2); @@ -116,7 +117,7 @@ contract BinTestUtils is DeployPermit2 { deltaIds: convertToRelative(binIds, currentActiveId), distributionX: distribX, distributionY: distribY, - to: address(this) + to: recipient }); Plan memory planner = Planner.init().add(Actions.BIN_ADD_LIQUIDITY, abi.encode(params)); @@ -126,8 +127,22 @@ contract BinTestUtils is DeployPermit2 { function exactInputSingle(IBinRouterBase.BinSwapExactInputSingleParams memory params) internal { Plan memory plan = Planner.init().add(Actions.BIN_SWAP_EXACT_IN_SINGLE, abi.encode(params)); - bytes memory data = - plan.finalizeSwap(params.poolKey.currency0, params.poolKey.currency1, ActionConstants.MSG_SENDER); + bytes memory data = params.swapForY + ? plan.finalizeSwap(params.poolKey.currency0, params.poolKey.currency1, ActionConstants.MSG_SENDER) + : plan.finalizeSwap(params.poolKey.currency1, params.poolKey.currency0, ActionConstants.MSG_SENDER); + + bytes memory commands = abi.encodePacked(bytes1(uint8(Commands.V4_SWAP))); + bytes[] memory inputs = new bytes[](1); + inputs[0] = data; + + universalRouter.execute(commands, inputs); + } + + function exactOutputSingle(IBinRouterBase.BinSwapExactOutputSingleParams memory params) internal { + Plan memory plan = Planner.init().add(Actions.BIN_SWAP_EXACT_OUT_SINGLE, abi.encode(params)); + bytes memory data = params.swapForY + ? plan.finalizeSwap(params.poolKey.currency0, params.poolKey.currency1, ActionConstants.MSG_SENDER) + : plan.finalizeSwap(params.poolKey.currency1, params.poolKey.currency0, ActionConstants.MSG_SENDER); bytes memory commands = abi.encodePacked(bytes1(uint8(Commands.V4_SWAP))); bytes[] memory inputs = new bytes[](1); diff --git a/test/pool-cl/CLCounterHook.t.sol b/test/pool-cl/CLCounterHook.t.sol index 5c1c77b..89ff2fe 100644 --- a/test/pool-cl/CLCounterHook.t.sol +++ b/test/pool-cl/CLCounterHook.t.sol @@ -47,7 +47,7 @@ contract CLCounterHookTest is Test, CLTestUtils { MockERC20(Currency.unwrap(currency0)).mint(address(this), 1 ether); MockERC20(Currency.unwrap(currency1)).mint(address(this), 1 ether); - addLiquidity(key, 1 ether, 1 ether, -60, 60); + addLiquidity(key, 1 ether, 1 ether, -60, 60, address(this)); assertEq(hook.beforeAddLiquidityCount(key.toId()), 1); assertEq(hook.afterAddLiquidityCount(key.toId()), 1); @@ -56,7 +56,7 @@ contract CLCounterHookTest is Test, CLTestUtils { function testSwapCallback() public { MockERC20(Currency.unwrap(currency0)).mint(address(this), 1 ether); MockERC20(Currency.unwrap(currency1)).mint(address(this), 1 ether); - addLiquidity(key, 1 ether, 1 ether, -60, 60); + addLiquidity(key, 1 ether, 1 ether, -60, 60, address(this)); assertEq(hook.beforeSwapCount(key.toId()), 0); assertEq(hook.afterSwapCount(key.toId()), 0); diff --git a/test/pool-cl/utils/CLTestUtils.sol b/test/pool-cl/utils/CLTestUtils.sol index aace0f5..b882070 100644 --- a/test/pool-cl/utils/CLTestUtils.sol +++ b/test/pool-cl/utils/CLTestUtils.sol @@ -77,9 +77,16 @@ contract CLTestUtils is DeployPermit2 { return SortTokens.sort(token0, token1); } - function addLiquidity(PoolKey memory key, uint128 amount0Max, uint128 amount1Max, int24 tickLower, int24 tickUpper) - internal - { + function addLiquidity( + PoolKey memory key, + uint128 amount0Max, + uint128 amount1Max, + int24 tickLower, + int24 tickUpper, + address recipient + ) internal returns (uint256 tokenId) { + tokenId = positionManager.nextTokenId(); + (uint160 sqrtPriceX96,,,) = poolManager.getSlot0(key.toId()); uint256 liquidity = LiquidityAmounts.getLiquidityForAmounts( sqrtPriceX96, @@ -90,7 +97,33 @@ contract CLTestUtils is DeployPermit2 { ); PositionConfig memory config = PositionConfig({poolKey: key, tickLower: tickLower, tickUpper: tickUpper}); Plan memory planner = Planner.init().add( - Actions.CL_MINT_POSITION, abi.encode(config, liquidity, amount0Max, amount1Max, address(this), new bytes(0)) + Actions.CL_MINT_POSITION, abi.encode(config, liquidity, amount0Max, amount1Max, recipient, new bytes(0)) + ); + bytes memory data = planner.finalizeModifyLiquidityWithClose(key); + positionManager.modifyLiquidities(data, block.timestamp); + } + + function decreaseLiquidity( + uint256 tokenId, + PoolKey memory key, + uint128 amount0, + uint128 amount1, + int24 tickLower, + int24 tickUpper + ) internal { + (uint160 sqrtPriceX96,,,) = poolManager.getSlot0(key.toId()); + uint256 liquidity = LiquidityAmounts.getLiquidityForAmounts( + sqrtPriceX96, + TickMath.getSqrtRatioAtTick(tickLower), + TickMath.getSqrtRatioAtTick(tickUpper), + amount0, + amount1 + ); + PositionConfig memory config = PositionConfig({poolKey: key, tickLower: tickLower, tickUpper: tickUpper}); + + // amount0Min and amount1Min is 0 as some hook takes a fee from here + Plan memory planner = Planner.init().add( + Actions.CL_DECREASE_LIQUIDITY, abi.encode(tokenId, config, liquidity, 0, 0, new bytes(0)) ); bytes memory data = planner.finalizeModifyLiquidityWithClose(key); positionManager.modifyLiquidities(data, block.timestamp); @@ -98,8 +131,22 @@ contract CLTestUtils is DeployPermit2 { function exactInputSingle(ICLRouterBase.CLSwapExactInputSingleParams memory params) internal { Plan memory plan = Planner.init().add(Actions.CL_SWAP_EXACT_IN_SINGLE, abi.encode(params)); - bytes memory data = - plan.finalizeSwap(params.poolKey.currency0, params.poolKey.currency1, ActionConstants.MSG_SENDER); + bytes memory data = params.zeroForOne + ? plan.finalizeSwap(params.poolKey.currency0, params.poolKey.currency1, ActionConstants.MSG_SENDER) + : plan.finalizeSwap(params.poolKey.currency1, params.poolKey.currency0, ActionConstants.MSG_SENDER); + + bytes memory commands = abi.encodePacked(bytes1(uint8(Commands.V4_SWAP))); + bytes[] memory inputs = new bytes[](1); + inputs[0] = data; + + universalRouter.execute(commands, inputs); + } + + function exactOutputSingle(ICLRouterBase.CLSwapExactOutputSingleParams memory params) internal { + Plan memory plan = Planner.init().add(Actions.CL_SWAP_EXACT_OUT_SINGLE, abi.encode(params)); + bytes memory data = params.zeroForOne + ? plan.finalizeSwap(params.poolKey.currency0, params.poolKey.currency1, ActionConstants.MSG_SENDER) + : plan.finalizeSwap(params.poolKey.currency1, params.poolKey.currency0, ActionConstants.MSG_SENDER); bytes memory commands = abi.encodePacked(bytes1(uint8(Commands.V4_SWAP))); bytes[] memory inputs = new bytes[](1);