Skip to content

Commit

Permalink
feat: updated testUtil and counter hook tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefMist committed Sep 5, 2024
1 parent 75c15f7 commit 93a85fb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 deletions.
4 changes: 2 additions & 2 deletions test/pool-bin/BinCounterHook.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
23 changes: 19 additions & 4 deletions test/pool-bin/utils/BinTestUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions test/pool-cl/CLCounterHook.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
59 changes: 53 additions & 6 deletions test/pool-cl/utils/CLTestUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -90,16 +97,56 @@ 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);
}

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);
Expand Down

0 comments on commit 93a85fb

Please sign in to comment.