Skip to content

Commit

Permalink
Merge branch 'main' into feat/ottersec-gas-optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefMist authored Oct 9, 2024
2 parents e75a9da + 5b1f932 commit 5f3676d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
249391
249529
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24269
24269
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163624
163762
4 changes: 4 additions & 0 deletions src/pool-bin/BinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {LPFeeLibrary} from "../libraries/LPFeeLibrary.sol";
import {PackedUint128Math} from "./libraries/math/PackedUint128Math.sol";
import {Extsload} from "../Extsload.sol";
import {BinHooks} from "./libraries/BinHooks.sol";
import {PriceHelper} from "./libraries/PriceHelper.sol";
import {BeforeSwapDelta} from "../types/BeforeSwapDelta.sol";
import "./interfaces/IBinHooks.sol";

Expand Down Expand Up @@ -107,6 +108,9 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
revert CurrenciesInitializedOutOfOrder(Currency.unwrap(key.currency0), Currency.unwrap(key.currency1));
}

// safety check, making sure that the price can be calculated
PriceHelper.getPriceFromId(activeId, binStep);

ParametersHelper.checkUnusedBitsAllZero(
key.parameters, BinPoolParametersHelper.OFFSET_MOST_SIGNIFICANT_UNUSED_BITS
);
Expand Down
1 change: 0 additions & 1 deletion src/pool-bin/libraries/BinPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ library BinPool {
function initialize(State storage self, uint24 activeId, uint24 protocolFee, uint24 lpFee) internal {
/// An initialized pool will not have activeId: 0
if (self.slot0.activeId != 0) revert PoolAlreadyInitialized();
if (activeId == 0) revert PoolInvalidParameter();

self.slot0 = Slot0({activeId: activeId, protocolFee: protocolFee, lpFee: lpFee});
}
Expand Down
37 changes: 32 additions & 5 deletions test/pool-bin/BinPoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper {

error PoolAlreadyInitialized();
error PoolNotInitialized();
error PoolInvalidParameter();
error CurrenciesInitializedOutOfOrder();
error MaxBinStepTooSmall(uint16 maxBinStep);
error ContractSizeTooLarge(uint256 diff);
Expand Down Expand Up @@ -221,10 +220,38 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper {
}

function testInitializeSamePool() public {
poolManager.initialize(key, 10, new bytes(0));
poolManager.initialize(key, 2 ** 23, new bytes(0));

vm.expectRevert(PoolAlreadyInitialized.selector);
poolManager.initialize(key, 10, new bytes(0));
poolManager.initialize(key, 2 ** 23 + 1, new bytes(0)); // different activeId but same pool
}

function test_fuzz_InvalidPrice(uint24 activeId, bool isLeft) public {
uint24 ONE_ONE_BIN_ID = 2 ** 23; // when tokenX and tokenY are at the same price

// assume binStep as 10, so a max of 88,767 bin on each side
if (isLeft) {
activeId = uint24(bound(activeId, 0, ONE_ONE_BIN_ID - 88_767 - 1));
} else {
activeId = uint24(bound(activeId, ONE_ONE_BIN_ID + 88767 + 1, type(uint24).max));
}

vm.expectRevert();
poolManager.initialize(key, activeId, new bytes(0));
}

function test_fuzz_ValidPrice(uint24 activeId, bool isLeft) public {
uint24 ONE_ONE_BIN_ID = 2 ** 23; // when tokenX and tokenY are at the same price

// assume binStep as 10, so a max of 88,767 bin on each side
if (isLeft) {
activeId = uint24(bound(activeId, ONE_ONE_BIN_ID - 88_767, ONE_ONE_BIN_ID)); // between
} else {
activeId = uint24(bound(activeId, ONE_ONE_BIN_ID, ONE_ONE_BIN_ID + 88767));
}

// no revert expected
poolManager.initialize(key, activeId, new bytes(0));
}

function testInitializeDynamicFeeTooLarge(uint24 dynamicSwapFee) public {
Expand Down Expand Up @@ -265,11 +292,11 @@ contract BinPoolManagerTest is Test, GasSnapshot, BinTestHelper {
});

vm.expectRevert(abi.encodeWithSelector(LPFeeLibrary.LPFeeTooLarge.selector, key.fee));
poolManager.initialize(key, 10, new bytes(0));
poolManager.initialize(key, 2 ** 23, new bytes(0));
}

function testInitializeInvalidId() public {
vm.expectRevert(PoolInvalidParameter.selector);
vm.expectRevert();
poolManager.initialize(key, 0, new bytes(0));
}

Expand Down

0 comments on commit 5f3676d

Please sign in to comment.