Skip to content

Commit

Permalink
Before after add/remove liquidity, remove all modifyPosition hooks (U…
Browse files Browse the repository at this point in the history
…niswap#434)

* Add before/after add/remove liquidity hooks

---------

Co-authored-by: Eric Zhong <[email protected]>
  • Loading branch information
2 people authored and hyunchel committed Feb 21, 2024
1 parent 731557d commit f07fcd3
Show file tree
Hide file tree
Showing 51 changed files with 1,034 additions and 440 deletions.
1 change: 1 addition & 0 deletions .forge-snapshots/addLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
323249
1 change: 1 addition & 0 deletions .forge-snapshots/addLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
200141
1 change: 1 addition & 0 deletions .forge-snapshots/addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
200109
Original file line number Diff line number Diff line change
@@ -1 +1 @@
194519
194300
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 @@
146864
146667
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 @@
137968
137771
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 @@
185390
185216
2 changes: 1 addition & 1 deletion .forge-snapshots/erc20 collect protocol fees.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26968
27033
2 changes: 1 addition & 1 deletion .forge-snapshots/gas overhead of no-op lock.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15246
15224
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
74268
74200
2 changes: 1 addition & 1 deletion .forge-snapshots/mintWithEmptyHookEOAInitiated.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
255158
255129
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 @@
58201
58013
2 changes: 1 addition & 1 deletion .forge-snapshots/native collect protocol fees.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
38634
38699
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23592
23823
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 @@
106962
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 @@
208322
1 change: 1 addition & 0 deletions .forge-snapshots/removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
204612
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 @@
195835
195638
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
204403
204206
2 changes: 1 addition & 1 deletion .forge-snapshots/simpleSwapEOAInitiated.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175669
175475
2 changes: 1 addition & 1 deletion .forge-snapshots/simpleSwapNativeEOAInitiated.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
172331
172131
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126463
126266
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
113954
113757
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 @@
133127
132930
1 change: 1 addition & 0 deletions .forge-snapshots/swap mint 1155 as output.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
169598
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 @@
216596
216399
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 @@
193620
193401
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
113932
113735
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with noop.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
51546
51304
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 @@
200363
200144
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Only the net balances owed to the pool (positive) or to the user (negative) are
Additionally, a pool may be initialized with a hook contract, that can implement any of the following callbacks in the lifecycle of pool actions:

- {before,after}Initialize
- {before,after}ModifyPosition
- {before,after}AddLiquidity
- {before,after}RemoveLiquidity
- {before,after}Swap
- {before,after}Donate

Expand Down
3 changes: 2 additions & 1 deletion docs/latex/main-zh.tex
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ \subsection{操作Hooks}

\begin{itemize}
\item beforeInitialize/afterInitialize
\item beforeModifyPosition/afterModifyPosition
\item beforeAddLiquidity/afterAddLiquidity
\item beforeRemoveLiquidity/afterRemoveLiquidity
\item beforeSwap/afterSwap
\item beforeDonate/afterDonate
\end{itemize}
Expand Down
3 changes: 2 additions & 1 deletion docs/latex/main.tex
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ \subsection{Action Hooks} \label{actionhooks}

\begin{itemize}
\item beforeInitialize/afterInitialize
\item beforeModifyPosition/afterModifyPosition
\item beforeAddLiquidity/afterAddLiquidity
\item beforeRemoveLiquidity/afterRemoveLiquidity
\item beforeSwap/afterSwap
\item beforeDonate/afterDonate
\end{itemize}
Expand Down
2 changes: 1 addition & 1 deletion lib/forge-gas-snapshot
10 changes: 5 additions & 5 deletions src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, Claims {
}

/// @inheritdoc IPoolManager
function modifyPosition(
function modifyLiquidity(
PoolKey memory key,
IPoolManager.ModifyPositionParams memory params,
IPoolManager.ModifyLiquidityParams memory params,
bytes calldata hookData
) external override noDelegateCall onlyByLocker returns (BalanceDelta delta) {
PoolId id = key.toId();
_checkPoolInitialized(id);

if (!key.hooks.beforeModifyPosition(key, params, hookData)) {
if (!key.hooks.beforeModifyLiquidity(key, params, hookData)) {
return BalanceDeltaLibrary.MAXIMUM_DELTA;
}

Expand All @@ -201,9 +201,9 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, Claims {

_accountPoolBalanceDelta(key, delta);

key.hooks.afterModifyPosition(key, params, delta, hookData);
key.hooks.afterModifyLiquidity(key, params, delta, hookData);

emit ModifyPosition(id, msg.sender, params.tickLower, params.tickUpper, params.liquidityDelta);
emit ModifyLiquidity(id, msg.sender, params.tickLower, params.tickUpper, params.liquidityDelta);
}

/// @inheritdoc IPoolManager
Expand Down
47 changes: 37 additions & 10 deletions src/interfaces/IHooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,56 @@ interface IHooks {
bytes calldata hookData
) external returns (bytes4);

/// @notice The hook called before a position is modified
/// @param sender The initial msg.sender for the modify position call
/// @notice The hook called before liquidity is added
/// @param sender The initial msg.sender for the add liquidity call
/// @param key The key for the pool
/// @param params The parameters for modifying the position
/// @param params The parameters for adding liquidity
/// @param hookData Arbitrary data handed into the PoolManager by the liquidty provider to be be passed on to the hook
/// @return bytes4 The function selector for the hook
function beforeModifyPosition(
function beforeAddLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyPositionParams calldata params,
IPoolManager.ModifyLiquidityParams calldata params,
bytes calldata hookData
) external returns (bytes4);

/// @notice The hook called after a position is modified
/// @param sender The initial msg.sender for the modify position call
/// @notice The hook called after liquidity is added
/// @param sender The initial msg.sender for the add liquidity call
/// @param key The key for the pool
/// @param params The parameters for modifying the position
/// @param params The parameters for adding liquidity
/// @param hookData Arbitrary data handed into the PoolManager by the liquidty provider to be be passed on to the hook
/// @return bytes4 The function selector for the hook
function afterModifyPosition(
function afterAddLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyPositionParams calldata params,
IPoolManager.ModifyLiquidityParams calldata params,
BalanceDelta delta,
bytes calldata hookData
) external returns (bytes4);

/// @notice The hook called before liquidity is removed
/// @param sender The initial msg.sender for the remove liquidity call
/// @param key The key for the pool
/// @param params The parameters for removing liquidity
/// @param hookData Arbitrary data handed into the PoolManager by the liquidty provider to be be passed on to the hook
/// @return bytes4 The function selector for the hook
function beforeRemoveLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
bytes calldata hookData
) external returns (bytes4);

/// @notice The hook called after liquidity is removed
/// @param sender The initial msg.sender for the remove liquidity call
/// @param key The key for the pool
/// @param params The parameters for removing liquidity
/// @param hookData Arbitrary data handed into the PoolManager by the liquidty provider to be be passed on to the hook
/// @return bytes4 The function selector for the hook
function afterRemoveLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
BalanceDelta delta,
bytes calldata hookData
) external returns (bytes4);
Expand Down
13 changes: 9 additions & 4 deletions src/interfaces/IPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface IPoolManager is IFees, IClaims {
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param liquidityDelta The amount of liquidity that was added or removed
event ModifyPosition(
event ModifyLiquidity(
PoolId indexed id, address indexed sender, int24 tickLower, int24 tickUpper, int256 liquidityDelta
);

Expand Down Expand Up @@ -147,16 +147,21 @@ interface IPoolManager is IFees, IClaims {
/// @return The data returned by the call to `ILockCallback(msg.sender).lockAcquired(data)`
function lock(address lockTarget, bytes calldata data) external payable returns (bytes memory);

struct ModifyPositionParams {
struct ModifyLiquidityParams {
// the lower and upper tick of the position
int24 tickLower;
int24 tickUpper;
// how to modify the liquidity
int256 liquidityDelta;
}

/// @notice Modify the position for the given pool
function modifyPosition(PoolKey memory key, ModifyPositionParams memory params, bytes calldata hookData)
/// @notice Modify the liquidity for the given pool
/// @dev Poke by calling with a zero liquidityDelta
/// @param key The pool to modify liquidity in
/// @param params The parameters for modifying the liquidity
/// @param hookData Any data to pass to the callback, via `ILockCallback(msg.sender).lockAcquired(data)`
/// @return delta The balance delta of the liquidity
function modifyLiquidity(PoolKey memory key, ModifyLiquidityParams memory params, bytes calldata hookData)
external
returns (BalanceDelta);

Expand Down
Loading

0 comments on commit f07fcd3

Please sign in to comment.