From 6a084d8757aa25c6048e800f853d1eda65722d32 Mon Sep 17 00:00:00 2001 From: Alice Henshaw Date: Thu, 29 Aug 2024 21:18:18 +0100 Subject: [PATCH 1/2] Spearbit core issues --- src/libraries/SqrtPriceMath.sol | 7 ++----- src/libraries/StateLibrary.sol | 2 -- src/libraries/SwapMath.sol | 2 +- src/libraries/UnsafeMath.sol | 6 ++---- src/types/PoolId.sol | 3 ++- 5 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/libraries/SqrtPriceMath.sol b/src/libraries/SqrtPriceMath.sol index 24b2fa7f3..659b750e1 100644 --- a/src/libraries/SqrtPriceMath.sol +++ b/src/libraries/SqrtPriceMath.sol @@ -238,11 +238,8 @@ library SqrtPriceMath { { uint256 numerator = absDiff(sqrtPriceAX96, sqrtPriceBX96); uint256 denominator = FixedPoint96.Q96; - uint256 _liquidity; - assembly ("memory-safe") { - // avoid implicit upcasting - _liquidity := and(liquidity, 0xffffffffffffffffffffffffffffffff) - } + uint256 _liquidity = uint256(liquidity); + /** * Equivalent to: * amount1 = roundUp diff --git a/src/libraries/StateLibrary.sol b/src/libraries/StateLibrary.sol index c884a07b0..e03f1f341 100644 --- a/src/libraries/StateLibrary.sol +++ b/src/libraries/StateLibrary.sol @@ -12,8 +12,6 @@ library StateLibrary { /// @notice index of feeGrowthGlobal0X128 in Pool.State uint256 public constant FEE_GROWTH_GLOBAL0_OFFSET = 1; - /// @notice index of feeGrowthGlobal1X128 in Pool.State - uint256 public constant FEE_GROWTH_GLOBAL1_OFFSET = 2; /// @notice index of liquidity in Pool.State uint256 public constant LIQUIDITY_OFFSET = 3; diff --git a/src/libraries/SwapMath.sol b/src/libraries/SwapMath.sol index a7147d34e..608c75bae 100644 --- a/src/libraries/SwapMath.sol +++ b/src/libraries/SwapMath.sol @@ -70,7 +70,7 @@ library SwapMath { // `amountIn` is capped by the target price sqrtPriceNextX96 = sqrtPriceTargetX96; feeAmount = _feePips == MAX_SWAP_FEE - ? amountIn + ? 0 // 0 as amountRemainingLessFee == 0 : FullMath.mulDivRoundingUp(amountIn, _feePips, MAX_SWAP_FEE - _feePips); } else { sqrtPriceNextX96 = SqrtPriceMath.getNextSqrtPriceFromInput( diff --git a/src/libraries/UnsafeMath.sol b/src/libraries/UnsafeMath.sol index c1cdf3eb2..c3f23c83c 100644 --- a/src/libraries/UnsafeMath.sol +++ b/src/libraries/UnsafeMath.sol @@ -10,10 +10,8 @@ library UnsafeMath { /// @param y The divisor /// @return z The quotient, ceil(x / y) function divRoundingUp(uint256 x, uint256 y) internal pure returns (uint256 z) { - unchecked { - assembly ("memory-safe") { - z := add(div(x, y), gt(mod(x, y), 0)) - } + assembly ("memory-safe") { + z := add(div(x, y), gt(mod(x, y), 0)) } } diff --git a/src/types/PoolId.sol b/src/types/PoolId.sol index 740dff007..0c4fd052c 100644 --- a/src/types/PoolId.sol +++ b/src/types/PoolId.sol @@ -10,7 +10,8 @@ library PoolIdLibrary { /// @notice Returns value equal to keccak256(abi.encode(poolKey)) function toId(PoolKey memory poolKey) internal pure returns (PoolId poolId) { assembly ("memory-safe") { - poolId := keccak256(poolKey, mul(32, 5)) + // 0xa0 is 32 * 5 + poolId := keccak256(poolKey, 0xa0) } } } From 0086920a8768b9e4eeb835f1fcdce2dd2ee46f59 Mon Sep 17 00:00:00 2001 From: Alice Henshaw Date: Fri, 30 Aug 2024 13:11:31 +0100 Subject: [PATCH 2/2] PR comments --- src/libraries/StateLibrary.sol | 2 ++ src/libraries/SwapMath.sol | 2 +- src/types/PoolId.sol | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libraries/StateLibrary.sol b/src/libraries/StateLibrary.sol index e03f1f341..c1540e2d3 100644 --- a/src/libraries/StateLibrary.sol +++ b/src/libraries/StateLibrary.sol @@ -13,6 +13,8 @@ library StateLibrary { /// @notice index of feeGrowthGlobal0X128 in Pool.State uint256 public constant FEE_GROWTH_GLOBAL0_OFFSET = 1; + // feeGrowthGlobal1X128 offset in Pool.State = 2 + /// @notice index of liquidity in Pool.State uint256 public constant LIQUIDITY_OFFSET = 3; diff --git a/src/libraries/SwapMath.sol b/src/libraries/SwapMath.sol index 991c46471..2142655a7 100644 --- a/src/libraries/SwapMath.sol +++ b/src/libraries/SwapMath.sol @@ -70,7 +70,7 @@ library SwapMath { // `amountIn` is capped by the target price sqrtPriceNextX96 = sqrtPriceTargetX96; feeAmount = _feePips == MAX_SWAP_FEE - ? 0 // 0 as amountRemainingLessFee == 0 + ? amountIn // amountIn is always 0 here, as amountRemainingLessFee == 0 and amountRemainingLessFee >= amountIn : FullMath.mulDivRoundingUp(amountIn, _feePips, MAX_SWAP_FEE - _feePips); } else { // exhaust the remaining amount diff --git a/src/types/PoolId.sol b/src/types/PoolId.sol index 0c4fd052c..22a0b4f91 100644 --- a/src/types/PoolId.sol +++ b/src/types/PoolId.sol @@ -10,7 +10,7 @@ library PoolIdLibrary { /// @notice Returns value equal to keccak256(abi.encode(poolKey)) function toId(PoolKey memory poolKey) internal pure returns (PoolId poolId) { assembly ("memory-safe") { - // 0xa0 is 32 * 5 + // 0xa0 represents the total size of the poolKey struct (5 slots of 32 bytes) poolId := keccak256(poolKey, 0xa0) } }