-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathHooksConfigLib.sol
More file actions
489 lines (438 loc) · 21.3 KB
/
Copy pathHooksConfigLib.sol
File metadata and controls
489 lines (438 loc) · 21.3 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.24;
import { IVaultErrors } from "@balancer-labs/v3-interfaces/contracts/vault/IVaultErrors.sol";
import { IHooks } from "@balancer-labs/v3-interfaces/contracts/vault/IHooks.sol";
import "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol";
import { WordCodec } from "@balancer-labs/v3-solidity-utils/contracts/helpers/WordCodec.sol";
import { PoolConfigConst } from "./PoolConfigConst.sol";
/**
* @notice Helper functions to read and write the packed hook configuration flags stored in `_poolConfigBits`.
* @dev This library has two additional functions. `toHooksConfig` constructs a `HooksConfig` structure from the
* PoolConfig and the hooks contract address. Also, there are `call<hook>` functions that forward the arguments
* to the corresponding functions in the hook contract, then validate and return the results.
*
* Note that the entire configuration of each pool is stored in the `_poolConfigBits` mapping (one slot per pool).
* This includes the data in the `PoolConfig` struct, plus the data in the `HookFlags` struct. The layout (i.e.,
* offsets for each data field) is specified in `PoolConfigConst`.
*
* There are two libraries for interpreting these data. This one parses fields related to hooks, and also
* contains helpers for the struct building and hooks contract forwarding functions described above. `PoolConfigLib`
* contains helpers related to the non-hook-related flags, along with aggregate fee percentages and other data
* associated with pools.
*
* The `PoolData` struct contains the raw bitmap with the entire pool state (`PoolConfigBits`), plus the token
* configuration, scaling factors, and dynamic information such as current balances and rates.
*
* The hooks contract addresses themselves are stored in a separate `_hooksContracts` mapping.
*/
library HooksConfigLib {
using WordCodec for bytes32;
using HooksConfigLib for PoolConfigBits;
function enableHookAdjustedAmounts(PoolConfigBits config) internal pure returns (bool) {
return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.ENABLE_HOOK_ADJUSTED_AMOUNTS_OFFSET);
}
function setHookAdjustedAmounts(PoolConfigBits config, bool value) internal pure returns (PoolConfigBits) {
return
PoolConfigBits.wrap(
PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.ENABLE_HOOK_ADJUSTED_AMOUNTS_OFFSET)
);
}
function shouldCallBeforeInitialize(PoolConfigBits config) internal pure returns (bool) {
return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.BEFORE_INITIALIZE_OFFSET);
}
function setShouldCallBeforeInitialize(PoolConfigBits config, bool value) internal pure returns (PoolConfigBits) {
return
PoolConfigBits.wrap(
PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.BEFORE_INITIALIZE_OFFSET)
);
}
function shouldCallAfterInitialize(PoolConfigBits config) internal pure returns (bool) {
return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.AFTER_INITIALIZE_OFFSET);
}
function setShouldCallAfterInitialize(PoolConfigBits config, bool value) internal pure returns (PoolConfigBits) {
return
PoolConfigBits.wrap(
PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.AFTER_INITIALIZE_OFFSET)
);
}
function shouldCallComputeDynamicSwapFee(PoolConfigBits config) internal pure returns (bool) {
return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.DYNAMIC_SWAP_FEE_OFFSET);
}
function setShouldCallComputeDynamicSwapFee(
PoolConfigBits config,
bool value
) internal pure returns (PoolConfigBits) {
return
PoolConfigBits.wrap(
PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.DYNAMIC_SWAP_FEE_OFFSET)
);
}
function shouldCallBeforeSwap(PoolConfigBits config) internal pure returns (bool) {
return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.BEFORE_SWAP_OFFSET);
}
function setShouldCallBeforeSwap(PoolConfigBits config, bool value) internal pure returns (PoolConfigBits) {
return PoolConfigBits.wrap(PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.BEFORE_SWAP_OFFSET));
}
function shouldCallAfterSwap(PoolConfigBits config) internal pure returns (bool) {
return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.AFTER_SWAP_OFFSET);
}
function setShouldCallAfterSwap(PoolConfigBits config, bool value) internal pure returns (PoolConfigBits) {
return PoolConfigBits.wrap(PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.AFTER_SWAP_OFFSET));
}
function shouldCallBeforeAddLiquidity(PoolConfigBits config) internal pure returns (bool) {
return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.BEFORE_ADD_LIQUIDITY_OFFSET);
}
function setShouldCallBeforeAddLiquidity(PoolConfigBits config, bool value) internal pure returns (PoolConfigBits) {
return
PoolConfigBits.wrap(
PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.BEFORE_ADD_LIQUIDITY_OFFSET)
);
}
function shouldCallAfterAddLiquidity(PoolConfigBits config) internal pure returns (bool) {
return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.AFTER_ADD_LIQUIDITY_OFFSET);
}
function setShouldCallAfterAddLiquidity(PoolConfigBits config, bool value) internal pure returns (PoolConfigBits) {
return
PoolConfigBits.wrap(
PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.AFTER_ADD_LIQUIDITY_OFFSET)
);
}
function shouldCallBeforeRemoveLiquidity(PoolConfigBits config) internal pure returns (bool) {
return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.BEFORE_REMOVE_LIQUIDITY_OFFSET);
}
function setShouldCallBeforeRemoveLiquidity(
PoolConfigBits config,
bool value
) internal pure returns (PoolConfigBits) {
return
PoolConfigBits.wrap(
PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.BEFORE_REMOVE_LIQUIDITY_OFFSET)
);
}
function shouldCallAfterRemoveLiquidity(PoolConfigBits config) internal pure returns (bool) {
return PoolConfigBits.unwrap(config).decodeBool(PoolConfigConst.AFTER_REMOVE_LIQUIDITY_OFFSET);
}
function setShouldCallAfterRemoveLiquidity(
PoolConfigBits config,
bool value
) internal pure returns (PoolConfigBits) {
return
PoolConfigBits.wrap(
PoolConfigBits.unwrap(config).insertBool(value, PoolConfigConst.AFTER_REMOVE_LIQUIDITY_OFFSET)
);
}
function toHooksConfig(PoolConfigBits config, IHooks hooksContract) internal pure returns (HooksConfig memory) {
return
HooksConfig({
enableHookAdjustedAmounts: config.enableHookAdjustedAmounts(),
shouldCallBeforeInitialize: config.shouldCallBeforeInitialize(),
shouldCallAfterInitialize: config.shouldCallAfterInitialize(),
shouldCallBeforeAddLiquidity: config.shouldCallBeforeAddLiquidity(),
shouldCallAfterAddLiquidity: config.shouldCallAfterAddLiquidity(),
shouldCallBeforeRemoveLiquidity: config.shouldCallBeforeRemoveLiquidity(),
shouldCallAfterRemoveLiquidity: config.shouldCallAfterRemoveLiquidity(),
shouldCallComputeDynamicSwapFee: config.shouldCallComputeDynamicSwapFee(),
shouldCallBeforeSwap: config.shouldCallBeforeSwap(),
shouldCallAfterSwap: config.shouldCallAfterSwap(),
hooksContract: address(hooksContract)
});
}
/**
* @dev Call the `onComputeDynamicSwapFeePercentage` hook and return the result. Reverts on failure.
* @param swapParams The swap parameters used to calculate the fee
* @param pool Pool address
* @param staticSwapFeePercentage Value of the static swap fee, for reference
* @param hooksContract Storage slot with the address of the hooks contract
* @return swapFeePercentage The calculated swap fee percentage
*/
function callComputeDynamicSwapFeeHook(
PoolSwapParams memory swapParams,
address pool,
uint256 staticSwapFeePercentage,
IHooks hooksContract
) internal view returns (uint256) {
(bool success, uint256 swapFeePercentage) = hooksContract.onComputeDynamicSwapFeePercentage(
swapParams,
pool,
staticSwapFeePercentage
);
if (success == false) {
revert IVaultErrors.DynamicSwapFeeHookFailed();
}
// A 100% fee is not supported. In the ExactOut case, the Vault divides by the complement of the swap fee.
// The minimum precision constraint provides an additional buffer.
if (swapFeePercentage > MAX_FEE_PERCENTAGE) {
revert IVaultErrors.PercentageAboveMax();
}
return swapFeePercentage;
}
/**
* @dev Call the `onBeforeSwap` hook. Reverts on failure.
* @param swapParams The swap parameters used in the hook
* @param pool Pool address
* @param hooksContract Storage slot with the address of the hooks contract
*/
function callBeforeSwapHook(PoolSwapParams memory swapParams, address pool, IHooks hooksContract) internal {
if (hooksContract.onBeforeSwap(swapParams, pool) == false) {
// Hook contract implements onBeforeSwap, but it has failed, so reverts the transaction.
revert IVaultErrors.BeforeSwapHookFailed();
}
}
/**
* @dev Call the `onAfterSwap` hook, then validate and return the result. Reverts on failure, or if the limits
* are violated. If the hook contract did not enable hook-adjusted amounts, it will ignore the hook results and
* return the original `amountCalculatedRaw`.
*
* @param config The encoded pool configuration
* @param amountCalculatedScaled18 Token amount calculated by the swap
* @param amountCalculatedRaw Token amount calculated by the swap
* @param router Router address
* @param vaultSwapParams The swap parameters
* @param state Temporary state used in swap operations
* @param poolData Struct containing balance and token information of the pool
* @param hooksContract Storage slot with the address of the hooks contract
* @return hookAdjustedAmountCalculatedRaw New amount calculated, potentially modified by the hook
*/
function callAfterSwapHook(
PoolConfigBits config,
uint256 amountCalculatedScaled18,
uint256 amountCalculatedRaw,
address router,
VaultSwapParams memory vaultSwapParams,
SwapState memory state,
PoolData memory poolData,
IHooks hooksContract
) internal returns (uint256) {
// Adjust balances for the AfterSwap hook.
(uint256 amountInScaled18, uint256 amountOutScaled18) = vaultSwapParams.kind == SwapKind.EXACT_IN
? (state.amountGivenScaled18, amountCalculatedScaled18)
: (amountCalculatedScaled18, state.amountGivenScaled18);
(bool success, uint256 hookAdjustedAmountCalculatedRaw) = hooksContract.onAfterSwap(
AfterSwapParams({
kind: vaultSwapParams.kind,
tokenIn: vaultSwapParams.tokenIn,
tokenOut: vaultSwapParams.tokenOut,
amountInScaled18: amountInScaled18,
amountOutScaled18: amountOutScaled18,
tokenInBalanceScaled18: poolData.balancesLiveScaled18[state.indexIn],
tokenOutBalanceScaled18: poolData.balancesLiveScaled18[state.indexOut],
amountCalculatedScaled18: amountCalculatedScaled18,
amountCalculatedRaw: amountCalculatedRaw,
router: router,
pool: vaultSwapParams.pool,
userData: vaultSwapParams.userData
})
);
if (success == false) {
// Hook contract implements onAfterSwap, but it has failed, so reverts the transaction.
revert IVaultErrors.AfterSwapHookFailed();
}
// If hook adjusted amounts is not enabled, ignore amounts returned by the hook
if (config.enableHookAdjustedAmounts() == false) {
return amountCalculatedRaw;
}
if (
(vaultSwapParams.kind == SwapKind.EXACT_IN && hookAdjustedAmountCalculatedRaw < vaultSwapParams.limitRaw) ||
(vaultSwapParams.kind == SwapKind.EXACT_OUT && hookAdjustedAmountCalculatedRaw > vaultSwapParams.limitRaw)
) {
revert IVaultErrors.HookAdjustedSwapLimit(hookAdjustedAmountCalculatedRaw, vaultSwapParams.limitRaw);
}
return hookAdjustedAmountCalculatedRaw;
}
/**
* @dev Call the `onBeforeAddLiquidity` hook. Reverts on failure.
* @param router Router address
* @param maxAmountsInScaled18 An array with maximum amounts for each input token of the add liquidity operation
* @param params The add liquidity parameters
* @param poolData Struct containing balance and token information of the pool
* @param hooksContract Storage slot with the address of the hooks contract
*/
function callBeforeAddLiquidityHook(
address router,
uint256[] memory maxAmountsInScaled18,
AddLiquidityParams memory params,
PoolData memory poolData,
IHooks hooksContract
) internal {
if (
hooksContract.onBeforeAddLiquidity(
router,
params.pool,
params.kind,
maxAmountsInScaled18,
params.minBptAmountOut,
poolData.balancesLiveScaled18,
params.userData
) == false
) {
revert IVaultErrors.BeforeAddLiquidityHookFailed();
}
}
/**
* @dev Call the `onAfterAddLiquidity` hook, then validate and return the result. Reverts on failure, or if
* the limits are violated. If the contract did not enable hook-adjusted amounts, it will ignore the hook
* results and return the original `amountsInRaw`.
*
* @param config The encoded pool configuration
* @param router Router address
* @param amountsInScaled18 An array with amounts for each input token of the add liquidity operation
* @param amountsInRaw An array with amounts for each input token of the add liquidity operation
* @param bptAmountOut The BPT amount a user will receive after add liquidity operation succeeds
* @param params The add liquidity parameters
* @param poolData Struct containing balance and token information of the pool
* @param hooksContract Storage slot with the address of the hooks contract
* @return hookAdjustedAmountsInRaw New amountsInRaw, potentially modified by the hook
*/
function callAfterAddLiquidityHook(
PoolConfigBits config,
address router,
uint256[] memory amountsInScaled18,
uint256[] memory amountsInRaw,
uint256 bptAmountOut,
AddLiquidityParams memory params,
PoolData memory poolData,
IHooks hooksContract
) internal returns (uint256[] memory) {
(bool success, uint256[] memory hookAdjustedAmountsInRaw) = hooksContract.onAfterAddLiquidity(
router,
params.pool,
params.kind,
amountsInScaled18,
amountsInRaw,
bptAmountOut,
poolData.balancesLiveScaled18,
params.userData
);
if (success == false || hookAdjustedAmountsInRaw.length != amountsInRaw.length) {
revert IVaultErrors.AfterAddLiquidityHookFailed();
}
// If hook adjusted amounts is not enabled, ignore amounts returned by the hook
if (config.enableHookAdjustedAmounts() == false) {
return amountsInRaw;
}
for (uint256 i = 0; i < hookAdjustedAmountsInRaw.length; i++) {
if (hookAdjustedAmountsInRaw[i] > params.maxAmountsIn[i]) {
revert IVaultErrors.HookAdjustedAmountInAboveMax(
poolData.tokens[i],
hookAdjustedAmountsInRaw[i],
params.maxAmountsIn[i]
);
}
}
return hookAdjustedAmountsInRaw;
}
/**
* @dev Call the `onBeforeRemoveLiquidity` hook. Reverts on failure.
* @param minAmountsOutScaled18 Minimum amounts for each output token of the remove liquidity operation
* @param router Router address
* @param params The remove liquidity parameters
* @param poolData Struct containing balance and token information of the pool
* @param hooksContract Storage slot with the address of the hooks contract
*/
function callBeforeRemoveLiquidityHook(
uint256[] memory minAmountsOutScaled18,
address router,
RemoveLiquidityParams memory params,
PoolData memory poolData,
IHooks hooksContract
) internal {
if (
hooksContract.onBeforeRemoveLiquidity(
router,
params.pool,
params.kind,
params.maxBptAmountIn,
minAmountsOutScaled18,
poolData.balancesLiveScaled18,
params.userData
) == false
) {
revert IVaultErrors.BeforeRemoveLiquidityHookFailed();
}
}
/**
* @dev Call the `onAfterRemoveLiquidity` hook, then validate and return the result. Reverts on failure, or if
* the limits are violated. If the contract did not enable hook-adjusted amounts, it will ignore the hook
* results and return the original `amountsOutRaw`.
*
* @param config The encoded pool configuration
* @param router Router address
* @param amountsOutScaled18 Scaled amount of tokens to receive, sorted in token registration order
* @param amountsOutRaw Actual amount of tokens to receive, sorted in token registration order
* @param bptAmountIn The BPT amount a user will need burn to remove the liquidity of the pool
* @param params The remove liquidity parameters
* @param poolData Struct containing balance and token information of the pool
* @param hooksContract Storage slot with the address of the hooks contract
* @return hookAdjustedAmountsOutRaw New amountsOutRaw, potentially modified by the hook
*/
function callAfterRemoveLiquidityHook(
PoolConfigBits config,
address router,
uint256[] memory amountsOutScaled18,
uint256[] memory amountsOutRaw,
uint256 bptAmountIn,
RemoveLiquidityParams memory params,
PoolData memory poolData,
IHooks hooksContract
) internal returns (uint256[] memory) {
(bool success, uint256[] memory hookAdjustedAmountsOutRaw) = hooksContract.onAfterRemoveLiquidity(
router,
params.pool,
params.kind,
bptAmountIn,
amountsOutScaled18,
amountsOutRaw,
poolData.balancesLiveScaled18,
params.userData
);
if (success == false || hookAdjustedAmountsOutRaw.length != amountsOutRaw.length) {
revert IVaultErrors.AfterRemoveLiquidityHookFailed();
}
// If hook adjusted amounts is not enabled, ignore amounts returned by the hook
if (config.enableHookAdjustedAmounts() == false) {
return amountsOutRaw;
}
for (uint256 i = 0; i < hookAdjustedAmountsOutRaw.length; i++) {
if (hookAdjustedAmountsOutRaw[i] < params.minAmountsOut[i]) {
revert IVaultErrors.HookAdjustedAmountOutBelowMin(
poolData.tokens[i],
hookAdjustedAmountsOutRaw[i],
params.minAmountsOut[i]
);
}
}
return hookAdjustedAmountsOutRaw;
}
/**
* @dev Call the `onBeforeInitialize` hook. Reverts on failure.
* @param exactAmountsInScaled18 An array with the initial liquidity of the pool
* @param userData Additional (optional) data required for adding initial liquidity
* @param hooksContract Storage slot with the address of the hooks contract
*/
function callBeforeInitializeHook(
uint256[] memory exactAmountsInScaled18,
bytes memory userData,
IHooks hooksContract
) internal {
if (hooksContract.onBeforeInitialize(exactAmountsInScaled18, userData) == false) {
revert IVaultErrors.BeforeInitializeHookFailed();
}
}
/**
* @dev Call the `onAfterInitialize` hook. Reverts on failure.
* @param exactAmountsInScaled18 An array with the initial liquidity of the pool
* @param bptAmountOut The BPT amount a user will receive after initialization operation succeeds
* @param userData Additional (optional) data required for adding initial liquidity
* @param hooksContract Storage slot with the address of the hooks contract
*/
function callAfterInitializeHook(
uint256[] memory exactAmountsInScaled18,
uint256 bptAmountOut,
bytes memory userData,
IHooks hooksContract
) internal {
if (hooksContract.onAfterInitialize(exactAmountsInScaled18, bptAmountOut, userData) == false) {
revert IVaultErrors.AfterInitializeHookFailed();
}
}
}