Skip to content

Commit

Permalink
[PART-1] enable hooks to return deltas to influence pool behavior (#44)
Browse files Browse the repository at this point in the history
* feat: enable hooks to return deltas to influence pool behavior

* test: fix & add  test cases hook returns delta

* [PART-2] support same owner multiple positions (#46)

* feat: add salt to distinguish positions for same owner in cl-pool

* feat: add salt to distinguish positions for same owner in bin-pool

* optimiazaiton: small tweaks per comments

* test: added salt != 0 cases

* [PART-3] allow `beforeSwap` to return temp lpFee & delta for both sides  (#47)

* feat: finish updating for cl-pool

* feat: finish updating for bin-pool

* chore: correct comments removed unnecessary isValid function
  • Loading branch information
chefburger authored May 27, 2024
1 parent 3d6abe4 commit 47315dc
Show file tree
Hide file tree
Showing 99 changed files with 3,327 additions and 877 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181124
182049
Original file line number Diff line number Diff line change
@@ -1 +1 @@
185453
185296
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137037
137576
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
329719
331250
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192675
193836
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137857
138533
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32585
32563
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30350
30417
Original file line number Diff line number Diff line change
@@ -1 +1 @@
147796
148635
Original file line number Diff line number Diff line change
@@ -1 +1 @@
295502
296486
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131067
131743
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120152
120042
Original file line number Diff line number Diff line change
@@ -1 +1 @@
974980
977200
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337174
339390
Original file line number Diff line number Diff line change
@@ -1 +1 @@
341313
342867
Original file line number Diff line number Diff line change
@@ -1 +1 @@
144254
145810
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179942
180562
Original file line number Diff line number Diff line change
@@ -1 +1 @@
185927
186545
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138369
138925
Original file line number Diff line number Diff line change
@@ -1 +1 @@
307829
309383
1 change: 0 additions & 1 deletion .forge-snapshots/BinPoolManagerTest#testNoOpGas_Burn.snap

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion .forge-snapshots/BinPoolManagerTest#testNoOpGas_Mint.snap

This file was deleted.

1 change: 0 additions & 1 deletion .forge-snapshots/BinPoolManagerTest#testNoOpGas_Swap.snap

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
34422
34487
Original file line number Diff line number Diff line change
@@ -1 +1 @@
362283
362972
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177451
178140
Original file line number Diff line number Diff line change
@@ -1 +1 @@
247570
248255
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170536
170425
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114459
114348
Original file line number Diff line number Diff line change
@@ -1 +1 @@
59393
59778
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123811
124482
Original file line number Diff line number Diff line change
@@ -1 +1 @@
141063
141710
Original file line number Diff line number Diff line change
@@ -1 +1 @@
174740
175389
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25078958
25094185
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
78932
79576
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153277
153925
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
95439
96383
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
78935
79579
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32281
32291

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion .forge-snapshots/CLPoolManagerTest#testNoOp_gas_Swap.snap

This file was deleted.

2 changes: 1 addition & 1 deletion .forge-snapshots/ExtsloadTest#extsload.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7468
7446
20 changes: 9 additions & 11 deletions src/libraries/Hooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ library Hooks {
using ParametersHelper for bytes32;
using LPFeeLibrary for uint24;

bytes4 constant NO_OP_SELECTOR = bytes4(keccak256(abi.encodePacked("NoOp")));

/// @notice Hook has no-op defined, but lacking before* call
error NoOpHookMissingBeforeCall();
/// @notice Hook permissions contain conflict
/// 1. enabled beforeSwapReturnsDelta, but lacking beforeSwap call
/// 2. enabled afterSwapReturnsDelta, but lacking afterSwap call
/// 3. enabled addLiquidityReturnsDelta/mintReturnsDelta, but lacking addLiquidity/mint call
/// 4. enabled removeLiquidityReturnsDelta/burnReturnsDelta, but lacking removeLiquidityburn call
error HookPermissionsValidationError();

/// @notice Hook config validation failed
/// 1. either registration bitmap mismatch
Expand All @@ -27,6 +29,9 @@ library Hooks {
/// @notice Hook did not return its selector
error InvalidHookResponse();

/// @notice Hook delta exceeds swap amount
error HookDeltaExceedsSwapAmount();

/// @notice Utility function intended to be used in pool initialization to ensure
/// the hook contract's hooks registration bitmap match the configration in the pool key
function validateHookConfig(PoolKey memory poolKey) internal view {
Expand Down Expand Up @@ -56,11 +61,4 @@ library Hooks {
function shouldCall(bytes32 parameters, uint8 offset, IHooks hook) internal view returns (bool) {
return hasOffsetEnabled(parameters, offset) && address(hook) != msg.sender;
}

/// @dev Verify hook return value matches no-op when these 2 conditions are met
/// 1) Hook have permission for no-op
/// 2) Return value is no-op selector
function isValidNoOpCall(bytes32 parameters, uint8 noOpOffset, bytes4 selector) internal pure returns (bool) {
return hasOffsetEnabled(parameters, noOpOffset) && selector == NO_OP_SELECTOR;
}
}
28 changes: 26 additions & 2 deletions src/libraries/LPFeeLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ library LPFeeLibrary {
error FeeTooLarge();

/// @dev the flag and mask
uint24 public constant STATIC_FEE_MASK = 0x0FFFFF;
uint24 public constant FEE_MASK = 0x7FFFFF;
uint24 public constant OVERRIDE_MASK = 0xBFFFFF;

// the top bit of the fee in a PoolKey is used to signal if a Pool's LP fee is dynamic
uint24 public constant DYNAMIC_FEE_FLAG = 0x800000;

// the second bit of the fee returned by beforeSwap is used to signal if the stored LP fee should be overridden in this swap
// only dynamic-fee pools can return a fee via the beforeSwap hook
uint24 public constant OVERRIDE_FEE_FLAG = 0x400000;

/// @dev the fee is represented in hundredths of a bip
/// @dev the max fee for cl pool is 100% and for bin, it is 10%
uint24 public constant ONE_HUNDRED_PERCENT_FEE = 1_000_000;
Expand All @@ -33,6 +40,23 @@ library LPFeeLibrary {
function getInitialLPFee(uint24 self) internal pure returns (uint24 lpFee) {
// the initial fee for a dynamic fee pool is 0
if (self.isDynamicLPFee()) return 0;
lpFee = self & STATIC_FEE_MASK;
lpFee = self & FEE_MASK;
}

/// @notice returns true if the fee has the override flag set (top bit of the uint24)
function isOverride(uint24 self) internal pure returns (bool) {
return self & OVERRIDE_FEE_FLAG != 0;
}

/// @notice returns a fee with the override flag removed
function removeOverrideFlag(uint24 self) internal pure returns (uint24) {
return self & OVERRIDE_MASK;
}

/// @notice Removes the override flag and validates the fee (reverts if the fee is too large)
function removeOverrideAndValidate(uint24 self, uint24 maxFee) internal pure returns (uint24) {
uint24 fee = self.removeOverrideFlag();
fee.validate(maxFee);
return fee;
}
}
Loading

0 comments on commit 47315dc

Please sign in to comment.