From b4c428c997d60f3fa192433e4169e82bfb6b7f37 Mon Sep 17 00:00:00 2001 From: Alice <34962750+hensha256@users.noreply.github.com> Date: Fri, 8 Dec 2023 11:54:35 -0500 Subject: [PATCH] A few cleanup tasks (#437) * Fix compiler warnings * todo for mapping transient * fixing tests with fuzzing * remove console logs * Update PoolDonateTest.sol * remove amount overload --------- Co-authored-by: Sara Reynolds --- .forge-snapshots/mint with empty hook.snap | 2 +- .forge-snapshots/mint with native token.snap | 2 +- .forge-snapshots/mint.snap | 2 +- foundry.toml | 5 +- src/PoolManager.sol | 5 +- src/test/PoolModifyPositionTest.sol | 2 +- src/test/ProtocolFeeControllerTest.sol | 6 +- test/AccessLock.t.sol | 172 ++++++------------- test/Fees.t.sol | 65 ++++--- test/Pool.t.sol | 11 +- test/PoolManager.t.sol | 32 ++-- test/PoolManagerInitialize.t.sol | 72 ++------ test/Tick.t.sol | 3 +- test/types/BalanceDelta.t.sol | 5 +- 14 files changed, 145 insertions(+), 239 deletions(-) diff --git a/.forge-snapshots/mint with empty hook.snap b/.forge-snapshots/mint with empty hook.snap index 978bf762d..6f5ed0e11 100644 --- a/.forge-snapshots/mint with empty hook.snap +++ b/.forge-snapshots/mint with empty hook.snap @@ -1 +1 @@ -319005 \ No newline at end of file +319063 \ No newline at end of file diff --git a/.forge-snapshots/mint with native token.snap b/.forge-snapshots/mint with native token.snap index 4173ed21a..7ed549245 100644 --- a/.forge-snapshots/mint with native token.snap +++ b/.forge-snapshots/mint with native token.snap @@ -1 +1 @@ -201786 \ No newline at end of file +201844 \ No newline at end of file diff --git a/.forge-snapshots/mint.snap b/.forge-snapshots/mint.snap index 50dbde71a..d86297139 100644 --- a/.forge-snapshots/mint.snap +++ b/.forge-snapshots/mint.snap @@ -1 +1 @@ -201706 \ No newline at end of file +201764 \ No newline at end of file diff --git a/foundry.toml b/foundry.toml index 7726681e0..9ce5bd437 100644 --- a/foundry.toml +++ b/foundry.toml @@ -6,14 +6,13 @@ fs_permissions = [{ access = "read-write", path = ".forge-snapshots/"}, { access cancun = true [profile.default.fuzz] -runs = 100 +runs = 1000 seed = "0x4444" [profile.ci.fuzz] -runs = 1000 +runs = 100000 [profile.ci] -fuzz_runs = 100000 solc = "./bin/solc-static-linux" # See more config options https://github.com/foundry-rs/foundry/tree/master/config diff --git a/src/PoolManager.sol b/src/PoolManager.sol index 9be42f5ca..a0a97bb55 100644 --- a/src/PoolManager.sol +++ b/src/PoolManager.sol @@ -42,6 +42,7 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, ERC6909Claims { /// @dev Represents the currencies due/owed to each locker. /// Must all net to zero when the last lock is released. + /// TODO this needs to be transient mapping(address locker => mapping(Currency currency => int256 currencyDelta)) public currencyDelta; /// @inheritdoc IPoolManager @@ -78,13 +79,13 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, ERC6909Claims { return pools[id].positions.get(_owner, tickLower, tickUpper).liquidity; } - function getPosition(PoolId id, address owner, int24 tickLower, int24 tickUpper) + function getPosition(PoolId id, address _owner, int24 tickLower, int24 tickUpper) external view override returns (Position.Info memory position) { - return pools[id].positions.get(owner, tickLower, tickUpper); + return pools[id].positions.get(_owner, tickLower, tickUpper); } /// @inheritdoc IPoolManager diff --git a/src/test/PoolModifyPositionTest.sol b/src/test/PoolModifyPositionTest.sol index c3b7a2365..5dc8be435 100644 --- a/src/test/PoolModifyPositionTest.sol +++ b/src/test/PoolModifyPositionTest.sol @@ -53,7 +53,7 @@ contract PoolModifyPositionTest is Test, PoolTestBase { (,,, int256 delta1) = _fetchBalances(data.key.currency1, data.sender); // These assertions only apply in non lock-accessing pools. - if (!data.key.hooks.hasPermissionToAccessLock()) { + if (!data.key.hooks.hasPermissionToAccessLock() && !data.key.fee.hasHookWithdrawFee()) { if (data.params.liquidityDelta > 0) { assert(delta0 > 0 || delta1 > 0 || data.key.hooks.hasPermissionToNoOp()); assert(!(delta0 < 0 || delta1 < 0)); diff --git a/src/test/ProtocolFeeControllerTest.sol b/src/test/ProtocolFeeControllerTest.sol index 676d3917c..ebb82079b 100644 --- a/src/test/ProtocolFeeControllerTest.sol +++ b/src/test/ProtocolFeeControllerTest.sol @@ -28,14 +28,14 @@ contract ProtocolFeeControllerTest is IProtocolFeeController { /// @notice Reverts on call contract RevertingProtocolFeeControllerTest is IProtocolFeeController { - function protocolFeesForPool(PoolKey memory /* key */ ) external view returns (uint24) { + function protocolFeesForPool(PoolKey memory /* key */ ) external pure returns (uint24) { revert(); } } /// @notice Returns an out of bounds protocol fee contract OutOfBoundsProtocolFeeControllerTest is IProtocolFeeController { - function protocolFeesForPool(PoolKey memory /* key */ ) external view returns (uint24) { + function protocolFeesForPool(PoolKey memory /* key */ ) external pure returns (uint24) { // set both swap and withdraw fees to 1, which is less than MIN_PROTOCOL_FEE_DENOMINATOR return 0x001001; } @@ -43,7 +43,7 @@ contract OutOfBoundsProtocolFeeControllerTest is IProtocolFeeController { /// @notice Return a value that overflows a uint24 contract OverflowProtocolFeeControllerTest is IProtocolFeeController { - function protocolFeesForPool(PoolKey memory /* key */ ) external view returns (uint24) { + function protocolFeesForPool(PoolKey memory /* key */ ) external pure returns (uint24) { assembly { let ptr := mload(0x40) mstore(ptr, 0xFFFFAAA001) diff --git a/test/AccessLock.t.sol b/test/AccessLock.t.sol index a9797e585..0fc6b8ea5 100644 --- a/test/AccessLock.t.sol +++ b/test/AccessLock.t.sol @@ -30,6 +30,8 @@ contract AccessLockTest is Test, Deployers { AccessLockHook3 accessLockHook3; AccessLockHook accessLockHook4; + uint128 amount = 1e18; + function setUp() public { // Initialize managers and routers. deployFreshManagerAndRouters(); @@ -118,15 +120,12 @@ contract AccessLockTest is Test, Deployers { * BEFORE MODIFY POSITION TESTS * */ - function test_beforeModifyPosition_mint_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount < uint128(type(int128).max)); + function test_beforeModifyPosition_mint_succeedsWithAccessLock() public { uint256 balanceOfBefore1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); uint256 balanceOfBefore0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); BalanceDelta delta = modifyPositionRouter.modifyPosition( - key, - IPoolManager.ModifyPositionParams(0, 60, 1 * 10 ** 18), - abi.encode(amount, AccessLockHook.LockAction.Mint) + key, IPoolManager.ModifyPositionParams(0, 60, 1e18), abi.encode(amount, AccessLockHook.LockAction.Mint) ); uint256 balanceOfAfter0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); @@ -139,24 +138,20 @@ contract AccessLockTest is Test, Deployers { assertEq(manager.balanceOf(address(accessLockHook), currency1.toId()), amount); } - function test_beforeModifyPosition_take_succeedsWithAccessLock(uint128 amount) public { + function test_beforeModifyPosition_take_succeedsWithAccessLock() public { // Add liquidity so there is something to take. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); - // Can't take more than the manager has. - vm.assume(amount < key.currency1.balanceOf(address(manager))); uint256 balanceOfBefore1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); uint256 balanceOfBefore0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); // Hook only takes currency 1 rn. BalanceDelta delta = modifyPositionRouter.modifyPosition( - key, - IPoolManager.ModifyPositionParams(-60, 60, 1 * 10 ** 18), - abi.encode(amount, AccessLockHook.LockAction.Take) + key, IPoolManager.ModifyPositionParams(-60, 60, 1e18), abi.encode(amount, AccessLockHook.LockAction.Take) ); uint256 balanceOfAfter0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); uint256 balanceOfAfter1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); @@ -167,13 +162,11 @@ contract AccessLockTest is Test, Deployers { assertEq(MockERC20(Currency.unwrap(currency1)).balanceOf(address(accessLockHook)), amount); } - function test_beforeModifyPosition_swap_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount > 10); // precision - + function test_beforeModifyPosition_swap_succeedsWithAccessLock() public { // Add liquidity so there is something to swap over. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); @@ -193,15 +186,13 @@ contract AccessLockTest is Test, Deployers { assertGt(balanceOfAfter1, balanceOfBefore1); } - function test_beforeModifyPosition_modifyPosition_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount > 10 && amount < Pool.tickSpacingToMaxLiquidityPerTick(60)); - + function test_beforeModifyPosition_modifyPosition_succeedsWithAccessLock() public { uint256 balanceOfBefore1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); uint256 balanceOfBefore0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams(-120, 120, 1 * 10 ** 18), + IPoolManager.ModifyPositionParams(-120, 120, 1e18), abi.encode(amount, AccessLockHook.LockAction.ModifyPosition) ); uint256 balanceOfAfter0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); @@ -212,12 +203,11 @@ contract AccessLockTest is Test, Deployers { assertLt(balanceOfAfter1, balanceOfBefore1); } - function test_beforeModifyPosition_donate_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount > 10 && amount < uint128(type(int128).max)); // precision + function test_beforeModifyPosition_donate_succeedsWithAccessLock() public { // Add liquidity so there is a position to receive fees. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); @@ -226,7 +216,7 @@ contract AccessLockTest is Test, Deployers { modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams(-120, 120, 1 * 10 ** 18), + IPoolManager.ModifyPositionParams(-120, 120, 1e18), abi.encode(amount, AccessLockHook.LockAction.Donate) ); uint256 balanceOfAfter0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); @@ -237,12 +227,11 @@ contract AccessLockTest is Test, Deployers { assertLt(balanceOfAfter1, balanceOfBefore1); } - function test_beforeModifyPosition_burn_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount > 10 && amount < uint128(type(int128).max)); // precision + function test_beforeModifyPosition_burn_succeedsWithAccessLock() public { // Add liquidity so there is a position to swap over. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); @@ -269,23 +258,18 @@ contract AccessLockTest is Test, Deployers { assertEq(balanceOfAfter1, balanceOfBefore1 + amount1); } - function test_beforeModifyPosition_settle_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount > 10 && amount < uint128(type(int128).max)); // precision - + function test_beforeModifyPosition_settle_succeedsWithAccessLock() public { // Add liquidity so there is something to take. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); - // Can't take more than the manager has. - vm.assume(amount < key.currency1.balanceOf(address(manager))); - // Assertions in the hook. Takes and then settles within the hook. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams(-120, 120, 1 * 10 ** 18), + IPoolManager.ModifyPositionParams(-120, 120, 1e18), abi.encode(amount, AccessLockHook.LockAction.Settle) ); } @@ -293,9 +277,7 @@ contract AccessLockTest is Test, Deployers { function test_beforeModifyPosition_initialize_succeedsWithAccessLock() public { // The hook intitializes a new pool with the new key at Constants.SQRT_RATIO_1_2; modifyPositionRouter.modifyPosition( - key, - IPoolManager.ModifyPositionParams(-120, 120, 1 * 10 ** 18), - abi.encode(0, AccessLockHook.LockAction.Initialize) + key, IPoolManager.ModifyPositionParams(-120, 120, 1e18), abi.encode(0, AccessLockHook.LockAction.Initialize) ); PoolKey memory newKey = PoolKey({ @@ -316,13 +298,11 @@ contract AccessLockTest is Test, Deployers { * */ - function test_beforeSwap_mint_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount < uint128(type(int128).max)); - + function test_beforeSwap_mint_succeedsWithAccessLock() public { // Add liquidity so there is something to swap against. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); @@ -347,17 +327,14 @@ contract AccessLockTest is Test, Deployers { assertEq(manager.balanceOf(address(accessLockHook), currency1.toId()), amount); } - function test_beforeSwap_take_succeedsWithAccessLock(uint128 amount) public { + function test_beforeSwap_take_succeedsWithAccessLock() public { // Add liquidity so there is something to take. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); - // Can't take more than the manager has. - vm.assume(amount < key.currency1.balanceOf(address(manager))); - uint256 balanceOfBefore1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); uint256 balanceOfBefore0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); @@ -378,13 +355,11 @@ contract AccessLockTest is Test, Deployers { assertEq(MockERC20(Currency.unwrap(currency1)).balanceOf(address(accessLockHook)), amount); } - function test_beforeSwap_swap_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount > 10); // precision - + function test_beforeSwap_swap_succeedsWithAccessLock() public { // Add liquidity so there is something to swap over. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); @@ -409,13 +384,11 @@ contract AccessLockTest is Test, Deployers { assertGt(balanceOfAfter1, balanceOfBefore1); } - function test_beforeSwap_modifyPosition_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount > 10 && amount < Pool.tickSpacingToMaxLiquidityPerTick(60)); - + function test_beforeSwap_modifyPosition_succeedsWithAccessLock() public { // Add liquidity so there is something to swap over. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); @@ -437,12 +410,11 @@ contract AccessLockTest is Test, Deployers { assertLt(balanceOfAfter1, balanceOfBefore1); } - function test_beforeSwap_donate_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount > 10 && amount < uint128(type(int128).max)); // precision + function test_beforeSwap_donate_succeedsWithAccessLock() public { // Add liquidity so there is a position to receive fees. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); @@ -470,21 +442,18 @@ contract AccessLockTest is Test, Deployers { * */ - function test_beforeDonate_mint_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount < uint128(type(int128).max)); - + function test_beforeDonate_mint_succeedsWithAccessLock() public { // Add liquidity so there is something to donate to. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); uint256 balanceOfBefore1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); uint256 balanceOfBefore0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); - BalanceDelta delta = - donateRouter.donate(key, 1 * 10 ** 18, 1 * 10 ** 18, abi.encode(amount, AccessLockHook.LockAction.Mint)); + BalanceDelta delta = donateRouter.donate(key, 1e18, 1e18, abi.encode(amount, AccessLockHook.LockAction.Mint)); uint256 balanceOfAfter0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); uint256 balanceOfAfter1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); @@ -496,23 +465,19 @@ contract AccessLockTest is Test, Deployers { assertEq(manager.balanceOf(address(accessLockHook), currency1.toId()), amount); } - function test_beforeDonate_take_succeedsWithAccessLock(uint128 amount) public { + function test_beforeDonate_take_succeedsWithAccessLock() public { // Add liquidity so there is something to take. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); - // Can't take more than the manager has. - vm.assume(amount < key.currency1.balanceOf(address(manager))); - uint256 balanceOfBefore1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); uint256 balanceOfBefore0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); // Hook only takes currency 1 rn. - BalanceDelta delta = - donateRouter.donate(key, 1 * 10 ** 18, 1 * 10 ** 18, abi.encode(amount, AccessLockHook.LockAction.Take)); + BalanceDelta delta = donateRouter.donate(key, 1e18, 1e18, abi.encode(amount, AccessLockHook.LockAction.Take)); // Take applies a positive delta in currency1. // Donate applies a positive delta in currency0 and currency1. uint256 balanceOfAfter0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); @@ -524,17 +489,14 @@ contract AccessLockTest is Test, Deployers { assertEq(MockERC20(Currency.unwrap(currency1)).balanceOf(address(accessLockHook)), amount); } - function test_beforeDonate_swap_succeedsWithAccessLock(uint128 amount) public { + function test_beforeDonate_swap_succeedsWithAccessLock() public { // Add liquidity so there is something to swap over. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); - // greater than 10 for precision, less than currency1 balance so that we still have liquidity we can donate to - vm.assume(amount != 0 && amount > 10 && amount < currency1.balanceOf(address(manager))); - uint256 balanceOfBefore1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); uint256 balanceOfBefore0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); @@ -550,22 +512,18 @@ contract AccessLockTest is Test, Deployers { assertGt(balanceOfAfter1, balanceOfBefore1); } - function test_beforeDonate_modifyPosition_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount > 10 && amount < Pool.tickSpacingToMaxLiquidityPerTick(60)); - + function test_beforeDonate_modifyPosition_succeedsWithAccessLock() public { // Add liquidity so there is something to donate to. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); uint256 balanceOfBefore1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); uint256 balanceOfBefore0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); - donateRouter.donate( - key, 1 * 10 ** 18, 1 * 10 ** 18, abi.encode(amount, AccessLockHook.LockAction.ModifyPosition) - ); + donateRouter.donate(key, 1e18, 1e18, abi.encode(amount, AccessLockHook.LockAction.ModifyPosition)); uint256 balanceOfAfter0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); uint256 balanceOfAfter1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); @@ -575,13 +533,11 @@ contract AccessLockTest is Test, Deployers { assertLt(balanceOfAfter1, balanceOfBefore1); } - function test_beforeDonate_donate_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount > 10 && amount < uint128(type(int128).max - 1)); // precision - + function test_beforeDonate_donate_succeedsWithAccessLock() public { // Add liquidity so there is a position to receive fees. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); @@ -589,7 +545,7 @@ contract AccessLockTest is Test, Deployers { uint256 balanceOfBefore0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); // Make the swap amount small (like a NoOp). - donateRouter.donate(key, 1 * 10 ** 18, 1 * 10 ** 18, abi.encode(amount, AccessLockHook.LockAction.Donate)); + donateRouter.donate(key, 1e18, 1e18, abi.encode(amount, AccessLockHook.LockAction.Donate)); uint256 balanceOfAfter0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); uint256 balanceOfAfter1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); @@ -604,9 +560,7 @@ contract AccessLockTest is Test, Deployers { * */ - function test_beforeInitialize_mint_succeedsWithAccessLock(uint128 amount) public { - vm.assume(amount != 0 && amount < uint128(type(int128).max)); - + function test_beforeInitialize_mint_succeedsWithAccessLock() public { PoolKey memory key1 = PoolKey({ currency0: currency0, currency1: currency1, @@ -620,7 +574,7 @@ contract AccessLockTest is Test, Deployers { assertEq(manager.balanceOf(address(accessLockHook4), currency1.toId()), amount); } - function test_beforeInitialize_take_succeedsWithAccessLock(uint128 amount) public { + function test_beforeInitialize_take_succeedsWithAccessLock() public { PoolKey memory key1 = PoolKey({ currency0: currency0, currency1: currency1, @@ -632,21 +586,16 @@ contract AccessLockTest is Test, Deployers { // Add liquidity to a different pool there is something to take. modifyPositionRouter.modifyPosition( key, - IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}), + IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1000e18}), ZERO_BYTES ); - // Can't take more than the manager has. - vm.assume(amount < key.currency1.balanceOf(address(manager))); - initializeRouter.initialize(key1, SQRT_RATIO_1_1, abi.encode(amount, AccessLockHook.LockAction.Take)); assertEq(MockERC20(Currency.unwrap(currency1)).balanceOf(address(accessLockHook4)), amount); } - function test_beforeInitialize_swap_revertsOnPoolNotInitialized(uint128 amount) public { - vm.assume(amount != 0 && amount > 10); // precision - + function test_beforeInitialize_swap_revertsOnPoolNotInitialized() public { PoolKey memory key1 = PoolKey({ currency0: currency0, currency1: currency1, @@ -659,9 +608,7 @@ contract AccessLockTest is Test, Deployers { initializeRouter.initialize(key1, SQRT_RATIO_1_1, abi.encode(amount, AccessLockHook.LockAction.Swap)); } - function test_beforeInitialize_modifyPosition_revertsOnPoolNotInitialized(uint128 amount) public { - vm.assume(amount != 0 && amount > 10); // precision - + function test_beforeInitialize_modifyPosition_revertsOnPoolNotInitialized() public { PoolKey memory key1 = PoolKey({ currency0: currency0, currency1: currency1, @@ -674,9 +621,7 @@ contract AccessLockTest is Test, Deployers { initializeRouter.initialize(key1, SQRT_RATIO_1_1, abi.encode(amount, AccessLockHook.LockAction.ModifyPosition)); } - function test_beforeInitialize_donate_revertsOnPoolNotInitialized(uint128 amount) public { - vm.assume(amount != 0 && amount > 10); // precision - + function test_beforeInitialize_donate_revertsOnPoolNotInitialized() public { PoolKey memory key1 = PoolKey({ currency0: currency0, currency1: currency1, @@ -697,14 +642,11 @@ contract AccessLockTest is Test, Deployers { function test_onlyByLocker_revertsWhenHookIsNotCurrentHook() public { // Call first access lock hook. Should succeed. - uint256 amount = 100; uint256 balanceOfBefore1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this)); uint256 balanceOfBefore0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); BalanceDelta delta = modifyPositionRouter.modifyPosition( - key, - IPoolManager.ModifyPositionParams(0, 60, 1 * 10 ** 18), - abi.encode(amount, AccessLockHook.LockAction.Mint) + key, IPoolManager.ModifyPositionParams(0, 60, 1e18), abi.encode(amount, AccessLockHook.LockAction.Mint) ); uint256 balanceOfAfter0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this)); @@ -729,7 +671,7 @@ contract AccessLockTest is Test, Deployers { ) ); delta = modifyPositionRouter.modifyPosition( - keyAccessLockHook2, IPoolManager.ModifyPositionParams(0, 60, 1 * 10 ** 18), abi.encode(true, key) + keyAccessLockHook2, IPoolManager.ModifyPositionParams(0, 60, 1e18), abi.encode(true, key) ); } @@ -741,18 +683,18 @@ contract AccessLockTest is Test, Deployers { initPool(currency0, currency1, IHooks(accessLockHook2), Constants.FEE_MEDIUM, SQRT_RATIO_1_1, ZERO_BYTES); modifyPositionRouter.modifyPosition( - keyAccessLockHook2, IPoolManager.ModifyPositionParams(0, 60, 1 * 10 ** 18), abi.encode(false, keyWithNoHook) + keyAccessLockHook2, IPoolManager.ModifyPositionParams(0, 60, 1e18), abi.encode(false, keyWithNoHook) ); assertEq(manager.balanceOf(address(accessLockHook2), currency1.toId()), 10); } function test_onlyByLocker_revertsWhenThereIsNoOutsideLock() public { - modifyPositionRouter.modifyPosition(key, IPoolManager.ModifyPositionParams(0, 60, 1 * 10 ** 18), ZERO_BYTES); + modifyPositionRouter.modifyPosition(key, IPoolManager.ModifyPositionParams(0, 60, 1e18), ZERO_BYTES); assertEq(address(manager.getCurrentHook()), address(0)); vm.expectRevert(abi.encodeWithSelector(IPoolManager.LockedBy.selector, address(0), address(0))); vm.prank(address(key.hooks)); - manager.modifyPosition(key, IPoolManager.ModifyPositionParams(0, 60, 1 * 10 ** 18), ZERO_BYTES); + manager.modifyPosition(key, IPoolManager.ModifyPositionParams(0, 60, 1e18), ZERO_BYTES); } function test_getCurrentHook_isClearedAfterNestedLock() public { @@ -773,7 +715,7 @@ contract AccessLockTest is Test, Deployers { // Asserts are in the AccessLockHook3. modifyPositionRouter.modifyPosition( - keyAccessLockHook3, IPoolManager.ModifyPositionParams(0, 60, 1 * 10 ** 18), ZERO_BYTES + keyAccessLockHook3, IPoolManager.ModifyPositionParams(0, 60, 1e18), ZERO_BYTES ); } @@ -790,7 +732,7 @@ contract AccessLockTest is Test, Deployers { ); // beforeDonate noOp - donateRouter.donate(noOpKey, 1 * 10 ** 18, 1 * 10 ** 18, abi.encode(0, AccessLockHook.LockAction.NoOp)); + donateRouter.donate(noOpKey, 1e18, 1e18, abi.encode(0, AccessLockHook.LockAction.NoOp)); // beforeSwap noOp swapRouter.swap( diff --git a/test/Fees.t.sol b/test/Fees.t.sol index f99b4b6e0..c73b46d93 100644 --- a/test/Fees.t.sol +++ b/test/Fees.t.sol @@ -117,7 +117,7 @@ contract FeesTest is Test, Deployers, GasSnapshot { } function testInitializeHookSwapFee(uint16 fee) public { - vm.assume(fee < 2 ** 12); + fee = uint16(bound(fee, 0, (2 ** 12) - 1)); (Pool.Slot0 memory slot0,,,) = manager.pools(key0.toId()); assertEq(getSwapFee(slot0.hookFees), 0); @@ -133,7 +133,7 @@ contract FeesTest is Test, Deployers, GasSnapshot { } function testInitializeHookWithdrawFee(uint16 fee) public { - vm.assume(fee < 2 ** 12); + fee = uint16(bound(fee, 0, (2 ** 12) - 1)); (Pool.Slot0 memory slot0,,,) = manager.pools(key1.toId()); assertEq(getWithdrawFee(slot0.hookFees), 0); @@ -149,7 +149,8 @@ contract FeesTest is Test, Deployers, GasSnapshot { } function testInitializeBothHookFee(uint16 swapFee, uint16 withdrawFee) public { - vm.assume(swapFee < 2 ** 12 && withdrawFee < 2 ** 12); + swapFee = uint16(bound(swapFee, 0, (2 ** 12) - 1)); + withdrawFee = uint16(bound(withdrawFee, 0, (2 ** 12) - 1)); (Pool.Slot0 memory slot0,,,) = manager.pools(key2.toId()); assertEq(getSwapFee(slot0.hookFees), 0); @@ -165,7 +166,8 @@ contract FeesTest is Test, Deployers, GasSnapshot { } function testInitializeHookProtocolSwapFee(uint16 hookSwapFee, uint16 protocolSwapFee) public { - vm.assume(hookSwapFee < 2 ** 12 && protocolSwapFee < 2 ** 12); + hookSwapFee = uint16(bound(hookSwapFee, 0, (2 ** 12) - 1)); + protocolSwapFee = uint16(bound(protocolSwapFee, 0, (2 ** 12) - 1)); (Pool.Slot0 memory slot0,,,) = manager.pools(key0.toId()); assertEq(getSwapFee(slot0.hookFees), 0); @@ -199,10 +201,10 @@ contract FeesTest is Test, Deployers, GasSnapshot { uint16 protocolSwapFee, uint16 protocolWithdrawFee ) public { - vm.assume( - hookSwapFee < 2 ** 12 && hookWithdrawFee < 2 ** 12 && protocolSwapFee < 2 ** 12 - && protocolWithdrawFee < 2 ** 12 - ); + hookSwapFee = uint16(bound(hookSwapFee, 0, (2 ** 12) - 1)); + hookWithdrawFee = uint16(bound(hookWithdrawFee, 0, (2 ** 12) - 1)); + protocolSwapFee = uint16(bound(protocolSwapFee, 0, (2 ** 12) - 1)); + protocolWithdrawFee = uint16(bound(protocolWithdrawFee, 0, (2 ** 12) - 1)); (Pool.Slot0 memory slot0,,,) = manager.pools(key2.toId()); assertEq(getSwapFee(slot0.hookFees), 0); @@ -243,11 +245,15 @@ contract FeesTest is Test, Deployers, GasSnapshot { function testProtocolFeeOnWithdrawalRemainsZeroIfNoHookWithdrawalFeeSet( uint16 hookSwapFee, - uint16 protocolWithdrawFee + uint8 protocolWithdrawFee0, + uint8 protocolWithdrawFee1 ) public { - vm.assume(hookSwapFee < 2 ** 12 && protocolWithdrawFee < 2 ** 12); - vm.assume(protocolWithdrawFee >> 6 >= 4); - vm.assume(protocolWithdrawFee % 64 >= 4); + hookSwapFee = uint16(bound(hookSwapFee, 0, (2 ** 12) - 1)); + + protocolWithdrawFee0 = uint8(bound(protocolWithdrawFee0, 4, (2 ** 6) - 1)); + protocolWithdrawFee1 = uint8(bound(protocolWithdrawFee1, 4, (2 ** 6) - 1)); + + uint16 protocolWithdrawFee = (uint16(protocolWithdrawFee0) << 6) | uint16(protocolWithdrawFee1); // On a pool whose hook has not set a withdraw fee, the protocol should not accrue any value even if it has set a withdraw fee. hook.setSwapFee(key0, hookSwapFee); @@ -298,11 +304,16 @@ contract FeesTest is Test, Deployers, GasSnapshot { // manager.setProtocolFees(key0); // } - function testHookWithdrawFeeProtocolWithdrawFee(uint16 hookWithdrawFee, uint16 protocolWithdrawFee) public { - vm.assume(protocolWithdrawFee < 2 ** 12); - vm.assume(hookWithdrawFee < 2 ** 12); - vm.assume(protocolWithdrawFee >> 6 >= 4); - vm.assume(protocolWithdrawFee % 64 >= 4); + function testHookWithdrawFeeProtocolWithdrawFee( + uint16 hookWithdrawFee, + uint8 protocolWithdrawFee0, + uint8 protocolWithdrawFee1 + ) public { + hookWithdrawFee = uint16(bound(hookWithdrawFee, 0, (2 ** 12) - 1)); + protocolWithdrawFee0 = uint8(bound(protocolWithdrawFee0, 4, (2 ** 6) - 1)); + protocolWithdrawFee1 = uint8(bound(protocolWithdrawFee1, 4, (2 ** 6) - 1)); + + uint16 protocolWithdrawFee = (uint16(protocolWithdrawFee0) << 6) | uint16(protocolWithdrawFee1); hook.setWithdrawFee(key1, hookWithdrawFee); manager.setHookFees(key1); @@ -355,13 +366,19 @@ contract FeesTest is Test, Deployers, GasSnapshot { assertEq(manager.hookFeesAccrued(address(key1.hooks), currency1), expectedHookFee1); } - function testNoHookProtocolFee(uint16 protocolSwapFee, uint16 protocolWithdrawFee) public { - vm.assume(protocolSwapFee < 2 ** 12 && protocolWithdrawFee < 2 ** 12); + function testNoHookProtocolFee( + uint8 protocolSwapFee0, + uint8 protocolSwapFee1, + uint8 protocolWithdrawFee0, + uint8 protocolWithdrawFee1 + ) public { + protocolWithdrawFee0 = uint8(bound(protocolWithdrawFee0, 4, (2 ** 6) - 1)); + protocolWithdrawFee1 = uint8(bound(protocolWithdrawFee1, 4, (2 ** 6) - 1)); + protocolSwapFee0 = uint8(bound(protocolSwapFee0, 4, (2 ** 6) - 1)); + protocolSwapFee1 = uint8(bound(protocolSwapFee1, 4, (2 ** 6) - 1)); - vm.assume(protocolSwapFee >> 6 >= 4); - vm.assume(protocolSwapFee % 64 >= 4); - vm.assume(protocolWithdrawFee >> 6 >= 4); - vm.assume(protocolWithdrawFee % 64 >= 4); + uint16 protocolWithdrawFee = (uint16(protocolWithdrawFee0) << 6) | uint16(protocolWithdrawFee1); + uint16 protocolSwapFee = (uint16(protocolSwapFee1) << 6) | uint16(protocolSwapFee0); feeController.setSwapFeeForPool(key3.toId(), protocolSwapFee); feeController.setWithdrawFeeForPool(key3.toId(), protocolWithdrawFee); @@ -386,8 +403,6 @@ contract FeesTest is Test, Deployers, GasSnapshot { IPoolManager.ModifyPositionParams memory params2 = IPoolManager.ModifyPositionParams(-60, 60, -liquidityDelta); modifyPositionRouter.modifyPosition(key3, params2, ZERO_BYTES); - uint16 protocolSwapFee1 = (protocolSwapFee >> 6); - // No fees should accrue bc there is no hook so the protocol cant take withdraw fees. assertEq(manager.protocolFeesAccrued(currency0), 0); assertEq(manager.protocolFeesAccrued(currency1), 0); diff --git a/test/Pool.t.sol b/test/Pool.t.sol index a4814c343..f105a2bee 100644 --- a/test/Pool.t.sol +++ b/test/Pool.t.sol @@ -18,7 +18,8 @@ contract PoolTest is Test { Pool.State state; function testPoolInitialize(uint160 sqrtPriceX96, uint16 protocolFee, uint16 hookFee, uint24 dynamicFee) public { - vm.assume(protocolFee < 2 ** 12 && hookFee < 2 ** 12); + protocolFee = uint16(bound(protocolFee, 0, (2 ** 12) - 1)); + hookFee = uint16(bound(hookFee, 0, (2 ** 12) - 1)); if (sqrtPriceX96 < TickMath.MIN_SQRT_RATIO || sqrtPriceX96 >= TickMath.MAX_SQRT_RATIO) { vm.expectRevert(TickMath.InvalidSqrtRatio.selector); @@ -45,8 +46,7 @@ contract PoolTest is Test { function testModifyPosition(uint160 sqrtPriceX96, Pool.ModifyPositionParams memory params) public { // Assumptions tested in PoolManager.t.sol - vm.assume(params.tickSpacing >= TickMath.MIN_TICK_SPACING); - vm.assume(params.tickSpacing <= TickMath.MAX_TICK_SPACING); + params.tickSpacing = int24(bound(params.tickSpacing, TickMath.MIN_TICK_SPACING, TickMath.MAX_TICK_SPACING)); testPoolInitialize(sqrtPriceX96, 0, 0, 0); @@ -91,9 +91,8 @@ contract PoolTest is Test { function testSwap(uint160 sqrtPriceX96, uint24 swapFee, Pool.SwapParams memory params) public { // Assumptions tested in PoolManager.t.sol - vm.assume(params.tickSpacing >= TickMath.MIN_TICK_SPACING); - vm.assume(params.tickSpacing <= TickMath.MAX_TICK_SPACING); - vm.assume(swapFee < 1000000); + params.tickSpacing = int24(bound(params.tickSpacing, TickMath.MIN_TICK_SPACING, TickMath.MAX_TICK_SPACING)); + swapFee = uint24(bound(swapFee, 0, 999999)); testPoolInitialize(sqrtPriceX96, 0, 0, 0); Pool.Slot0 memory slot0 = state.slot0; diff --git a/test/PoolManager.t.sol b/test/PoolManager.t.sol index 9a96fe381..ec283958e 100644 --- a/test/PoolManager.t.sol +++ b/test/PoolManager.t.sol @@ -84,8 +84,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot { } function test_mint_succeedsIfInitialized(uint160 sqrtPriceX96) public { - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); vm.expectEmit(true, true, true, true); emit ModifyPosition( @@ -100,8 +99,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot { } function test_mint_succeedsForNativeTokensIfInitialized(uint160 sqrtPriceX96) public { - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); vm.expectEmit(true, true, true, true); emit ModifyPosition( @@ -116,8 +114,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot { } function test_mint_succeedsWithHooksIfInitialized(uint160 sqrtPriceX96) public { - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); address payable mockAddr = payable(address(uint160(Hooks.BEFORE_MODIFY_POSITION_FLAG | Hooks.AFTER_MODIFY_POSITION_FLAG))); @@ -315,8 +312,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot { } function test_swap_failsIfNotInitialized(uint160 sqrtPriceX96) public { - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); key.fee = 100; IPoolManager.SwapParams memory params = @@ -638,8 +634,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot { } function test_donate_failsIfNoLiquidity(uint160 sqrtPriceX96) public { - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); (key,) = initPool(currency0, currency1, IHooks(address(0)), 100, sqrtPriceX96, ZERO_BYTES); @@ -772,8 +767,6 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot { } function test_setProtocolFee_failsWithInvalidProtocolFeeControllers() public { - uint24 protocolFee = 4; - (Pool.Slot0 memory slot0,,,) = manager.pools(key.toId()); assertEq(slot0.protocolFees, 0); @@ -834,8 +827,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot { function test_noop_gas(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); address payable hookAddr = payable( address( @@ -875,8 +867,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot { function test_noop_succeedsOnAllActions(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); address payable hookAddr = payable( address( @@ -925,8 +916,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot { function test_noop_failsOnUninitializedPools(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); address payable hookAddr = payable( address( @@ -963,8 +953,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot { function test_noop_failsOnForbiddenFunctions(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); address payable hookAddr = payable( address( @@ -1017,8 +1006,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot { function test_noop_failsWithoutNoOpFlag(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); address payable hookAddr = payable( address(uint160(Hooks.BEFORE_MODIFY_POSITION_FLAG | Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_DONATE_FLAG)) diff --git a/test/PoolManagerInitialize.t.sol b/test/PoolManagerInitialize.t.sol index ff3c30c92..be8c4e27e 100644 --- a/test/PoolManagerInitialize.t.sol +++ b/test/PoolManagerInitialize.t.sol @@ -55,8 +55,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize(PoolKey memory key0, uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); // tested in Hooks.t.sol key0.hooks = IHooks(ADDRESS_ZERO); @@ -89,8 +88,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_forNativeTokens(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); uninitializedKey.currency0 = CurrencyLibrary.NATIVE; vm.expectEmit(true, true, true, true); @@ -112,8 +110,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_succeedsWithHooks(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); address payable mockAddr = payable(address(uint160(Hooks.BEFORE_INITIALIZE_FLAG | Hooks.AFTER_INITIALIZE_FLAG))); address payable hookAddr = payable(MOCK_HOOKS); @@ -145,8 +142,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_succeedsWithMaxTickSpacing(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); uninitializedKey.tickSpacing = manager.MAX_TICK_SPACING(); @@ -165,8 +161,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_succeedsWithEmptyHooks(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); address hookEmptyAddr = EMPTY_HOOKS; @@ -183,8 +178,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_revertsWithIdenticalTokens(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); // Both currencies are currency0 uninitializedKey.currency1 = currency0; @@ -195,8 +189,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_revertsWithSameTokenCombo(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); uninitializedKey.currency1 = currency0; uninitializedKey.currency0 = currency1; @@ -207,8 +200,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_fetchFeeWhenController(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); manager.setProtocolFeeController(feeController); uint16 poolProtocolFee = 4; @@ -223,8 +215,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_revertsWhenPoolAlreadyInitialized(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); initializeRouter.initialize(uninitializedKey, sqrtPriceX96, ZERO_BYTES); vm.expectRevert(Pool.PoolAlreadyInitialized.selector); @@ -280,8 +271,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_failsIfTickSpaceTooLarge(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); uninitializedKey.tickSpacing = manager.MAX_TICK_SPACING() + 1; @@ -291,8 +281,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_failsIfTickSpaceZero(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); uninitializedKey.tickSpacing = 0; @@ -302,8 +291,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_failsIfTickSpaceNeg(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); uninitializedKey.tickSpacing = -1; @@ -313,13 +301,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_succeedsWithOutOfBoundsFeeController(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); - - address hookEmptyAddr = EMPTY_HOOKS; - MockHooks impl = new MockHooks(); - vm.etch(hookEmptyAddr, address(impl).code); - MockHooks mockHooks = MockHooks(hookEmptyAddr); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); manager.setProtocolFeeController(outOfBoundsFeeController); // expect initialize to succeed even though the controller reverts @@ -344,13 +326,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_succeedsWithRevertingFeeController(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); - - address hookEmptyAddr = EMPTY_HOOKS; - MockHooks impl = new MockHooks(); - vm.etch(hookEmptyAddr, address(impl).code); - MockHooks mockHooks = MockHooks(hookEmptyAddr); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); manager.setProtocolFeeController(revertingFeeController); // expect initialize to succeed even though the controller reverts @@ -372,13 +348,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_succeedsWithOverflowFeeController(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); - - address hookEmptyAddr = EMPTY_HOOKS; - MockHooks impl = new MockHooks(); - vm.etch(hookEmptyAddr, address(impl).code); - MockHooks mockHooks = MockHooks(hookEmptyAddr); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); manager.setProtocolFeeController(overflowFeeController); // expect initialize to succeed @@ -400,12 +370,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_succeedsWithWrongReturnSizeFeeController(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); - address hookEmptyAddr = EMPTY_HOOKS; - MockHooks impl = new MockHooks(); - vm.etch(hookEmptyAddr, address(impl).code); - MockHooks mockHooks = MockHooks(hookEmptyAddr); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); manager.setProtocolFeeController(invalidReturnSizeFeeController); // expect initialize to succeed @@ -427,10 +392,7 @@ contract PoolManagerInitializeTest is Test, Deployers, GasSnapshot { function test_initialize_succeedsAndSetsHookFeeIfControllerReverts(uint160 sqrtPriceX96) public { // Assumptions tested in Pool.t.sol - vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO); - vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO); - - address payable mockAddr = payable(address(uint160(Hooks.BEFORE_INITIALIZE_FLAG | Hooks.AFTER_INITIALIZE_FLAG))); + sqrtPriceX96 = uint160(bound(sqrtPriceX96, TickMath.MIN_SQRT_RATIO, TickMath.MAX_SQRT_RATIO - 1)); address hookAddr = address(99); // can't be a zero address, but does not have to have any other hook flags specified MockHooks impl = new MockHooks(); diff --git a/test/Tick.t.sol b/test/Tick.t.sol index 0972124d3..565572ddb 100644 --- a/test/Tick.t.sol +++ b/test/Tick.t.sol @@ -480,8 +480,7 @@ contract TickTest is Test, GasSnapshot { } function testTick_tickSpacingToParametersInvariants_fuzz(int24 tickSpacing) public { - vm.assume(tickSpacing <= TickMath.MAX_TICK_SPACING); - vm.assume(tickSpacing >= TickMath.MIN_TICK_SPACING); + tickSpacing = int24(bound(tickSpacing, TickMath.MIN_TICK_SPACING, TickMath.MAX_TICK_SPACING)); int24 minTick = (TickMath.MIN_TICK / tickSpacing) * tickSpacing; int24 maxTick = (TickMath.MAX_TICK / tickSpacing) * tickSpacing; diff --git a/test/types/BalanceDelta.t.sol b/test/types/BalanceDelta.t.sol index 06534b139..aafe83f2e 100644 --- a/test/types/BalanceDelta.t.sol +++ b/test/types/BalanceDelta.t.sol @@ -38,8 +38,9 @@ contract TestBalanceDelta is Test { int256 bd = int256(b) + d; // make sure the addition doesn't overflow - vm.assume(ac == int128(ac)); - vm.assume(bd == int128(bd)); + if (ac != int128(ac) || bd != int128(bd)) { + vm.expectRevert(); + } BalanceDelta balanceDelta = toBalanceDelta(a, b) + toBalanceDelta(c, d); assertEq(balanceDelta.amount0(), ac);