Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension of #198 #201

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178172
178194
Original file line number Diff line number Diff line change
@@ -1 +1 @@
188435
188457
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
311226
311248
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
189430
189455
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24175
25587
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133811
139117
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142616
149248
Original file line number Diff line number Diff line change
@@ -1 +1 @@
289602
294908
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126984
132289
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118513
118552
Original file line number Diff line number Diff line change
@@ -1 +1 @@
968244
968283
Original file line number Diff line number Diff line change
@@ -1 +1 @@
327556
327595
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337280
337319
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139831
139870
Original file line number Diff line number Diff line change
@@ -1 +1 @@
172918
177505
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178946
183533
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132949
137536
Original file line number Diff line number Diff line change
@@ -1 +1 @@
304363
304402
Original file line number Diff line number Diff line change
@@ -1 +1 @@
347359
347398
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162820
162859
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238365
238404
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163100
163139
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
108172
108211
Original file line number Diff line number Diff line change
@@ -1 +1 @@
112713
117296
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130739
135323
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163100
167684
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148519
153103
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
71220
71259
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143103
147687
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
87408
87447
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
71223
71262
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7792
8394
2 changes: 2 additions & 0 deletions src/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {SafeCast} from "./libraries/SafeCast.sol";
import {VaultReserve} from "./libraries/VaultReserve.sol";
import {VaultToken} from "./VaultToken.sol";

import {console2} from "forge-std/console2.sol";

contract Vault is IVault, VaultToken, Ownable {
using SafeCast for *;
using CurrencyLibrary for Currency;
Expand Down
36 changes: 33 additions & 3 deletions src/pool-bin/BinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {BeforeSwapDelta} from "../types/BeforeSwapDelta.sol";
import "./interfaces/IBinHooks.sol";
import {BinSlot0} from "./types/BinSlot0.sol";

import {console2} from "forge-std/console2.sol";

/// @notice Holds the state for all bin pools
contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
using BinPool for *;
Expand Down Expand Up @@ -181,11 +183,39 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
BalanceDelta hookDelta;
(delta, hookDelta) = BinHooks.afterSwap(key, swapForY, amountSpecified, delta, hookData, beforeSwapDelta);

// if (hookDelta != BalanceDeltaLibrary.ZERO_DELTA) {
// vault.accountAppBalanceDelta(key.currency0, key.currency1, hookDelta, address(key.hooks));
// }

// vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, msg.sender);

if (hookDelta != BalanceDeltaLibrary.ZERO_DELTA) {
vault.accountAppBalanceDelta(key.currency0, key.currency1, hookDelta, address(key.hooks));
}
int128 hookDelta0 = hookDelta.amount0();
int128 hookDelta1 = hookDelta.amount1();
int128 delta0 = delta.amount0();
int128 delta1 = delta.amount1();

// positive deduct reservesOfApp
if (delta0 < 0) {
/// @dev account for delta0 first so reserveOfApp will be positive, then deduct hookDelta if required
vault.accountAppBalanceDelta(key.currency0, delta0, msg.sender);
if (hookDelta0 != 0) vault.accountAppBalanceDelta(key.currency0, hookDelta0, address(key.hooks));
} else {
/// @dev account for hookDelta0 first
if (hookDelta0 != 0) vault.accountAppBalanceDelta(key.currency0, hookDelta0, address(key.hooks));
if (delta0 != 0) vault.accountAppBalanceDelta(key.currency0, delta0, msg.sender);
}

vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, 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.currency0, key.currency1, delta, msg.sender);
}
}

/// @inheritdoc IBinPoolManager
Expand Down
4 changes: 4 additions & 0 deletions src/test/MockVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ contract MockVault {
_accountDeltaForApp(currency1, delta.amount1());
}

function accountAppBalanceDelta(Currency currency, int128 delta, address settler) external {
_accountDeltaForApp(currency, delta);
}

function _accountDeltaForApp(Currency currency, int128 delta) internal {
if (delta == 0) return;

Expand Down
Loading
Loading