Skip to content

Commit

Permalink
feat: updated universal router dependencies (#29)
Browse files Browse the repository at this point in the history
* feat: update universal router with new action and position call protection

* test: fix test case

* feat: reduce optimizer runs

* cleanup: remove unused code

* refactor 3: move v3 position manager refactoring to v3tov4migrator

* refactor 4: tweak permit2 call

* feat: updated test from latest periphery

* updated pancake-v4-periphery dependency

* feat: bump foundry to 20_000 runs to reduce gas

* feat: updated gas cost

* doc: update command comments
  • Loading branch information
ChefMist authored Nov 18, 2024
1 parent 0dcafa4 commit 89abc3f
Show file tree
Hide file tree
Showing 43 changed files with 85 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146493
146600
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123551
123658
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138896
138516
Original file line number Diff line number Diff line change
@@ -1 +1 @@
147458
147526
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178619
178993
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149260
149328
Original file line number Diff line number Diff line change
@@ -1 +1 @@
182464
182832
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153635
153700
Original file line number Diff line number Diff line change
@@ -1 +1 @@
159086
158706
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151835
151900
Original file line number Diff line number Diff line change
@@ -1 +1 @@
172172
171287
Original file line number Diff line number Diff line change
@@ -1 +1 @@
174498
173679
Original file line number Diff line number Diff line change
@@ -1 +1 @@
140202
139286
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181612
180567
Original file line number Diff line number Diff line change
@@ -1 +1 @@
246440
245086
Original file line number Diff line number Diff line change
@@ -1 +1 @@
183158
182362
Original file line number Diff line number Diff line change
@@ -1 +1 @@
185947
184903
Original file line number Diff line number Diff line change
@@ -1 +1 @@
250199
248853
Original file line number Diff line number Diff line change
@@ -1 +1 @@
187490
186698
Original file line number Diff line number Diff line change
@@ -1 +1 @@
160342
159426
Original file line number Diff line number Diff line change
@@ -1 +1 @@
100185
100167
Original file line number Diff line number Diff line change
@@ -1 +1 @@
100797
100791
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154817
154811
Original file line number Diff line number Diff line change
@@ -1 +1 @@
194002
193988
Original file line number Diff line number Diff line change
@@ -1 +1 @@
194070
194056
2 changes: 1 addition & 1 deletion .forge-snapshots/UniversalRouterBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24246
24429
2 changes: 1 addition & 1 deletion .forge-snapshots/UniversalRouterTest#test_sweep_token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
55435
55429
Original file line number Diff line number Diff line change
@@ -1 +1 @@
561379
561167
Original file line number Diff line number Diff line change
@@ -1 +1 @@
291572
291562
Original file line number Diff line number Diff line change
@@ -1 +1 @@
597025
596229
Original file line number Diff line number Diff line change
@@ -1 +1 @@
572739
572106
Original file line number Diff line number Diff line change
@@ -1 +1 @@
585675
585306
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ src = "src"
out = 'foundry-out'
libs = ["lib"]
via_ir = true
optimizer_runs = 10_000
optimizer_runs = 20_000
ffi = true
fs_permissions = [
{ access = "read-write", path = ".forge-snapshots/" },
Expand Down
2 changes: 1 addition & 1 deletion lib/pancake-v4-periphery
12 changes: 4 additions & 8 deletions src/base/Dispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -290,21 +290,17 @@ abstract contract Dispatcher is
poolKey := inputs.offset
sqrtPriceX96 := calldataload(add(inputs.offset, 0xc0)) // poolKey has 6 variable, so it takes 192 space = 0xc0
}
// <wip> remove "" hookData once we updated universal-router dependencies
(success, output) = address(clPoolManager).call(
abi.encodeCall(ICLPoolManager.initialize, (poolKey, sqrtPriceX96, ""))
);
(success, output) =
address(clPoolManager).call(abi.encodeCall(ICLPoolManager.initialize, (poolKey, sqrtPriceX96)));
} else if (command == Commands.V4_BIN_INITIALIZE_POOL) {
PoolKey calldata poolKey;
uint24 activeId;
assembly {
poolKey := inputs.offset
activeId := calldataload(add(inputs.offset, 0xc0)) // poolKey has 6 variable, so it takes 192 space = 0xc0
}
// <wip> remove "" hookData once we updated universal-router dependencies
(success, output) = address(binPoolManager).call(
abi.encodeCall(IBinPoolManager.initialize, (poolKey, activeId, ""))
);
(success, output) =
address(binPoolManager).call(abi.encodeCall(IBinPoolManager.initialize, (poolKey, activeId)));
} else if (command == Commands.V4_CL_POSITION_CALL) {
_checkV4ClPositionManagerCall(inputs);
(success, output) = address(V4_CL_POSITION_MANAGER).call{value: address(this).balance}(inputs);
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Commands.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ library Commands {
uint256 constant V4_BIN_INITIALIZE_POOL = 0x14;
uint256 constant V4_CL_POSITION_CALL = 0x15;
uint256 constant V4_BIN_POSITION_CALL = 0x16;
// COMMAND_PLACEHOLDER = 0x15 -> 0x20
// COMMAND_PLACEHOLDER = 0x17 -> 0x20

// Command Types where 0x21<=value<=0x3f
uint256 constant EXECUTE_SUB_PLAN = 0x21;
Expand Down
18 changes: 7 additions & 11 deletions test/UniversalRouter.crossVersion.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.15;
import {Test, console} from "forge-std/Test.sol";
import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";
import {IAllowanceTransfer} from "permit2/src/interfaces/IAllowanceTransfer.sol";
import {IWETH9} from "pancake-v4-periphery/src/interfaces/external/IWETH9.sol";
import {WETH} from "solmate/src/tokens/WETH.sol";
import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol";

Expand All @@ -21,6 +22,7 @@ import {CLPoolParametersHelper} from "pancake-v4-core/src/pool-cl/libraries/CLPo
import {BinPoolParametersHelper} from "pancake-v4-core/src/pool-bin/libraries/BinPoolParametersHelper.sol";
import {ActionConstants} from "pancake-v4-periphery/src/libraries/ActionConstants.sol";
import {Plan, Planner} from "pancake-v4-periphery/src/libraries/Planner.sol";
import {CLPositionDescriptorOffChain} from "pancake-v4-periphery/src/pool-cl/CLPositionDescriptorOffChain.sol";
import {CLPositionManager} from "pancake-v4-periphery/src/pool-cl/CLPositionManager.sol";
import {BinPositionManager} from "pancake-v4-periphery/src/pool-bin/BinPositionManager.sol";
import {Actions} from "pancake-v4-periphery/src/libraries/Actions.sol";
Expand Down Expand Up @@ -112,8 +114,10 @@ contract UniversalRouterCrossVersionTest is BasePancakeSwapV4, OldVersionHelper,
vault.registerApp(address(binPoolManager));
vault.registerApp(address(clPoolManager));

binPositionManager = new BinPositionManager(vault, binPoolManager, permit2);
clPositionManager = new CLPositionManager(vault, clPoolManager, permit2, 100_000);
binPositionManager = new BinPositionManager(vault, binPoolManager, permit2, IWETH9(address(weth)));
CLPositionDescriptorOffChain pd =
new CLPositionDescriptorOffChain("https://pancakeswap.finance/v4/pool-cl/positions/");
clPositionManager = new CLPositionManager(vault, clPoolManager, permit2, 100_000, pd, IWETH9(address(weth)));

///////////////////////////////////
//////////// Router setup /////////////
Expand Down Expand Up @@ -204,8 +208,6 @@ contract UniversalRouterCrossVersionTest is BasePancakeSwapV4, OldVersionHelper,
// OPEN_DELTA indicates using the amount from vault delta
amountIn: ActionConstants.OPEN_DELTA,
amountOutMinimum: 0.8 ether,
// TODO: price limit is removed in next version
sqrtPriceLimitX96: TickMath.MIN_SQRT_RATIO + 1,
hookData: new bytes(0)
});
planner.add(Actions.CL_SWAP_EXACT_IN_SINGLE, abi.encode(params));
Expand Down Expand Up @@ -276,8 +278,6 @@ contract UniversalRouterCrossVersionTest is BasePancakeSwapV4, OldVersionHelper,
// OPEN_DELTA indicates using the amount from vault delta
amountIn: ActionConstants.OPEN_DELTA,
amountOutMinimum: 0.8 ether,
// TODO: price limit is removed in next version
sqrtPriceLimitX96: zeroForOne ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1,
hookData: new bytes(0)
});
planner.add(Actions.CL_SWAP_EXACT_IN_SINGLE, abi.encode(params));
Expand Down Expand Up @@ -335,8 +335,6 @@ contract UniversalRouterCrossVersionTest is BasePancakeSwapV4, OldVersionHelper,
amountIn: 1 ether,
// we only need to check at the very end
amountOutMinimum: 0,
// TODO: price limit is removed in next version
sqrtPriceLimitX96: TickMath.MAX_SQRT_RATIO - 1,
hookData: new bytes(0)
});
planner.add(Actions.CL_SWAP_EXACT_IN_SINGLE, abi.encode(params));
Expand Down Expand Up @@ -418,8 +416,6 @@ contract UniversalRouterCrossVersionTest is BasePancakeSwapV4, OldVersionHelper,
amountIn: 1 ether,
// we only need to check at the very end
amountOutMinimum: 0,
// TODO: price limit is removed in next version
sqrtPriceLimitX96: zeroForOne ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1,
hookData: new bytes(0)
});
planner.add(Actions.CL_SWAP_EXACT_IN_SINGLE, abi.encode(params));
Expand Down Expand Up @@ -516,7 +512,7 @@ contract UniversalRouterCrossVersionTest is BasePancakeSwapV4, OldVersionHelper,
fee: uint24(LP_FEE),
parameters: bytes32(0).setTickSpacing(10)
});
clPoolManager.initialize(key, SQRT_PRICE_1_1, new bytes(0));
clPoolManager.initialize(key, SQRT_PRICE_1_1);

// prep position manager action to mint liquidity
Plan memory planner = Planner.init();
Expand Down
12 changes: 8 additions & 4 deletions test/V3ToV4Migration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.15;
import {Test, console} from "forge-std/Test.sol";
import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";
import {IAllowanceTransfer} from "permit2/src/interfaces/IAllowanceTransfer.sol";
import {IWETH9} from "pancake-v4-periphery/src/interfaces/external/IWETH9.sol";
import {WETH} from "solmate/src/tokens/WETH.sol";
import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol";

Expand All @@ -22,6 +23,7 @@ import {BinPoolParametersHelper} from "pancake-v4-core/src/pool-bin/libraries/Bi
import {ActionConstants} from "pancake-v4-periphery/src/libraries/ActionConstants.sol";
import {Plan, Planner} from "pancake-v4-periphery/src/libraries/Planner.sol";
import {CLPositionManager} from "pancake-v4-periphery/src/pool-cl/CLPositionManager.sol";
import {CLPositionDescriptorOffChain} from "pancake-v4-periphery/src/pool-cl/CLPositionDescriptorOffChain.sol";
import {BinPositionManager} from "pancake-v4-periphery/src/pool-bin/BinPositionManager.sol";
import {Actions} from "pancake-v4-periphery/src/libraries/Actions.sol";
import {IV3NonfungiblePositionManager} from
Expand Down Expand Up @@ -119,11 +121,13 @@ contract V3ToV4MigrationTest is BasePancakeSwapV4, OldVersionHelper, BinLiquidit
vault.registerApp(address(binPoolManager));
vault.registerApp(address(clPoolManager));

binPositionManager = new BinPositionManager(vault, binPoolManager, permit2);
binPositionManager = new BinPositionManager(vault, binPoolManager, permit2, IWETH9(address(weth)));
_approvePermit2ForCurrency(address(this), currency0, address(binPositionManager), permit2);
_approvePermit2ForCurrency(address(this), currency1, address(binPositionManager), permit2);

clPositionManager = new CLPositionManager(vault, clPoolManager, permit2, 100_000);
CLPositionDescriptorOffChain pd =
new CLPositionDescriptorOffChain("https://pancakeswap.finance/v4/pool-cl/positions/");
clPositionManager = new CLPositionManager(vault, clPoolManager, permit2, 100_000, pd, IWETH9(address(weth)));
_approvePermit2ForCurrency(address(this), currency0, address(clPositionManager), permit2);
_approvePermit2ForCurrency(address(this), currency1, address(clPositionManager), permit2);

Expand All @@ -135,7 +139,7 @@ contract V3ToV4MigrationTest is BasePancakeSwapV4, OldVersionHelper, BinLiquidit
fee: uint24(3000),
parameters: bytes32(0).setTickSpacing(10)
});
clPoolManager.initialize(clPoolKey, SQRT_PRICE_1_1, new bytes(0));
clPoolManager.initialize(clPoolKey, SQRT_PRICE_1_1);

binPoolKey = PoolKey({
currency0: currency0,
Expand All @@ -145,7 +149,7 @@ contract V3ToV4MigrationTest is BasePancakeSwapV4, OldVersionHelper, BinLiquidit
fee: uint24(3000),
parameters: bytes32(0).setBinStep(10)
});
binPoolManager.initialize(binPoolKey, ACTIVE_ID_1_1, new bytes(0));
binPoolManager.initialize(binPoolKey, ACTIVE_ID_1_1);

///////////////////////////////////
//////////// Router setup /////////////
Expand Down
12 changes: 8 additions & 4 deletions test/V3ToV4MigrationNative.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.15;
import {Test, console} from "forge-std/Test.sol";
import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";
import {IAllowanceTransfer} from "permit2/src/interfaces/IAllowanceTransfer.sol";
import {IWETH9} from "pancake-v4-periphery/src/interfaces/external/IWETH9.sol";
import {WETH} from "solmate/src/tokens/WETH.sol";
import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol";

Expand All @@ -22,6 +23,7 @@ import {BinPoolParametersHelper} from "pancake-v4-core/src/pool-bin/libraries/Bi
import {ActionConstants} from "pancake-v4-periphery/src/libraries/ActionConstants.sol";
import {Plan, Planner} from "pancake-v4-periphery/src/libraries/Planner.sol";
import {CLPositionManager} from "pancake-v4-periphery/src/pool-cl/CLPositionManager.sol";
import {CLPositionDescriptorOffChain} from "pancake-v4-periphery/src/pool-cl/CLPositionDescriptorOffChain.sol";
import {BinPositionManager} from "pancake-v4-periphery/src/pool-bin/BinPositionManager.sol";
import {Actions} from "pancake-v4-periphery/src/libraries/Actions.sol";
import {IV3NonfungiblePositionManager} from
Expand Down Expand Up @@ -88,10 +90,12 @@ contract V3ToV4MigrationNativeTest is BasePancakeSwapV4, OldVersionHelper, BinLi
vault.registerApp(address(binPoolManager));
vault.registerApp(address(clPoolManager));

binPositionManager = new BinPositionManager(vault, binPoolManager, permit2);
binPositionManager = new BinPositionManager(vault, binPoolManager, permit2, IWETH9(address(weth)));
_approvePermit2ForCurrency(address(this), currency1, address(binPositionManager), permit2);

clPositionManager = new CLPositionManager(vault, clPoolManager, permit2, 100_000);
CLPositionDescriptorOffChain pd =
new CLPositionDescriptorOffChain("https://pancakeswap.finance/v4/pool-cl/positions/");
clPositionManager = new CLPositionManager(vault, clPoolManager, permit2, 100_000, pd, IWETH9(address(weth)));
_approvePermit2ForCurrency(address(this), currency1, address(clPositionManager), permit2);

clPoolKey = PoolKey({
Expand All @@ -102,7 +106,7 @@ contract V3ToV4MigrationNativeTest is BasePancakeSwapV4, OldVersionHelper, BinLi
fee: uint24(3000),
parameters: bytes32(0).setTickSpacing(10)
});
clPoolManager.initialize(clPoolKey, SQRT_PRICE_1_1, new bytes(0));
clPoolManager.initialize(clPoolKey, SQRT_PRICE_1_1);

binPoolKey = PoolKey({
currency0: CurrencyLibrary.NATIVE,
Expand All @@ -112,7 +116,7 @@ contract V3ToV4MigrationNativeTest is BasePancakeSwapV4, OldVersionHelper, BinLi
fee: uint24(3000),
parameters: bytes32(0).setBinStep(10)
});
binPoolManager.initialize(binPoolKey, ACTIVE_ID_1_1, new bytes(0));
binPoolManager.initialize(binPoolKey, ACTIVE_ID_1_1);

///////////////////////////////////
//////////// Router setup /////////////
Expand Down
Loading

0 comments on commit 89abc3f

Please sign in to comment.