@@ -77,9 +77,16 @@ contract CLTestUtils is DeployPermit2 {
7777 return SortTokens.sort (token0, token1);
7878 }
7979
80- function addLiquidity (PoolKey memory key , uint128 amount0Max , uint128 amount1Max , int24 tickLower , int24 tickUpper )
81- internal
82- {
80+ function addLiquidity (
81+ PoolKey memory key ,
82+ uint128 amount0Max ,
83+ uint128 amount1Max ,
84+ int24 tickLower ,
85+ int24 tickUpper ,
86+ address recipient
87+ ) internal returns (uint256 tokenId ) {
88+ tokenId = positionManager.nextTokenId ();
89+
8390 (uint160 sqrtPriceX96 ,,,) = poolManager.getSlot0 (key.toId ());
8491 uint256 liquidity = LiquidityAmounts.getLiquidityForAmounts (
8592 sqrtPriceX96,
@@ -90,16 +97,56 @@ contract CLTestUtils is DeployPermit2 {
9097 );
9198 PositionConfig memory config = PositionConfig ({poolKey: key, tickLower: tickLower, tickUpper: tickUpper});
9299 Plan memory planner = Planner.init ().add (
93- Actions.CL_MINT_POSITION, abi.encode (config, liquidity, amount0Max, amount1Max, address (this ), new bytes (0 ))
100+ Actions.CL_MINT_POSITION, abi.encode (config, liquidity, amount0Max, amount1Max, recipient, new bytes (0 ))
101+ );
102+ bytes memory data = planner.finalizeModifyLiquidityWithClose (key);
103+ positionManager.modifyLiquidities (data, block .timestamp );
104+ }
105+
106+ function decreaseLiquidity (
107+ uint256 tokenId ,
108+ PoolKey memory key ,
109+ uint128 amount0 ,
110+ uint128 amount1 ,
111+ int24 tickLower ,
112+ int24 tickUpper
113+ ) internal {
114+ (uint160 sqrtPriceX96 ,,,) = poolManager.getSlot0 (key.toId ());
115+ uint256 liquidity = LiquidityAmounts.getLiquidityForAmounts (
116+ sqrtPriceX96,
117+ TickMath.getSqrtRatioAtTick (tickLower),
118+ TickMath.getSqrtRatioAtTick (tickUpper),
119+ amount0,
120+ amount1
121+ );
122+ PositionConfig memory config = PositionConfig ({poolKey: key, tickLower: tickLower, tickUpper: tickUpper});
123+
124+ // amount0Min and amount1Min is 0 as some hook takes a fee from here
125+ Plan memory planner = Planner.init ().add (
126+ Actions.CL_DECREASE_LIQUIDITY, abi.encode (tokenId, config, liquidity, 0 , 0 , new bytes (0 ))
94127 );
95128 bytes memory data = planner.finalizeModifyLiquidityWithClose (key);
96129 positionManager.modifyLiquidities (data, block .timestamp );
97130 }
98131
99132 function exactInputSingle (ICLRouterBase.CLSwapExactInputSingleParams memory params ) internal {
100133 Plan memory plan = Planner.init ().add (Actions.CL_SWAP_EXACT_IN_SINGLE, abi.encode (params));
101- bytes memory data =
102- plan.finalizeSwap (params.poolKey.currency0, params.poolKey.currency1, ActionConstants.MSG_SENDER);
134+ bytes memory data = params.zeroForOne
135+ ? plan.finalizeSwap (params.poolKey.currency0, params.poolKey.currency1, ActionConstants.MSG_SENDER)
136+ : plan.finalizeSwap (params.poolKey.currency1, params.poolKey.currency0, ActionConstants.MSG_SENDER);
137+
138+ bytes memory commands = abi.encodePacked (bytes1 (uint8 (Commands.V4_SWAP)));
139+ bytes [] memory inputs = new bytes [](1 );
140+ inputs[0 ] = data;
141+
142+ universalRouter.execute (commands, inputs);
143+ }
144+
145+ function exactOutputSingle (ICLRouterBase.CLSwapExactOutputSingleParams memory params ) internal {
146+ Plan memory plan = Planner.init ().add (Actions.CL_SWAP_EXACT_OUT_SINGLE, abi.encode (params));
147+ bytes memory data = params.zeroForOne
148+ ? plan.finalizeSwap (params.poolKey.currency0, params.poolKey.currency1, ActionConstants.MSG_SENDER)
149+ : plan.finalizeSwap (params.poolKey.currency1, params.poolKey.currency0, ActionConstants.MSG_SENDER);
103150
104151 bytes memory commands = abi.encodePacked (bytes1 (uint8 (Commands.V4_SWAP)));
105152 bytes [] memory inputs = new bytes [](1 );
0 commit comments