Skip to content

Commit

Permalink
feat: necessary updates to align with latest periphery design
Browse files Browse the repository at this point in the history
  • Loading branch information
chefburger committed Aug 27, 2024
1 parent 42463d3 commit a7fd359
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 125 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/pancake-v4-periphery"]
path = lib/pancake-v4-periphery
url = https://github.com/pancakeswap/pancake-v4-periphery
[submodule "lib/pancake-v4-universal-router"]
path = lib/pancake-v4-universal-router
url = https://github.com/pancakeswap/pancake-v4-universal-router
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[profile.default]
src = 'src'
out = 'foundry-out'
solc_version = '0.8.24'
libs = ["lib"]
solc_version = '0.8.26'
evm_version = 'cancun'
via_ir = true
1 change: 0 additions & 1 deletion lib/pancake-v4-periphery
Submodule pancake-v4-periphery deleted from d3ccae
1 change: 1 addition & 0 deletions lib/pancake-v4-universal-router
10 changes: 6 additions & 4 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
forge-std/=lib/forge-std/src/
ds-test/=lib/forge-std/lib/ds-test/src/
pancake-v4-periphery/=lib/pancake-v4-periphery/
pancake-v4-core/=lib/pancake-v4-periphery/lib/pancake-v4-core/
openzeppelin-contracts/=lib/pancake-v4-periphery/lib/pancake-v4-core/lib/openzeppelin-contracts/
solmate/=lib/pancake-v4-periphery/lib/pancake-v4-core/lib/solmate/
openzeppelin-contracts/=lib/pancake-v4-universal-router/lib/pancake-v4-periphery/lib/pancake-v4-core/lib/openzeppelin-contracts/
solmate/=lib/pancake-v4-universal-router/lib/pancake-v4-periphery/lib/pancake-v4-core/lib/solmate/
pancake-v4-core/=lib/pancake-v4-universal-router/lib/pancake-v4-periphery/lib/pancake-v4-core/
pancake-v4-periphery/=lib/pancake-v4-universal-router/lib/pancake-v4-periphery/
pancake-v4-universal-router/=lib/pancake-v4-universal-router/
permit2/=lib/pancake-v4-universal-router/lib/pancake-v4-periphery/lib/permit2/
20 changes: 10 additions & 10 deletions src/pool-bin/BinBaseHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import {
HOOKS_AFTER_SWAP_RETURNS_DELTA_OFFSET,
HOOKS_AFTER_MINT_RETURNS_DELTA_OFFSET,
HOOKS_AFTER_BURN_RETURNS_DELTA_OFFSET
} from "@pancakeswap/v4-core/src/pool-bin/interfaces/IBinHooks.sol";
import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol";
import {BeforeSwapDelta} from "@pancakeswap/v4-core/src/types/BeforeSwapDelta.sol";
import {BalanceDelta} from "@pancakeswap/v4-core/src/types/BalanceDelta.sol";
import {IHooks} from "@pancakeswap/v4-core/src/interfaces/IHooks.sol";
import {IVault} from "@pancakeswap/v4-core/src/interfaces/IVault.sol";
import {IBinHooks} from "@pancakeswap/v4-core/src/pool-bin/interfaces/IBinHooks.sol";
import {IBinPoolManager} from "@pancakeswap/v4-core/src/pool-bin/interfaces/IBinPoolManager.sol";
import {BinPoolManager} from "@pancakeswap/v4-core/src/pool-bin/BinPoolManager.sol";
} from "pancake-v4-core/src/pool-bin/interfaces/IBinHooks.sol";
import {PoolKey} from "pancake-v4-core/src/types/PoolKey.sol";
import {BeforeSwapDelta} from "pancake-v4-core/src/types/BeforeSwapDelta.sol";
import {BalanceDelta} from "pancake-v4-core/src/types/BalanceDelta.sol";
import {IHooks} from "pancake-v4-core/src/interfaces/IHooks.sol";
import {IVault} from "pancake-v4-core/src/interfaces/IVault.sol";
import {IBinHooks} from "pancake-v4-core/src/pool-bin/interfaces/IBinHooks.sol";
import {IBinPoolManager} from "pancake-v4-core/src/pool-bin/interfaces/IBinPoolManager.sol";
import {BinPoolManager} from "pancake-v4-core/src/pool-bin/BinPoolManager.sol";

abstract contract BinBaseHook is IBinHooks {
error NotPoolManager();
Expand Down Expand Up @@ -110,7 +110,7 @@ abstract contract BinBaseHook is IBinHooks {
function beforeMint(address, PoolKey calldata, IBinPoolManager.MintParams calldata, bytes calldata)
external
virtual
returns (bytes4)
returns (bytes4, uint24)
{
revert HookNotImplemented();
}
Expand Down
14 changes: 7 additions & 7 deletions src/pool-bin/BinCounterHook.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol";
import {BalanceDelta, BalanceDeltaLibrary} from "@pancakeswap/v4-core/src/types/BalanceDelta.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@pancakeswap/v4-core/src/types/BeforeSwapDelta.sol";
import {PoolId, PoolIdLibrary} from "@pancakeswap/v4-core/src/types/PoolId.sol";
import {IBinPoolManager} from "@pancakeswap/v4-core/src/pool-bin/interfaces/IBinPoolManager.sol";
import {PoolKey} from "pancake-v4-core/src/types/PoolKey.sol";
import {BalanceDelta, BalanceDeltaLibrary} from "pancake-v4-core/src/types/BalanceDelta.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "pancake-v4-core/src/types/BeforeSwapDelta.sol";
import {PoolId, PoolIdLibrary} from "pancake-v4-core/src/types/PoolId.sol";
import {IBinPoolManager} from "pancake-v4-core/src/pool-bin/interfaces/IBinPoolManager.sol";
import {BinBaseHook} from "./BinBaseHook.sol";

/// @notice BinCounterHook is a contract that counts the number of times a hook is called
Expand Down Expand Up @@ -45,10 +45,10 @@ contract BinCounterHook is BinBaseHook {
external
override
poolManagerOnly
returns (bytes4)
returns (bytes4, uint24)
{
beforeMintCount[key.toId()]++;
return this.beforeMint.selector;
return (this.beforeMint.selector, 0);
}

function afterMint(address, PoolKey calldata key, IBinPoolManager.MintParams calldata, BalanceDelta, bytes calldata)
Expand Down
18 changes: 9 additions & 9 deletions src/pool-cl/CLBaseHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import {
HOOKS_AFTER_SWAP_RETURNS_DELTA_OFFSET,
HOOKS_AFTER_ADD_LIQUIDIY_RETURNS_DELTA_OFFSET,
HOOKS_AFTER_REMOVE_LIQUIDIY_RETURNS_DELTA_OFFSET
} from "@pancakeswap/v4-core/src/pool-cl/interfaces/ICLHooks.sol";
import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol";
import {BalanceDelta} from "@pancakeswap/v4-core/src/types/BalanceDelta.sol";
import {BeforeSwapDelta} from "@pancakeswap/v4-core/src/types/BeforeSwapDelta.sol";
import {IHooks} from "@pancakeswap/v4-core/src/interfaces/IHooks.sol";
import {IVault} from "@pancakeswap/v4-core/src/interfaces/IVault.sol";
import {ICLHooks} from "@pancakeswap/v4-core/src/pool-cl/interfaces/ICLHooks.sol";
import {ICLPoolManager} from "@pancakeswap/v4-core/src/pool-cl/interfaces/ICLPoolManager.sol";
import {CLPoolManager} from "@pancakeswap/v4-core/src/pool-cl/CLPoolManager.sol";
} from "pancake-v4-core/src/pool-cl/interfaces/ICLHooks.sol";
import {PoolKey} from "pancake-v4-core/src/types/PoolKey.sol";
import {BalanceDelta} from "pancake-v4-core/src/types/BalanceDelta.sol";
import {BeforeSwapDelta} from "pancake-v4-core/src/types/BeforeSwapDelta.sol";
import {IHooks} from "pancake-v4-core/src/interfaces/IHooks.sol";
import {IVault} from "pancake-v4-core/src/interfaces/IVault.sol";
import {ICLHooks} from "pancake-v4-core/src/pool-cl/interfaces/ICLHooks.sol";
import {ICLPoolManager} from "pancake-v4-core/src/pool-cl/interfaces/ICLPoolManager.sol";
import {CLPoolManager} from "pancake-v4-core/src/pool-cl/CLPoolManager.sol";

abstract contract CLBaseHook is ICLHooks {
error NotPoolManager();
Expand Down
10 changes: 5 additions & 5 deletions src/pool-cl/CLCounterHook.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol";
import {BalanceDelta, BalanceDeltaLibrary} from "@pancakeswap/v4-core/src/types/BalanceDelta.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@pancakeswap/v4-core/src/types/BeforeSwapDelta.sol";
import {PoolId, PoolIdLibrary} from "@pancakeswap/v4-core/src/types/PoolId.sol";
import {ICLPoolManager} from "@pancakeswap/v4-core/src/pool-cl/interfaces/ICLPoolManager.sol";
import {PoolKey} from "pancake-v4-core/src/types/PoolKey.sol";
import {BalanceDelta, BalanceDeltaLibrary} from "pancake-v4-core/src/types/BalanceDelta.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "pancake-v4-core/src/types/BeforeSwapDelta.sol";
import {PoolId, PoolIdLibrary} from "pancake-v4-core/src/types/PoolId.sol";
import {ICLPoolManager} from "pancake-v4-core/src/pool-cl/interfaces/ICLPoolManager.sol";
import {CLBaseHook} from "./CLBaseHook.sol";

/// @notice CLCounterHook is a contract that counts the number of times a hook is called
Expand Down
20 changes: 9 additions & 11 deletions test/pool-bin/BinCounterHook.t.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {MockERC20} from "solmate/test/utils/mocks/MockERC20.sol";
import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol";
import {Test} from "forge-std/Test.sol";
import {Currency} from "@pancakeswap/v4-core/src/types/Currency.sol";
import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol";
import {BinPoolParametersHelper} from "@pancakeswap/v4-core/src/pool-bin/libraries/BinPoolParametersHelper.sol";
import {Currency} from "pancake-v4-core/src/types/Currency.sol";
import {PoolKey} from "pancake-v4-core/src/types/PoolKey.sol";
import {BinPoolParametersHelper} from "pancake-v4-core/src/pool-bin/libraries/BinPoolParametersHelper.sol";
import {BinCounterHook} from "../../src/pool-bin/BinCounterHook.sol";
import {BinTestUtils} from "./utils/BinTestUtils.sol";
import {PoolIdLibrary} from "@pancakeswap/v4-core/src/types/PoolId.sol";
import {IBinSwapRouterBase} from "@pancakeswap/v4-periphery/src/pool-bin/interfaces/IBinSwapRouterBase.sol";
import {PoolIdLibrary} from "pancake-v4-core/src/types/PoolId.sol";
import {IBinRouterBase} from "pancake-v4-periphery/src/pool-bin/interfaces/IBinRouterBase.sol";

contract BinCounterHookTest is Test, BinTestUtils {
using PoolIdLibrary for PoolKey;
Expand Down Expand Up @@ -61,16 +61,14 @@ contract BinCounterHookTest is Test, BinTestUtils {
assertEq(counterHook.afterSwapCount(key.toId()), 0);

MockERC20(Currency.unwrap(currency0)).mint(address(this), 0.1 ether);
swapRouter.exactInputSingle(
IBinSwapRouterBase.V4BinExactInputSingleParams({
exactInputSingle(
IBinRouterBase.BinSwapExactInputSingleParams({
poolKey: key,
swapForY: true,
recipient: address(this),
amountIn: 0.1 ether,
amountOutMinimum: 0,
hookData: new bytes(0)
}),
block.timestamp + 60
})
);

assertEq(counterHook.beforeSwapCount(key.toId()), 1);
Expand Down
96 changes: 70 additions & 26 deletions test/pool-bin/utils/BinTestUtils.sol
Original file line number Diff line number Diff line change
@@ -1,43 +1,74 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {MockERC20} from "solmate/test/utils/mocks/MockERC20.sol";
import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol";
import {Test} from "forge-std/Test.sol";
import {BinPoolManager} from "@pancakeswap/v4-core/src/pool-bin/BinPoolManager.sol";
import {Vault} from "@pancakeswap/v4-core/src/Vault.sol";
import {Currency} from "@pancakeswap/v4-core/src/types/Currency.sol";
import {SortTokens} from "@pancakeswap/v4-core/test/helpers/SortTokens.sol";
import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol";
import {SafeCast} from "@pancakeswap/v4-core/src/pool-bin/libraries/math/SafeCast.sol";
import {BinSwapRouter} from "@pancakeswap/v4-periphery/src/pool-bin/BinSwapRouter.sol";
import {BinFungiblePositionManager} from "@pancakeswap/v4-periphery/src/pool-bin/BinFungiblePositionManager.sol";
import {IBinFungiblePositionManager} from
"@pancakeswap/v4-periphery/src/pool-bin/interfaces/IBinFungiblePositionManager.sol";

contract BinTestUtils {
import {BinPoolManager} from "pancake-v4-core/src/pool-bin/BinPoolManager.sol";
import {Vault} from "pancake-v4-core/src/Vault.sol";
import {Currency} from "pancake-v4-core/src/types/Currency.sol";
import {SortTokens} from "pancake-v4-core/test/helpers/SortTokens.sol";
import {PoolKey} from "pancake-v4-core/src/types/PoolKey.sol";
import {SafeCast} from "pancake-v4-core/src/pool-bin/libraries/math/SafeCast.sol";
import {BinPositionManager} from "pancake-v4-periphery/src/pool-bin/BinPositionManager.sol";
import {IBinPositionManager} from "pancake-v4-periphery/src/pool-bin/interfaces/IBinPositionManager.sol";
import {IBinRouterBase} from "pancake-v4-periphery/src/pool-bin/interfaces/IBinRouterBase.sol";
import {Planner, Plan} from "pancake-v4-periphery/src/libraries/Planner.sol";
import {Actions} from "pancake-v4-periphery/src/libraries/Actions.sol";
import {DeployPermit2} from "permit2/test/utils/DeployPermit2.sol";
import {IAllowanceTransfer} from "permit2/src/interfaces/IAllowanceTransfer.sol";
import {UniversalRouter, RouterParameters} from "pancake-v4-universal-router/src/UniversalRouter.sol";
import {Commands} from "pancake-v4-universal-router/src/libraries/Commands.sol";
import {ActionConstants} from "pancake-v4-periphery/src/libraries/ActionConstants.sol";

contract BinTestUtils is DeployPermit2 {
using SafeCast for uint256;
using Planner for Plan;

Vault vault;
BinPoolManager poolManager;
BinFungiblePositionManager positionManager;
BinSwapRouter swapRouter;
BinPositionManager positionManager;
IAllowanceTransfer permit2;
UniversalRouter universalRouter;

function deployContractsWithTokens() internal returns (Currency, Currency) {
vault = new Vault();
poolManager = new BinPoolManager(vault, 500000);
vault.registerApp(address(poolManager));

positionManager = new BinFungiblePositionManager(vault, poolManager, address(0));
swapRouter = new BinSwapRouter(vault, poolManager, address(0));
permit2 = IAllowanceTransfer(deployPermit2());
positionManager = new BinPositionManager(vault, poolManager, permit2);

RouterParameters memory params = RouterParameters({
permit2: address(permit2),
weth9: address(0),
v2Factory: address(0),
v3Factory: address(0),
v3Deployer: address(0),
v2InitCodeHash: bytes32(0),
v3InitCodeHash: bytes32(0),
stableFactory: address(0),
stableInfo: address(0),
v4Vault: address(vault),
v4ClPoolManager: address(0),
v4BinPoolManager: address(poolManager),
v3NFTPositionManager: address(0),
v4ClPositionManager: address(0),
v4BinPositionManager: address(positionManager)
});
universalRouter = new UniversalRouter(params);

MockERC20 token0 = new MockERC20("token0", "T0", 18);
MockERC20 token1 = new MockERC20("token1", "T1", 18);

address[2] memory approvalAddress = [address(positionManager), address(swapRouter)];
for (uint256 i; i < approvalAddress.length; i++) {
token0.approve(approvalAddress[i], type(uint256).max);
token1.approve(approvalAddress[i], type(uint256).max);
}
// approve permit2 contract to transfer our funds
token0.approve(address(permit2), type(uint256).max);
token1.approve(address(permit2), type(uint256).max);

permit2.approve(address(token0), address(positionManager), type(uint160).max, uint48(block.timestamp + 1000));
permit2.approve(address(token1), address(positionManager), type(uint160).max, uint48(block.timestamp + 1000));

permit2.approve(address(token0), address(universalRouter), type(uint160).max, uint48(block.timestamp + 1000));
permit2.approve(address(token1), address(universalRouter), type(uint160).max, uint48(block.timestamp + 1000));

return SortTokens.sort(token0, token1);
}
Expand Down Expand Up @@ -73,7 +104,7 @@ contract BinTestUtils {
distribY[i] = binId <= currentActiveId && nbBinY > 0 ? uint256(1e18 / nbBinY).safe64() : 0;
}

IBinFungiblePositionManager.AddLiquidityParams memory params = IBinFungiblePositionManager.AddLiquidityParams({
IBinPositionManager.BinAddLiquidityParams memory params = IBinPositionManager.BinAddLiquidityParams({
poolKey: key,
amount0: amountX,
amount1: amountY,
Expand All @@ -84,11 +115,24 @@ contract BinTestUtils {
deltaIds: convertToRelative(binIds, currentActiveId),
distributionX: distribX,
distributionY: distribY,
to: address(this),
deadline: block.timestamp + 600
to: address(this)
});

positionManager.addLiquidity(params);
Plan memory planner = Planner.init().add(Actions.BIN_ADD_LIQUIDITY, abi.encode(params));
bytes memory data = planner.finalizeModifyLiquidityWithClose(params.poolKey);
positionManager.modifyLiquidities(data, block.timestamp);
}

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 commands = abi.encodePacked(bytes1(uint8(Commands.V4_SWAP)));
bytes[] memory inputs = new bytes[](1);
inputs[0] = data;

universalRouter.execute(commands, inputs);
}

/// @dev Given list of binIds and activeIds, return the delta ids.
Expand Down
24 changes: 11 additions & 13 deletions test/pool-cl/CLCounterHook.t.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {MockERC20} from "solmate/test/utils/mocks/MockERC20.sol";
import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol";
import {Test} from "forge-std/Test.sol";
import {Constants} from "@pancakeswap/v4-core/test/pool-cl/helpers/Constants.sol";
import {Currency} from "@pancakeswap/v4-core/src/types/Currency.sol";
import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol";
import {CLPoolParametersHelper} from "@pancakeswap/v4-core/src/pool-cl/libraries/CLPoolParametersHelper.sol";
import {Constants} from "pancake-v4-core/test/pool-cl/helpers/Constants.sol";
import {Currency} from "pancake-v4-core/src/types/Currency.sol";
import {PoolKey} from "pancake-v4-core/src/types/PoolKey.sol";
import {CLPoolParametersHelper} from "pancake-v4-core/src/pool-cl/libraries/CLPoolParametersHelper.sol";
import {CLCounterHook} from "../../src/pool-cl/CLCounterHook.sol";
import {CLTestUtils} from "./utils/CLTestUtils.sol";
import {CLPoolParametersHelper} from "@pancakeswap/v4-core/src/pool-cl/libraries/CLPoolParametersHelper.sol";
import {PoolIdLibrary} from "@pancakeswap/v4-core/src/types/PoolId.sol";
import {ICLSwapRouterBase} from "@pancakeswap/v4-periphery/src/pool-cl/interfaces/ICLSwapRouterBase.sol";
import {CLPoolParametersHelper} from "pancake-v4-core/src/pool-cl/libraries/CLPoolParametersHelper.sol";
import {PoolIdLibrary} from "pancake-v4-core/src/types/PoolId.sol";
import {ICLRouterBase} from "pancake-v4-periphery/src/pool-cl/interfaces/ICLRouterBase.sol";

contract CLCounterHookTest is Test, CLTestUtils {
using PoolIdLibrary for PoolKey;
Expand Down Expand Up @@ -62,17 +62,15 @@ contract CLCounterHookTest is Test, CLTestUtils {
assertEq(hook.afterSwapCount(key.toId()), 0);

MockERC20(Currency.unwrap(currency0)).mint(address(this), 0.1 ether);
swapRouter.exactInputSingle(
ICLSwapRouterBase.V4CLExactInputSingleParams({
exactInputSingle(
ICLRouterBase.CLSwapExactInputSingleParams({
poolKey: key,
zeroForOne: true,
recipient: address(this),
amountIn: 0.1 ether,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0,
hookData: new bytes(0)
}),
block.timestamp
})
);

assertEq(hook.beforeSwapCount(key.toId()), 1);
Expand Down
Loading

0 comments on commit a7fd359

Please sign in to comment.