Skip to content

Commit

Permalink
use 2nd bit of uint24 to signal override
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed May 16, 2024
1 parent 60e1060 commit 9499de9
Show file tree
Hide file tree
Showing 20 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62333
62202
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22178
22121
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 @@
117497
117431
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132658
132592
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 @@
134837
134711
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 @@
183796
183730
Original file line number Diff line number Diff line change
@@ -1 +1 @@
113122
113056
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124465
124399
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 @@
136518
136452
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 @@
125646
125580
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 @@
147707
147641
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 @@
164512
164446
Original file line number Diff line number Diff line change
@@ -1 +1 @@
223504
223312
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 @@
148713
148647
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124477
124411
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 @@
181073
180884
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 @@
156643
156517
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 @@
159310
159121
15 changes: 7 additions & 8 deletions src/libraries/LPFeeLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/test/DynamicReturnFeeTestHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9499de9

Please sign in to comment.