Skip to content

Commit

Permalink
General gas tweaks (#63)
Browse files Browse the repository at this point in the history
* feat: gas tweak on tickMath

* feat: gas tweak on sqrtPriceMath

* feat: gas tweak tickmath getTickAtSqrtRatio

* test: fix test warning and added more test to TickMath

* test: add more test on TickMath

* feat: optimise tickbitMap flipTick

* feat: gas optimise tickbvitMap compressed and position

* feat: protocol fee lib tweaks
  • Loading branch information
ChefMist authored Jun 3, 2024
1 parent f2abcc4 commit 03ebac9
Show file tree
Hide file tree
Showing 58 changed files with 353 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
180732
180720
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137531
137534
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
331216
331204
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1824
1818
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30405
30417
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119509
119497
Original file line number Diff line number Diff line change
@@ -1 +1 @@
34487
34340
Original file line number Diff line number Diff line change
@@ -1 +1 @@
455
456
Original file line number Diff line number Diff line change
@@ -1 +1 @@
457
458
Original file line number Diff line number Diff line change
@@ -1 +1 @@
453
454
Original file line number Diff line number Diff line change
@@ -1 +1 @@
360023
358846
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175191
174344
Original file line number Diff line number Diff line change
@@ -1 +1 @@
245111
244098
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
112417
112420
Original file line number Diff line number Diff line change
@@ -1 +1 @@
59963
59775
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121271
120646
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137753
137041
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170905
169737
Original file line number Diff line number Diff line change
@@ -1 +1 @@
157667
156973
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
76212
75510
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149916
149059
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
93104
92402
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
76215
75513
Original file line number Diff line number Diff line change
@@ -1 +1 @@
520
369
Original file line number Diff line number Diff line change
@@ -1 +1 @@
648
370
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1942
1673
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2697
2147
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1692
1423
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2697
2147
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1911
1769
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2831
2689
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1661
1519
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2831
2689
2 changes: 1 addition & 1 deletion .forge-snapshots/TickTest#checkTicks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
350
186
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1159
1072
2 changes: 1 addition & 1 deletion .forge-snapshots/TickTest#update.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134827
134740
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#collectFee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25138
25135
Original file line number Diff line number Diff line change
@@ -1 +1 @@
81463
81494
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121269
121363
Original file line number Diff line number Diff line change
@@ -1 +1 @@
45005
45036
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
45004
45032
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#registerPoolManager.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
47916
47913
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5409
5244
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22506
22341
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5515
5350
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2592
2488
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2592
2488
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2592
2488
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2591
2475
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2900
2784
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2591
2475
30 changes: 16 additions & 14 deletions src/libraries/ProtocolFeeLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,38 @@ library ProtocolFeeLibrary {
// Max protocol fee is 0.1% (1000 pips)
uint16 public constant MAX_PROTOCOL_FEE = 1000;

// Thresholds used for optimized bounds checks on protocol fees
uint24 internal constant FEE_0_THRESHOLD = 1001;
uint24 internal constant FEE_1_THRESHOLD = 1001 << 12;

// the protocol fee is represented in hundredths of a bip
uint256 internal constant PIPS_DENOMINATOR = 1_000_000;

function getZeroForOneFee(uint24 self) internal pure returns (uint16) {
return uint16(self & (4096 - 1));
return uint16(self & 0xfff);
}

function getOneForZeroFee(uint24 self) internal pure returns (uint16) {
return uint16(self >> 12);
}

function validate(uint24 self) internal pure returns (bool) {
if (self != 0) {
uint16 fee0 = getZeroForOneFee(self);
uint16 fee1 = getOneForZeroFee(self);
// The fee is represented in pips and it cannot be greater than the MAX_PROTOCOL_FEE.
if ((fee0 > MAX_PROTOCOL_FEE) || (fee1 > MAX_PROTOCOL_FEE)) {
return false;
}
function validate(uint24 self) internal pure returns (bool valid) {
// Equivalent to: getZeroForOneFee(self) <= MAX_PROTOCOL_FEE && getOneForZeroFee(self) <= MAX_PROTOCOL_FEE
assembly {
let isZeroForOneFeeOk := lt(and(self, 0xfff), FEE_0_THRESHOLD)
let isOneForZeroFeeOk := lt(self, FEE_1_THRESHOLD)
valid := and(isZeroForOneFeeOk, isOneForZeroFeeOk)
}
return true;
}

// The protocol fee is taken from the input amount first and then the LP fee is taken from the remaining
// The swap fee is capped at 100%
// equivalent to protocolFee + lpFee(1_000_000 - protocolFee) / 1_000_000
function calculateSwapFee(uint24 self, uint24 lpFee) internal pure returns (uint24) {
unchecked {
uint256 numerator = uint256(self) * uint256(lpFee);
return uint24(uint256(self) + lpFee - UnsafeMath.divRoundingUp(numerator, PIPS_DENOMINATOR));
function calculateSwapFee(uint24 self, uint24 lpFee) internal pure returns (uint24 swapFee) {
assembly {
let numerator := mul(self, lpFee)
let divRoundingUp := add(div(numerator, PIPS_DENOMINATOR), gt(mod(numerator, PIPS_DENOMINATOR), 0))
swapFee := sub(add(self, lpFee), divRoundingUp)
}
}
}
36 changes: 31 additions & 5 deletions src/pool-cl/libraries/SqrtPriceMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ library SqrtPriceMath {
}
}

/// @notice Equivalent to: `a >= b ? a - b : b - a`
function absDiff(uint160 a, uint160 b) internal pure returns (uint256 res) {
assembly {
let diff := sub(a, b)
// mask = 0 if a >= b else -1 (all 1s)
let mask := sar(255, diff)
// if a >= b, res = a - b = 0 ^ (a - b)
// if a < b, res = b - a = ~~(b - a) = ~(-(b - a) - 1) = ~(a - b - 1) = (-1) ^ (a - b - 1)
// either way, res = mask ^ (a - b + mask)
res := xor(mask, add(mask, diff))
}
}

/// @notice Gets the amount1 delta between two prices
/// @dev Calculates liquidity * (sqrt(upper) - sqrt(lower))
/// @param sqrtRatioAX96 A sqrt price
Expand All @@ -212,11 +225,24 @@ library SqrtPriceMath {
pure
returns (uint256 amount1)
{
if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);

return roundUp
? FullMath.mulDivRoundingUp(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96)
: FullMath.mulDiv(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96);
uint256 numerator = absDiff(sqrtRatioAX96, sqrtRatioBX96);
uint256 denominator = FixedPoint96.Q96;
uint256 _liquidity;
assembly {
// avoid implicit upcasting
_liquidity := liquidity
}
/**
* Equivalent to:
* amount1 = roundUp
* ? FullMath.mulDivRoundingUp(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96)
* : FullMath.mulDiv(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96);
* Cannot overflow because `type(uint128).max * type(uint160).max >> 96 < (1 << 192)`.
*/
amount1 = FullMath.mulDiv(_liquidity, numerator, denominator);
assembly {
amount1 := add(amount1, and(gt(mulmod(_liquidity, numerator, denominator), 0), roundUp))
}
}

/// @notice Helper that gets signed currency0 delta
Expand Down
Loading

0 comments on commit 03ebac9

Please sign in to comment.