Skip to content

Commit

Permalink
Merge branch 'main' into audit/hexens-2nd-round-part3
Browse files Browse the repository at this point in the history
  • Loading branch information
chefburger committed Nov 13, 2024
2 parents 1ca20e9 + 15eb7d1 commit 2407d51
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142463
142475
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
115965
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
171392
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
63109
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
63078
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
165863
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
57403
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
57442
39 changes: 37 additions & 2 deletions test/pool-bin/libraries/BinPoolSwap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import {BinTestHelper} from "../helpers/BinTestHelper.sol";
import {IProtocolFeeController} from "../../../src/interfaces/IProtocolFeeController.sol";
import {MockProtocolFeeController} from "../../../src/test/fee/MockProtocolFeeController.sol";
import {Constants} from "../../../src/pool-bin/libraries/Constants.sol";
import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";

contract BinPoolSwapTest is BinTestHelper {
contract BinPoolSwapTest is BinTestHelper, GasSnapshot {
using PackedUint128Math for bytes32;
using BinPoolParametersHelper for bytes32;
using SafeCast for uint256;
Expand Down Expand Up @@ -57,7 +58,9 @@ contract BinPoolSwapTest is BinTestHelper {
poolManager.initialize(key, activeId);
addLiquidityToBin(key, poolManager, bob, activeId, 1e18, 1e18, 1e18, 1e18, "");

snapStart("BinPoolSwapTest#test_exactInputSingleBin_SwapForY");
BalanceDelta delta = poolManager.swap(key, true, -int128(1e18), "");
snapEnd();
assertEq(delta.amount0(), -int128(1e18));
assertEq(delta.amount1(), 997000000000000000);
}
Expand All @@ -66,7 +69,9 @@ contract BinPoolSwapTest is BinTestHelper {
poolManager.initialize(key, activeId);
addLiquidityToBin(key, poolManager, bob, activeId, 1e18, 1e18, 1e18, 1e18, "");

snapStart("BinPoolSwapTest#test_exactInputSingleBin_SwapForX");
BalanceDelta delta = poolManager.swap(key, false, -int128(1e18), "");
snapEnd();
assertEq(delta.amount0(), 997000000000000000);
assertEq(delta.amount1(), -1e18);
}
Expand All @@ -75,7 +80,9 @@ contract BinPoolSwapTest is BinTestHelper {
poolManager.initialize(key, activeId);
addLiquidityToBin(key, poolManager, bob, activeId, 1e18, 1e18, 1e18, 1e18, "");

snapStart("BinPoolSwapTest#test_exactOutputSingleBin_SwapForY");
BalanceDelta delta = poolManager.swap(key, true, 1e18, "");
snapEnd();
assertEq(delta.amount0(), -1003009027081243732);
assertEq(delta.amount1(), 1e18);
}
Expand All @@ -84,7 +91,9 @@ contract BinPoolSwapTest is BinTestHelper {
poolManager.initialize(key, activeId);
addLiquidityToBin(key, poolManager, bob, activeId, 1e18, 1e18, 1e18, 1e18, "");

snapStart("BinPoolSwapTest#test_exactOutputSingleBin_SwapForX");
BalanceDelta delta = poolManager.swap(key, false, 1e18, "");
snapEnd();
assertEq(delta.amount0(), 1e18);
assertEq(delta.amount1(), -1003009027081243732);
}
Expand All @@ -93,7 +102,9 @@ contract BinPoolSwapTest is BinTestHelper {
poolManager.initialize(key, activeId);
addLiquidity(key, poolManager, bob, activeId, 1e18, 1e18, 10, 10);

snapStart("BinPoolSwapTest#test_exactInputMultipleBin");
BalanceDelta delta = poolManager.swap(key, true, -1e18, "");
snapEnd();
assertEq(delta.amount0(), -1e18);
assertEq(delta.amount1(), 992555250358834498);
}
Expand All @@ -102,11 +113,35 @@ contract BinPoolSwapTest is BinTestHelper {
poolManager.initialize(key, activeId);
addLiquidity(key, poolManager, bob, activeId, 1e18, 1e18, 10, 10);

snapStart("BinPoolSwapTest#test_exactOutputMultipleBin");
BalanceDelta delta = poolManager.swap(key, true, 1e18, "");
snapEnd();
assertEq(delta.amount0(), -1007534624899920784);
assertEq(delta.amount1(), 1e18);
}

/// @dev Attempt to swap with scenario that a bin has 0 liquidity (add/remove liqudiity)
/// however the bin might still be in TreeMath due to min share locked up
function testGas_exactOutputMultipleBin_WithEmptyBins() public {
poolManager.initialize(key, activeId);
// add liquidity to 10 bins
addLiquidity(key, poolManager, bob, activeId, 2e18, 2e18, 10, 10);
uint256 bobBal;

/// remove 3 bin of liquidity from left and right
for (uint24 i = 1; i < 4; i++) {
bobBal = poolManager.getPosition(key.toId(), bob, activeId + i, 0).share;
removeLiquidityFromBin(key, poolManager, bob, activeId + i, bobBal, "");

bobBal = poolManager.getPosition(key.toId(), bob, activeId - i, 0).share;
removeLiquidityFromBin(key, poolManager, bob, activeId - i, bobBal, "");
}

snapStart("BinPoolSwapTest#testGas_exactOutputMultipleBin_WithEmptyBins");
poolManager.swap(key, true, 1e18, "");
snapEnd();
}

function test_SwapWithProtocolFee_ExactIn_SwapForY() public {
// Pre-req: set protocol fee at 0.1% for token0 and 0.05% for token1
MockProtocolFeeController feeController = new MockProtocolFeeController();
Expand Down Expand Up @@ -152,7 +187,7 @@ contract BinPoolSwapTest is BinTestHelper {
assertEq(delta.amount0(), 996501000000000000);
assertEq(delta.amount1(), -1e18);

// // after swap, verify 0.05% fee
// after swap, verify 0.05% fee
assertEq(poolManager.protocolFeesAccrued(key.currency0), 0);
assertEq(poolManager.protocolFeesAccrued(key.currency1), 0.0005 * 1e18);
}
Expand Down

0 comments on commit 2407d51

Please sign in to comment.