Skip to content

Commit

Permalink
update hook miner usage
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed Mar 16, 2024
1 parent 4f6851d commit 5f72147
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 51 deletions.
9 changes: 2 additions & 7 deletions test/FullRange.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
MockERC20 token1;
MockERC20 token2;

Currency currency0;
Currency currency1;

PoolManager manager;
FullRange fullRange;

PoolId id;
Expand All @@ -87,9 +83,8 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
token1 = tokens[1];
token2 = tokens[2];

uint160 flags =
uint160(Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_MODIFY_POSITION_FLAG | Hooks.BEFORE_SWAP_FLAG);
(, bytes32 salt) = HookMiner.find(address(this), flags, 0, type(FullRange).creationCode, abi.encode(manager));
uint160 flags = uint160(Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG | Hooks.BEFORE_SWAP_FLAG);
(, bytes32 salt) = HookMiner.find(address(this), flags, type(FullRange).creationCode, abi.encode(manager));
fullRange = new FullRange{salt: salt}(manager);

key = createPoolKey(token0, token1);
Expand Down
14 changes: 4 additions & 10 deletions test/GeomeanOracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ contract TestGeomeanOracle is Test, Deployers {
manager = new PoolManager(500000);

uint160 flags = uint160(
Hooks.BEFORE_INITIALIZE_FLAG | Hooks.AFTER_INITIALIZE_FLAG | Hooks.BEFORE_MODIFY_POSITION_FLAG
| Hooks.BEFORE_SWAP_FLAG
Hooks.BEFORE_INITIALIZE_FLAG | Hooks.AFTER_INITIALIZE_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG
| Hooks.BEFORE_REMOVE_LIQUIDITY_FLAG | Hooks.BEFORE_SWAP_FLAG
);
(, bytes32 salt) =
HookMiner.find(address(this), flags, 0, type(GeomeanOracle).creationCode, abi.encode(manager));
(, bytes32 salt) = HookMiner.find(address(this), flags, type(GeomeanOracle).creationCode, abi.encode(manager));
geomeanOracle = new GeomeanOracle{salt: salt}(manager);

vm.warp(1);
Expand Down Expand Up @@ -132,13 +131,8 @@ contract TestGeomeanOracle is Test, Deployers {

function testBeforeModifyPositionObservation() public {
manager.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
<<<<<<< HEAD
skip(2); // advance 2 seconds
modifyPositionRouter.modifyPosition(
=======
geomeanOracle.setTime(3); // advance 2 seconds
modifyLiquidityRouter.modifyLiquidity(
>>>>>>> main
key,
IPoolManager.ModifyLiquidityParams(
TickMath.minUsableTick(MAX_TICK_SPACING), TickMath.maxUsableTick(MAX_TICK_SPACING), 1000
Expand Down Expand Up @@ -198,7 +192,7 @@ contract TestGeomeanOracle is Test, Deployers {

function testPermanentLiquidity() public {
manager.initialize(key, SQRT_RATIO_2_1, ZERO_BYTES);
geomeanOracle.setTime(3); // advance 2 seconds
skip(2); // advance 2 seconds
modifyLiquidityRouter.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams(
Expand Down
3 changes: 1 addition & 2 deletions test/LimitOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ contract TestLimitOrder is Test, Deployers {
TestERC20 token0;
TestERC20 token1;
LimitOrder limitOrder;
PoolKey key;
PoolId id;

function setUp() public {
Expand All @@ -38,7 +37,7 @@ contract TestLimitOrder is Test, Deployers {
token1 = TestERC20(Currency.unwrap(currency1));

uint160 flags = uint160(Hooks.AFTER_INITIALIZE_FLAG | Hooks.AFTER_SWAP_FLAG);
(, bytes32 salt) = HookMiner.find(address(this), flags, 0, type(LimitOrder).creationCode, abi.encode(manager));
(, bytes32 salt) = HookMiner.find(address(this), flags, type(LimitOrder).creationCode, abi.encode(manager));
limitOrder = new LimitOrder{salt: salt}(manager);

// key = PoolKey(currency0, currency1, 3000, 60, limitOrder);
Expand Down
6 changes: 2 additions & 4 deletions test/TWAMM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ contract TWAMMTest is Test, Deployers, GasSnapshot {
token1 = MockERC20(Currency.unwrap(currency1));

// Find a salt that produces a hook address with the desired `flags`
uint160 flags =
uint160(Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_MODIFY_POSITION_FLAG);
(, bytes32 salt) =
HookMiner.find(address(this), flags, 0, type(TWAMM).creationCode, abi.encode(manager, 10_000));
uint160 flags = uint160(Hooks.BEFORE_INITIALIZE_FLAG | Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG);
(, bytes32 salt) = HookMiner.find(address(this), flags, type(TWAMM).creationCode, abi.encode(manager, 10_000));

// Deploy hook to the precomputed address using the salt
twamm = new TWAMM{salt: salt}(manager, 10_000);
Expand Down
45 changes: 17 additions & 28 deletions test/utils/HookMiner.sol
Original file line number Diff line number Diff line change
@@ -1,54 +1,43 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.19;
pragma solidity ^0.8.21;

/// @title HookMiner - a library for mining hook addresses
/// @dev This library is intended for `forge test` environments. There may be gotchas when using salts in `forge script` or `forge create`
library HookMiner {
// mask to slice out the top 8 bit of the address
uint160 constant FLAG_MASK = 0xFF << 152;
// mask to slice out the top 12 bit of the address
uint160 constant FLAG_MASK = 0xFFF << 148;

// Maximum number of iterations to find a salt, avoid infinite loops
uint256 constant MAX_LOOP = 10_000;
uint256 constant MAX_LOOP = 20_000;

/// @notice Find a salt that produces a hook address with the desired `flags`
/// @param deployer The address that will deploy the hook.
/// In `forge test`, this will be the test contract `address(this)` or the pranking address
/// In `forge script`, this should be `0x4e59b44847b379578588920cA78FbF26c0B4956C` (CREATE2 Deployer Proxy)
/// @param deployer The address that will deploy the hook. In `forge test`, this will be the test contract `address(this)` or the pranking address
/// In `forge script`, this should be `0x4e59b44847b379578588920cA78FbF26c0B4956C` (CREATE2 Deployer Proxy)
/// @param flags The desired flags for the hook address
/// @param seed Use 0 for as a default. An optional starting salt when linearly searching for a salt
/// Useful for finding salts for multiple hooks with the same flags
/// @param creationCode The creation code of a hook contract. Example: `type(Counter).creationCode`
/// @param constructorArgs The encoded constructor arguments of a hook contract. Example: `abi.encode(address(manager))`
/// @return hookAddress salt and corresponding address that was found
/// The salt can be used in `new Hook{salt: salt}(<constructor arguments>)`
function find(
address deployer,
uint160 flags,
uint256 seed,
bytes memory creationCode,
bytes memory constructorArgs
) external pure returns (address, bytes32) {
/// @return hookAddress salt and corresponding address that was found. The salt can be used in `new Hook{salt: salt}(<constructor arguments>)`
function find(address deployer, uint160 flags, bytes memory creationCode, bytes memory constructorArgs)
internal
view
returns (address, bytes32)
{
address hookAddress;
bytes memory creationCodeWithArgs = abi.encodePacked(creationCode, constructorArgs);

uint256 salt = seed;
for (salt; salt < MAX_LOOP;) {
uint256 salt;
for (salt; salt < MAX_LOOP; salt++) {
hookAddress = computeAddress(deployer, salt, creationCodeWithArgs);
if (uint160(hookAddress) & FLAG_MASK == flags) {
if (uint160(hookAddress) & FLAG_MASK == flags && hookAddress.code.length == 0) {
return (hookAddress, bytes32(salt));
}

unchecked {
++salt;
}
}
revert("HookMiner: could not find salt");
}

/// @notice Precompute a contract address deployed via CREATE2
/// @param deployer The address that will deploy the hook
/// In `forge test`, this will be the test contract `address(this)` or the pranking address
/// In `forge script`, this should be `0x4e59b44847b379578588920cA78FbF26c0B4956C` (CREATE2 Deployer Proxy)
/// @param deployer The address that will deploy the hook. In `forge test`, this will be the test contract `address(this)` or the pranking address
/// In `forge script`, this should be `0x4e59b44847b379578588920cA78FbF26c0B4956C` (CREATE2 Deployer Proxy)
/// @param salt The salt used to deploy the hook
/// @param creationCode The creation code of a hook contract
function computeAddress(address deployer, uint256 salt, bytes memory creationCode)
Expand Down

0 comments on commit 5f72147

Please sign in to comment.