diff --git a/.forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap b/.forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap index 2413854..0cc3515 100644 --- a/.forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap +++ b/.forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap @@ -1 +1 @@ -188203 \ No newline at end of file +189455 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerBytecodeSize.snap b/.forge-snapshots/BinPoolManagerBytecodeSize.snap index 01dee15..93d1048 100644 --- a/.forge-snapshots/BinPoolManagerBytecodeSize.snap +++ b/.forge-snapshots/BinPoolManagerBytecodeSize.snap @@ -1 +1 @@ -25155 \ No newline at end of file +25587 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap b/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap index fe9c6eb..b696a4b 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasSwapMultipleBins.snap @@ -1 +1 @@ -182003 \ No newline at end of file +177505 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap b/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap index 1c511f5..567b974 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasSwapOverBigBinIdGate.snap @@ -1 +1 @@ -188031 \ No newline at end of file +183533 \ No newline at end of file diff --git a/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap b/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap index 02f83f9..49b9987 100644 --- a/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap +++ b/.forge-snapshots/BinPoolManagerTest#testGasSwapSingleBin.snap @@ -1 +1 @@ -142034 \ No newline at end of file +137536 \ No newline at end of file diff --git a/src/pool-bin/BinPoolManager.sol b/src/pool-bin/BinPoolManager.sol index ac4f0bb..31b8fe4 100644 --- a/src/pool-bin/BinPoolManager.sol +++ b/src/pool-bin/BinPoolManager.sol @@ -186,25 +186,30 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload { // } // vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, msg.sender); - int128 hookDelta0 = hookDelta.amount0(); - int128 hookDelta1 = hookDelta.amount1(); - int128 delta0 = delta.amount0(); - int128 delta1 = delta.amount1(); - - if (delta0 < 0) { - vault.accountAppBalanceDelta(key.currency0, delta0, msg.sender); - vault.accountAppBalanceDelta(key.currency0, hookDelta0, address(key.hooks)); - } else { - vault.accountAppBalanceDelta(key.currency0, hookDelta0, address(key.hooks)); - vault.accountAppBalanceDelta(key.currency0, delta0, msg.sender); - } - if (delta1 < 0) { - vault.accountAppBalanceDelta(key.currency1, delta1, msg.sender); - vault.accountAppBalanceDelta(key.currency1, hookDelta1, address(key.hooks)); + if (hookDelta != BalanceDeltaLibrary.ZERO_DELTA) { + int128 hookDelta0 = hookDelta.amount0(); + int128 hookDelta1 = hookDelta.amount1(); + int128 delta0 = delta.amount0(); + int128 delta1 = delta.amount1(); + + if (delta0 < 0) { + vault.accountAppBalanceDelta(key.currency0, delta0, msg.sender); + if (hookDelta0 != 0) vault.accountAppBalanceDelta(key.currency0, hookDelta0, address(key.hooks)); + } else { + if (hookDelta0 != 0) vault.accountAppBalanceDelta(key.currency0, hookDelta0, address(key.hooks)); + if (delta0 != 0) vault.accountAppBalanceDelta(key.currency0, delta0, msg.sender); + } + + if (delta1 < 0) { + vault.accountAppBalanceDelta(key.currency1, delta1, msg.sender); + if (hookDelta1 != 0) vault.accountAppBalanceDelta(key.currency1, hookDelta1, address(key.hooks)); + } else { + if (hookDelta1 != 0) vault.accountAppBalanceDelta(key.currency1, hookDelta1, address(key.hooks)); + if (delta1 != 0) vault.accountAppBalanceDelta(key.currency1, delta1, msg.sender); + } } else { - vault.accountAppBalanceDelta(key.currency1, hookDelta1, address(key.hooks)); - vault.accountAppBalanceDelta(key.currency1, delta1, msg.sender); + vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, msg.sender); } } diff --git a/test/pool-bin/BinReturnsDeltaOverwriteSwap.t.sol b/test/pool-bin/BinReturnsDeltaOverwriteSwap.t.sol index 181a11e..054fa48 100644 --- a/test/pool-bin/BinReturnsDeltaOverwriteSwap.t.sol +++ b/test/pool-bin/BinReturnsDeltaOverwriteSwap.t.sol @@ -101,5 +101,24 @@ contract BinReturnsDeltaOverwriteSwap is Test, GasSnapshot, BinTestHelper { BalanceDelta delta = binSwapHelper.swap(key, true, -int128(1 ether), BinSwapHelper.TestSettings(true, true), ""); } + function testFuzz_Swap_xx(bool swapForY, uint256 swapAmount, bool exactInput) external { + swapAmount = bound(swapAmount, 0.1 ether, 99 ether); + int128 swapAmountInt = int128(uint128(swapAmount)); + if (exactInput) { + swapAmountInt = -swapAmountInt; + } + + // assume liquidity added via hook and sent to the vault + token0.mint(address(vault), 100 ether); + token1.mint(address(vault), 100 ether); + + // assume hook has some liqudiity from earlier add liqudiity + token0.mint(address(binReturnsDeltaHookOverwriteSwap), 100 ether); + token1.mint(address(binReturnsDeltaHookOverwriteSwap), 100 ether); + + BalanceDelta delta = + binSwapHelper.swap(key, swapForY, swapAmountInt, BinSwapHelper.TestSettings(true, true), ""); + } + receive() external payable {} }