Skip to content

Commit 93a85fb

Browse files
committed
feat: updated testUtil and counter hook tests
1 parent 75c15f7 commit 93a85fb

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

test/pool-bin/BinCounterHook.t.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ contract BinCounterHookTest is Test, BinTestUtils {
4646

4747
MockERC20(Currency.unwrap(currency0)).mint(address(this), 1 ether);
4848
MockERC20(Currency.unwrap(currency1)).mint(address(this), 1 ether);
49-
addLiquidity(key, 1 ether, 1 ether, ACTIVE_ID, 3);
49+
addLiquidity(key, 1 ether, 1 ether, ACTIVE_ID, 3, address(this));
5050

5151
assertEq(counterHook.beforeMintCount(key.toId()), 1);
5252
assertEq(counterHook.afterMintCount(key.toId()), 1);
@@ -55,7 +55,7 @@ contract BinCounterHookTest is Test, BinTestUtils {
5555
function testSwapCallback() public {
5656
MockERC20(Currency.unwrap(currency0)).mint(address(this), 1 ether);
5757
MockERC20(Currency.unwrap(currency1)).mint(address(this), 1 ether);
58-
addLiquidity(key, 1 ether, 1 ether, ACTIVE_ID, 3);
58+
addLiquidity(key, 1 ether, 1 ether, ACTIVE_ID, 3, address(this));
5959

6060
assertEq(counterHook.beforeSwapCount(key.toId()), 0);
6161
assertEq(counterHook.afterSwapCount(key.toId()), 0);

test/pool-bin/utils/BinTestUtils.sol

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ contract BinTestUtils is DeployPermit2 {
8080
uint128 amountX,
8181
uint128 amountY,
8282
uint24 currentActiveId,
83-
uint24 numOfBins
83+
uint24 numOfBins,
84+
address recipient
8485
) internal {
8586
uint24[] memory binIds = new uint24[](numOfBins);
8687
uint24 startId = currentActiveId - (numOfBins / 2);
@@ -116,7 +117,7 @@ contract BinTestUtils is DeployPermit2 {
116117
deltaIds: convertToRelative(binIds, currentActiveId),
117118
distributionX: distribX,
118119
distributionY: distribY,
119-
to: address(this)
120+
to: recipient
120121
});
121122

122123
Plan memory planner = Planner.init().add(Actions.BIN_ADD_LIQUIDITY, abi.encode(params));
@@ -126,8 +127,22 @@ contract BinTestUtils is DeployPermit2 {
126127

127128
function exactInputSingle(IBinRouterBase.BinSwapExactInputSingleParams memory params) internal {
128129
Plan memory plan = Planner.init().add(Actions.BIN_SWAP_EXACT_IN_SINGLE, abi.encode(params));
129-
bytes memory data =
130-
plan.finalizeSwap(params.poolKey.currency0, params.poolKey.currency1, ActionConstants.MSG_SENDER);
130+
bytes memory data = params.swapForY
131+
? plan.finalizeSwap(params.poolKey.currency0, params.poolKey.currency1, ActionConstants.MSG_SENDER)
132+
: plan.finalizeSwap(params.poolKey.currency1, params.poolKey.currency0, ActionConstants.MSG_SENDER);
133+
134+
bytes memory commands = abi.encodePacked(bytes1(uint8(Commands.V4_SWAP)));
135+
bytes[] memory inputs = new bytes[](1);
136+
inputs[0] = data;
137+
138+
universalRouter.execute(commands, inputs);
139+
}
140+
141+
function exactOutputSingle(IBinRouterBase.BinSwapExactOutputSingleParams memory params) internal {
142+
Plan memory plan = Planner.init().add(Actions.BIN_SWAP_EXACT_OUT_SINGLE, abi.encode(params));
143+
bytes memory data = params.swapForY
144+
? plan.finalizeSwap(params.poolKey.currency0, params.poolKey.currency1, ActionConstants.MSG_SENDER)
145+
: plan.finalizeSwap(params.poolKey.currency1, params.poolKey.currency0, ActionConstants.MSG_SENDER);
131146

132147
bytes memory commands = abi.encodePacked(bytes1(uint8(Commands.V4_SWAP)));
133148
bytes[] memory inputs = new bytes[](1);

test/pool-cl/CLCounterHook.t.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ contract CLCounterHookTest is Test, CLTestUtils {
4747

4848
MockERC20(Currency.unwrap(currency0)).mint(address(this), 1 ether);
4949
MockERC20(Currency.unwrap(currency1)).mint(address(this), 1 ether);
50-
addLiquidity(key, 1 ether, 1 ether, -60, 60);
50+
addLiquidity(key, 1 ether, 1 ether, -60, 60, address(this));
5151

5252
assertEq(hook.beforeAddLiquidityCount(key.toId()), 1);
5353
assertEq(hook.afterAddLiquidityCount(key.toId()), 1);
@@ -56,7 +56,7 @@ contract CLCounterHookTest is Test, CLTestUtils {
5656
function testSwapCallback() public {
5757
MockERC20(Currency.unwrap(currency0)).mint(address(this), 1 ether);
5858
MockERC20(Currency.unwrap(currency1)).mint(address(this), 1 ether);
59-
addLiquidity(key, 1 ether, 1 ether, -60, 60);
59+
addLiquidity(key, 1 ether, 1 ether, -60, 60, address(this));
6060

6161
assertEq(hook.beforeSwapCount(key.toId()), 0);
6262
assertEq(hook.afterSwapCount(key.toId()), 0);

test/pool-cl/utils/CLTestUtils.sol

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)