Skip to content

Commit

Permalink
Remove initialize hookdata (#894)
Browse files Browse the repository at this point in the history
* remove hookData from initialize hooks

* fix ABIs in tests

* snapshots

* forge fmt

* foundryup'd gas snaps
  • Loading branch information
saucepoint authored Oct 8, 2024
1 parent 9293e5a commit 88482f7
Show file tree
Hide file tree
Showing 35 changed files with 131 additions and 204 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
274206
274240
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
60013
59536
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24073
24001
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
141217
141195
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130585
130597
2 changes: 1 addition & 1 deletion .forge-snapshots/swap CA custom curve + swap noop.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124651
124629
2 changes: 1 addition & 1 deletion .forge-snapshots/swap CA fee on unspecified.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154771
154749
Original file line number Diff line number Diff line change
@@ -1 +1 @@
206437
206415
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132331
132321
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with lp fee and protocol fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
169615
169593
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with return dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
145695
145673
2 changes: 1 addition & 1 deletion .forge-snapshots/update dynamic fee in before swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
147978
147956
10 changes: 3 additions & 7 deletions src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
}

/// @inheritdoc IPoolManager
function initialize(PoolKey memory key, uint160 sqrtPriceX96, bytes calldata hookData)
external
noDelegateCall
returns (int24 tick)
{
function initialize(PoolKey memory key, uint160 sqrtPriceX96) external noDelegateCall returns (int24 tick) {
// see TickBitmap.sol for overflow conditions that can arise from tick spacing being too large
if (key.tickSpacing > MAX_TICK_SPACING) TickSpacingTooLarge.selector.revertWith(key.tickSpacing);
if (key.tickSpacing < MIN_TICK_SPACING) TickSpacingTooSmall.selector.revertWith(key.tickSpacing);
Expand All @@ -129,14 +125,14 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim

uint24 lpFee = key.fee.getInitialLPFee();

key.hooks.beforeInitialize(key, sqrtPriceX96, hookData);
key.hooks.beforeInitialize(key, sqrtPriceX96);

PoolId id = key.toId();
uint24 protocolFee = _fetchProtocolFee(key);

tick = _pools[id].initialize(sqrtPriceX96, protocolFee, lpFee);

key.hooks.afterInitialize(key, sqrtPriceX96, tick, hookData);
key.hooks.afterInitialize(key, sqrtPriceX96, tick);

// emit all details of a pool key. poolkeys are not saved in storage and must always be provided by the caller
// the key's fee may be a static fee or a sentinel to denote a dynamic fee.
Expand Down
16 changes: 4 additions & 12 deletions src/interfaces/IHooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,18 @@ interface IHooks {
/// @param sender The initial msg.sender for the initialize call
/// @param key The key for the pool being initialized
/// @param sqrtPriceX96 The sqrt(price) of the pool as a Q64.96
/// @param hookData Arbitrary data handed into the PoolManager by the initializer to be be passed on to the hook
/// @return bytes4 The function selector for the hook
function beforeInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96, bytes calldata hookData)
external
returns (bytes4);
function beforeInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96) external returns (bytes4);

/// @notice The hook called after the state of a pool is initialized
/// @param sender The initial msg.sender for the initialize call
/// @param key The key for the pool being initialized
/// @param sqrtPriceX96 The sqrt(price) of the pool as a Q64.96
/// @param tick The current tick after the state of a pool is initialized
/// @param hookData Arbitrary data handed into the PoolManager by the initializer to be be passed on to the hook
/// @return bytes4 The function selector for the hook
function afterInitialize(
address sender,
PoolKey calldata key,
uint160 sqrtPriceX96,
int24 tick,
bytes calldata hookData
) external returns (bytes4);
function afterInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96, int24 tick)
external
returns (bytes4);

/// @notice The hook called before liquidity is added
/// @param sender The initial msg.sender for the add liquidity call
Expand Down
5 changes: 1 addition & 4 deletions src/interfaces/IPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,8 @@ interface IPoolManager is IProtocolFees, IERC6909Claims, IExtsload, IExttload {
/// @dev A swap fee totaling MAX_SWAP_FEE (100%) makes exact output swaps impossible since the input is entirely consumed by the fee
/// @param key The pool key for the pool to initialize
/// @param sqrtPriceX96 The initial square root price
/// @param hookData The data to pass through to the initialize hooks
/// @return tick The initial tick of the pool
function initialize(PoolKey memory key, uint160 sqrtPriceX96, bytes calldata hookData)
external
returns (int24 tick);
function initialize(PoolKey memory key, uint160 sqrtPriceX96) external returns (int24 tick);

struct ModifyLiquidityParams {
// the lower and upper tick of the position
Expand Down
11 changes: 4 additions & 7 deletions src/libraries/Hooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,19 @@ library Hooks {
}

/// @notice calls beforeInitialize hook if permissioned and validates return value
function beforeInitialize(IHooks self, PoolKey memory key, uint160 sqrtPriceX96, bytes calldata hookData)
internal
noSelfCall(self)
{
function beforeInitialize(IHooks self, PoolKey memory key, uint160 sqrtPriceX96) internal noSelfCall(self) {
if (self.hasPermission(BEFORE_INITIALIZE_FLAG)) {
self.callHook(abi.encodeCall(IHooks.beforeInitialize, (msg.sender, key, sqrtPriceX96, hookData)));
self.callHook(abi.encodeCall(IHooks.beforeInitialize, (msg.sender, key, sqrtPriceX96)));
}
}

/// @notice calls afterInitialize hook if permissioned and validates return value
function afterInitialize(IHooks self, PoolKey memory key, uint160 sqrtPriceX96, int24 tick, bytes calldata hookData)
function afterInitialize(IHooks self, PoolKey memory key, uint160 sqrtPriceX96, int24 tick)
internal
noSelfCall(self)
{
if (self.hasPermission(AFTER_INITIALIZE_FLAG)) {
self.callHook(abi.encodeCall(IHooks.afterInitialize, (msg.sender, key, sqrtPriceX96, tick, hookData)));
self.callHook(abi.encodeCall(IHooks.afterInitialize, (msg.sender, key, sqrtPriceX96, tick)));
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/test/BaseTestHooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@ import {IPoolManager} from "../interfaces/IPoolManager.sol";
contract BaseTestHooks is IHooks {
error HookNotImplemented();

function beforeInitialize(
address, /* sender **/
PoolKey calldata, /* key **/
uint160, /* sqrtPriceX96 **/
bytes calldata /* hookData **/
) external virtual returns (bytes4) {
function beforeInitialize(address, /* sender **/ PoolKey calldata, /* key **/ uint160 /* sqrtPriceX96 **/ )
external
virtual
returns (bytes4)
{
revert HookNotImplemented();
}

function afterInitialize(
address, /* sender **/
PoolKey calldata, /* key **/
uint160, /* sqrtPriceX96 **/
int24, /* tick **/
bytes calldata /* hookData **/
int24 /* tick **/
) external virtual returns (bytes4) {
revert HookNotImplemented();
}
Expand Down
6 changes: 1 addition & 5 deletions src/test/DynamicFeesTestHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ contract DynamicFeesTestHook is BaseTestHooks {
fee = _fee;
}

function afterInitialize(address, PoolKey calldata key, uint160, int24, bytes calldata)
external
override
returns (bytes4)
{
function afterInitialize(address, PoolKey calldata key, uint160, int24) external override returns (bytes4) {
manager.updateDynamicLPFee(key, fee);
return IHooks.afterInitialize.selector;
}
Expand Down
14 changes: 2 additions & 12 deletions src/test/EmptyTestHooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,11 @@ contract EmptyTestHooks is IHooks {
);
}

function beforeInitialize(address, PoolKey calldata, uint160, bytes calldata)
external
pure
override
returns (bytes4)
{
function beforeInitialize(address, PoolKey calldata, uint160) external pure override returns (bytes4) {
return IHooks.beforeInitialize.selector;
}

function afterInitialize(address, PoolKey calldata, uint160, int24, bytes calldata)
external
pure
override
returns (bytes4)
{
function afterInitialize(address, PoolKey calldata, uint160, int24) external pure override returns (bytes4) {
return IHooks.afterInitialize.selector;
}

Expand Down
16 changes: 4 additions & 12 deletions src/test/MockHooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,14 @@ contract MockHooks is IHooks {

mapping(PoolId => uint16) public lpFees;

function beforeInitialize(address, PoolKey calldata, uint160, bytes calldata hookData)
external
override
returns (bytes4)
{
beforeInitializeData = hookData;
function beforeInitialize(address, PoolKey calldata, uint160) external override returns (bytes4) {
beforeInitializeData = new bytes(123);
bytes4 selector = MockHooks.beforeInitialize.selector;
return returnValues[selector] == bytes4(0) ? selector : returnValues[selector];
}

function afterInitialize(address, PoolKey calldata, uint160, int24, bytes calldata hookData)
external
override
returns (bytes4)
{
afterInitializeData = hookData;
function afterInitialize(address, PoolKey calldata, uint160, int24) external override returns (bytes4) {
afterInitializeData = new bytes(123);
bytes4 selector = MockHooks.afterInitialize.selector;
return returnValues[selector] == bytes4(0) ? selector : returnValues[selector];
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/PoolNestedActionsTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ contract NestedActionExecutor is Test, PoolTestBase {
PoolId id = key.toId();
(uint256 price,,,) = manager.getSlot0(id);
assertEq(price, 0);
manager.initialize(key, Constants.SQRT_PRICE_1_2, Constants.ZERO_BYTES);
manager.initialize(key, Constants.SQRT_PRICE_1_2);
(price,,,) = manager.getSlot0(id);
assertEq(price, Constants.SQRT_PRICE_1_2);
}
Expand Down
10 changes: 3 additions & 7 deletions src/test/ProxyPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ contract ProxyPoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909
}

/// @inheritdoc IPoolManager
function initialize(PoolKey memory key, uint160 sqrtPriceX96, bytes calldata hookData)
external
noDelegateCall
returns (int24 tick)
{
function initialize(PoolKey memory key, uint160 sqrtPriceX96) external noDelegateCall returns (int24 tick) {
// see TickBitmap.sol for overflow conditions that can arise from tick spacing being too large
if (key.tickSpacing > MAX_TICK_SPACING) TickSpacingTooLarge.selector.revertWith(key.tickSpacing);
if (key.tickSpacing < MIN_TICK_SPACING) TickSpacingTooSmall.selector.revertWith(key.tickSpacing);
Expand All @@ -85,14 +81,14 @@ contract ProxyPoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909

uint24 lpFee = key.fee.getInitialLPFee();

key.hooks.beforeInitialize(key, sqrtPriceX96, hookData);
key.hooks.beforeInitialize(key, sqrtPriceX96);

PoolId id = key.toId();
uint24 protocolFee = _fetchProtocolFee(key);

tick = _pools[id].initialize(sqrtPriceX96, protocolFee, lpFee);

key.hooks.afterInitialize(key, sqrtPriceX96, tick, hookData);
key.hooks.afterInitialize(key, sqrtPriceX96, tick);

// emit all details of a pool key. poolkeys are not saved in storage and must always be provided by the caller
// the key's fee may be a static fee or a sentinel to denote a dynamic fee.
Expand Down
16 changes: 6 additions & 10 deletions src/test/SkipCallsTestHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,19 @@ contract SkipCallsTestHook is BaseTestHooks, Test {
manager = _manager;
}

function beforeInitialize(address, PoolKey calldata key, uint160 sqrtPriceX96, bytes calldata hookData)
external
override
returns (bytes4)
{
function beforeInitialize(address, PoolKey calldata key, uint160 sqrtPriceX96) external override returns (bytes4) {
counter++;
_initialize(key, sqrtPriceX96, hookData);
_initialize(key, sqrtPriceX96);
return IHooks.beforeInitialize.selector;
}

function afterInitialize(address, PoolKey calldata key, uint160 sqrtPriceX96, int24, bytes calldata hookData)
function afterInitialize(address, PoolKey calldata key, uint160 sqrtPriceX96, int24)
external
override
returns (bytes4)
{
counter++;
_initialize(key, sqrtPriceX96, hookData);
_initialize(key, sqrtPriceX96);
return IHooks.afterInitialize.selector;
}

Expand Down Expand Up @@ -137,10 +133,10 @@ contract SkipCallsTestHook is BaseTestHooks, Test {
return IHooks.afterDonate.selector;
}

function _initialize(PoolKey memory key, uint160 sqrtPriceX96, bytes calldata hookData) public {
function _initialize(PoolKey memory key, uint160 sqrtPriceX96) public {
// initialize a new pool with different fee
key.fee = 2000;
IPoolManager(manager).initialize(key, sqrtPriceX96, hookData);
IPoolManager(manager).initialize(key, sqrtPriceX96);
}

function _swap(PoolKey calldata key, IPoolManager.SwapParams memory params, bytes calldata hookData) public {
Expand Down
2 changes: 1 addition & 1 deletion test/CustomAccounting.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ contract CustomAccountingTest is Test, Deployers, GasSnapshot {
vm.etch(hookAddr, implAddr.code);
hook = hookAddr;

(key,) = initPoolAndAddLiquidity(currency0, currency1, IHooks(hookAddr), 100, SQRT_PRICE_1_1, ZERO_BYTES);
(key,) = initPoolAndAddLiquidity(currency0, currency1, IHooks(hookAddr), 100, SQRT_PRICE_1_1);
}

// ------------------------ SWAP ------------------------
Expand Down
Loading

0 comments on commit 88482f7

Please sign in to comment.