Skip to content

Commit

Permalink
Fixed PoolModifyPositionTest and some more breaking tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rileydcampbell committed Dec 7, 2023
1 parent 3fd4079 commit 7ec6d8d
Show file tree
Hide file tree
Showing 31 changed files with 499 additions and 125 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
319666
320115
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
202489
202901
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
202409
202847
Original file line number Diff line number Diff line change
@@ -1 +1 @@
193153
193154
2 changes: 1 addition & 1 deletion .forge-snapshots/cached dynamic fee, no hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148516
148495
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 1 token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139130
139131
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 2 tokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186552
186576
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
78675
78673
2 changes: 1 addition & 1 deletion .forge-snapshots/mintWithEmptyHookEOAInitiated.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
250947
251882
2 changes: 1 addition & 1 deletion .forge-snapshots/modify position with noop.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
56515
56828
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25626
26246
1 change: 1 addition & 0 deletions .forge-snapshots/removeLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
105888
1 change: 1 addition & 0 deletions .forge-snapshots/removeLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
211271
1 change: 1 addition & 0 deletions .forge-snapshots/removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
207538
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap with native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
197401
197380
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
205969
205948
2 changes: 1 addition & 1 deletion .forge-snapshots/simpleSwapEOAInitiated.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177213
177214
2 changes: 1 addition & 1 deletion .forge-snapshots/simpleSwapNativeEOAInitiated.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
173875
173876
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127994
127973
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115485
115464
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn claim for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134557
134513
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint output as claim.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
218140
218141
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192395
192396
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115463
115442
2 changes: 1 addition & 1 deletion .forge-snapshots/update dynamic fee in before swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
198966
198967
2 changes: 1 addition & 1 deletion src/libraries/Hooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {FeeLibrary} from "../libraries/FeeLibrary.sol";
/// @notice V4 decides whether to invoke specific hooks by inspecting the leading bits of the address that
/// the hooks contract is deployed to.
/// For example, a hooks contract deployed to address: 0x9000000000000000000000000000000000000000
/// has leading bits '1001' which would cause the 'before initialize' and 'after modify position' hooks to be used.
/// has leading bits '1001' which would cause the 'before initialize' and 'after add liquidity' hooks to be used.
library Hooks {
using FeeLibrary for uint24;

Expand Down
9 changes: 9 additions & 0 deletions src/test/AccessLockHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ contract AccessLockHook is Test, BaseTestHooks {
return _executeAction(key, hookData, IHooks.beforeAddLiquidity.selector);
}

function beforeRemoveLiquidity(
address, /* sender **/
PoolKey calldata key,
IPoolManager.ModifyPositionParams calldata, /* params **/
bytes calldata hookData
) external override returns (bytes4) {
return _executeAction(key, hookData, IHooks.beforeRemoveLiquidity.selector);
}

function _executeAction(PoolKey memory key, bytes calldata hookData, bytes4 selector) internal returns (bytes4) {
if (hookData.length == 0) {
// We have re-entered the hook or we are initializing liquidity in the pool before testing the lock actions.
Expand Down
27 changes: 18 additions & 9 deletions src/test/PoolModifyPositionTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,46 @@ contract PoolModifyPositionTest is Test, PoolTestBase {
using Hooks for IHooks;
using FeeLibrary for uint24;

enum LockAction {
AddLiquidity,
RemoveLiquidity
}

constructor(IPoolManager _manager) PoolTestBase(_manager) {}

struct CallbackData {
address sender;
PoolKey key;
IPoolManager.ModifyPositionParams params;
bytes hookData;
LockAction action;
}

function removeLiquidity(PoolKey memory key, IPoolManager.ModifyPositionParams memory params, bytes memory hookData)
external
payable
returns (BalanceDelta delta)
{
delta = _modifyPosition(key, params, hookData);
delta = _modifyPosition(key, params, hookData, LockAction.RemoveLiquidity);
}

function addLiquidity(PoolKey memory key, IPoolManager.ModifyPositionParams memory params, bytes memory hookData)
external
payable
returns (BalanceDelta delta)
{
delta = _modifyPosition(key, params, hookData);
delta = _modifyPosition(key, params, hookData, LockAction.AddLiquidity);
}

function _modifyPosition(PoolKey memory key, IPoolManager.ModifyPositionParams memory params, bytes memory hookData)
internal
returns (BalanceDelta delta)
{
function _modifyPosition(
PoolKey memory key,
IPoolManager.ModifyPositionParams memory params,
bytes memory hookData,
LockAction action
) internal returns (BalanceDelta delta) {
delta = abi.decode(
manager.lock(address(this), abi.encode(CallbackData(msg.sender, key, params, hookData))), (BalanceDelta)
manager.lock(address(this), abi.encode(CallbackData(msg.sender, key, params, hookData, action))),
(BalanceDelta)
);

uint256 ethBalance = address(this).balance;
Expand All @@ -61,7 +70,7 @@ contract PoolModifyPositionTest is Test, PoolTestBase {
CallbackData memory data = abi.decode(rawData, (CallbackData));

BalanceDelta delta;
if (data.params.liquidityDelta >= 0) {
if (data.action == LockAction.AddLiquidity) {
delta = manager.addLiquidity(data.key, data.params, data.hookData);
} else {
delta = manager.removeLiquidity(data.key, data.params, data.hookData);
Expand All @@ -75,7 +84,7 @@ contract PoolModifyPositionTest is Test, PoolTestBase {

// These assertions only apply in non lock-accessing pools.
if (!data.key.hooks.hasPermissionToAccessLock()) {
if (data.params.liquidityDelta > 0) {
if (data.action == LockAction.AddLiquidity) {
require(delta0 > 0 || delta1 > 0 || data.key.hooks.hasPermissionToNoOp(), "assert 1 failed");
require(!(delta0 < 0 || delta1 < 0), "assert 2 failed");
} else {
Expand Down
Loading

0 comments on commit 7ec6d8d

Please sign in to comment.