From 36c948c83a60164e3d4c7cae869420edd6ac97c0 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Thu, 5 Sep 2024 14:30:50 -0400 Subject: [PATCH 01/11] scoping to avoid IR --- src/PoolManager.sol | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/PoolManager.sol b/src/PoolManager.sol index 3fb55aaff..f366d33aa 100644 --- a/src/PoolManager.sol +++ b/src/PoolManager.sol @@ -150,25 +150,27 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim bytes calldata hookData ) external onlyWhenUnlocked noDelegateCall returns (BalanceDelta callerDelta, BalanceDelta feesAccrued) { PoolId id = key.toId(); - Pool.State storage pool = _getPool(id); - pool.checkPoolInitialized(); - - key.hooks.beforeModifyLiquidity(key, params, hookData); - - BalanceDelta principalDelta; - (principalDelta, feesAccrued) = pool.modifyLiquidity( - Pool.ModifyLiquidityParams({ - owner: msg.sender, - tickLower: params.tickLower, - tickUpper: params.tickUpper, - liquidityDelta: params.liquidityDelta.toInt128(), - tickSpacing: key.tickSpacing, - salt: params.salt - }) - ); + { + Pool.State storage pool = _getPool(id); + pool.checkPoolInitialized(); + + key.hooks.beforeModifyLiquidity(key, params, hookData); + + BalanceDelta principalDelta; + (principalDelta, feesAccrued) = pool.modifyLiquidity( + Pool.ModifyLiquidityParams({ + owner: msg.sender, + tickLower: params.tickLower, + tickUpper: params.tickUpper, + liquidityDelta: params.liquidityDelta.toInt128(), + tickSpacing: key.tickSpacing, + salt: params.salt + }) + ); - // fee delta and principal delta are both accrued to the caller - callerDelta = principalDelta + feesAccrued; + // fee delta and principal delta are both accrued to the caller + callerDelta = principalDelta + feesAccrued; + } // event is emitted before the afterModifyLiquidity call to ensure events are always emitted in order emit ModifyLiquidity(id, msg.sender, params.tickLower, params.tickUpper, params.liquidityDelta, params.salt); From 0d1cf66b1db143425c397e476de91773235a9387 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Thu, 5 Sep 2024 14:44:57 -0400 Subject: [PATCH 02/11] pick 4 variables to pack --- ...o already existing position with salt.snap | 2 +- .forge-snapshots/addLiquidity CA fee.snap | 2 +- .../addLiquidity with empty hook.snap | 2 +- .../addLiquidity with native token.snap | 2 +- ...new liquidity to a position with salt.snap | 2 +- .forge-snapshots/donate gas with 1 token.snap | 2 +- .../donate gas with 2 tokens.snap | 2 +- .../erc20 collect protocol fees.snap | 2 +- .../extsload getFeeGrowthGlobals.snap | 2 +- .../extsload getPositionInfo.snap | 2 +- .../extsload getTickFeeGrowthOutside.snap | 2 +- .forge-snapshots/extsload getTickInfo.snap | 2 +- .forge-snapshots/initialize.snap | 2 +- .../native collect protocol fees.snap | 2 +- .../poolManager bytecode size.snap | 2 +- .forge-snapshots/removeLiquidity CA fee.snap | 2 +- .../removeLiquidity with empty hook.snap | 2 +- .../removeLiquidity with native token.snap | 2 +- ...dLiquidity second addition same range.snap | 2 +- .forge-snapshots/simple addLiquidity.snap | 2 +- ...emoveLiquidity some liquidity remains.snap | 2 +- .forge-snapshots/simple removeLiquidity.snap | 2 +- .forge-snapshots/simple swap with native.snap | 2 +- .forge-snapshots/simple swap.snap | 2 +- .../swap CA custom curve + swap noop.snap | 2 +- .../swap CA fee on unspecified.snap | 2 +- ...p against liquidity with native token.snap | 2 +- .forge-snapshots/swap against liquidity.snap | 2 +- .../swap burn 6909 for input.snap | 2 +- .../swap burn native 6909 for input.snap | 2 +- .../swap mint native output as 6909.snap | 2 +- .../swap mint output as 6909.snap | 2 +- ...wap 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/Pool.sol | 50 ++++++++++++------- 39 files changed, 70 insertions(+), 56 deletions(-) diff --git a/.forge-snapshots/add liquidity to already existing position with salt.snap b/.forge-snapshots/add liquidity to already existing position with salt.snap index 7931cc0d3..dad62a459 100644 --- a/.forge-snapshots/add liquidity to already existing position with salt.snap +++ b/.forge-snapshots/add liquidity to already existing position with salt.snap @@ -1 +1 @@ -144608 \ No newline at end of file +144656 \ No newline at end of file diff --git a/.forge-snapshots/addLiquidity CA fee.snap b/.forge-snapshots/addLiquidity CA fee.snap index 329e55959..1984ea6ac 100644 --- a/.forge-snapshots/addLiquidity CA fee.snap +++ b/.forge-snapshots/addLiquidity CA fee.snap @@ -1 +1 @@ -170897 \ No newline at end of file +170958 \ No newline at end of file diff --git a/.forge-snapshots/addLiquidity with empty hook.snap b/.forge-snapshots/addLiquidity with empty hook.snap index cb223237c..8062b1c6b 100644 --- a/.forge-snapshots/addLiquidity with empty hook.snap +++ b/.forge-snapshots/addLiquidity with empty hook.snap @@ -1 +1 @@ -274175 \ No newline at end of file +274223 \ No newline at end of file diff --git a/.forge-snapshots/addLiquidity with native token.snap b/.forge-snapshots/addLiquidity with native token.snap index 3ec6fa53a..9b42c822b 100644 --- a/.forge-snapshots/addLiquidity with native token.snap +++ b/.forge-snapshots/addLiquidity with native token.snap @@ -1 +1 @@ -135101 \ No newline at end of file +135143 \ No newline at end of file diff --git a/.forge-snapshots/create new liquidity to a position with salt.snap b/.forge-snapshots/create new liquidity to a position with salt.snap index ebcbdf72c..380c90efb 100644 --- a/.forge-snapshots/create new liquidity to a position with salt.snap +++ b/.forge-snapshots/create new liquidity to a position with salt.snap @@ -1 +1 @@ -292800 \ No newline at end of file +292848 \ No newline at end of file diff --git a/.forge-snapshots/donate gas with 1 token.snap b/.forge-snapshots/donate gas with 1 token.snap index a54ba22c4..cb54169dc 100644 --- a/.forge-snapshots/donate gas with 1 token.snap +++ b/.forge-snapshots/donate gas with 1 token.snap @@ -1 +1 @@ -106321 \ No newline at end of file +106345 \ No newline at end of file diff --git a/.forge-snapshots/donate gas with 2 tokens.snap b/.forge-snapshots/donate gas with 2 tokens.snap index 60eb0640f..3c42b6b3d 100644 --- a/.forge-snapshots/donate gas with 2 tokens.snap +++ b/.forge-snapshots/donate gas with 2 tokens.snap @@ -1 +1 @@ -145736 \ No newline at end of file +145772 \ No newline at end of file diff --git a/.forge-snapshots/erc20 collect protocol fees.snap b/.forge-snapshots/erc20 collect protocol fees.snap index 2a8ab2b2d..35d01b80f 100644 --- a/.forge-snapshots/erc20 collect protocol fees.snap +++ b/.forge-snapshots/erc20 collect protocol fees.snap @@ -1 +1 @@ -57439 \ No newline at end of file +57445 \ No newline at end of file diff --git a/.forge-snapshots/extsload getFeeGrowthGlobals.snap b/.forge-snapshots/extsload getFeeGrowthGlobals.snap index fc0b67d07..eb90589d8 100644 --- a/.forge-snapshots/extsload getFeeGrowthGlobals.snap +++ b/.forge-snapshots/extsload getFeeGrowthGlobals.snap @@ -1 +1 @@ -774 \ No newline at end of file +780 \ No newline at end of file diff --git a/.forge-snapshots/extsload getPositionInfo.snap b/.forge-snapshots/extsload getPositionInfo.snap index 397787a80..0c8ddea6a 100644 --- a/.forge-snapshots/extsload getPositionInfo.snap +++ b/.forge-snapshots/extsload getPositionInfo.snap @@ -1 +1 @@ -949 \ No newline at end of file +955 \ No newline at end of file diff --git a/.forge-snapshots/extsload getTickFeeGrowthOutside.snap b/.forge-snapshots/extsload getTickFeeGrowthOutside.snap index fc0b67d07..eb90589d8 100644 --- a/.forge-snapshots/extsload getTickFeeGrowthOutside.snap +++ b/.forge-snapshots/extsload getTickFeeGrowthOutside.snap @@ -1 +1 @@ -774 \ No newline at end of file +780 \ No newline at end of file diff --git a/.forge-snapshots/extsload getTickInfo.snap b/.forge-snapshots/extsload getTickInfo.snap index 397787a80..0c8ddea6a 100644 --- a/.forge-snapshots/extsload getTickInfo.snap +++ b/.forge-snapshots/extsload getTickInfo.snap @@ -1 +1 @@ -949 \ No newline at end of file +955 \ No newline at end of file diff --git a/.forge-snapshots/initialize.snap b/.forge-snapshots/initialize.snap index 236da0ded..36d6ed2cb 100644 --- a/.forge-snapshots/initialize.snap +++ b/.forge-snapshots/initialize.snap @@ -1 +1 @@ -60021 \ No newline at end of file +60028 \ No newline at end of file diff --git a/.forge-snapshots/native collect protocol fees.snap b/.forge-snapshots/native collect protocol fees.snap index a9aafc5e5..fb642bda8 100644 --- a/.forge-snapshots/native collect protocol fees.snap +++ b/.forge-snapshots/native collect protocol fees.snap @@ -1 +1 @@ -59723 \ No newline at end of file +59729 \ No newline at end of file diff --git a/.forge-snapshots/poolManager bytecode size.snap b/.forge-snapshots/poolManager bytecode size.snap index 991b253d9..45f90e6b8 100644 --- a/.forge-snapshots/poolManager bytecode size.snap +++ b/.forge-snapshots/poolManager bytecode size.snap @@ -1 +1 @@ -24006 \ No newline at end of file +24111 \ No newline at end of file diff --git a/.forge-snapshots/removeLiquidity CA fee.snap b/.forge-snapshots/removeLiquidity CA fee.snap index 38003fc44..60669f8d8 100644 --- a/.forge-snapshots/removeLiquidity CA fee.snap +++ b/.forge-snapshots/removeLiquidity CA fee.snap @@ -1 +1 @@ -141204 \ No newline at end of file +141253 \ No newline at end of file diff --git a/.forge-snapshots/removeLiquidity with empty hook.snap b/.forge-snapshots/removeLiquidity with empty hook.snap index d9f5f7111..2765071df 100644 --- a/.forge-snapshots/removeLiquidity with empty hook.snap +++ b/.forge-snapshots/removeLiquidity with empty hook.snap @@ -1 +1 @@ -130579 \ No newline at end of file +130615 \ No newline at end of file diff --git a/.forge-snapshots/removeLiquidity with native token.snap b/.forge-snapshots/removeLiquidity with native token.snap index 19f882d70..a587c04e1 100644 --- a/.forge-snapshots/removeLiquidity with native token.snap +++ b/.forge-snapshots/removeLiquidity with native token.snap @@ -1 +1 @@ -112523 \ No newline at end of file +112547 \ No newline at end of file diff --git a/.forge-snapshots/simple addLiquidity second addition same range.snap b/.forge-snapshots/simple addLiquidity second addition same range.snap index 513cda23b..2124d4dd4 100644 --- a/.forge-snapshots/simple addLiquidity second addition same range.snap +++ b/.forge-snapshots/simple addLiquidity second addition same range.snap @@ -1 +1 @@ -98819 \ No newline at end of file +98843 \ No newline at end of file diff --git a/.forge-snapshots/simple addLiquidity.snap b/.forge-snapshots/simple addLiquidity.snap index 0d3371c3f..10276bfb3 100644 --- a/.forge-snapshots/simple addLiquidity.snap +++ b/.forge-snapshots/simple addLiquidity.snap @@ -1 +1 @@ -161364 \ No newline at end of file +161388 \ No newline at end of file diff --git a/.forge-snapshots/simple removeLiquidity some liquidity remains.snap b/.forge-snapshots/simple removeLiquidity some liquidity remains.snap index d4e0d16bd..e1aa38c3c 100644 --- a/.forge-snapshots/simple removeLiquidity some liquidity remains.snap +++ b/.forge-snapshots/simple removeLiquidity some liquidity remains.snap @@ -1 +1 @@ -92974 \ No newline at end of file +92992 \ No newline at end of file diff --git a/.forge-snapshots/simple removeLiquidity.snap b/.forge-snapshots/simple removeLiquidity.snap index 69b94841f..d2b0f756f 100644 --- a/.forge-snapshots/simple removeLiquidity.snap +++ b/.forge-snapshots/simple removeLiquidity.snap @@ -1 +1 @@ -85087 \ No newline at end of file +85105 \ 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 46cc1e235..ba4b897d0 100644 --- a/.forge-snapshots/simple swap with native.snap +++ b/.forge-snapshots/simple swap with native.snap @@ -1 +1 @@ -108369 \ No newline at end of file +108679 \ No newline at end of file diff --git a/.forge-snapshots/simple swap.snap b/.forge-snapshots/simple swap.snap index 3e232d361..0bc420b43 100644 --- a/.forge-snapshots/simple swap.snap +++ b/.forge-snapshots/simple swap.snap @@ -1 +1 @@ -123186 \ No newline at end of file +123502 \ 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 8dc70d3a8..13f983512 100644 --- a/.forge-snapshots/swap CA custom curve + swap noop.snap +++ b/.forge-snapshots/swap CA custom curve + swap noop.snap @@ -1 +1 @@ -126821 \ No newline at end of file +127027 \ 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 0650bd07a..85ff6e54b 100644 --- a/.forge-snapshots/swap CA fee on unspecified.snap +++ b/.forge-snapshots/swap CA fee on unspecified.snap @@ -1 +1 @@ -154631 \ No newline at end of file +154953 \ 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 0fb278943..562facfa5 100644 --- a/.forge-snapshots/swap against liquidity with native token.snap +++ b/.forge-snapshots/swap against liquidity with native token.snap @@ -1 +1 @@ -105493 \ No newline at end of file +105780 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity.snap b/.forge-snapshots/swap against liquidity.snap index 7a0df15bb..6fb751900 100644 --- a/.forge-snapshots/swap against liquidity.snap +++ b/.forge-snapshots/swap against liquidity.snap @@ -1 +1 @@ -116558 \ No newline at end of file +116851 \ 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 8fcd144f2..90a749962 100644 --- a/.forge-snapshots/swap burn 6909 for input.snap +++ b/.forge-snapshots/swap burn 6909 for input.snap @@ -1 +1 @@ -129116 \ No newline at end of file +129416 \ 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 8b650b781..c715f1cac 100644 --- a/.forge-snapshots/swap burn native 6909 for input.snap +++ b/.forge-snapshots/swap burn native 6909 for input.snap @@ -1 +1 @@ -118569 \ No newline at end of file +118868 \ 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 41417754a..27b4c8b6e 100644 --- a/.forge-snapshots/swap mint native output as 6909.snap +++ b/.forge-snapshots/swap mint native output as 6909.snap @@ -1 +1 @@ -139603 \ No newline at end of file +139897 \ 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 f1a50bc46..3114d3993 100644 --- a/.forge-snapshots/swap mint output as 6909.snap +++ b/.forge-snapshots/swap mint output as 6909.snap @@ -1 +1 @@ -155027 \ No newline at end of file +155343 \ 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 0ab4deeab..db1a4abc0 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 @@ -206132 \ No newline at end of file +206735 \ 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 3c8c45efc..fcb0b3c1c 100644 --- a/.forge-snapshots/swap with dynamic fee.snap +++ b/.forge-snapshots/swap with dynamic fee.snap @@ -1 +1 @@ -139207 \ No newline at end of file +139523 \ No newline at end of file diff --git a/.forge-snapshots/swap with hooks.snap b/.forge-snapshots/swap with hooks.snap index 185d91737..8f58b6925 100644 --- a/.forge-snapshots/swap with hooks.snap +++ b/.forge-snapshots/swap with hooks.snap @@ -1 +1 @@ -132195 \ No newline at end of file +132489 \ 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 a0a94cd21..5c2958293 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 @@ -169398 \ No newline at end of file +169769 \ 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 37efa1363..f510f06f9 100644 --- a/.forge-snapshots/swap with return dynamic fee.snap +++ b/.forge-snapshots/swap with return dynamic fee.snap @@ -1 +1 @@ -145534 \ No newline at end of file +145850 \ 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 4394bf909..3cd2d6dd6 100644 --- a/.forge-snapshots/update dynamic fee in before swap.snap +++ b/.forge-snapshots/update dynamic fee in before swap.snap @@ -1 +1 @@ -147814 \ No newline at end of file +148136 \ No newline at end of file diff --git a/src/libraries/Pool.sol b/src/libraries/Pool.sol index d58e316ef..9975f0211 100644 --- a/src/libraries/Pool.sol +++ b/src/libraries/Pool.sol @@ -272,6 +272,13 @@ library Pool { uint24 lpFeeOverride; } + struct SwapStart { + uint256 protocolFee; + uint256 feeGrowthGlobalX128; + uint128 liquidityStart; + bool exactInput; + } + /// @notice Executes a swap against the state, and returns the amount deltas of the pool /// @dev PoolManager checks that the pool is initialized before calling function swap(State storage self, SwapParams memory params) @@ -281,22 +288,29 @@ library Pool { Slot0 slot0Start = self.slot0; bool zeroForOne = params.zeroForOne; - uint128 liquidityStart = self.liquidity; - uint256 protocolFee = - zeroForOne ? slot0Start.protocolFee().getZeroForOneFee() : slot0Start.protocolFee().getOneForZeroFee(); + SwapStart memory start = SwapStart({ + protocolFee: zeroForOne ? slot0Start.protocolFee().getZeroForOneFee() : slot0Start.protocolFee().getOneForZeroFee(), + feeGrowthGlobalX128: zeroForOne ? self.feeGrowthGlobal0X128 : self.feeGrowthGlobal1X128, + liquidityStart: self.liquidity, + exactInput: params.amountSpecified < 0 + }); + + // uint128 liquidityStart = self.liquidity; + // uint256 protocolFee = + // zeroForOne ? slot0Start.protocolFee().getZeroForOneFee() : slot0Start.protocolFee().getOneForZeroFee(); // the amount remaining to be swapped in/out of the input/output asset. initially set to the amountSpecified int256 amountSpecifiedRemaining = params.amountSpecified; // the amount swapped out/in of the output/input asset. initially set to 0 int256 amountCalculated = 0; // the global fee growth of the input token. updated in storage at the end of swap - uint256 feeGrowthGlobalX128 = zeroForOne ? self.feeGrowthGlobal0X128 : self.feeGrowthGlobal1X128; + // uint256 feeGrowthGlobalX128 = zeroForOne ? self.feeGrowthGlobal0X128 : self.feeGrowthGlobal1X128; // initialize to the current sqrt(price) result.sqrtPriceX96 = slot0Start.sqrtPriceX96(); // initialize to the current tick result.tick = slot0Start.tick(); // initialize to the current liquidity - result.liquidity = liquidityStart; + result.liquidity = start.liquidityStart; // if the beforeSwap hook returned a valid fee override, use that as the LP fee, otherwise load from storage // lpFee, swapFee, and protocolFee are all in pips @@ -305,14 +319,14 @@ library Pool { ? params.lpFeeOverride.removeOverrideFlagAndValidate() : slot0Start.lpFee(); - swapFee = protocolFee == 0 ? lpFee : uint16(protocolFee).calculateSwapFee(lpFee); + swapFee = start.protocolFee == 0 ? lpFee : uint16(start.protocolFee).calculateSwapFee(lpFee); } - bool exactInput = params.amountSpecified < 0; + // bool exactInput = params.amountSpecified < 0; // a swap fee totaling MAX_SWAP_FEE (100%) makes exact output swaps impossible since the input is entirely consumed by the fee if (swapFee >= SwapMath.MAX_SWAP_FEE) { - if (!exactInput) { + if (!start.exactInput) { InvalidFeeForExactOut.selector.revertWith(); } } @@ -368,7 +382,7 @@ library Pool { swapFee ); - if (!exactInput) { + if (!start.exactInput) { unchecked { amountSpecifiedRemaining -= step.amountOut.toInt256(); } @@ -382,12 +396,12 @@ library Pool { } // if the protocol fee is on, calculate how much is owed, decrement feeAmount, and increment protocolFee - if (protocolFee > 0) { + if (start.protocolFee > 0) { unchecked { // step.amountIn does not include the swap fee, as it's already been taken from it, // so add it back to get the total amountIn and use that to calculate the amount of fees owed to the protocol // this line cannot overflow due to limits on the size of protocolFee and params.amountSpecified - uint256 delta = (step.amountIn + step.feeAmount) * protocolFee / ProtocolFeeLibrary.PIPS_DENOMINATOR; + uint256 delta = (step.amountIn + step.feeAmount) * start.protocolFee / ProtocolFeeLibrary.PIPS_DENOMINATOR; // subtract it from the total fee and add it to the protocol fee step.feeAmount -= delta; amountToProtocol += delta; @@ -398,7 +412,7 @@ library Pool { if (result.liquidity > 0) { unchecked { // FullMath.mulDiv isn't needed as the numerator can't overflow uint256 since tokens have a max supply of type(uint128).max - feeGrowthGlobalX128 += UnsafeMath.simpleMulDiv(step.feeAmount, FixedPoint128.Q128, result.liquidity); + start.feeGrowthGlobalX128 += UnsafeMath.simpleMulDiv(step.feeAmount, FixedPoint128.Q128, result.liquidity); } } @@ -410,8 +424,8 @@ library Pool { // if the tick is initialized, run the tick transition if (step.initialized) { (uint256 feeGrowthGlobal0X128, uint256 feeGrowthGlobal1X128) = zeroForOne - ? (feeGrowthGlobalX128, self.feeGrowthGlobal1X128) - : (self.feeGrowthGlobal0X128, feeGrowthGlobalX128); + ? (start.feeGrowthGlobalX128, self.feeGrowthGlobal1X128) + : (self.feeGrowthGlobal0X128, start.feeGrowthGlobalX128); int128 liquidityNet = Pool.crossTick(self, step.tickNext, feeGrowthGlobal0X128, feeGrowthGlobal1X128); // if we're moving leftward, we interpret liquidityNet as the opposite sign @@ -435,17 +449,17 @@ library Pool { self.slot0 = slot0Start.setTick(result.tick).setSqrtPriceX96(result.sqrtPriceX96); // update liquidity if it changed - if (liquidityStart != result.liquidity) self.liquidity = result.liquidity; + if (start.liquidityStart != result.liquidity) self.liquidity = result.liquidity; // update fee growth global if (!zeroForOne) { - self.feeGrowthGlobal1X128 = feeGrowthGlobalX128; + self.feeGrowthGlobal1X128 = start.feeGrowthGlobalX128; } else { - self.feeGrowthGlobal0X128 = feeGrowthGlobalX128; + self.feeGrowthGlobal0X128 = start.feeGrowthGlobalX128; } unchecked { - if (zeroForOne != exactInput) { + if (zeroForOne != start.exactInput) { swapDelta = toBalanceDelta( amountCalculated.toInt128(), (params.amountSpecified - amountSpecifiedRemaining).toInt128() ); From 223d3f9e925f95989f08a157b8324482b9a118d4 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Thu, 5 Sep 2024 14:57:45 -0400 Subject: [PATCH 03/11] save 12 gas --- .forge-snapshots/poolManager bytecode size.snap | 2 +- .forge-snapshots/simple swap with native.snap | 2 +- .forge-snapshots/simple swap.snap | 2 +- .forge-snapshots/swap CA custom curve + swap noop.snap | 2 +- .forge-snapshots/swap CA fee on unspecified.snap | 2 +- .forge-snapshots/swap against liquidity with native token.snap | 2 +- .forge-snapshots/swap against liquidity.snap | 2 +- .forge-snapshots/swap burn 6909 for input.snap | 2 +- .forge-snapshots/swap burn native 6909 for input.snap | 2 +- .forge-snapshots/swap mint native output as 6909.snap | 2 +- .forge-snapshots/swap mint output as 6909.snap | 2 +- .forge-snapshots/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 +- .forge-snapshots/swap with lp fee and protocol fee.snap | 2 +- .forge-snapshots/swap with return dynamic fee.snap | 2 +- .forge-snapshots/update dynamic fee in before swap.snap | 2 +- src/libraries/Pool.sol | 2 +- 18 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.forge-snapshots/poolManager bytecode size.snap b/.forge-snapshots/poolManager bytecode size.snap index 45f90e6b8..56ff543ac 100644 --- a/.forge-snapshots/poolManager bytecode size.snap +++ b/.forge-snapshots/poolManager bytecode size.snap @@ -1 +1 @@ -24111 \ No newline at end of file +24091 \ 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 ba4b897d0..cb310a17b 100644 --- a/.forge-snapshots/simple swap with native.snap +++ b/.forge-snapshots/simple swap with native.snap @@ -1 +1 @@ -108679 \ No newline at end of file +108667 \ No newline at end of file diff --git a/.forge-snapshots/simple swap.snap b/.forge-snapshots/simple swap.snap index 0bc420b43..d1f847f3d 100644 --- a/.forge-snapshots/simple swap.snap +++ b/.forge-snapshots/simple swap.snap @@ -1 +1 @@ -123502 \ No newline at end of file +123490 \ 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 13f983512..0718d6ecd 100644 --- a/.forge-snapshots/swap CA custom curve + swap noop.snap +++ b/.forge-snapshots/swap CA custom curve + swap noop.snap @@ -1 +1 @@ -127027 \ No newline at end of file +127015 \ 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 85ff6e54b..ecc791035 100644 --- a/.forge-snapshots/swap CA fee on unspecified.snap +++ b/.forge-snapshots/swap CA fee on unspecified.snap @@ -1 +1 @@ -154953 \ No newline at end of file +154941 \ 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 562facfa5..8ebfe9994 100644 --- a/.forge-snapshots/swap against liquidity with native token.snap +++ b/.forge-snapshots/swap against liquidity with native token.snap @@ -1 +1 @@ -105780 \ No newline at end of file +105768 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity.snap b/.forge-snapshots/swap against liquidity.snap index 6fb751900..a4f81d951 100644 --- a/.forge-snapshots/swap against liquidity.snap +++ b/.forge-snapshots/swap against liquidity.snap @@ -1 +1 @@ -116851 \ No newline at end of file +116839 \ 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 90a749962..f5d08e03f 100644 --- a/.forge-snapshots/swap burn 6909 for input.snap +++ b/.forge-snapshots/swap burn 6909 for input.snap @@ -1 +1 @@ -129416 \ No newline at end of file +129404 \ 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 c715f1cac..565e313b7 100644 --- a/.forge-snapshots/swap burn native 6909 for input.snap +++ b/.forge-snapshots/swap burn native 6909 for input.snap @@ -1 +1 @@ -118868 \ No newline at end of file +118856 \ 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 27b4c8b6e..0fda550d3 100644 --- a/.forge-snapshots/swap mint native output as 6909.snap +++ b/.forge-snapshots/swap mint native output as 6909.snap @@ -1 +1 @@ -139897 \ No newline at end of file +139885 \ 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 3114d3993..c91598e55 100644 --- a/.forge-snapshots/swap mint output as 6909.snap +++ b/.forge-snapshots/swap mint output as 6909.snap @@ -1 +1 @@ -155343 \ No newline at end of file +155331 \ 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 db1a4abc0..a4db33d85 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 @@ -206735 \ No newline at end of file +206711 \ 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 fcb0b3c1c..9a17134ae 100644 --- a/.forge-snapshots/swap with dynamic fee.snap +++ b/.forge-snapshots/swap with dynamic fee.snap @@ -1 +1 @@ -139523 \ No newline at end of file +139511 \ No newline at end of file diff --git a/.forge-snapshots/swap with hooks.snap b/.forge-snapshots/swap with hooks.snap index 8f58b6925..87bd40696 100644 --- a/.forge-snapshots/swap with hooks.snap +++ b/.forge-snapshots/swap with hooks.snap @@ -1 +1 @@ -132489 \ No newline at end of file +132477 \ 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 5c2958293..a949a4cb7 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 @@ -169769 \ No newline at end of file +169757 \ 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 f510f06f9..7aaa028cf 100644 --- a/.forge-snapshots/swap with return dynamic fee.snap +++ b/.forge-snapshots/swap with return dynamic fee.snap @@ -1 +1 @@ -145850 \ No newline at end of file +145838 \ 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 3cd2d6dd6..5b8842ef0 100644 --- a/.forge-snapshots/update dynamic fee in before swap.snap +++ b/.forge-snapshots/update dynamic fee in before swap.snap @@ -1 +1 @@ -148136 \ No newline at end of file +148124 \ No newline at end of file diff --git a/src/libraries/Pool.sol b/src/libraries/Pool.sol index 9975f0211..3122a9ceb 100644 --- a/src/libraries/Pool.sol +++ b/src/libraries/Pool.sol @@ -310,7 +310,7 @@ library Pool { // initialize to the current tick result.tick = slot0Start.tick(); // initialize to the current liquidity - result.liquidity = start.liquidityStart; + result.liquidity = self.liquidity; // if the beforeSwap hook returned a valid fee override, use that as the LP fee, otherwise load from storage // lpFee, swapFee, and protocolFee are all in pips From fb5d9e4557962aed3dfeb58d11f3ec389f833db7 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Thu, 5 Sep 2024 15:03:04 -0400 Subject: [PATCH 04/11] micro gas savings i hate it here --- .forge-snapshots/poolManager bytecode size.snap | 2 +- .forge-snapshots/simple swap with native.snap | 2 +- .forge-snapshots/simple swap.snap | 2 +- .forge-snapshots/swap CA custom curve + swap noop.snap | 2 +- .forge-snapshots/swap CA fee on unspecified.snap | 2 +- .forge-snapshots/swap against liquidity with native token.snap | 2 +- .forge-snapshots/swap against liquidity.snap | 2 +- .forge-snapshots/swap burn 6909 for input.snap | 2 +- .forge-snapshots/swap burn native 6909 for input.snap | 2 +- .forge-snapshots/swap mint native output as 6909.snap | 2 +- .forge-snapshots/swap mint output as 6909.snap | 2 +- .forge-snapshots/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 +- .forge-snapshots/swap with lp fee and protocol fee.snap | 2 +- .forge-snapshots/swap with return dynamic fee.snap | 2 +- .forge-snapshots/update dynamic fee in before swap.snap | 2 +- src/libraries/Pool.sol | 2 +- 18 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.forge-snapshots/poolManager bytecode size.snap b/.forge-snapshots/poolManager bytecode size.snap index 56ff543ac..a3a44666a 100644 --- a/.forge-snapshots/poolManager bytecode size.snap +++ b/.forge-snapshots/poolManager bytecode size.snap @@ -1 +1 @@ -24091 \ No newline at end of file +24080 \ 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 cb310a17b..c35412130 100644 --- a/.forge-snapshots/simple swap with native.snap +++ b/.forge-snapshots/simple swap with native.snap @@ -1 +1 @@ -108667 \ No newline at end of file +108658 \ No newline at end of file diff --git a/.forge-snapshots/simple swap.snap b/.forge-snapshots/simple swap.snap index d1f847f3d..edda22cc2 100644 --- a/.forge-snapshots/simple swap.snap +++ b/.forge-snapshots/simple swap.snap @@ -1 +1 @@ -123490 \ No newline at end of file +123481 \ 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 0718d6ecd..8701b3f89 100644 --- a/.forge-snapshots/swap CA custom curve + swap noop.snap +++ b/.forge-snapshots/swap CA custom curve + swap noop.snap @@ -1 +1 @@ -127015 \ No newline at end of file +127024 \ 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 ecc791035..15476183a 100644 --- a/.forge-snapshots/swap CA fee on unspecified.snap +++ b/.forge-snapshots/swap CA fee on unspecified.snap @@ -1 +1 @@ -154941 \ No newline at end of file +154932 \ 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 8ebfe9994..0e7048494 100644 --- a/.forge-snapshots/swap against liquidity with native token.snap +++ b/.forge-snapshots/swap against liquidity with native token.snap @@ -1 +1 @@ -105768 \ No newline at end of file +105765 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity.snap b/.forge-snapshots/swap against liquidity.snap index a4f81d951..8048c67c3 100644 --- a/.forge-snapshots/swap against liquidity.snap +++ b/.forge-snapshots/swap against liquidity.snap @@ -1 +1 @@ -116839 \ No newline at end of file +116836 \ 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 f5d08e03f..9e8aa1311 100644 --- a/.forge-snapshots/swap burn 6909 for input.snap +++ b/.forge-snapshots/swap burn 6909 for input.snap @@ -1 +1 @@ -129404 \ No newline at end of file +129401 \ 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 565e313b7..5d637418e 100644 --- a/.forge-snapshots/swap burn native 6909 for input.snap +++ b/.forge-snapshots/swap burn native 6909 for input.snap @@ -1 +1 @@ -118856 \ No newline at end of file +118853 \ 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 0fda550d3..c15068869 100644 --- a/.forge-snapshots/swap mint native output as 6909.snap +++ b/.forge-snapshots/swap mint native output as 6909.snap @@ -1 +1 @@ -139885 \ No newline at end of file +139882 \ 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 c91598e55..79234805f 100644 --- a/.forge-snapshots/swap mint output as 6909.snap +++ b/.forge-snapshots/swap mint output as 6909.snap @@ -1 +1 @@ -155331 \ No newline at end of file +155322 \ 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 a4db33d85..c7cbd5675 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 @@ -206711 \ No newline at end of file +206699 \ 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 9a17134ae..a7fdfcdc3 100644 --- a/.forge-snapshots/swap with dynamic fee.snap +++ b/.forge-snapshots/swap with dynamic fee.snap @@ -1 +1 @@ -139511 \ No newline at end of file +139502 \ No newline at end of file diff --git a/.forge-snapshots/swap with hooks.snap b/.forge-snapshots/swap with hooks.snap index 87bd40696..8fbcc812b 100644 --- a/.forge-snapshots/swap with hooks.snap +++ b/.forge-snapshots/swap with hooks.snap @@ -1 +1 @@ -132477 \ No newline at end of file +132474 \ 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 a949a4cb7..ce8d74e7e 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 @@ -169757 \ No newline at end of file +169748 \ 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 7aaa028cf..a837620df 100644 --- a/.forge-snapshots/swap with return dynamic fee.snap +++ b/.forge-snapshots/swap with return dynamic fee.snap @@ -1 +1 @@ -145838 \ No newline at end of file +145829 \ 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 5b8842ef0..52bb47e75 100644 --- a/.forge-snapshots/update dynamic fee in before swap.snap +++ b/.forge-snapshots/update dynamic fee in before swap.snap @@ -1 +1 @@ -148124 \ No newline at end of file +148115 \ No newline at end of file diff --git a/src/libraries/Pool.sol b/src/libraries/Pool.sol index 3122a9ceb..f322d447e 100644 --- a/src/libraries/Pool.sol +++ b/src/libraries/Pool.sol @@ -273,8 +273,8 @@ library Pool { } struct SwapStart { - uint256 protocolFee; uint256 feeGrowthGlobalX128; + uint256 protocolFee; uint128 liquidityStart; bool exactInput; } From cbf9c815a1b5966f62a8c091d35026839a9e6600 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Thu, 5 Sep 2024 15:20:43 -0400 Subject: [PATCH 05/11] cursed micro optimizations --- .forge-snapshots/poolManager bytecode size.snap | 2 +- .forge-snapshots/simple swap with native.snap | 2 +- .forge-snapshots/simple swap.snap | 2 +- .forge-snapshots/swap CA custom curve + swap noop.snap | 2 +- .forge-snapshots/swap CA fee on unspecified.snap | 2 +- .forge-snapshots/swap against liquidity with native token.snap | 2 +- .forge-snapshots/swap against liquidity.snap | 2 +- .forge-snapshots/swap burn 6909 for input.snap | 2 +- .forge-snapshots/swap burn native 6909 for input.snap | 2 +- .forge-snapshots/swap mint native output as 6909.snap | 2 +- .forge-snapshots/swap mint output as 6909.snap | 2 +- .forge-snapshots/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 +- .forge-snapshots/swap with lp fee and protocol fee.snap | 2 +- .forge-snapshots/swap with return dynamic fee.snap | 2 +- .forge-snapshots/update dynamic fee in before swap.snap | 2 +- src/libraries/Pool.sol | 2 +- 18 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.forge-snapshots/poolManager bytecode size.snap b/.forge-snapshots/poolManager bytecode size.snap index a3a44666a..1d9275c9d 100644 --- a/.forge-snapshots/poolManager bytecode size.snap +++ b/.forge-snapshots/poolManager bytecode size.snap @@ -1 +1 @@ -24080 \ No newline at end of file +24068 \ 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 c35412130..a3e94411e 100644 --- a/.forge-snapshots/simple swap with native.snap +++ b/.forge-snapshots/simple swap with native.snap @@ -1 +1 @@ -108658 \ No newline at end of file +108646 \ No newline at end of file diff --git a/.forge-snapshots/simple swap.snap b/.forge-snapshots/simple swap.snap index edda22cc2..4c25874fc 100644 --- a/.forge-snapshots/simple swap.snap +++ b/.forge-snapshots/simple swap.snap @@ -1 +1 @@ -123481 \ No newline at end of file +123469 \ 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 8701b3f89..47d06d7db 100644 --- a/.forge-snapshots/swap CA custom curve + swap noop.snap +++ b/.forge-snapshots/swap CA custom curve + swap noop.snap @@ -1 +1 @@ -127024 \ No newline at end of file +127006 \ 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 15476183a..33e3a5bb7 100644 --- a/.forge-snapshots/swap CA fee on unspecified.snap +++ b/.forge-snapshots/swap CA fee on unspecified.snap @@ -1 +1 @@ -154932 \ No newline at end of file +154920 \ 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 0e7048494..f1aa014cb 100644 --- a/.forge-snapshots/swap against liquidity with native token.snap +++ b/.forge-snapshots/swap against liquidity with native token.snap @@ -1 +1 @@ -105765 \ No newline at end of file +105747 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity.snap b/.forge-snapshots/swap against liquidity.snap index 8048c67c3..309aa4f57 100644 --- a/.forge-snapshots/swap against liquidity.snap +++ b/.forge-snapshots/swap against liquidity.snap @@ -1 +1 @@ -116836 \ No newline at end of file +116818 \ 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 9e8aa1311..b287e68f0 100644 --- a/.forge-snapshots/swap burn 6909 for input.snap +++ b/.forge-snapshots/swap burn 6909 for input.snap @@ -1 +1 @@ -129401 \ No newline at end of file +129383 \ 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 5d637418e..a55dfd443 100644 --- a/.forge-snapshots/swap burn native 6909 for input.snap +++ b/.forge-snapshots/swap burn native 6909 for input.snap @@ -1 +1 @@ -118853 \ No newline at end of file +118835 \ 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 c15068869..7d279303a 100644 --- a/.forge-snapshots/swap mint native output as 6909.snap +++ b/.forge-snapshots/swap mint native output as 6909.snap @@ -1 +1 @@ -139882 \ No newline at end of file +139864 \ 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 79234805f..674f39743 100644 --- a/.forge-snapshots/swap mint output as 6909.snap +++ b/.forge-snapshots/swap mint output as 6909.snap @@ -1 +1 @@ -155322 \ No newline at end of file +155310 \ 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 c7cbd5675..5e122e041 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 @@ -206699 \ No newline at end of file +206669 \ 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 a7fdfcdc3..65d10e1de 100644 --- a/.forge-snapshots/swap with dynamic fee.snap +++ b/.forge-snapshots/swap with dynamic fee.snap @@ -1 +1 @@ -139502 \ No newline at end of file +139490 \ No newline at end of file diff --git a/.forge-snapshots/swap with hooks.snap b/.forge-snapshots/swap with hooks.snap index 8fbcc812b..7dcd0d133 100644 --- a/.forge-snapshots/swap with hooks.snap +++ b/.forge-snapshots/swap with hooks.snap @@ -1 +1 @@ -132474 \ No newline at end of file +132456 \ 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 ce8d74e7e..a0c457a6a 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 @@ -169748 \ No newline at end of file +169736 \ 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 a837620df..7c495a831 100644 --- a/.forge-snapshots/swap with return dynamic fee.snap +++ b/.forge-snapshots/swap with return dynamic fee.snap @@ -1 +1 @@ -145829 \ No newline at end of file +145817 \ 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 52bb47e75..cb4c5f75a 100644 --- a/.forge-snapshots/update dynamic fee in before swap.snap +++ b/.forge-snapshots/update dynamic fee in before swap.snap @@ -1 +1 @@ -148115 \ No newline at end of file +148103 \ No newline at end of file diff --git a/src/libraries/Pool.sol b/src/libraries/Pool.sol index f322d447e..56e210675 100644 --- a/src/libraries/Pool.sol +++ b/src/libraries/Pool.sol @@ -265,9 +265,9 @@ library Pool { } struct SwapParams { + int256 amountSpecified; int24 tickSpacing; bool zeroForOne; - int256 amountSpecified; uint160 sqrtPriceLimitX96; uint24 lpFeeOverride; } From a6a536442e046b8327b38372a34d070fe3e44b22 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Thu, 5 Sep 2024 15:34:21 -0400 Subject: [PATCH 06/11] =?UTF-8?q?what=20im=20calling=20good=20enough=20?= =?UTF-8?q?=F0=9F=98=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...quidity to already existing position with salt.snap | 2 +- .forge-snapshots/addLiquidity CA fee.snap | 2 +- .forge-snapshots/addLiquidity with empty hook.snap | 2 +- .forge-snapshots/addLiquidity with native token.snap | 2 +- .../create new liquidity to a position with salt.snap | 2 +- .forge-snapshots/donate gas with 1 token.snap | 2 +- .forge-snapshots/donate gas with 2 tokens.snap | 2 +- .forge-snapshots/erc20 collect protocol fees.snap | 2 +- .forge-snapshots/extsload getFeeGrowthGlobals.snap | 2 +- .forge-snapshots/extsload getPositionInfo.snap | 2 +- .forge-snapshots/extsload getTickFeeGrowthOutside.snap | 2 +- .forge-snapshots/extsload getTickInfo.snap | 2 +- .forge-snapshots/initialize.snap | 2 +- .forge-snapshots/native collect protocol fees.snap | 2 +- .forge-snapshots/poolManager bytecode size.snap | 2 +- .forge-snapshots/removeLiquidity CA fee.snap | 2 +- .forge-snapshots/removeLiquidity with empty hook.snap | 2 +- .../removeLiquidity with native token.snap | 2 +- ...simple addLiquidity second addition same range.snap | 2 +- .forge-snapshots/simple addLiquidity.snap | 2 +- .../simple removeLiquidity some liquidity remains.snap | 2 +- .forge-snapshots/simple removeLiquidity.snap | 2 +- .forge-snapshots/simple swap with native.snap | 2 +- .forge-snapshots/simple swap.snap | 2 +- .forge-snapshots/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 +- .forge-snapshots/swap burn native 6909 for input.snap | 2 +- .forge-snapshots/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 +- .forge-snapshots/swap with return dynamic fee.snap | 2 +- .../update dynamic fee in before swap.snap | 2 +- src/libraries/Pool.sol | 10 ++++------ 39 files changed, 42 insertions(+), 44 deletions(-) diff --git a/.forge-snapshots/add liquidity to already existing position with salt.snap b/.forge-snapshots/add liquidity to already existing position with salt.snap index dad62a459..c51d442df 100644 --- a/.forge-snapshots/add liquidity to already existing position with salt.snap +++ b/.forge-snapshots/add liquidity to already existing position with salt.snap @@ -1 +1 @@ -144656 \ No newline at end of file +144632 \ No newline at end of file diff --git a/.forge-snapshots/addLiquidity CA fee.snap b/.forge-snapshots/addLiquidity CA fee.snap index 1984ea6ac..8342ac755 100644 --- a/.forge-snapshots/addLiquidity CA fee.snap +++ b/.forge-snapshots/addLiquidity CA fee.snap @@ -1 +1 @@ -170958 \ No newline at end of file +170928 \ No newline at end of file diff --git a/.forge-snapshots/addLiquidity with empty hook.snap b/.forge-snapshots/addLiquidity with empty hook.snap index 8062b1c6b..9725e3a4b 100644 --- a/.forge-snapshots/addLiquidity with empty hook.snap +++ b/.forge-snapshots/addLiquidity with empty hook.snap @@ -1 +1 @@ -274223 \ No newline at end of file +274199 \ No newline at end of file diff --git a/.forge-snapshots/addLiquidity with native token.snap b/.forge-snapshots/addLiquidity with native token.snap index 9b42c822b..29c3bf567 100644 --- a/.forge-snapshots/addLiquidity with native token.snap +++ b/.forge-snapshots/addLiquidity with native token.snap @@ -1 +1 @@ -135143 \ No newline at end of file +135122 \ No newline at end of file diff --git a/.forge-snapshots/create new liquidity to a position with salt.snap b/.forge-snapshots/create new liquidity to a position with salt.snap index 380c90efb..7170abcec 100644 --- a/.forge-snapshots/create new liquidity to a position with salt.snap +++ b/.forge-snapshots/create new liquidity to a position with salt.snap @@ -1 +1 @@ -292848 \ No newline at end of file +292824 \ No newline at end of file diff --git a/.forge-snapshots/donate gas with 1 token.snap b/.forge-snapshots/donate gas with 1 token.snap index cb54169dc..f2a99d3bb 100644 --- a/.forge-snapshots/donate gas with 1 token.snap +++ b/.forge-snapshots/donate gas with 1 token.snap @@ -1 +1 @@ -106345 \ No newline at end of file +106333 \ No newline at end of file diff --git a/.forge-snapshots/donate gas with 2 tokens.snap b/.forge-snapshots/donate gas with 2 tokens.snap index 3c42b6b3d..81e2b0d13 100644 --- a/.forge-snapshots/donate gas with 2 tokens.snap +++ b/.forge-snapshots/donate gas with 2 tokens.snap @@ -1 +1 @@ -145772 \ No newline at end of file +145754 \ No newline at end of file diff --git a/.forge-snapshots/erc20 collect protocol fees.snap b/.forge-snapshots/erc20 collect protocol fees.snap index 35d01b80f..bb8641235 100644 --- a/.forge-snapshots/erc20 collect protocol fees.snap +++ b/.forge-snapshots/erc20 collect protocol fees.snap @@ -1 +1 @@ -57445 \ No newline at end of file +57442 \ No newline at end of file diff --git a/.forge-snapshots/extsload getFeeGrowthGlobals.snap b/.forge-snapshots/extsload getFeeGrowthGlobals.snap index eb90589d8..5c1d4541a 100644 --- a/.forge-snapshots/extsload getFeeGrowthGlobals.snap +++ b/.forge-snapshots/extsload getFeeGrowthGlobals.snap @@ -1 +1 @@ -780 \ No newline at end of file +777 \ No newline at end of file diff --git a/.forge-snapshots/extsload getPositionInfo.snap b/.forge-snapshots/extsload getPositionInfo.snap index 0c8ddea6a..9f02e1c9e 100644 --- a/.forge-snapshots/extsload getPositionInfo.snap +++ b/.forge-snapshots/extsload getPositionInfo.snap @@ -1 +1 @@ -955 \ No newline at end of file +952 \ No newline at end of file diff --git a/.forge-snapshots/extsload getTickFeeGrowthOutside.snap b/.forge-snapshots/extsload getTickFeeGrowthOutside.snap index eb90589d8..5c1d4541a 100644 --- a/.forge-snapshots/extsload getTickFeeGrowthOutside.snap +++ b/.forge-snapshots/extsload getTickFeeGrowthOutside.snap @@ -1 +1 @@ -780 \ No newline at end of file +777 \ No newline at end of file diff --git a/.forge-snapshots/extsload getTickInfo.snap b/.forge-snapshots/extsload getTickInfo.snap index 0c8ddea6a..9f02e1c9e 100644 --- a/.forge-snapshots/extsload getTickInfo.snap +++ b/.forge-snapshots/extsload getTickInfo.snap @@ -1 +1 @@ -955 \ No newline at end of file +952 \ No newline at end of file diff --git a/.forge-snapshots/initialize.snap b/.forge-snapshots/initialize.snap index 36d6ed2cb..c9e91716c 100644 --- a/.forge-snapshots/initialize.snap +++ b/.forge-snapshots/initialize.snap @@ -1 +1 @@ -60028 \ No newline at end of file +60025 \ No newline at end of file diff --git a/.forge-snapshots/native collect protocol fees.snap b/.forge-snapshots/native collect protocol fees.snap index fb642bda8..1c6c78447 100644 --- a/.forge-snapshots/native collect protocol fees.snap +++ b/.forge-snapshots/native collect protocol fees.snap @@ -1 +1 @@ -59729 \ No newline at end of file +59726 \ No newline at end of file diff --git a/.forge-snapshots/poolManager bytecode size.snap b/.forge-snapshots/poolManager bytecode size.snap index 1d9275c9d..c522624d9 100644 --- a/.forge-snapshots/poolManager bytecode size.snap +++ b/.forge-snapshots/poolManager bytecode size.snap @@ -1 +1 @@ -24068 \ No newline at end of file +24025 \ No newline at end of file diff --git a/.forge-snapshots/removeLiquidity CA fee.snap b/.forge-snapshots/removeLiquidity CA fee.snap index 60669f8d8..ec2dbf546 100644 --- a/.forge-snapshots/removeLiquidity CA fee.snap +++ b/.forge-snapshots/removeLiquidity CA fee.snap @@ -1 +1 @@ -141253 \ No newline at end of file +141229 \ No newline at end of file diff --git a/.forge-snapshots/removeLiquidity with empty hook.snap b/.forge-snapshots/removeLiquidity with empty hook.snap index 2765071df..12528ddad 100644 --- a/.forge-snapshots/removeLiquidity with empty hook.snap +++ b/.forge-snapshots/removeLiquidity with empty hook.snap @@ -1 +1 @@ -130615 \ No newline at end of file +130597 \ No newline at end of file diff --git a/.forge-snapshots/removeLiquidity with native token.snap b/.forge-snapshots/removeLiquidity with native token.snap index a587c04e1..35f7a9f26 100644 --- a/.forge-snapshots/removeLiquidity with native token.snap +++ b/.forge-snapshots/removeLiquidity with native token.snap @@ -1 +1 @@ -112547 \ No newline at end of file +112535 \ No newline at end of file diff --git a/.forge-snapshots/simple addLiquidity second addition same range.snap b/.forge-snapshots/simple addLiquidity second addition same range.snap index 2124d4dd4..b3a7c8c4c 100644 --- a/.forge-snapshots/simple addLiquidity second addition same range.snap +++ b/.forge-snapshots/simple addLiquidity second addition same range.snap @@ -1 +1 @@ -98843 \ No newline at end of file +98831 \ No newline at end of file diff --git a/.forge-snapshots/simple addLiquidity.snap b/.forge-snapshots/simple addLiquidity.snap index 10276bfb3..aa78c3a2d 100644 --- a/.forge-snapshots/simple addLiquidity.snap +++ b/.forge-snapshots/simple addLiquidity.snap @@ -1 +1 @@ -161388 \ No newline at end of file +161376 \ No newline at end of file diff --git a/.forge-snapshots/simple removeLiquidity some liquidity remains.snap b/.forge-snapshots/simple removeLiquidity some liquidity remains.snap index e1aa38c3c..c2a4006d2 100644 --- a/.forge-snapshots/simple removeLiquidity some liquidity remains.snap +++ b/.forge-snapshots/simple removeLiquidity some liquidity remains.snap @@ -1 +1 @@ -92992 \ No newline at end of file +92983 \ No newline at end of file diff --git a/.forge-snapshots/simple removeLiquidity.snap b/.forge-snapshots/simple removeLiquidity.snap index d2b0f756f..81dfb1045 100644 --- a/.forge-snapshots/simple removeLiquidity.snap +++ b/.forge-snapshots/simple removeLiquidity.snap @@ -1 +1 @@ -85105 \ No newline at end of file +85096 \ 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 a3e94411e..4e734a549 100644 --- a/.forge-snapshots/simple swap with native.snap +++ b/.forge-snapshots/simple swap with native.snap @@ -1 +1 @@ -108646 \ No newline at end of file +108517 \ No newline at end of file diff --git a/.forge-snapshots/simple swap.snap b/.forge-snapshots/simple swap.snap index 4c25874fc..e94e8cc60 100644 --- a/.forge-snapshots/simple swap.snap +++ b/.forge-snapshots/simple swap.snap @@ -1 +1 @@ -123469 \ No newline at end of file +123337 \ 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 47d06d7db..d165a7399 100644 --- a/.forge-snapshots/swap CA custom curve + swap noop.snap +++ b/.forge-snapshots/swap CA custom curve + swap noop.snap @@ -1 +1 @@ -127006 \ No newline at end of file +126934 \ 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 33e3a5bb7..e4339fa85 100644 --- a/.forge-snapshots/swap CA fee on unspecified.snap +++ b/.forge-snapshots/swap CA fee on unspecified.snap @@ -1 +1 @@ -154920 \ No newline at end of file +154785 \ 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 f1aa014cb..bfc91a68a 100644 --- a/.forge-snapshots/swap against liquidity with native token.snap +++ b/.forge-snapshots/swap against liquidity with native token.snap @@ -1 +1 @@ -105747 \ No newline at end of file +105646 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity.snap b/.forge-snapshots/swap against liquidity.snap index 309aa4f57..da6da4ca0 100644 --- a/.forge-snapshots/swap against liquidity.snap +++ b/.forge-snapshots/swap against liquidity.snap @@ -1 +1 @@ -116818 \ No newline at end of file +116714 \ 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 b287e68f0..49f9bc7b0 100644 --- a/.forge-snapshots/swap burn 6909 for input.snap +++ b/.forge-snapshots/swap burn 6909 for input.snap @@ -1 +1 @@ -129383 \ No newline at end of file +129278 \ 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 a55dfd443..4c0789729 100644 --- a/.forge-snapshots/swap burn native 6909 for input.snap +++ b/.forge-snapshots/swap burn native 6909 for input.snap @@ -1 +1 @@ -118835 \ No newline at end of file +118734 \ 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 7d279303a..66b1f831d 100644 --- a/.forge-snapshots/swap mint native output as 6909.snap +++ b/.forge-snapshots/swap mint native output as 6909.snap @@ -1 +1 @@ -139864 \ No newline at end of file +139756 \ 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 674f39743..e231b294e 100644 --- a/.forge-snapshots/swap mint output as 6909.snap +++ b/.forge-snapshots/swap mint output as 6909.snap @@ -1 +1 @@ -155310 \ No newline at end of file +155178 \ 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 5e122e041..2d821ebed 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 @@ -206669 \ No newline at end of file +206437 \ 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 65d10e1de..8f0535d0c 100644 --- a/.forge-snapshots/swap with dynamic fee.snap +++ b/.forge-snapshots/swap with dynamic fee.snap @@ -1 +1 @@ -139490 \ No newline at end of file +139358 \ No newline at end of file diff --git a/.forge-snapshots/swap with hooks.snap b/.forge-snapshots/swap with hooks.snap index 7dcd0d133..c045a71b5 100644 --- a/.forge-snapshots/swap with hooks.snap +++ b/.forge-snapshots/swap with hooks.snap @@ -1 +1 @@ -132456 \ No newline at end of file +132352 \ 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 a0c457a6a..c23126986 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 @@ -169736 \ No newline at end of file +169601 \ 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 7c495a831..79ca694af 100644 --- a/.forge-snapshots/swap with return dynamic fee.snap +++ b/.forge-snapshots/swap with return dynamic fee.snap @@ -1 +1 @@ -145817 \ No newline at end of file +145685 \ 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 cb4c5f75a..8513cf6e8 100644 --- a/.forge-snapshots/update dynamic fee in before swap.snap +++ b/.forge-snapshots/update dynamic fee in before swap.snap @@ -1 +1 @@ -148103 \ No newline at end of file +147968 \ No newline at end of file diff --git a/src/libraries/Pool.sol b/src/libraries/Pool.sol index 56e210675..874db50b5 100644 --- a/src/libraries/Pool.sol +++ b/src/libraries/Pool.sol @@ -276,7 +276,6 @@ library Pool { uint256 feeGrowthGlobalX128; uint256 protocolFee; uint128 liquidityStart; - bool exactInput; } /// @notice Executes a swap against the state, and returns the amount deltas of the pool @@ -291,8 +290,7 @@ library Pool { SwapStart memory start = SwapStart({ protocolFee: zeroForOne ? slot0Start.protocolFee().getZeroForOneFee() : slot0Start.protocolFee().getOneForZeroFee(), feeGrowthGlobalX128: zeroForOne ? self.feeGrowthGlobal0X128 : self.feeGrowthGlobal1X128, - liquidityStart: self.liquidity, - exactInput: params.amountSpecified < 0 + liquidityStart: self.liquidity }); // uint128 liquidityStart = self.liquidity; @@ -326,7 +324,7 @@ library Pool { // a swap fee totaling MAX_SWAP_FEE (100%) makes exact output swaps impossible since the input is entirely consumed by the fee if (swapFee >= SwapMath.MAX_SWAP_FEE) { - if (!start.exactInput) { + if (!(params.amountSpecified < 0)) { // if not exactInput InvalidFeeForExactOut.selector.revertWith(); } } @@ -382,7 +380,7 @@ library Pool { swapFee ); - if (!start.exactInput) { + if (!(params.amountSpecified < 0)) { // if not exact-input unchecked { amountSpecifiedRemaining -= step.amountOut.toInt256(); } @@ -459,7 +457,7 @@ library Pool { } unchecked { - if (zeroForOne != start.exactInput) { + if (zeroForOne != (params.amountSpecified < 0)) { // "if currency1 is specified" swapDelta = toBalanceDelta( amountCalculated.toInt128(), (params.amountSpecified - amountSpecifiedRemaining).toInt128() ); From f6a38379f745cad84e824b5093cf6ff5e5b4687f Mon Sep 17 00:00:00 2001 From: saucepoint Date: Thu, 5 Sep 2024 15:40:49 -0400 Subject: [PATCH 07/11] cleanup deadcode --- src/libraries/Pool.sol | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/libraries/Pool.sol b/src/libraries/Pool.sol index 874db50b5..c71f6a403 100644 --- a/src/libraries/Pool.sol +++ b/src/libraries/Pool.sol @@ -272,8 +272,9 @@ library Pool { uint24 lpFeeOverride; } + /// @dev caching some swap state to avoid stack too deep errors struct SwapStart { - uint256 feeGrowthGlobalX128; + uint256 feeGrowthGlobalX128; // the global fee growth of the input token. updated in storage at the end of swap uint256 protocolFee; uint128 liquidityStart; } @@ -288,21 +289,17 @@ library Pool { bool zeroForOne = params.zeroForOne; SwapStart memory start = SwapStart({ - protocolFee: zeroForOne ? slot0Start.protocolFee().getZeroForOneFee() : slot0Start.protocolFee().getOneForZeroFee(), + protocolFee: zeroForOne + ? slot0Start.protocolFee().getZeroForOneFee() + : slot0Start.protocolFee().getOneForZeroFee(), feeGrowthGlobalX128: zeroForOne ? self.feeGrowthGlobal0X128 : self.feeGrowthGlobal1X128, liquidityStart: self.liquidity }); - // uint128 liquidityStart = self.liquidity; - // uint256 protocolFee = - // zeroForOne ? slot0Start.protocolFee().getZeroForOneFee() : slot0Start.protocolFee().getOneForZeroFee(); - // the amount remaining to be swapped in/out of the input/output asset. initially set to the amountSpecified int256 amountSpecifiedRemaining = params.amountSpecified; // the amount swapped out/in of the output/input asset. initially set to 0 int256 amountCalculated = 0; - // the global fee growth of the input token. updated in storage at the end of swap - // uint256 feeGrowthGlobalX128 = zeroForOne ? self.feeGrowthGlobal0X128 : self.feeGrowthGlobal1X128; // initialize to the current sqrt(price) result.sqrtPriceX96 = slot0Start.sqrtPriceX96(); // initialize to the current tick @@ -320,11 +317,10 @@ library Pool { swapFee = start.protocolFee == 0 ? lpFee : uint16(start.protocolFee).calculateSwapFee(lpFee); } - // bool exactInput = params.amountSpecified < 0; - // a swap fee totaling MAX_SWAP_FEE (100%) makes exact output swaps impossible since the input is entirely consumed by the fee if (swapFee >= SwapMath.MAX_SWAP_FEE) { - if (!(params.amountSpecified < 0)) { // if not exactInput + if (!(params.amountSpecified < 0)) { + // if not exactInput InvalidFeeForExactOut.selector.revertWith(); } } @@ -380,7 +376,8 @@ library Pool { swapFee ); - if (!(params.amountSpecified < 0)) { // if not exact-input + if (!(params.amountSpecified < 0)) { + // if not exact-input unchecked { amountSpecifiedRemaining -= step.amountOut.toInt256(); } @@ -399,7 +396,8 @@ library Pool { // step.amountIn does not include the swap fee, as it's already been taken from it, // so add it back to get the total amountIn and use that to calculate the amount of fees owed to the protocol // this line cannot overflow due to limits on the size of protocolFee and params.amountSpecified - uint256 delta = (step.amountIn + step.feeAmount) * start.protocolFee / ProtocolFeeLibrary.PIPS_DENOMINATOR; + uint256 delta = + (step.amountIn + step.feeAmount) * start.protocolFee / ProtocolFeeLibrary.PIPS_DENOMINATOR; // subtract it from the total fee and add it to the protocol fee step.feeAmount -= delta; amountToProtocol += delta; @@ -410,7 +408,8 @@ library Pool { if (result.liquidity > 0) { unchecked { // FullMath.mulDiv isn't needed as the numerator can't overflow uint256 since tokens have a max supply of type(uint128).max - start.feeGrowthGlobalX128 += UnsafeMath.simpleMulDiv(step.feeAmount, FixedPoint128.Q128, result.liquidity); + start.feeGrowthGlobalX128 += + UnsafeMath.simpleMulDiv(step.feeAmount, FixedPoint128.Q128, result.liquidity); } } @@ -457,7 +456,8 @@ library Pool { } unchecked { - if (zeroForOne != (params.amountSpecified < 0)) { // "if currency1 is specified" + if (zeroForOne != (params.amountSpecified < 0)) { + // "if currency1 is specified" swapDelta = toBalanceDelta( amountCalculated.toInt128(), (params.amountSpecified - amountSpecifiedRemaining).toInt128() ); From 7dfd7b4b3a6577e49e456dc74007c09d4ff64450 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Thu, 5 Sep 2024 15:48:51 -0400 Subject: [PATCH 08/11] reorganize comments --- src/libraries/Pool.sol | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/Pool.sol b/src/libraries/Pool.sol index c71f6a403..f9a731128 100644 --- a/src/libraries/Pool.sol +++ b/src/libraries/Pool.sol @@ -319,8 +319,8 @@ library Pool { // a swap fee totaling MAX_SWAP_FEE (100%) makes exact output swaps impossible since the input is entirely consumed by the fee if (swapFee >= SwapMath.MAX_SWAP_FEE) { + // if not exactInput if (!(params.amountSpecified < 0)) { - // if not exactInput InvalidFeeForExactOut.selector.revertWith(); } } @@ -376,8 +376,8 @@ library Pool { swapFee ); + // if not exactInput if (!(params.amountSpecified < 0)) { - // if not exact-input unchecked { amountSpecifiedRemaining -= step.amountOut.toInt256(); } @@ -456,8 +456,8 @@ library Pool { } unchecked { + // "if currency1 is specified" if (zeroForOne != (params.amountSpecified < 0)) { - // "if currency1 is specified" swapDelta = toBalanceDelta( amountCalculated.toInt128(), (params.amountSpecified - amountSpecifiedRemaining).toInt128() ); From df5583b956c7ea2797c01ca8b11d7beb344c3b25 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Thu, 5 Sep 2024 16:28:04 -0400 Subject: [PATCH 09/11] wip move feeGrowth to step --- .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/Pool.sol | 15 ++++++++------- 18 files changed, 25 insertions(+), 24 deletions(-) diff --git a/.forge-snapshots/poolManager bytecode size.snap b/.forge-snapshots/poolManager bytecode size.snap index c522624d9..93e095023 100644 --- a/.forge-snapshots/poolManager bytecode size.snap +++ b/.forge-snapshots/poolManager bytecode size.snap @@ -1 +1 @@ -24025 \ No newline at end of file +24074 \ 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 4e734a549..4d88e0e39 100644 --- a/.forge-snapshots/simple swap with native.snap +++ b/.forge-snapshots/simple swap with native.snap @@ -1 +1 @@ -108517 \ No newline at end of file +108528 \ No newline at end of file diff --git a/.forge-snapshots/simple swap.snap b/.forge-snapshots/simple swap.snap index e94e8cc60..6871c3fa0 100644 --- a/.forge-snapshots/simple swap.snap +++ b/.forge-snapshots/simple swap.snap @@ -1 +1 @@ -123337 \ No newline at end of file +123348 \ 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 d165a7399..738031f2a 100644 --- a/.forge-snapshots/swap CA custom curve + swap noop.snap +++ b/.forge-snapshots/swap CA custom curve + swap noop.snap @@ -1 +1 @@ -126934 \ No newline at end of file +124763 \ 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 e4339fa85..524655dd2 100644 --- a/.forge-snapshots/swap CA fee on unspecified.snap +++ b/.forge-snapshots/swap CA fee on unspecified.snap @@ -1 +1 @@ -154785 \ No newline at end of file +154796 \ 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 bfc91a68a..409ef1e7a 100644 --- a/.forge-snapshots/swap against liquidity with native token.snap +++ b/.forge-snapshots/swap against liquidity with native token.snap @@ -1 +1 @@ -105646 \ No newline at end of file +105645 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity.snap b/.forge-snapshots/swap against liquidity.snap index da6da4ca0..0495dce00 100644 --- a/.forge-snapshots/swap against liquidity.snap +++ b/.forge-snapshots/swap against liquidity.snap @@ -1 +1 @@ -116714 \ No newline at end of file +116713 \ 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 49f9bc7b0..355732f7f 100644 --- a/.forge-snapshots/swap burn 6909 for input.snap +++ b/.forge-snapshots/swap burn 6909 for input.snap @@ -1 +1 @@ -129278 \ No newline at end of file +129277 \ 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 4c0789729..ebd457acd 100644 --- a/.forge-snapshots/swap burn native 6909 for input.snap +++ b/.forge-snapshots/swap burn native 6909 for input.snap @@ -1 +1 @@ -118734 \ No newline at end of file +118733 \ 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 66b1f831d..616f4eba5 100644 --- a/.forge-snapshots/swap mint native output as 6909.snap +++ b/.forge-snapshots/swap mint native output as 6909.snap @@ -1 +1 @@ -139756 \ No newline at end of file +139755 \ 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 e231b294e..778c4e34a 100644 --- a/.forge-snapshots/swap mint output as 6909.snap +++ b/.forge-snapshots/swap mint output as 6909.snap @@ -1 +1 @@ -155178 \ No newline at end of file +155189 \ 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 2d821ebed..00b928e65 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 @@ -206437 \ No newline at end of file +206447 \ 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 8f0535d0c..2b65a1fc0 100644 --- a/.forge-snapshots/swap with dynamic fee.snap +++ b/.forge-snapshots/swap with dynamic fee.snap @@ -1 +1 @@ -139358 \ No newline at end of file +139369 \ No newline at end of file diff --git a/.forge-snapshots/swap with hooks.snap b/.forge-snapshots/swap with hooks.snap index c045a71b5..a81113a69 100644 --- a/.forge-snapshots/swap with hooks.snap +++ b/.forge-snapshots/swap with hooks.snap @@ -1 +1 @@ -132352 \ No newline at end of file +132351 \ 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 c23126986..48cac232c 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 @@ -169601 \ No newline at end of file +169612 \ 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 79ca694af..ff44690e3 100644 --- a/.forge-snapshots/swap with return dynamic fee.snap +++ b/.forge-snapshots/swap with return dynamic fee.snap @@ -1 +1 @@ -145685 \ No newline at end of file +145696 \ 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 8513cf6e8..82802428c 100644 --- a/.forge-snapshots/update dynamic fee in before swap.snap +++ b/.forge-snapshots/update dynamic fee in before swap.snap @@ -1 +1 @@ -147968 \ No newline at end of file +147979 \ No newline at end of file diff --git a/src/libraries/Pool.sol b/src/libraries/Pool.sol index f9a731128..a5fbd7a55 100644 --- a/src/libraries/Pool.sol +++ b/src/libraries/Pool.sol @@ -262,6 +262,8 @@ library Pool { uint256 amountOut; // how much fee is being paid in uint256 feeAmount; + // the global fee growth of the input token. updated in storage at the end of swap + uint256 feeGrowthGlobalX128; } struct SwapParams { @@ -274,7 +276,6 @@ library Pool { /// @dev caching some swap state to avoid stack too deep errors struct SwapStart { - uint256 feeGrowthGlobalX128; // the global fee growth of the input token. updated in storage at the end of swap uint256 protocolFee; uint128 liquidityStart; } @@ -292,7 +293,6 @@ library Pool { protocolFee: zeroForOne ? slot0Start.protocolFee().getZeroForOneFee() : slot0Start.protocolFee().getOneForZeroFee(), - feeGrowthGlobalX128: zeroForOne ? self.feeGrowthGlobal0X128 : self.feeGrowthGlobal1X128, liquidityStart: self.liquidity }); @@ -348,6 +348,7 @@ library Pool { } StepComputations memory step; + step.feeGrowthGlobalX128 = zeroForOne ? self.feeGrowthGlobal0X128 : self.feeGrowthGlobal1X128; // continue swapping as long as we haven't used the entire input/output and haven't reached the price limit while (!(amountSpecifiedRemaining == 0 || result.sqrtPriceX96 == params.sqrtPriceLimitX96)) { @@ -408,7 +409,7 @@ library Pool { if (result.liquidity > 0) { unchecked { // FullMath.mulDiv isn't needed as the numerator can't overflow uint256 since tokens have a max supply of type(uint128).max - start.feeGrowthGlobalX128 += + step.feeGrowthGlobalX128 += UnsafeMath.simpleMulDiv(step.feeAmount, FixedPoint128.Q128, result.liquidity); } } @@ -421,8 +422,8 @@ library Pool { // if the tick is initialized, run the tick transition if (step.initialized) { (uint256 feeGrowthGlobal0X128, uint256 feeGrowthGlobal1X128) = zeroForOne - ? (start.feeGrowthGlobalX128, self.feeGrowthGlobal1X128) - : (self.feeGrowthGlobal0X128, start.feeGrowthGlobalX128); + ? (step.feeGrowthGlobalX128, self.feeGrowthGlobal1X128) + : (self.feeGrowthGlobal0X128, step.feeGrowthGlobalX128); int128 liquidityNet = Pool.crossTick(self, step.tickNext, feeGrowthGlobal0X128, feeGrowthGlobal1X128); // if we're moving leftward, we interpret liquidityNet as the opposite sign @@ -450,9 +451,9 @@ library Pool { // update fee growth global if (!zeroForOne) { - self.feeGrowthGlobal1X128 = start.feeGrowthGlobalX128; + self.feeGrowthGlobal1X128 = step.feeGrowthGlobalX128; } else { - self.feeGrowthGlobal0X128 = start.feeGrowthGlobalX128; + self.feeGrowthGlobal0X128 = step.feeGrowthGlobalX128; } unchecked { From d437e981387efcfae4fc93797bce66978837e70a Mon Sep 17 00:00:00 2001 From: saucepoint Date: Thu, 5 Sep 2024 17:10:55 -0400 Subject: [PATCH 10/11] remove swapstart --- .../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 +- .../swap CA fee on unspecified.snap | 2 +- ...p against liquidity with native token.snap | 2 +- .forge-snapshots/swap against liquidity.snap | 2 +- .../swap burn 6909 for input.snap | 2 +- .../swap burn native 6909 for input.snap | 2 +- .../swap mint native output as 6909.snap | 2 +- .../swap mint output as 6909.snap | 2 +- ...wap 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/Pool.sol | 23 +++++-------------- 18 files changed, 23 insertions(+), 34 deletions(-) diff --git a/.forge-snapshots/poolManager bytecode size.snap b/.forge-snapshots/poolManager bytecode size.snap index 93e095023..5152957dc 100644 --- a/.forge-snapshots/poolManager bytecode size.snap +++ b/.forge-snapshots/poolManager bytecode size.snap @@ -1 +1 @@ -24074 \ No newline at end of file +24035 \ 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 4d88e0e39..e519550b9 100644 --- a/.forge-snapshots/simple swap with native.snap +++ b/.forge-snapshots/simple swap with native.snap @@ -1 +1 @@ -108528 \ No newline at end of file +108521 \ No newline at end of file diff --git a/.forge-snapshots/simple swap.snap b/.forge-snapshots/simple swap.snap index 6871c3fa0..ae9f35605 100644 --- a/.forge-snapshots/simple swap.snap +++ b/.forge-snapshots/simple swap.snap @@ -1 +1 @@ -123348 \ No newline at end of file +123341 \ 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 738031f2a..2bae0f8b5 100644 --- a/.forge-snapshots/swap CA custom curve + swap noop.snap +++ b/.forge-snapshots/swap CA custom curve + swap noop.snap @@ -1 +1 @@ -124763 \ No newline at end of file +124663 \ 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 524655dd2..f06925132 100644 --- a/.forge-snapshots/swap CA fee on unspecified.snap +++ b/.forge-snapshots/swap CA fee on unspecified.snap @@ -1 +1 @@ -154796 \ No newline at end of file +154789 \ 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 409ef1e7a..702783812 100644 --- a/.forge-snapshots/swap against liquidity with native token.snap +++ b/.forge-snapshots/swap against liquidity with native token.snap @@ -1 +1 @@ -105645 \ No newline at end of file +105640 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity.snap b/.forge-snapshots/swap against liquidity.snap index 0495dce00..3c2c70935 100644 --- a/.forge-snapshots/swap against liquidity.snap +++ b/.forge-snapshots/swap against liquidity.snap @@ -1 +1 @@ -116713 \ No newline at end of file +116708 \ 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 355732f7f..07f00fd69 100644 --- a/.forge-snapshots/swap burn 6909 for input.snap +++ b/.forge-snapshots/swap burn 6909 for input.snap @@ -1 +1 @@ -129277 \ No newline at end of file +129272 \ 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 ebd457acd..be5b7d56f 100644 --- a/.forge-snapshots/swap burn native 6909 for input.snap +++ b/.forge-snapshots/swap burn native 6909 for input.snap @@ -1 +1 @@ -118733 \ No newline at end of file +118728 \ 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 616f4eba5..f32e7fa4e 100644 --- a/.forge-snapshots/swap mint native output as 6909.snap +++ b/.forge-snapshots/swap mint native output as 6909.snap @@ -1 +1 @@ -139755 \ No newline at end of file +139750 \ 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 778c4e34a..cb7421cce 100644 --- a/.forge-snapshots/swap mint output as 6909.snap +++ b/.forge-snapshots/swap mint output as 6909.snap @@ -1 +1 @@ -155189 \ No newline at end of file +155182 \ 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 00b928e65..1d836eb75 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 @@ -206447 \ No newline at end of file +206434 \ 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 2b65a1fc0..61d8c9782 100644 --- a/.forge-snapshots/swap with dynamic fee.snap +++ b/.forge-snapshots/swap with dynamic fee.snap @@ -1 +1 @@ -139369 \ No newline at end of file +139362 \ No newline at end of file diff --git a/.forge-snapshots/swap with hooks.snap b/.forge-snapshots/swap with hooks.snap index a81113a69..7499dbc93 100644 --- a/.forge-snapshots/swap with hooks.snap +++ b/.forge-snapshots/swap with hooks.snap @@ -1 +1 @@ -132351 \ No newline at end of file +132346 \ 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 48cac232c..2f60479bb 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 @@ -169612 \ No newline at end of file +169609 \ 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 ff44690e3..6f44fb53e 100644 --- a/.forge-snapshots/swap with return dynamic fee.snap +++ b/.forge-snapshots/swap with return dynamic fee.snap @@ -1 +1 @@ -145696 \ No newline at end of file +145689 \ 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 82802428c..9ba0338b8 100644 --- a/.forge-snapshots/update dynamic fee in before swap.snap +++ b/.forge-snapshots/update dynamic fee in before swap.snap @@ -1 +1 @@ -147979 \ No newline at end of file +147972 \ No newline at end of file diff --git a/src/libraries/Pool.sol b/src/libraries/Pool.sol index a5fbd7a55..41e894549 100644 --- a/src/libraries/Pool.sol +++ b/src/libraries/Pool.sol @@ -274,12 +274,6 @@ library Pool { uint24 lpFeeOverride; } - /// @dev caching some swap state to avoid stack too deep errors - struct SwapStart { - uint256 protocolFee; - uint128 liquidityStart; - } - /// @notice Executes a swap against the state, and returns the amount deltas of the pool /// @dev PoolManager checks that the pool is initialized before calling function swap(State storage self, SwapParams memory params) @@ -289,12 +283,8 @@ library Pool { Slot0 slot0Start = self.slot0; bool zeroForOne = params.zeroForOne; - SwapStart memory start = SwapStart({ - protocolFee: zeroForOne - ? slot0Start.protocolFee().getZeroForOneFee() - : slot0Start.protocolFee().getOneForZeroFee(), - liquidityStart: self.liquidity - }); + uint256 protocolFee = + zeroForOne ? slot0Start.protocolFee().getZeroForOneFee() : slot0Start.protocolFee().getOneForZeroFee(); // the amount remaining to be swapped in/out of the input/output asset. initially set to the amountSpecified int256 amountSpecifiedRemaining = params.amountSpecified; @@ -314,7 +304,7 @@ library Pool { ? params.lpFeeOverride.removeOverrideFlagAndValidate() : slot0Start.lpFee(); - swapFee = start.protocolFee == 0 ? lpFee : uint16(start.protocolFee).calculateSwapFee(lpFee); + swapFee = protocolFee == 0 ? lpFee : uint16(protocolFee).calculateSwapFee(lpFee); } // a swap fee totaling MAX_SWAP_FEE (100%) makes exact output swaps impossible since the input is entirely consumed by the fee @@ -392,13 +382,12 @@ library Pool { } // if the protocol fee is on, calculate how much is owed, decrement feeAmount, and increment protocolFee - if (start.protocolFee > 0) { + if (protocolFee > 0) { unchecked { // step.amountIn does not include the swap fee, as it's already been taken from it, // so add it back to get the total amountIn and use that to calculate the amount of fees owed to the protocol // this line cannot overflow due to limits on the size of protocolFee and params.amountSpecified - uint256 delta = - (step.amountIn + step.feeAmount) * start.protocolFee / ProtocolFeeLibrary.PIPS_DENOMINATOR; + uint256 delta = (step.amountIn + step.feeAmount) * protocolFee / ProtocolFeeLibrary.PIPS_DENOMINATOR; // subtract it from the total fee and add it to the protocol fee step.feeAmount -= delta; amountToProtocol += delta; @@ -447,7 +436,7 @@ library Pool { self.slot0 = slot0Start.setTick(result.tick).setSqrtPriceX96(result.sqrtPriceX96); // update liquidity if it changed - if (start.liquidityStart != result.liquidity) self.liquidity = result.liquidity; + if (self.liquidity != result.liquidity) self.liquidity = result.liquidity; // update fee growth global if (!zeroForOne) { From 1c25afef8b0c6a03abb5f4e6859656728875c503 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Thu, 5 Sep 2024 18:09:05 -0400 Subject: [PATCH 11/11] gas things --- .forge-snapshots/simple swap with native.snap | 2 +- .forge-snapshots/simple swap.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 +- .forge-snapshots/swap burn native 6909 for input.snap | 2 +- .forge-snapshots/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 +- .forge-snapshots/swap with lp fee and protocol fee.snap | 2 +- .forge-snapshots/swap with return dynamic fee.snap | 2 +- .forge-snapshots/update dynamic fee in before swap.snap | 2 +- src/libraries/Pool.sol | 8 ++++---- 16 files changed, 19 insertions(+), 19 deletions(-) diff --git a/.forge-snapshots/simple swap with native.snap b/.forge-snapshots/simple swap with native.snap index e519550b9..4bf8e4447 100644 --- a/.forge-snapshots/simple swap with native.snap +++ b/.forge-snapshots/simple swap with native.snap @@ -1 +1 @@ -108521 \ No newline at end of file +108515 \ No newline at end of file diff --git a/.forge-snapshots/simple swap.snap b/.forge-snapshots/simple swap.snap index ae9f35605..71f991199 100644 --- a/.forge-snapshots/simple swap.snap +++ b/.forge-snapshots/simple swap.snap @@ -1 +1 @@ -123341 \ No newline at end of file +123335 \ 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 f06925132..0e69c9078 100644 --- a/.forge-snapshots/swap CA fee on unspecified.snap +++ b/.forge-snapshots/swap CA fee on unspecified.snap @@ -1 +1 @@ -154789 \ No newline at end of file +154783 \ 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 702783812..24cd0f5c3 100644 --- a/.forge-snapshots/swap against liquidity with native token.snap +++ b/.forge-snapshots/swap against liquidity with native token.snap @@ -1 +1 @@ -105640 \ No newline at end of file +105637 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity.snap b/.forge-snapshots/swap against liquidity.snap index 3c2c70935..5cf1c8096 100644 --- a/.forge-snapshots/swap against liquidity.snap +++ b/.forge-snapshots/swap against liquidity.snap @@ -1 +1 @@ -116708 \ No newline at end of file +116705 \ 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 07f00fd69..9f63f70cf 100644 --- a/.forge-snapshots/swap burn 6909 for input.snap +++ b/.forge-snapshots/swap burn 6909 for input.snap @@ -1 +1 @@ -129272 \ No newline at end of file +129269 \ 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 be5b7d56f..4b0be0884 100644 --- a/.forge-snapshots/swap burn native 6909 for input.snap +++ b/.forge-snapshots/swap burn native 6909 for input.snap @@ -1 +1 @@ -118728 \ No newline at end of file +118725 \ 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 f32e7fa4e..63a0a75cf 100644 --- a/.forge-snapshots/swap mint native output as 6909.snap +++ b/.forge-snapshots/swap mint native output as 6909.snap @@ -1 +1 @@ -139750 \ No newline at end of file +139747 \ 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 cb7421cce..fb2c12b45 100644 --- a/.forge-snapshots/swap mint output as 6909.snap +++ b/.forge-snapshots/swap mint output as 6909.snap @@ -1 +1 @@ -155182 \ No newline at end of file +155176 \ 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 1d836eb75..9ffba8461 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 @@ -206434 \ No newline at end of file +206425 \ 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 61d8c9782..7e8319d12 100644 --- a/.forge-snapshots/swap with dynamic fee.snap +++ b/.forge-snapshots/swap with dynamic fee.snap @@ -1 +1 @@ -139362 \ No newline at end of file +139356 \ No newline at end of file diff --git a/.forge-snapshots/swap with hooks.snap b/.forge-snapshots/swap with hooks.snap index 7499dbc93..a7fd1c54a 100644 --- a/.forge-snapshots/swap with hooks.snap +++ b/.forge-snapshots/swap with hooks.snap @@ -1 +1 @@ -132346 \ No newline at end of file +132343 \ 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 2f60479bb..8bf4f1a5f 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 @@ -169609 \ No newline at end of file +169603 \ 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 6f44fb53e..8e88648ff 100644 --- a/.forge-snapshots/swap with return dynamic fee.snap +++ b/.forge-snapshots/swap with return dynamic fee.snap @@ -1 +1 @@ -145689 \ No newline at end of file +145683 \ 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 9ba0338b8..0422eb27f 100644 --- a/.forge-snapshots/update dynamic fee in before swap.snap +++ b/.forge-snapshots/update dynamic fee in before swap.snap @@ -1 +1 @@ -147972 \ No newline at end of file +147966 \ No newline at end of file diff --git a/src/libraries/Pool.sol b/src/libraries/Pool.sol index 3884a2d3b..0f64f5e00 100644 --- a/src/libraries/Pool.sol +++ b/src/libraries/Pool.sol @@ -309,8 +309,8 @@ library Pool { // a swap fee totaling MAX_SWAP_FEE (100%) makes exact output swaps impossible since the input is entirely consumed by the fee if (swapFee >= SwapMath.MAX_SWAP_FEE) { - // if not exactInput - if (!(params.amountSpecified < 0)) { + // if exactOutput + if (params.amountSpecified > 0) { InvalidFeeForExactOut.selector.revertWith(); } } @@ -367,8 +367,8 @@ library Pool { swapFee ); - // if not exactInput - if (!(params.amountSpecified < 0)) { + // if exactOutput + if (params.amountSpecified > 0) { unchecked { amountSpecifiedRemaining -= step.amountOut.toInt256(); }