Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rileydcampbell committed Dec 12, 2023
1 parent 832e6b1 commit 3912e4a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 72 deletions.
11 changes: 5 additions & 6 deletions src/test/PoolModifyPositionTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ contract PoolModifyPositionTest is Test, PoolTestBase {

CallbackData memory data = abi.decode(rawData, (CallbackData));

BalanceDelta delta;
delta = manager.modifyPosition(data.key, data.params, data.hookData);
BalanceDelta delta = manager.modifyPosition(data.key, data.params, data.hookData);

// Checks that the current hook is cleared if there is an access lock. Note that if this router is ever used in a nested lock this will fail.
assertEq(address(manager.getCurrentHook()), address(0));
Expand All @@ -57,11 +56,11 @@ contract PoolModifyPositionTest is Test, PoolTestBase {
// These assertions only apply in non lock-accessing pools.
if (!data.key.hooks.hasPermission(Hooks.ACCESS_LOCK_FLAG)) {
if (data.params.liquidityDelta >= 0) {
require(delta0 > 0 || delta1 > 0 || data.key.hooks.hasPermission(Hooks.NO_OP_FLAG), "assert 1 failed");
require(!(delta0 < 0 || delta1 < 0), "assert 2 failed");
assert(delta0 > 0 || delta1 > 0 || data.key.hooks.hasPermission(Hooks.NO_OP_FLAG));
assert(!(delta0 < 0 || delta1 < 0));
} else {
require(delta0 < 0 || delta1 < 0 || data.key.hooks.hasPermission(Hooks.NO_OP_FLAG), "assert 3 failed");
require(!(delta0 > 0 || delta1 > 0), "assert 4 failed");
assert(delta0 < 0 || delta1 < 0 || data.key.hooks.hasPermission(Hooks.NO_OP_FLAG));
assert(!(delta0 > 0 || delta1 > 0));
}
}

Expand Down
44 changes: 8 additions & 36 deletions test/AccessLock.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -241,37 +241,13 @@ contract AccessLockTest is Test, Deployers {
assertGt(balanceOfAfter1, balanceOfBefore1);
}

function test_beforeRemoveLiquidity_swap_succeedsWithAccessLock() public {
// Add liquidity so there is something to swap over.
modifyPositionRouter.modifyPosition(
key,
IPoolManager.ModifyPositionParams({tickLower: -120, tickUpper: 120, liquidityDelta: 100 * 10e18}),
ZERO_BYTES
);

uint256 balanceOfBefore1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this));
uint256 balanceOfBefore0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this));

// Essentially "no-op"s the modifyLiquidity call and executes a swap before hand, applying the deltas from the swap to the locker.
modifyPositionRouter.modifyPosition(
key, IPoolManager.ModifyPositionParams(-120, 120, 0), abi.encode(amount, AccessLockHook.LockAction.Swap)
);
uint256 balanceOfAfter0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this));
uint256 balanceOfAfter1 = MockERC20(Currency.unwrap(currency1)).balanceOf(address(this));

// Balance decreases because we are swapping currency0 for currency1.
assertLt(balanceOfAfter0, balanceOfBefore0);
// Balance should be greater in currency1.
assertGt(balanceOfAfter1, balanceOfBefore1);
}

function test_beforeAddLiquidity_addLiquidity_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, 10e18),
abi.encode(amount, AccessLockHook.LockAction.ModifyPosition)
);
uint256 balanceOfAfter0 = MockERC20(Currency.unwrap(currency0)).balanceOf(address(this));
Expand Down Expand Up @@ -356,9 +332,7 @@ contract AccessLockTest is Test, Deployers {
function test_beforeAddLiquidity_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({
Expand Down Expand Up @@ -604,9 +578,7 @@ contract AccessLockTest is Test, Deployers {
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));
Expand Down Expand Up @@ -842,7 +814,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)
);
}

Expand All @@ -854,18 +826,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), 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 {
Expand All @@ -886,7 +858,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
);
}

Expand Down
40 changes: 20 additions & 20 deletions test/Hooks.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ contract HooksTest is Test, Deployers, GasSnapshot {
}

function test_beforeAfterAddLiquidity_beforeAfterRemoveLiquidity_succeedsWithHook() public {
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 10 ** 18);
modifyPositionRouter.modifyPosition(key, IPoolManager.ModifyPositionParams(0, 60, 10 ** 18), new bytes(111));
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 1e18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 1e18);
modifyPositionRouter.modifyPosition(key, IPoolManager.ModifyPositionParams(0, 60, 1e18), new bytes(111));
assertEq(mockHooks.beforeAddLiquidityData(), new bytes(111));
assertEq(mockHooks.afterAddLiquidityData(), new bytes(111));

Expand All @@ -77,17 +77,17 @@ contract HooksTest is Test, Deployers, GasSnapshot {
}

function test_beforeAfterAddLiquidity_calledWithPositiveLiquidityDelta() public {
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 1e18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 1e18);
modifyPositionRouter.modifyPosition(key, IPoolManager.ModifyPositionParams(0, 60, 100), new bytes(111));
assertEq(mockHooks.beforeAddLiquidityData(), new bytes(111));
assertEq(mockHooks.afterAddLiquidityData(), new bytes(111));
}

function test_beforeAfterAddLiquidity_calledWithZeroLiquidityDelta() public {
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 10 ** 18);
modifyPositionRouter.modifyPosition(key, IPoolManager.ModifyPositionParams(0, 60, 10 ** 18), new bytes(111));
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 1e18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 1e18);
modifyPositionRouter.modifyPosition(key, IPoolManager.ModifyPositionParams(0, 60, 1e18), new bytes(111));
assertEq(mockHooks.beforeAddLiquidityData(), new bytes(111));
assertEq(mockHooks.afterAddLiquidityData(), new bytes(111));

Expand All @@ -97,43 +97,43 @@ contract HooksTest is Test, Deployers, GasSnapshot {
}

function test_beforeAfterRemoveLiquidity_calledWithPositiveLiquidityDelta() public {
modifyPositionRouter.modifyPosition(key, IPoolManager.ModifyPositionParams(0, 60, 10 ** 18), new bytes(111));
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 10 ** 18);
modifyPositionRouter.modifyPosition(key, IPoolManager.ModifyPositionParams(0, 60, 1e18), new bytes(111));
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 1e18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 1e18);
modifyPositionRouter.modifyPosition(key, IPoolManager.ModifyPositionParams(0, 60, -1e18), new bytes(111));
assertEq(mockHooks.beforeRemoveLiquidityData(), new bytes(111));
assertEq(mockHooks.afterRemoveLiquidityData(), new bytes(111));
}

function test_beforeAddLiquidity_invalidReturn() public {
mockHooks.setReturnValue(mockHooks.beforeAddLiquidity.selector, bytes4(0xdeadbeef));
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 1e18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 1e18);
vm.expectRevert(Hooks.InvalidHookResponse.selector);
modifyPositionRouter.modifyPosition(key, LIQ_PARAMS, ZERO_BYTES);
}

function test_beforeRemoveLiquidity_invalidReturn() public {
mockHooks.setReturnValue(mockHooks.beforeRemoveLiquidity.selector, bytes4(0xdeadbeef));
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 1e18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 1e18);
modifyPositionRouter.modifyPosition(key, LIQ_PARAMS, ZERO_BYTES);
vm.expectRevert(Hooks.InvalidHookResponse.selector);
modifyPositionRouter.modifyPosition(key, REMOVE_LIQ_PARAMS, ZERO_BYTES);
}

function test_afterAddLiquidity_invalidReturn() public {
mockHooks.setReturnValue(mockHooks.afterAddLiquidity.selector, bytes4(0xdeadbeef));
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 1e18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 1e18);
vm.expectRevert(Hooks.InvalidHookResponse.selector);
modifyPositionRouter.modifyPosition(key, LIQ_PARAMS, ZERO_BYTES);
}

function test_afterRemoveLiquidity_invalidReturn() public {
mockHooks.setReturnValue(mockHooks.afterRemoveLiquidity.selector, bytes4(0xdeadbeef));
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 10 ** 18);
MockERC20(Currency.unwrap(key.currency0)).mint(address(this), 1e18);
MockERC20(Currency.unwrap(key.currency0)).approve(address(modifyPositionRouter), 1e18);
modifyPositionRouter.modifyPosition(key, LIQ_PARAMS, ZERO_BYTES);
vm.expectRevert(Hooks.InvalidHookResponse.selector);
modifyPositionRouter.modifyPosition(key, REMOVE_LIQ_PARAMS, ZERO_BYTES);
Expand Down Expand Up @@ -174,7 +174,7 @@ contract HooksTest is Test, Deployers, GasSnapshot {
}

function test_donate_succeedsWithHook() public {
donateRouter.donate(key, 10, 200, new bytes(333));
donateRouter.donate(key, 100, 200, new bytes(333));
assertEq(mockHooks.beforeDonateData(), new bytes(333));
assertEq(mockHooks.afterDonateData(), new bytes(333));
}
Expand Down
20 changes: 10 additions & 10 deletions test/PoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {
address(modifyPositionRouter),
LIQ_PARAMS.tickLower,
LIQ_PARAMS.tickUpper,
int128(int256(LIQ_PARAMS.liquidityDelta))
LIQ_PARAMS.liquidityDelta
);

modifyPositionRouter.modifyPosition(key, LIQ_PARAMS, ZERO_BYTES);
Expand All @@ -109,9 +109,9 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {
emit ModifyPosition(
key.toId(),
address(modifyPositionRouter),
LIQ_PARAMS.tickLower,
LIQ_PARAMS.tickUpper,
-int128(int256(LIQ_PARAMS.liquidityDelta))
REMOVE_LIQ_PARAMS.tickLower,
REMOVE_LIQ_PARAMS.tickUpper,
REMOVE_LIQ_PARAMS.liquidityDelta
);

modifyPositionRouter.modifyPosition(key, REMOVE_LIQ_PARAMS, ZERO_BYTES);
Expand All @@ -126,7 +126,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {
address(modifyPositionRouter),
LIQ_PARAMS.tickLower,
LIQ_PARAMS.tickUpper,
int128(int256(LIQ_PARAMS.liquidityDelta))
LIQ_PARAMS.liquidityDelta
);

modifyPositionRouter.modifyPosition{value: 1 ether}(nativeKey, LIQ_PARAMS, ZERO_BYTES);
Expand All @@ -139,9 +139,9 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {
emit ModifyPosition(
nativeKey.toId(),
address(modifyPositionRouter),
LIQ_PARAMS.tickLower,
LIQ_PARAMS.tickUpper,
-int128(int256(LIQ_PARAMS.liquidityDelta))
REMOVE_LIQ_PARAMS.tickLower,
REMOVE_LIQ_PARAMS.tickUpper,
REMOVE_LIQ_PARAMS.liquidityDelta
);

modifyPositionRouter.modifyPosition{value: 1 ether}(nativeKey, REMOVE_LIQ_PARAMS, ZERO_BYTES);
Expand Down Expand Up @@ -267,7 +267,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {
address(modifyPositionRouter),
LIQ_PARAMS.tickLower,
LIQ_PARAMS.tickUpper,
int128(int256(LIQ_PARAMS.liquidityDelta))
LIQ_PARAMS.liquidityDelta
);

modifyPositionRouter.modifyPosition(key, LIQ_PARAMS, ZERO_BYTES);
Expand All @@ -292,7 +292,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {
address(modifyPositionRouter),
REMOVE_LIQ_PARAMS.tickLower,
REMOVE_LIQ_PARAMS.tickUpper,
int128(REMOVE_LIQ_PARAMS.liquidityDelta)
REMOVE_LIQ_PARAMS.liquidityDelta
);

modifyPositionRouter.modifyPosition(key, REMOVE_LIQ_PARAMS, ZERO_BYTES);
Expand Down

0 comments on commit 3912e4a

Please sign in to comment.