From 9499de9e9a49589993bd0bb4cff4010c2b15b9f3 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Thu, 16 May 2024 17:47:58 -0400 Subject: [PATCH] use 2nd bit of uint24 to signal override --- .forge-snapshots/initialize.snap | 2 +- .forge-snapshots/poolManager bytecode size.snap | 2 +- .forge-snapshots/simple swap with native.snap | 2 +- .forge-snapshots/simple swap.snap | 2 +- .../swap CA custom curve + swap noop.snap | 2 +- .forge-snapshots/swap CA fee on unspecified.snap | 2 +- .../swap against liquidity with native token.snap | 2 +- .forge-snapshots/swap against liquidity.snap | 2 +- .forge-snapshots/swap burn 6909 for input.snap | 2 +- .../swap burn native 6909 for input.snap | 2 +- .../swap mint native output as 6909.snap | 2 +- .forge-snapshots/swap mint output as 6909.snap | 2 +- .../swap skips hook call if hook is caller.snap | 2 +- .forge-snapshots/swap with dynamic fee.snap | 2 +- .forge-snapshots/swap with hooks.snap | 2 +- .../swap with lp fee and protocol fee.snap | 2 +- .../swap with return dynamic fee.snap | 2 +- .../update dynamic fee in before swap.snap | 2 +- src/libraries/LPFeeLibrary.sol | 15 +++++++-------- src/test/DynamicReturnFeeTestHook.sol | 2 +- 20 files changed, 26 insertions(+), 27 deletions(-) diff --git a/.forge-snapshots/initialize.snap b/.forge-snapshots/initialize.snap index 0b63319b8..844cfc7d4 100644 --- a/.forge-snapshots/initialize.snap +++ b/.forge-snapshots/initialize.snap @@ -1 +1 @@ -62333 \ No newline at end of file +62202 \ No newline at end of file diff --git a/.forge-snapshots/poolManager bytecode size.snap b/.forge-snapshots/poolManager bytecode size.snap index 3109f436a..92cecb45e 100644 --- a/.forge-snapshots/poolManager bytecode size.snap +++ b/.forge-snapshots/poolManager bytecode size.snap @@ -1 +1 @@ -22178 \ No newline at end of file +22121 \ No newline at end of file diff --git a/.forge-snapshots/simple swap with native.snap b/.forge-snapshots/simple swap with native.snap index 42824e184..2ad268097 100644 --- a/.forge-snapshots/simple swap with native.snap +++ b/.forge-snapshots/simple swap with native.snap @@ -1 +1 @@ -117497 \ No newline at end of file +117431 \ No newline at end of file diff --git a/.forge-snapshots/simple swap.snap b/.forge-snapshots/simple swap.snap index c15fae4d9..37bd6d94c 100644 --- a/.forge-snapshots/simple swap.snap +++ b/.forge-snapshots/simple swap.snap @@ -1 +1 @@ -132658 \ No newline at end of file +132592 \ No newline at end of file diff --git a/.forge-snapshots/swap CA custom curve + swap noop.snap b/.forge-snapshots/swap CA custom curve + swap noop.snap index 5c6e6efc1..b90101cb9 100644 --- a/.forge-snapshots/swap CA custom curve + swap noop.snap +++ b/.forge-snapshots/swap CA custom curve + swap noop.snap @@ -1 +1 @@ -134837 \ No newline at end of file +134711 \ No newline at end of file diff --git a/.forge-snapshots/swap CA fee on unspecified.snap b/.forge-snapshots/swap CA fee on unspecified.snap index 5a47a52d2..b4673db0a 100644 --- a/.forge-snapshots/swap CA fee on unspecified.snap +++ b/.forge-snapshots/swap CA fee on unspecified.snap @@ -1 +1 @@ -183796 \ No newline at end of file +183730 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity with native token.snap b/.forge-snapshots/swap against liquidity with native token.snap index a2feb1274..976368665 100644 --- a/.forge-snapshots/swap against liquidity with native token.snap +++ b/.forge-snapshots/swap against liquidity with native token.snap @@ -1 +1 @@ -113122 \ No newline at end of file +113056 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity.snap b/.forge-snapshots/swap against liquidity.snap index 7534dc57f..c7dc9c39b 100644 --- a/.forge-snapshots/swap against liquidity.snap +++ b/.forge-snapshots/swap against liquidity.snap @@ -1 +1 @@ -124465 \ No newline at end of file +124399 \ No newline at end of file diff --git a/.forge-snapshots/swap burn 6909 for input.snap b/.forge-snapshots/swap burn 6909 for input.snap index 56e7cdb9f..dd0a4cda7 100644 --- a/.forge-snapshots/swap burn 6909 for input.snap +++ b/.forge-snapshots/swap burn 6909 for input.snap @@ -1 +1 @@ -136518 \ No newline at end of file +136452 \ No newline at end of file diff --git a/.forge-snapshots/swap burn native 6909 for input.snap b/.forge-snapshots/swap burn native 6909 for input.snap index f178d599c..a3b40bd26 100644 --- a/.forge-snapshots/swap burn native 6909 for input.snap +++ b/.forge-snapshots/swap burn native 6909 for input.snap @@ -1 +1 @@ -125646 \ No newline at end of file +125580 \ No newline at end of file diff --git a/.forge-snapshots/swap mint native output as 6909.snap b/.forge-snapshots/swap mint native output as 6909.snap index c77d7d546..eae2906eb 100644 --- a/.forge-snapshots/swap mint native output as 6909.snap +++ b/.forge-snapshots/swap mint native output as 6909.snap @@ -1 +1 @@ -147707 \ No newline at end of file +147641 \ No newline at end of file diff --git a/.forge-snapshots/swap mint output as 6909.snap b/.forge-snapshots/swap mint output as 6909.snap index a18cca339..9b81ca131 100644 --- a/.forge-snapshots/swap mint output as 6909.snap +++ b/.forge-snapshots/swap mint output as 6909.snap @@ -1 +1 @@ -164512 \ No newline at end of file +164446 \ No newline at end of file diff --git a/.forge-snapshots/swap skips hook call if hook is caller.snap b/.forge-snapshots/swap skips hook call if hook is caller.snap index 4415098ec..d8c43d6d2 100644 --- a/.forge-snapshots/swap skips hook call if hook is caller.snap +++ b/.forge-snapshots/swap skips hook call if hook is caller.snap @@ -1 +1 @@ -223504 \ No newline at end of file +223312 \ No newline at end of file diff --git a/.forge-snapshots/swap with dynamic fee.snap b/.forge-snapshots/swap with dynamic fee.snap index 549ab28a0..fc2465906 100644 --- a/.forge-snapshots/swap with dynamic fee.snap +++ b/.forge-snapshots/swap with dynamic fee.snap @@ -1 +1 @@ -148713 \ No newline at end of file +148647 \ No newline at end of file diff --git a/.forge-snapshots/swap with hooks.snap b/.forge-snapshots/swap with hooks.snap index c735c47a3..ca9e7dbe2 100644 --- a/.forge-snapshots/swap with hooks.snap +++ b/.forge-snapshots/swap with hooks.snap @@ -1 +1 @@ -124477 \ No newline at end of file +124411 \ No newline at end of file diff --git a/.forge-snapshots/swap with lp fee and protocol fee.snap b/.forge-snapshots/swap with lp fee and protocol fee.snap index c79983274..c0266ee6c 100644 --- a/.forge-snapshots/swap with lp fee and protocol fee.snap +++ b/.forge-snapshots/swap with lp fee and protocol fee.snap @@ -1 +1 @@ -181073 \ No newline at end of file +180884 \ No newline at end of file diff --git a/.forge-snapshots/swap with return dynamic fee.snap b/.forge-snapshots/swap with return dynamic fee.snap index 12c2c3ff9..b5ec628a7 100644 --- a/.forge-snapshots/swap with return dynamic fee.snap +++ b/.forge-snapshots/swap with return dynamic fee.snap @@ -1 +1 @@ -156643 \ No newline at end of file +156517 \ No newline at end of file diff --git a/.forge-snapshots/update dynamic fee in before swap.snap b/.forge-snapshots/update dynamic fee in before swap.snap index cca83b2ad..7453e1ef0 100644 --- a/.forge-snapshots/update dynamic fee in before swap.snap +++ b/.forge-snapshots/update dynamic fee in before swap.snap @@ -1 +1 @@ -159310 \ No newline at end of file +159121 \ No newline at end of file diff --git a/src/libraries/LPFeeLibrary.sol b/src/libraries/LPFeeLibrary.sol index fab282220..ca4738f12 100644 --- a/src/libraries/LPFeeLibrary.sol +++ b/src/libraries/LPFeeLibrary.sol @@ -10,20 +10,19 @@ library LPFeeLibrary { error FeeTooLarge(); uint24 public constant FEE_MASK = 0x7FFFFF; + uint24 public constant OVERRIDE_MASK = 0xBFFFFF; // the top bit of the fee is used to signal if a Pool's is dynamic - // it is also used to signal if a fee returned from beforeSwap should override the pool's stored fee uint24 public constant DYNAMIC_FEE_FLAG = 0x800000; + // the second bit of the fee (returned by beforeSwap) is used to signal if the stored fee should be overridden + uint24 public constant OVERRIDE_FLAG = 0x400000; + // the lp fee is represented in hundredths of a bip, so the max is 100% uint24 public constant MAX_LP_FEE = 1000000; - function _isFlagged(uint24 self) private pure returns (bool) { - return self & DYNAMIC_FEE_FLAG != 0; - } - function isDynamicFee(uint24 self) internal pure returns (bool) { - return _isFlagged(self); + return self & DYNAMIC_FEE_FLAG != 0; } function isValid(uint24 self) internal pure returns (bool) { @@ -43,12 +42,12 @@ library LPFeeLibrary { /// @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 _isFlagged(self); + return self & OVERRIDE_FLAG != 0; } /// @notice returns a fee with the override flag removed function removeOverrideFlag(uint24 self) internal pure returns (uint24) { - return self & FEE_MASK; + return self & OVERRIDE_MASK; } /// @notice Removes the override flag and validates the fee (reverts if the fee is too large) diff --git a/src/test/DynamicReturnFeeTestHook.sol b/src/test/DynamicReturnFeeTestHook.sol index abd1a53fd..c3627e2f8 100644 --- a/src/test/DynamicReturnFeeTestHook.sol +++ b/src/test/DynamicReturnFeeTestHook.sol @@ -28,7 +28,7 @@ contract DynamicReturnFeeTestHook is BaseTestHooks { returns (bytes4, BeforeSwapDelta, uint24) { // attach the fee flag to `fee` to enable overriding the pool's stored fee - return (IHooks.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, fee | LPFeeLibrary.DYNAMIC_FEE_FLAG); + return (IHooks.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, fee | LPFeeLibrary.OVERRIDE_FLAG); } function forcePoolFeeUpdate(PoolKey calldata _key, uint24 _fee) external {