Skip to content

Commit

Permalink
feat: Update hook delta logic , add fuzz test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefSnoopy committed Nov 5, 2024
1 parent 952b4b8 commit c490cab
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
188203
189455
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25155
25587
Original file line number Diff line number Diff line change
@@ -1 +1 @@
182003
177505
Original file line number Diff line number Diff line change
@@ -1 +1 @@
188031
183533
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142034
137536
39 changes: 22 additions & 17 deletions src/pool-bin/BinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/pool-bin/BinReturnsDeltaOverwriteSwap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}

0 comments on commit c490cab

Please sign in to comment.