Skip to content

Commit

Permalink
Optimise beforeSwap return fee
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 committed May 14, 2024
1 parent 761ccb9 commit 0f3f0a4
Show file tree
Hide file tree
Showing 31 changed files with 41 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151168
151174
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
329534
329540
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 @@
284169
284175
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 @@
141301
141307
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151144
151150
Original file line number Diff line number Diff line change
@@ -1 +1 @@
299696
299702
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 1 token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
108690
108696
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 2 tokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149228
149234
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62239
62245
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24031
23891
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
185021
185027
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 @@
121039
121045
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 @@
117814
117820
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121027
121033
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 @@
117517
117523
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132756
132762
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 @@
136229
135805
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 @@
185023
185029
Original file line number Diff line number Diff line change
@@ -1 +1 @@
113970
113976
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125391
125397
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 @@
137393
137399
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 @@
126493
126499
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 @@
148571
148577
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 @@
165380
165386
Original file line number Diff line number Diff line change
@@ -1 +1 @@
225454
225012
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 @@
149647
149653
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125403
125409
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 @@
182375
181943
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 @@
157794
157355
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 @@
160612
160180
34 changes: 11 additions & 23 deletions src/libraries/Hooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,6 @@ library Hooks {
(, delta) = abi.decode(result, (bytes4, int256));
}

/// @notice performs a hook call using the given calldata on the given hook
/// @return delta The delta returned by the hook
/// @return fee The fee returned by the hook
function callHookWithReturnDeltaAndFee(IHooks self, bytes memory data, bool parseReturn)
internal
returns (int256 delta, uint24 fee)
{
bytes memory result = callHook(self, data);
(, delta, fee) = abi.decode(result, (bytes4, int256, uint24));

if (!parseReturn) {
delta = 0;
}
}

/// @notice modifier to prevent calling a hook if they initiated the action
modifier noSelfCall(IHooks self) {
if (msg.sender != address(self)) {
Expand Down Expand Up @@ -258,17 +243,20 @@ library Hooks {
if (msg.sender == address(self)) return (amountToSwap, BeforeSwapDeltaLibrary.ZERO_DELTA, lpFee);

if (self.hasPermission(BEFORE_SWAP_FLAG)) {
bool canReturnDelta = self.hasPermission(BEFORE_SWAP_RETURNS_DELTA_FLAG);
(int256 result, uint24 _lpFee) = self.callHookWithReturnDeltaAndFee(
abi.encodeWithSelector(IHooks.beforeSwap.selector, msg.sender, key, params, hookData),
canReturnDelta
);
if (key.fee.isDynamicFee()) lpFee = _lpFee;
bytes memory result =
callHook(self, abi.encodeWithSelector(IHooks.beforeSwap.selector, msg.sender, key, params, hookData));

hookReturn = BeforeSwapDelta.wrap(result);
if (key.fee.isDynamicFee()) {
assembly {
lpFee := mload(add(result, 0x60))
}
}

// skip this logic for the case where the hook return is 0
if (canReturnDelta) {
if (self.hasPermission(BEFORE_SWAP_RETURNS_DELTA_FLAG)) {
assembly {
hookReturn := mload(add(result, 0x40))
}
// any return in unspecified is passed to the afterSwap hook for handling
int128 hookDeltaSpecified = hookReturn.getSpecifiedDelta();

Expand Down

0 comments on commit 0f3f0a4

Please sign in to comment.