Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge hooks config with pool config #659

Merged
merged 24 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/interfaces/contracts/test/IVaultMainMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ interface IVaultMainMock {

function mintERC20(address token, address to, uint256 amount) external;

function setHooksConfig(address pool, HooksConfig calldata config) external;

function manualRegisterPool(address pool, IERC20[] memory tokens) external;

function manualRegisterPoolWithSwapFee(address pool, IERC20[] memory tokens, uint256 swapFeePercentage) external;
Expand Down Expand Up @@ -46,7 +44,9 @@ interface IVaultMainMock {

function manualSetPoolTokenInfo(address, IERC20[] memory, TokenInfo[] memory) external;

function manualSetPoolConfig(address, PoolConfig memory) external;
function manualSetPoolConfig(address pool, PoolConfig memory config) external;

function manualSetHooksConfig(address pool, HooksConfig memory config) external;

function manualSetPoolTokenBalances(address, IERC20[] memory, uint256[] memory, uint256[] memory) external;

Expand Down
1 change: 1 addition & 0 deletions pkg/interfaces/contracts/vault/VaultTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.24;

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IERC4626 } from "@openzeppelin/contracts/interfaces/IERC4626.sol";

import { IRateProvider } from "./IRateProvider.sol";

struct LiquidityManagement {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177.0k
175.1k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
160.5k
158.6k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
210.3k
208.4k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
176.7k
174.8k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
180.0k
178.4k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
349.8k
347.9k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337.2k
335.4k
Original file line number Diff line number Diff line change
@@ -1 +1 @@
722011
727080
Original file line number Diff line number Diff line change
@@ -1 +1 @@
748853
753961
2 changes: 1 addition & 1 deletion pkg/vault/.forge-snapshots/forkBoostedPoolSwapExactIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
285003
289582
Original file line number Diff line number Diff line change
@@ -1 +1 @@
307825
312404
Original file line number Diff line number Diff line change
@@ -1 +1 @@
665616
670685
Original file line number Diff line number Diff line change
@@ -1 +1 @@
692464
697572
56 changes: 37 additions & 19 deletions pkg/vault/contracts/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { StorageSlot } from "@balancer-labs/v3-solidity-utils/contracts/openzepp

import { VaultStateLib, VaultStateBits, VaultStateBits } from "./lib/VaultStateLib.sol";
import { PoolConfigLib } from "./lib/PoolConfigLib.sol";
import { HooksConfigLib, HooksConfigBits } from "./lib/HooksConfigLib.sol";
import { HooksConfigLib } from "./lib/HooksConfigLib.sol";
import { PackedTokenBalance } from "./lib/PackedTokenBalance.sol";
import { PoolDataLib } from "./lib/PoolDataLib.sol";
import { BufferPackedTokenBalance } from "./lib/BufferPackedBalance.sol";
Expand All @@ -49,6 +49,7 @@ contract Vault is IVaultMain, VaultCommon, Proxy {
using Address for *;
using SafeERC20 for IERC20;
using PoolConfigLib for PoolConfigBits;
using HooksConfigLib for PoolConfigBits;
using ScalingHelpers for *;
using BufferPackedTokenBalance for bytes32;
using TransientStorageHelpers for *;
Expand Down Expand Up @@ -181,7 +182,6 @@ contract Vault is IVaultMain, VaultCommon, Proxy {
returns (uint256 amountCalculated, uint256 amountIn, uint256 amountOut)
{
_ensureUnpaused(params.pool);
HooksConfigBits hooksConfig = _hooksConfigBits[params.pool];

if (params.amountGivenRaw == 0) {
revert AmountGivenZero();
Expand All @@ -204,7 +204,8 @@ contract Vault is IVaultMain, VaultCommon, Proxy {

IBasePool.PoolSwapParams memory swapParams = _buildPoolSwapParams(params, state, poolData);

if (hooksConfig.callBeforeSwapHook(swapParams, params.pool)) {
StorageSlot.AddressSlot storage hooksContract = _hooksContracts[params.pool];
elshan-eth marked this conversation as resolved.
Show resolved Hide resolved
if (poolData.poolConfigBits.callBeforeSwapHook(swapParams, params.pool, hooksContract)) {
// The call to `onBeforeSwap` could potentially update token rates and balances.
// We update `poolData.tokenRates`, `poolData.rawBalances` and `poolData.balancesLiveScaled18`
// to ensure the `onSwap` and `onComputeDynamicSwapFee` are called with the current values.
Expand All @@ -221,9 +222,10 @@ contract Vault is IVaultMain, VaultCommon, Proxy {
// At this point, the static swap fee percentage is loaded in the swap state as the default,
// to be used unless the pool has a dynamic swap fee. It is also passed into the hook, to support common cases
// where the dynamic fee computation logic uses it.
(bool dynamicSwapFeeCalculated, uint256 dynamicSwapFee) = hooksConfig.callComputeDynamicSwapFeeHook(
(bool dynamicSwapFeeCalculated, uint256 dynamicSwapFee) = poolData.poolConfigBits.callComputeDynamicSwapFeeHook(
swapParams,
state.swapFeePercentage
state.swapFeePercentage,
hooksContract
);
if (dynamicSwapFeeCalculated) {
state.swapFeePercentage = dynamicSwapFee;
Expand All @@ -235,17 +237,17 @@ contract Vault is IVaultMain, VaultCommon, Proxy {
uint256 amountCalculatedScaled18;
(amountCalculated, amountCalculatedScaled18, amountIn, amountOut) = _swap(params, state, poolData, swapParams);

// If the hook contract does not exist or does not implement onAfterSwap, HooksConfigLib returns the original
// If the hook contract does not exist or does not implement onAfterSwap, PoolConfigLib returns the original
// amountCalculated. Otherwise, the new amount calculated is 'amountCalculated + delta'. If the underlying
// hook fails, or limits are violated, `onAfterSwap` will revert.
// Uses msg.sender as the router (the contract that called the vault)
amountCalculated = hooksConfig.callAfterSwapHook(
amountCalculated = poolData.poolConfigBits.callAfterSwapHook(
amountCalculatedScaled18,
amountCalculated,
msg.sender,
params,
state,
poolData
poolData,
hooksContract
);

if (params.kind == SwapKind.EXACT_IN) {
Expand Down Expand Up @@ -508,7 +510,6 @@ contract Vault is IVaultMain, VaultCommon, Proxy {
// bptOut = supply * (ratio - 1), so lower ratio = less bptOut, favoring the pool.

_ensureUnpaused(params.pool);
HooksConfigBits hooksConfig = _hooksConfigBits[params.pool];

// `_loadPoolDataUpdatingBalancesAndYieldFees` is non-reentrant, as it updates storage as well
// as filling in poolData in memory. Since the add liquidity hooks are reentrant and could do anything,
Expand All @@ -528,8 +529,16 @@ contract Vault is IVaultMain, VaultCommon, Proxy {
poolData.tokenRates
);

// Uses msg.sender as the router (the contract that called the vault)
elshan-eth marked this conversation as resolved.
Show resolved Hide resolved
if (hooksConfig.callBeforeAddLiquidityHook(msg.sender, maxAmountsInScaled18, params, poolData)) {
StorageSlot.AddressSlot storage hooksContract = _hooksContracts[params.pool];
if (
poolData.poolConfigBits.callBeforeAddLiquidityHook(
msg.sender,
maxAmountsInScaled18,
params,
poolData,
hooksContract
)
) {
// If the hook library returns true, the hook code was executed, and might have altered the balances,
// so we need to read them again to ensure that the data is fresh moving forward.
// We also need to upscale (adding liquidity, so round up) again.
Expand Down Expand Up @@ -557,13 +566,14 @@ contract Vault is IVaultMain, VaultCommon, Proxy {

// AmountsIn can be changed by onAfterAddLiquidity if the hook charges fees or gives discounts
// Uses msg.sender as the router (the contract that called the vault)
amountsIn = hooksConfig.callAfterAddLiquidityHook(
amountsIn = poolData.poolConfigBits.callAfterAddLiquidityHook(
msg.sender,
amountsInScaled18,
amountsIn,
bptAmountOut,
params,
poolData
poolData,
hooksContract
);
}

Expand Down Expand Up @@ -736,8 +746,6 @@ contract Vault is IVaultMain, VaultCommon, Proxy {
// bptIn = supply * (1 - ratio), so lower ratio = more bptIn, favoring the pool.
_ensureUnpaused(params.pool);

HooksConfigBits hooksConfig = _hooksConfigBits[params.pool];

// `_loadPoolDataUpdatingBalancesAndYieldFees` is non-reentrant, as it updates storage as well
// as filling in poolData in memory. Since the swap hooks are reentrant and could do anything, including
// change these balances, we cannot defer settlement until `_removeLiquidity`.
Expand All @@ -755,8 +763,17 @@ contract Vault is IVaultMain, VaultCommon, Proxy {
poolData.tokenRates
);

StorageSlot.AddressSlot storage hooksContract = _hooksContracts[params.pool];
// Uses msg.sender as the router (the contract that called the vault)
if (hooksConfig.callBeforeRemoveLiquidityHook(minAmountsOutScaled18, msg.sender, params, poolData)) {
if (
poolData.poolConfigBits.callBeforeRemoveLiquidityHook(
minAmountsOutScaled18,
msg.sender,
params,
poolData,
hooksContract
)
) {
// The hook might alter the balances, so we need to read them again to ensure that the data is
// fresh moving forward.
// We also need to upscale (removing liquidity, so round down) again.
Expand All @@ -782,13 +799,14 @@ contract Vault is IVaultMain, VaultCommon, Proxy {

// AmountsOut can be changed by onAfterRemoveLiquidity if the hook charges fees or gives discounts
// Uses msg.sender as the router (the contract that called the vault)
amountsOut = hooksConfig.callAfterRemoveLiquidityHook(
amountsOut = poolData.poolConfigBits.callAfterRemoveLiquidityHook(
msg.sender,
amountsOutScaled18,
amountsOut,
bptAmountIn,
params,
poolData
poolData,
hooksContract
);
}

Expand Down
Loading
Loading