Skip to content
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

Refactor hook call to use inline assembly #705

Merged
merged 3 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
325929
325681
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 @@
280141
279677
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19837
19769
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181448
181200
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 @@
135835
135371
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 @@
131947
131734
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 @@
178853
178608
Original file line number Diff line number Diff line change
@@ -1 +1 @@
216627
216411
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138307
137849
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 @@
175673
175460
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 @@
151773
151560
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 @@
154415
154202
32 changes: 21 additions & 11 deletions src/libraries/Hooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,27 @@ library Hooks {
/// @notice performs a hook call using the given calldata on the given hook that doesnt return a delta
/// @return result The complete data returned by the hook
function callHook(IHooks self, bytes memory data) internal returns (bytes memory result) {
bool success;
(success, result) = address(self).call(data);
if (!success) _revert(result);
/// @solidity memory-safe-assembly
assembly {
if iszero(call(gas(), self, 0, add(data, 0x20), mload(data), 0, 0)) {
if iszero(returndatasize()) {
// if the call failed without a revert reason, revert with `FailedHookCall()`
mstore(0, 0x36bc48c5)
revert(0x1c, 0x04)
}
// bubble up revert
returndatacopy(0, 0, returndatasize())
revert(0, returndatasize())
}
// allocate result byte array from the free memory pointer
result := mload(0x40)
// store new free memory pointer at the end of the array padded to 32 bytes
mstore(0x40, add(result, and(add(returndatasize(), 0x3f), not(0x1f))))
// store length in memory
mstore(result, returndatasize())
// copy return data to result
returndatacopy(add(result, 0x20), 0, returndatasize())
}

// Check expected selector and returned selector match.
if (result.parseSelector() != data.parseSelector()) InvalidHookResponse.selector.revertWith();
Expand Down Expand Up @@ -322,12 +340,4 @@ library Hooks {
function hasPermission(IHooks self, uint160 flag) internal pure returns (bool) {
return uint160(address(self)) & flag != 0;
}

/// @notice bubble up revert if present. Else throw FailedHookCall
function _revert(bytes memory result) private pure {
if (result.length == 0) FailedHookCall.selector.revertWith();
assembly {
revert(add(0x20, result), mload(result))
}
}
}
Loading