-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: hooks refactor POC #411
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
34 | ||
116 |
2 changes: 1 addition & 1 deletion
2
.forge-snapshots/before swap hook, already cached dynamic fee.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
189798 | ||
190434 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
145314 | ||
145735 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
134915 | ||
135307 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
186636 | ||
187028 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
14918 | ||
14910 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
53843 | ||
54228 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
319184 | ||
319684 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
198926 | ||
199307 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
202041 | ||
202422 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
24805 | ||
25224 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
190722 | ||
191143 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
202425 | ||
202846 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
121318 | ||
121739 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
109143 | ||
109564 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
127882 | ||
128303 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
212594 | ||
213015 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
189038 | ||
189521 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
109122 | ||
109543 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
195628 | ||
196264 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,18 @@ | |
pragma solidity ^0.8.20; | ||
|
||
import {IHooks} from "../interfaces/IHooks.sol"; | ||
import {IPoolManager} from "../interfaces/IPoolManager.sol"; | ||
import {FeeLibrary} from "../libraries/FeeLibrary.sol"; | ||
import {BalanceDelta} from "../types/BalanceDelta.sol"; | ||
import {PoolKey} from "../types/PoolKey.sol"; | ||
|
||
/// @notice V4 decides whether to invoke specific hooks by inspecting the leading bits of the address that | ||
/// the hooks contract is deployed to. | ||
/// For example, a hooks contract deployed to address: 0x9000000000000000000000000000000000000000 | ||
/// has leading bits '1001' which would cause the 'before initialize' and 'after modify position' hooks to be used. | ||
library Hooks { | ||
using FeeLibrary for uint24; | ||
using Hooks for IHooks; | ||
|
||
uint256 internal constant BEFORE_INITIALIZE_FLAG = 1 << 159; | ||
uint256 internal constant AFTER_INITIALIZE_FLAG = 1 << 158; | ||
|
@@ -44,12 +48,14 @@ library Hooks { | |
/// @dev calls param is memory as the function will be called from constructors | ||
function validateHookAddress(IHooks self, Calls memory calls) internal pure { | ||
if ( | ||
calls.beforeInitialize != shouldCallBeforeInitialize(self) | ||
|| calls.afterInitialize != shouldCallAfterInitialize(self) | ||
|| calls.beforeModifyPosition != shouldCallBeforeModifyPosition(self) | ||
|| calls.afterModifyPosition != shouldCallAfterModifyPosition(self) | ||
|| calls.beforeSwap != shouldCallBeforeSwap(self) || calls.afterSwap != shouldCallAfterSwap(self) | ||
|| calls.beforeDonate != shouldCallBeforeDonate(self) || calls.afterDonate != shouldCallAfterDonate(self) | ||
calls.beforeInitialize != self.shouldCall(BEFORE_INITIALIZE_FLAG) | ||
|| calls.afterInitialize != self.shouldCall(AFTER_INITIALIZE_FLAG) | ||
|| calls.beforeModifyPosition != self.shouldCall(BEFORE_MODIFY_POSITION_FLAG) | ||
|| calls.afterModifyPosition != self.shouldCall(AFTER_MODIFY_POSITION_FLAG) | ||
|| calls.beforeSwap != self.shouldCall(BEFORE_SWAP_FLAG) | ||
|| calls.afterSwap != self.shouldCall(AFTER_SWAP_FLAG) | ||
|| calls.beforeDonate != self.shouldCall(BEFORE_DONATE_FLAG) | ||
|| calls.afterDonate != self.shouldCall(AFTER_DONATE_FLAG) | ||
) { | ||
revert HookAddressNotValid(address(self)); | ||
} | ||
|
@@ -67,35 +73,79 @@ library Hooks { | |
); | ||
} | ||
|
||
function shouldCallBeforeInitialize(IHooks self) internal pure returns (bool) { | ||
return uint256(uint160(address(self))) & BEFORE_INITIALIZE_FLAG != 0; | ||
function validateHooksResponse(bytes4 selector, bytes4 expectedSelector) internal pure { | ||
if (selector != expectedSelector) { | ||
revert InvalidHookResponse(); | ||
} | ||
} | ||
|
||
function shouldCall(IHooks self, uint256 call) internal pure returns (bool) { | ||
return uint256(uint160(address(self))) & call != 0; | ||
} | ||
|
||
function beforeInitialize(PoolKey memory key, uint160 sqrtPriceX96, bytes memory hookData) internal { | ||
if (!shouldCall(key.hooks, BEFORE_INITIALIZE_FLAG)) return; | ||
validateHooksResponse( | ||
key.hooks.beforeInitialize(msg.sender, key, sqrtPriceX96, hookData), IHooks.beforeInitialize.selector | ||
); | ||
} | ||
|
||
function shouldCallAfterInitialize(IHooks self) internal pure returns (bool) { | ||
return uint256(uint160(address(self))) & AFTER_INITIALIZE_FLAG != 0; | ||
function afterInitialize(PoolKey memory key, uint160 sqrtPriceX96, int24 tick, bytes memory hookData) internal { | ||
if (!shouldCall(key.hooks, AFTER_INITIALIZE_FLAG)) return; | ||
validateHooksResponse( | ||
key.hooks.afterInitialize(msg.sender, key, sqrtPriceX96, tick, hookData), IHooks.afterInitialize.selector | ||
); | ||
} | ||
|
||
function shouldCallBeforeModifyPosition(IHooks self) internal pure returns (bool) { | ||
return uint256(uint160(address(self))) & BEFORE_MODIFY_POSITION_FLAG != 0; | ||
function beforeModifyPosition( | ||
PoolKey memory key, | ||
IPoolManager.ModifyPositionParams memory params, | ||
bytes memory hookData | ||
) internal { | ||
if (!key.hooks.shouldCall(BEFORE_MODIFY_POSITION_FLAG)) return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to add: CA handling |
||
validateHooksResponse( | ||
key.hooks.beforeModifyPosition(msg.sender, key, params, hookData), IHooks.beforeModifyPosition.selector | ||
); | ||
} | ||
|
||
function shouldCallAfterModifyPosition(IHooks self) internal pure returns (bool) { | ||
return uint256(uint160(address(self))) & AFTER_MODIFY_POSITION_FLAG != 0; | ||
function afterModifyPosition( | ||
PoolKey memory key, | ||
IPoolManager.ModifyPositionParams memory params, | ||
BalanceDelta delta, | ||
bytes memory hookData | ||
) internal { | ||
if (!key.hooks.shouldCall(AFTER_MODIFY_POSITION_FLAG)) return; | ||
validateHooksResponse( | ||
key.hooks.afterModifyPosition(msg.sender, key, params, delta, hookData), IHooks.afterModifyPosition.selector | ||
); | ||
} | ||
|
||
function shouldCallBeforeSwap(IHooks self) internal pure returns (bool) { | ||
return uint256(uint160(address(self))) & BEFORE_SWAP_FLAG != 0; | ||
function beforeSwap(PoolKey memory key, IPoolManager.SwapParams memory params, bytes memory hookData) internal { | ||
if (!key.hooks.shouldCall(BEFORE_SWAP_FLAG)) return; | ||
validateHooksResponse(key.hooks.beforeSwap(msg.sender, key, params, hookData), IHooks.beforeSwap.selector); | ||
} | ||
|
||
function shouldCallAfterSwap(IHooks self) internal pure returns (bool) { | ||
return uint256(uint160(address(self))) & AFTER_SWAP_FLAG != 0; | ||
function afterSwap( | ||
PoolKey memory key, | ||
IPoolManager.SwapParams memory params, | ||
BalanceDelta delta, | ||
bytes memory hookData | ||
) internal { | ||
if (!key.hooks.shouldCall(AFTER_SWAP_FLAG)) return; | ||
validateHooksResponse(key.hooks.afterSwap(msg.sender, key, params, delta, hookData), IHooks.afterSwap.selector); | ||
} | ||
|
||
function shouldCallBeforeDonate(IHooks self) internal pure returns (bool) { | ||
return uint256(uint160(address(self))) & BEFORE_DONATE_FLAG != 0; | ||
function beforeDonate(PoolKey memory key, uint256 amount0, uint256 amount1, bytes memory hookData) internal { | ||
if (!key.hooks.shouldCall(BEFORE_DONATE_FLAG)) return; | ||
validateHooksResponse( | ||
key.hooks.beforeDonate(msg.sender, key, amount0, amount1, hookData), IHooks.beforeDonate.selector | ||
); | ||
} | ||
|
||
function shouldCallAfterDonate(IHooks self) internal pure returns (bool) { | ||
return uint256(uint160(address(self))) & AFTER_DONATE_FLAG != 0; | ||
function afterDonate(PoolKey memory key, uint256 amount0, uint256 amount1, bytes memory hookData) internal { | ||
if (!key.hooks.shouldCall(AFTER_DONATE_FLAG)) return; | ||
validateHooksResponse( | ||
key.hooks.afterDonate(msg.sender, key, amount0, amount1, hookData), IHooks.afterDonate.selector | ||
); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to add: noop handling here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this refactor will be strange with NoOp because we have to run the clear currentHook logic before returning