Skip to content

Commit

Permalink
Revert SqrtPriceMath errors in assembly (#694)
Browse files Browse the repository at this point in the history
* Optimize condition check and revert `PriceOverflow` error in assembly

* Revert `SqrtPriceMath` errors in assembly

* Revert `InvalidPrice` with inline assembly in `getAmount0Delta`

* Add comments about Solidity equivalence
  • Loading branch information
shuhuiluo authored May 23, 2024
1 parent 3351c80 commit 03636de
Show file tree
Hide file tree
Showing 39 changed files with 79 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactInCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1961
1947
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactInPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2468
2390
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactOutCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1700
1686
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactOutPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2956
2831
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactInCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2073
2059
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactInPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3021
2941
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactOutCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1812
1798
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactOutPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2554
2458
Original file line number Diff line number Diff line change
@@ -1 +1 @@
150602
150588
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
327348
327334
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 @@
281462
281448
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 @@
140808
140794
Original file line number Diff line number Diff line change
@@ -1 +1 @@
298780
298766
Original file line number Diff line number Diff line change
@@ -1 +1 @@
516
502
Original file line number Diff line number Diff line change
@@ -1 +1 @@
652
638
Original file line number Diff line number Diff line change
@@ -1 +1 @@
572
520
Original file line number Diff line number Diff line change
@@ -1 +1 @@
763
711
Original file line number Diff line number Diff line change
@@ -1 +1 @@
843
746
Original file line number Diff line number Diff line change
@@ -1 +1 @@
468
398
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20725
20580
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
182941
182927
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 @@
137230
137216
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 @@
117016
117002
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 @@
115044
114950
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130209
130115
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 @@
179850
179756
Original file line number Diff line number Diff line change
@@ -1 +1 @@
110546
110466
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121893
121813
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 @@
133767
133642
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 @@
123014
122930
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 @@
144784
144718
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 @@
161442
161348
Original file line number Diff line number Diff line change
@@ -1 +1 @@
218072
217898
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 @@
145643
145549
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138950
138870
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 @@
176884
176786
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 @@
152694
152600
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 @@
155296
155202
47 changes: 41 additions & 6 deletions src/libraries/SqrtPriceMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ library SqrtPriceMath {
uint256 product = amount * sqrtPX96;
// if the product overflows, we know the denominator underflows
// in addition, we must check that the denominator does not underflow
if (product / amount != sqrtPX96 || numerator1 <= product) revert PriceOverflow();
// equivalent: if (product / amount != sqrtPX96 || numerator1 <= product) revert PriceOverflow();
/// @solidity memory-safe-assembly
assembly {
if iszero(and(eq(div(product, amount), sqrtPX96), gt(numerator1, product))) {
mstore(0, 0xf5c787f1) // selector for PriceOverflow()
revert(0x1c, 0x04)
}
}
uint256 denominator = numerator1 - product;
return FullMath.mulDivRoundingUp(numerator1, sqrtPX96, denominator).toUint160();
}
Expand Down Expand Up @@ -94,7 +101,14 @@ library SqrtPriceMath {
: FullMath.mulDivRoundingUp(amount, FixedPoint96.Q96, liquidity)
);

if (sqrtPX96 <= quotient) revert NotEnoughLiquidity();
// equivalent: if (sqrtPX96 <= quotient) revert NotEnoughLiquidity();
/// @solidity memory-safe-assembly
assembly {
if iszero(gt(sqrtPX96, quotient)) {
mstore(0, 0x4323a555) // selector for NotEnoughLiquidity()
revert(0x1c, 0x04)
}
}
// always fits 160 bits
unchecked {
return uint160(sqrtPX96 - quotient);
Expand All @@ -114,7 +128,14 @@ library SqrtPriceMath {
pure
returns (uint160 sqrtQX96)
{
if (sqrtPX96 == 0 || liquidity == 0) revert InvalidPriceOrLiquidity();
// equivalent: if (sqrtPX96 == 0 || liquidity == 0) revert InvalidPriceOrLiquidity();
/// @solidity memory-safe-assembly
assembly {
if or(iszero(sqrtPX96), iszero(liquidity)) {
mstore(0, 0x4f2461b8) // selector for InvalidPriceOrLiquidity()
revert(0x1c, 0x04)
}
}

// round to make sure that we don't pass the target price
return zeroForOne
Expand All @@ -134,7 +155,14 @@ library SqrtPriceMath {
pure
returns (uint160 sqrtQX96)
{
if (sqrtPX96 == 0 || liquidity == 0) revert InvalidPriceOrLiquidity();
// equivalent: if (sqrtPX96 == 0 || liquidity == 0) revert InvalidPriceOrLiquidity();
/// @solidity memory-safe-assembly
assembly {
if or(iszero(sqrtPX96), iszero(liquidity)) {
mstore(0, 0x4f2461b8) // selector for InvalidPriceOrLiquidity()
revert(0x1c, 0x04)
}
}

// round to make sure that we pass the target price
return zeroForOne
Expand All @@ -158,11 +186,18 @@ library SqrtPriceMath {
unchecked {
if (sqrtPriceAX96 > sqrtPriceBX96) (sqrtPriceAX96, sqrtPriceBX96) = (sqrtPriceBX96, sqrtPriceAX96);

// equivalent: if (sqrtPriceAX96 == 0) revert InvalidPrice();
/// @solidity memory-safe-assembly
assembly {
if iszero(sqrtPriceAX96) {
mstore(0, 0x00bfc921) // selector for InvalidPrice()
revert(0x1c, 0x04)
}
}

uint256 numerator1 = uint256(liquidity) << FixedPoint96.RESOLUTION;
uint256 numerator2 = sqrtPriceBX96 - sqrtPriceAX96;

if (sqrtPriceAX96 == 0) revert InvalidPrice();

return roundUp
? UnsafeMath.divRoundingUp(FullMath.mulDivRoundingUp(numerator1, numerator2, sqrtPriceBX96), sqrtPriceAX96)
: FullMath.mulDiv(numerator1, numerator2, sqrtPriceBX96) / sqrtPriceAX96;
Expand Down

0 comments on commit 03636de

Please sign in to comment.