-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Hooks.sol
269 lines (240 loc) Β· 11.6 KB
/
Hooks.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {PoolKey} from "../types/PoolKey.sol";
import {IHooks} from "../interfaces/IHooks.sol";
import {FeeLibrary} from "./FeeLibrary.sol";
import {BalanceDelta} from "../types/BalanceDelta.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";
import {Lockers} from "./Lockers.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 add liquidity' hooks to be used.
library Hooks {
using FeeLibrary for uint24;
using Hooks for IHooks;
uint256 internal constant BEFORE_INITIALIZE_FLAG = 1 << 159;
uint256 internal constant AFTER_INITIALIZE_FLAG = 1 << 158;
uint256 internal constant BEFORE_ADD_LIQUIDITY_FLAG = 1 << 157;
uint256 internal constant AFTER_ADD_LIQUIDITY_FLAG = 1 << 156;
uint256 internal constant BEFORE_REMOVE_LIQUIDITY_FLAG = 1 << 155;
uint256 internal constant AFTER_REMOVE_LIQUIDITY_FLAG = 1 << 154;
uint256 internal constant BEFORE_SWAP_FLAG = 1 << 153;
uint256 internal constant AFTER_SWAP_FLAG = 1 << 152;
uint256 internal constant BEFORE_DONATE_FLAG = 1 << 151;
uint256 internal constant AFTER_DONATE_FLAG = 1 << 150;
uint256 internal constant NO_OP_FLAG = 1 << 149;
uint256 internal constant ACCESS_LOCK_FLAG = 1 << 148;
bytes4 public constant NO_OP_SELECTOR = bytes4(keccak256(abi.encodePacked("NoOp")));
struct Permissions {
bool beforeInitialize;
bool afterInitialize;
bool beforeAddLiquidity;
bool afterAddLiquidity;
bool beforeRemoveLiquidity;
bool afterRemoveLiquidity;
bool beforeSwap;
bool afterSwap;
bool beforeDonate;
bool afterDonate;
bool noOp;
bool accessLock;
}
/// @notice Thrown if the address will not lead to the specified hook calls being called
/// @param hooks The address of the hooks contract
error HookAddressNotValid(address hooks);
/// @notice Hook did not return its selector
error InvalidHookResponse();
/// @notice thrown when a hook call fails
error FailedHookCall();
/// @notice Utility function intended to be used in hook constructors to ensure
/// the deployed hooks address causes the intended hooks to be called
/// @param permissions The hooks that are intended to be called
/// @dev permissions param is memory as the function will be called from constructors
function validateHookPermissions(IHooks self, Permissions memory permissions) internal pure {
if (
permissions.beforeInitialize != self.hasPermission(BEFORE_INITIALIZE_FLAG)
|| permissions.afterInitialize != self.hasPermission(AFTER_INITIALIZE_FLAG)
|| permissions.beforeAddLiquidity != self.hasPermission(BEFORE_ADD_LIQUIDITY_FLAG)
|| permissions.afterAddLiquidity != self.hasPermission(AFTER_ADD_LIQUIDITY_FLAG)
|| permissions.beforeRemoveLiquidity != self.hasPermission(BEFORE_REMOVE_LIQUIDITY_FLAG)
|| permissions.afterRemoveLiquidity != self.hasPermission(AFTER_REMOVE_LIQUIDITY_FLAG)
|| permissions.beforeSwap != self.hasPermission(BEFORE_SWAP_FLAG)
|| permissions.afterSwap != self.hasPermission(AFTER_SWAP_FLAG)
|| permissions.beforeDonate != self.hasPermission(BEFORE_DONATE_FLAG)
|| permissions.afterDonate != self.hasPermission(AFTER_DONATE_FLAG)
|| permissions.noOp != self.hasPermission(NO_OP_FLAG)
|| permissions.accessLock != self.hasPermission(ACCESS_LOCK_FLAG)
) {
revert HookAddressNotValid(address(self));
}
}
/// @notice Ensures that the hook address includes at least one hook flag or dynamic fees, or is the 0 address
/// @param hook The hook to verify
function isValidHookAddress(IHooks hook, uint24 fee) internal pure returns (bool) {
// if NoOp is allowed, at least one of beforeRemoveLiquidity, beforeAddLiquidity, beforeSwap and beforeDonate should be allowed
if (
hook.hasPermission(NO_OP_FLAG) && !hook.hasPermission(BEFORE_ADD_LIQUIDITY_FLAG)
&& !hook.hasPermission(BEFORE_REMOVE_LIQUIDITY_FLAG) && !hook.hasPermission(BEFORE_SWAP_FLAG)
&& !hook.hasPermission(BEFORE_DONATE_FLAG)
) {
return false;
}
// If there is no hook contract set, then fee cannot be dynamic
// If a hook contract is set, it must have at least 1 flag set, or have a dynamic fee
return address(hook) == address(0)
? !fee.isDynamicFee()
: (uint160(address(hook)) >= ACCESS_LOCK_FLAG || fee.isDynamicFee());
}
/// @notice performs a hook call using the given calldata on the given hook
/// @return expectedSelector The selector that the hook is expected to return
/// @return selector The selector that the hook actually returned
function _callHook(IHooks self, bytes memory data) private returns (bytes4 expectedSelector, bytes4 selector) {
bool set = Lockers.setCurrentHook(self);
assembly {
expectedSelector := mload(add(data, 0x20))
}
(bool success, bytes memory result) = address(self).call(data);
if (!success) _revert(result);
selector = abi.decode(result, (bytes4));
// We only want to clear the current hook if it was set in setCurrentHook in this execution frame.
if (set) Lockers.clearCurrentHook();
}
/// @notice performs a hook call using the given calldata on the given hook
function callHook(IHooks self, bytes memory data) internal {
(bytes4 expectedSelector, bytes4 selector) = _callHook(self, data);
if (selector != expectedSelector) {
revert InvalidHookResponse();
}
}
/// @notice performs a hook call using the given calldata on the given hook
/// @return shouldExecute Whether the operation should be executed or nooped
function callHookNoopable(IHooks self, bytes memory data) internal returns (bool shouldExecute) {
(bytes4 expectedSelector, bytes4 selector) = _callHook(self, data);
if (selector == expectedSelector) {
shouldExecute = true;
} else if (selector == NO_OP_SELECTOR && self.hasPermission(NO_OP_FLAG)) {
shouldExecute = false;
} else {
revert InvalidHookResponse();
}
}
/// @notice calls beforeInitialize hook if permissioned and validates return value
function beforeInitialize(IHooks self, PoolKey memory key, uint160 sqrtPriceX96, bytes calldata hookData)
internal
{
if (self.hasPermission(BEFORE_INITIALIZE_FLAG)) {
self.callHook(
abi.encodeWithSelector(IHooks.beforeInitialize.selector, msg.sender, key, sqrtPriceX96, hookData)
);
}
}
/// @notice calls afterInitialize hook if permissioned and validates return value
function afterInitialize(IHooks self, PoolKey memory key, uint160 sqrtPriceX96, int24 tick, bytes calldata hookData)
internal
{
if (self.hasPermission(AFTER_INITIALIZE_FLAG)) {
self.callHook(
abi.encodeWithSelector(IHooks.afterInitialize.selector, msg.sender, key, sqrtPriceX96, tick, hookData)
);
}
}
/// @notice calls beforeModifyLiquidity hook if permissioned and validates return value
function beforeModifyLiquidity(
IHooks self,
PoolKey memory key,
IPoolManager.ModifyLiquidityParams memory params,
bytes calldata hookData
) internal returns (bool shouldExecute) {
if (params.liquidityDelta > 0 && key.hooks.hasPermission(BEFORE_ADD_LIQUIDITY_FLAG)) {
shouldExecute = self.callHookNoopable(
abi.encodeWithSelector(IHooks.beforeAddLiquidity.selector, msg.sender, key, params, hookData)
);
} else if (params.liquidityDelta <= 0 && key.hooks.hasPermission(BEFORE_REMOVE_LIQUIDITY_FLAG)) {
shouldExecute = self.callHookNoopable(
abi.encodeWithSelector(IHooks.beforeRemoveLiquidity.selector, msg.sender, key, params, hookData)
);
} else {
shouldExecute = true;
}
}
/// @notice calls afterModifyLiquidity hook if permissioned and validates return value
function afterModifyLiquidity(
IHooks self,
PoolKey memory key,
IPoolManager.ModifyLiquidityParams memory params,
BalanceDelta delta,
bytes calldata hookData
) internal {
if (params.liquidityDelta > 0 && key.hooks.hasPermission(AFTER_ADD_LIQUIDITY_FLAG)) {
self.callHook(
abi.encodeWithSelector(IHooks.afterAddLiquidity.selector, msg.sender, key, params, delta, hookData)
);
} else if (params.liquidityDelta <= 0 && key.hooks.hasPermission(AFTER_REMOVE_LIQUIDITY_FLAG)) {
self.callHook(
abi.encodeWithSelector(IHooks.afterRemoveLiquidity.selector, msg.sender, key, params, delta, hookData)
);
}
}
/// @notice calls beforeSwap hook if permissioned and validates return value
function beforeSwap(IHooks self, PoolKey memory key, IPoolManager.SwapParams memory params, bytes calldata hookData)
internal
returns (bool shouldExecute)
{
if (key.hooks.hasPermission(BEFORE_SWAP_FLAG)) {
shouldExecute = self.callHookNoopable(
abi.encodeWithSelector(IHooks.beforeSwap.selector, msg.sender, key, params, hookData)
);
} else {
return true;
}
}
/// @notice calls afterSwap hook if permissioned and validates return value
function afterSwap(
IHooks self,
PoolKey memory key,
IPoolManager.SwapParams memory params,
BalanceDelta delta,
bytes calldata hookData
) internal {
if (key.hooks.hasPermission(AFTER_SWAP_FLAG)) {
self.callHook(abi.encodeWithSelector(IHooks.afterSwap.selector, msg.sender, key, params, delta, hookData));
}
}
/// @notice calls beforeDonate hook if permissioned and validates return value
function beforeDonate(IHooks self, PoolKey memory key, uint256 amount0, uint256 amount1, bytes calldata hookData)
internal
returns (bool shouldExecute)
{
if (key.hooks.hasPermission(BEFORE_DONATE_FLAG)) {
shouldExecute = self.callHookNoopable(
abi.encodeWithSelector(IHooks.beforeDonate.selector, msg.sender, key, amount0, amount1, hookData)
);
} else {
return true;
}
}
/// @notice calls afterDonate hook if permissioned and validates return value
function afterDonate(IHooks self, PoolKey memory key, uint256 amount0, uint256 amount1, bytes calldata hookData)
internal
{
if (key.hooks.hasPermission(AFTER_DONATE_FLAG)) {
self.callHook(
abi.encodeWithSelector(IHooks.afterDonate.selector, msg.sender, key, amount0, amount1, hookData)
);
}
}
function hasPermission(IHooks self, uint256 flag) internal pure returns (bool) {
return uint256(uint160(address(self))) & flag != 0;
}
/// @notice bubble up revert if present. Else throw FailedHookCall
function _revert(bytes memory result) private pure {
if (result.length > 0) {
assembly {
revert(add(0x20, result), mload(result))
}
} else {
revert FailedHookCall();
}
}
}