Skip to content

Commit

Permalink
Change Hook Flags to LSB (#558)
Browse files Browse the repository at this point in the history
* merge

* update snaps

* run tests with --isolate

* changes

* update comment

* fix test_isValidHookAddress_valid_noFlagsWithDynamicFee

* add constant

* constants
  • Loading branch information
nishim3 authored May 22, 2024
1 parent b538356 commit e26ed2b
Show file tree
Hide file tree
Showing 39 changed files with 113 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/HooksShouldCallBeforeSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114
108
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153012
153006
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
330678
330666
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 @@
285671
285659
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143222
143216
Original file line number Diff line number Diff line change
@@ -1 +1 @@
301190
301184
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
61682
61634
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21335
21173
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186266
186254
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 @@
122634
122628
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119421
119415
Original file line number Diff line number Diff line change
@@ -1 +1 @@
104331
104325
2 changes: 1 addition & 1 deletion .forge-snapshots/simple addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
166823
166817
Original file line number Diff line number Diff line change
@@ -1 +1 @@
98050
98044
2 changes: 1 addition & 1 deletion .forge-snapshots/simple removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
90090
90084
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap with native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115825
115813
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130986
130974
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 @@
134432
134402
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 @@
182124
182094
Original file line number Diff line number Diff line change
@@ -1 +1 @@
111954
111942
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123297
123285
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135162
135150
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn native 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124409
124397
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint native output as 6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146149
146137
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint output as 6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162840
162828
Original file line number Diff line number Diff line change
@@ -1 +1 @@
220603
220585
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
147041
147029
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123309
123297
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 @@
179078
179060
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 @@
154900
154882
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 @@
157503
157485
38 changes: 20 additions & 18 deletions src/libraries/Hooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,38 @@ import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "../types/BeforeSwapDelta.
import {IPoolManager} from "../interfaces/IPoolManager.sol";
import {ParseBytes} from "../libraries/ParseBytes.sol";

/// @notice V4 decides whether to invoke specific hooks by inspecting the leading bits of the address that
/// @notice V4 decides whether to invoke specific hooks by inspecting the lowest significant 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 add liquidity' hooks to be used.
/// For example, a hooks contract deployed to address: 0x0000000000000000000000000000000000002400
/// has the lowest bits '10 0100 0000 0000' which would cause the 'before initialize' and 'after add liquidity' hooks to be used.
library Hooks {
using LPFeeLibrary for uint24;
using Hooks for IHooks;
using SafeCast for int256;
using BeforeSwapDeltaLibrary for BeforeSwapDelta;
using ParseBytes for bytes;

uint160 internal constant BEFORE_INITIALIZE_FLAG = 1 << 159;
uint160 internal constant AFTER_INITIALIZE_FLAG = 1 << 158;
uint160 internal constant ALL_HOOK_MASK = uint160((1 << 14) - 1);

uint160 internal constant BEFORE_ADD_LIQUIDITY_FLAG = 1 << 157;
uint160 internal constant AFTER_ADD_LIQUIDITY_FLAG = 1 << 156;
uint160 internal constant BEFORE_INITIALIZE_FLAG = 1 << 13;
uint160 internal constant AFTER_INITIALIZE_FLAG = 1 << 12;

uint160 internal constant BEFORE_REMOVE_LIQUIDITY_FLAG = 1 << 155;
uint160 internal constant AFTER_REMOVE_LIQUIDITY_FLAG = 1 << 154;
uint160 internal constant BEFORE_ADD_LIQUIDITY_FLAG = 1 << 11;
uint160 internal constant AFTER_ADD_LIQUIDITY_FLAG = 1 << 10;

uint160 internal constant BEFORE_SWAP_FLAG = 1 << 153;
uint160 internal constant AFTER_SWAP_FLAG = 1 << 152;
uint160 internal constant BEFORE_REMOVE_LIQUIDITY_FLAG = 1 << 9;
uint160 internal constant AFTER_REMOVE_LIQUIDITY_FLAG = 1 << 8;

uint160 internal constant BEFORE_DONATE_FLAG = 1 << 151;
uint160 internal constant AFTER_DONATE_FLAG = 1 << 150;
uint160 internal constant BEFORE_SWAP_FLAG = 1 << 7;
uint160 internal constant AFTER_SWAP_FLAG = 1 << 6;

uint160 internal constant BEFORE_SWAP_RETURNS_DELTA_FLAG = 1 << 149;
uint160 internal constant AFTER_SWAP_RETURNS_DELTA_FLAG = 1 << 148;
uint160 internal constant AFTER_ADD_LIQUIDITY_RETURNS_DELTA_FLAG = 1 << 147;
uint160 internal constant AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG = 1 << 146;
uint160 internal constant BEFORE_DONATE_FLAG = 1 << 5;
uint160 internal constant AFTER_DONATE_FLAG = 1 << 4;

uint160 internal constant BEFORE_SWAP_RETURNS_DELTA_FLAG = 1 << 3;
uint160 internal constant AFTER_SWAP_RETURNS_DELTA_FLAG = 1 << 2;
uint160 internal constant AFTER_ADD_LIQUIDITY_RETURNS_DELTA_FLAG = 1 << 1;
uint160 internal constant AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG = 1 << 0;

struct Permissions {
bool beforeInitialize;
Expand Down Expand Up @@ -117,7 +119,7 @@ library Hooks {
// If a hook contract is set, it must have at least 1 flag set, or have a dynamic fee
return address(self) == address(0)
? !fee.isDynamicFee()
: (uint160(address(self)) >= AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG || fee.isDynamicFee());
: (uint160(address(self)) & ALL_HOOK_MASK > 0 || fee.isDynamicFee());
}

/// @notice performs a hook call using the given calldata on the given hook that doesnt return a delta
Expand Down
4 changes: 2 additions & 2 deletions test/DynamicFees.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ contract TestDynamicFees is Test, Deployers, GasSnapshot {
DynamicFeesTestHook dynamicFeesHooks = DynamicFeesTestHook(
address(
uint160(
uint256(type(uint160).max) & clearAllHookPermisssionsMask | Hooks.BEFORE_SWAP_FLAG
uint256(type(uint160).max) & clearAllHookPermissionsMask | Hooks.BEFORE_SWAP_FLAG
| Hooks.AFTER_INITIALIZE_FLAG
)
)
);

DynamicFeesTestHook dynamicFeesNoHooks =
DynamicFeesTestHook(address(uint160(uint256(type(uint160).max) & clearAllHookPermisssionsMask)));
DynamicFeesTestHook(address(uint160(uint256(type(uint160).max) & clearAllHookPermissionsMask)));

event Swap(
PoolId indexed poolId,
Expand Down
2 changes: 1 addition & 1 deletion test/DynamicReturnFees.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract TestDynamicReturnFees is Test, Deployers, GasSnapshot {
using LPFeeLibrary for uint24;

DynamicReturnFeeTestHook dynamicReturnFeesHook = DynamicReturnFeeTestHook(
address(uint160(uint256(type(uint160).max) & clearAllHookPermisssionsMask | Hooks.BEFORE_SWAP_FLAG))
address(uint160(uint256(type(uint160).max) & clearAllHookPermissionsMask | Hooks.BEFORE_SWAP_FLAG))
);

event Swap(
Expand Down
25 changes: 12 additions & 13 deletions test/SkipCallsTestHook.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contract SkipCallsTest is Test, Deployers, GasSnapshot {

function test_beforeInitialize_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermisssionsMask | Hooks.BEFORE_INITIALIZE_FLAG))
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.BEFORE_INITIALIZE_FLAG))
);

// initializes pool and increments counter
Expand All @@ -53,7 +53,7 @@ contract SkipCallsTest is Test, Deployers, GasSnapshot {

function test_afterInitialize_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermisssionsMask | Hooks.AFTER_INITIALIZE_FLAG))
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.AFTER_INITIALIZE_FLAG))
);

// initializes pool and increments counter
Expand All @@ -63,7 +63,7 @@ contract SkipCallsTest is Test, Deployers, GasSnapshot {

function test_beforeAddLiquidity_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermisssionsMask | Hooks.BEFORE_ADD_LIQUIDITY_FLAG))
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.BEFORE_ADD_LIQUIDITY_FLAG))
);

deploy(skipCallsTestHook);
Expand All @@ -79,7 +79,7 @@ contract SkipCallsTest is Test, Deployers, GasSnapshot {

function test_afterAddLiquidity_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermisssionsMask | Hooks.AFTER_ADD_LIQUIDITY_FLAG))
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.AFTER_ADD_LIQUIDITY_FLAG))
);

deploy(skipCallsTestHook);
Expand All @@ -95,7 +95,7 @@ contract SkipCallsTest is Test, Deployers, GasSnapshot {

function test_beforeRemoveLiquidity_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermisssionsMask | Hooks.BEFORE_REMOVE_LIQUIDITY_FLAG))
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.BEFORE_REMOVE_LIQUIDITY_FLAG))
);

deploy(skipCallsTestHook);
Expand All @@ -114,7 +114,7 @@ contract SkipCallsTest is Test, Deployers, GasSnapshot {

function test_afterRemoveLiquidity_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermisssionsMask | Hooks.AFTER_REMOVE_LIQUIDITY_FLAG))
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.AFTER_REMOVE_LIQUIDITY_FLAG))
);

deploy(skipCallsTestHook);
Expand All @@ -133,7 +133,7 @@ contract SkipCallsTest is Test, Deployers, GasSnapshot {

function test_beforeSwap_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermisssionsMask | Hooks.BEFORE_SWAP_FLAG))
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.BEFORE_SWAP_FLAG))
);

deploy(skipCallsTestHook);
Expand All @@ -150,7 +150,7 @@ contract SkipCallsTest is Test, Deployers, GasSnapshot {

function test_gas_beforeSwap_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermisssionsMask | Hooks.BEFORE_SWAP_FLAG))
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.BEFORE_SWAP_FLAG))
);

deploy(skipCallsTestHook);
Expand All @@ -164,9 +164,8 @@ contract SkipCallsTest is Test, Deployers, GasSnapshot {
}

function test_afterSwap_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermisssionsMask | Hooks.AFTER_SWAP_FLAG))
);
SkipCallsTestHook skipCallsTestHook =
SkipCallsTestHook(address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.AFTER_SWAP_FLAG)));

deploy(skipCallsTestHook);
approveAndAddLiquidity(skipCallsTestHook);
Expand All @@ -182,7 +181,7 @@ contract SkipCallsTest is Test, Deployers, GasSnapshot {

function test_beforeDonate_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermisssionsMask | Hooks.BEFORE_DONATE_FLAG))
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.BEFORE_DONATE_FLAG))
);

deploy(skipCallsTestHook);
Expand All @@ -199,7 +198,7 @@ contract SkipCallsTest is Test, Deployers, GasSnapshot {

function test_afterDonate_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermisssionsMask | Hooks.AFTER_DONATE_FLAG))
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.AFTER_DONATE_FLAG))
);

deploy(skipCallsTestHook);
Expand Down
Loading

0 comments on commit e26ed2b

Please sign in to comment.