Hooks

Hooks have access to different data shared from the Vault. These allow a developer to build powerful execution logic when additionally utilizing the shared data.

onRegister

function onRegister(
    address factory,
    address pool,
    TokenConfig[] memory tokenConfig,
    LiquidityManagement calldata liquidityManagement
) external returns (bool);

Hook to be executed when pool is registered. If it returns false, the registration is reverted. Vault address can be accessed with msg.sender.

Parameters:

NameTypeDescription
factoryaddressAddress of the pool factory
pooladdressAddress of the pool
tokenConfigTokenConfig[] memoryAn array of descriptors for the tokens the pool will manage
liquidityManagementLiquidityManagement calldataLiquidity management flags with implemented methods

Returns:

NameTypeDescription
successboolTrue if the hook allowed the registration, false otherwise

getHookFlags

function getHookFlags() external returns (HookFlags memory hookFlags);

Returns flags informing which hooks are implemented in the contract.

Returns:

NameTypeDescription
hookFlagsHookFlags memoryFlags indicating which hooks the contract supports

onBeforeInitialize

function onBeforeInitialize(uint256[] memory exactAmountsIn, bytes memory userData) external returns (bool);

Optional hook to be executed before pool initialization. Note that unlike the swap and liquidity hooks, the initialize hooks are non-reentrant.

Parameters:

NameTypeDescription
exactAmountsInuint256[] memoryExact amounts of input tokens
userDatabytes memoryOptional, arbitrary data with the encoded request

Returns:

NameTypeDescription
successboolTrue if the pool wishes to proceed with initialization

onAfterInitialize

function onAfterInitialize(
    uint256[] memory exactAmountsIn,
    uint256 bptAmountOut,
    bytes memory userData
) external returns (bool);

Optional hook to be executed after pool initialization. Note that unlike the swap and liquidity hooks, the initialize hooks are non-reentrant.

Parameters:

NameTypeDescription
exactAmountsInuint256[] memoryExact amounts of input tokens
bptAmountOutuint256Amount of pool tokens minted during initialization
userDatabytes memoryOptional, arbitrary data with the encoded request

Returns:

NameTypeDescription
successboolTrue if the pool wishes to proceed with initialization

onBeforeAddLiquidity

function onBeforeAddLiquidity(
    address router,
    address pool,
    AddLiquidityKind kind,
    uint256[] memory maxAmountsInScaled18,
    uint256 minBptAmountOut,
    uint256[] memory balancesScaled18,
    bytes memory userData
) external returns (bool success);

Optional hook to be executed before adding liquidity.

Parameters:

NameTypeDescription
routeraddressThe address (usually a router contract) that initiated a swap operation on the Vault
pooladdressPool address, used to fetch pool information from the Vault (pool config, tokens, etc.)
kindAddLiquidityKindThe type of add liquidity operation (e.g., proportional, custom)
maxAmountsInScaled18uint256[] memoryMaximum amounts of input tokens
minBptAmountOutuint256Minimum amount of output pool tokens
balancesScaled18uint256[] memoryCurrent pool balances in token registration order
userDatabytes memoryOptional, arbitrary data with the encoded request

Returns:

NameTypeDescription
successboolTrue if the pool wishes to proceed with settlement

onAfterAddLiquidity

function onAfterAddLiquidity(
    address router,
    address pool,
    uint256[] memory amountsInScaled18,
    uint256[] memory amountsInRaw,
    uint256 bptAmountOut,
    uint256[] memory balancesScaled18,
    bytes memory userData
) external returns (bool success, uint256[] memory hookAdjustedAmountsInRaw);

Optional hook to be executed after adding liquidity.

Parameters:

NameTypeDescription
routeraddressThe address (usually a router contract) that initiated a swap operation on the Vault
pooladdressPool address, used to fetch pool information from the Vault (pool config, tokens, etc.)
amountsInScaled18uint256[] memoryActual amounts of tokens added, sorted in token registration order
amountsInRawuint256[] memoryActual amounts of tokens added, sorted in token registration order
bptAmountOutuint256Amount of pool tokens minted
balancesScaled18uint256[] memoryCurrent pool balances in token registration order
userDatabytes memoryAdditional (optional) data provided by the user

Returns:

NameTypeDescription
successboolTrue if the pool wishes to proceed with settlement
hookAdjustedAmountsInRawuint256[] memoryNew amountsInRaw, potentially modified by the hook

onBeforeRemoveLiquidity

function onBeforeRemoveLiquidity(
    address router,
    address pool,
    RemoveLiquidityKind kind,
    uint256 maxBptAmountIn,
    uint256[] memory minAmountsOutScaled18,
    uint256[] memory balancesScaled18,
    bytes memory userData
) external returns (bool success);

Optional hook to be executed before removing liquidity.

Parameters:

NameTypeDescription
routeraddressThe address (usually a router contract) that initiated a swap operation on the Vault
pooladdressPool address, used to fetch pool information from the Vault (pool config, tokens, etc.)
kindRemoveLiquidityKindThe type of remove liquidity operation (e.g., proportional, custom)
maxBptAmountInuint256Maximum amount of input pool tokens
minAmountsOutScaled18uint256[] memoryMinimum output amounts in token registration order
balancesScaled18uint256[] memoryCurrent pool balances in token registration order
userDatabytes memoryOptional, arbitrary data with the encoded request

Returns:

NameTypeDescription
successboolTrue if the pool wishes to proceed with settlement

onAfterRemoveLiquidity

function onAfterRemoveLiquidity(
    address router,
    address pool,
    RemoveLiquidityKind kind,
    uint256 bptAmountIn,
    uint256[] memory amountsOutScaled18,
    uint256[] memory amountsOutRaw,
    uint256[] memory balancesScaled18,
    bytes memory userData
) external returns (bool success, uint256[] memory hookAdjustedAmountsOutRaw);

Optional hook to be executed after removing liquidity.

Parameters:

NameTypeDescription
routeraddressThe address (usually a router contract) that initiated a swap operation on the Vault
pooladdressPool address, used to fetch pool information from the Vault (pool config, tokens, etc.)
kindRemoveLiquidityKindThe type of remove liquidity operation (e.g., proportional, custom)
bptAmountInuint256Amount of pool tokens to burn
amountsOutScaled18uint256[] memoryScaled amount of tokens to receive, in token registration order
amountsOutRawuint256[] memoryActual amount of tokens to receive, in token registration order
balancesScaled18uint256[] memoryCurrent pool balances in token registration order
userDatabytes memoryAdditional (optional) data provided by the user

Returns:

NameTypeDescription
successboolTrue if the pool wishes to proceed with settlement
hookAdjustedAmountsOutRawuint256[] memoryNew amountsOutRaw, potentially modified by the hook

onBeforeSwap

function onBeforeSwap(PoolSwapParams calldata params, address pool) external returns (bool success);

Called before a swap to give the Pool an opportunity to perform actions.

Parameters:

NameTypeDescription
paramsPoolSwapParamsSwap parameters
pooladdressPool address, used to get pool information from the vault

Returns:

NameTypeDescription
successboolTrue if the pool wishes to proceed with settlement

onAfterSwap

function onAfterSwap(
    AfterSwapParams calldata params
) external returns (bool success, uint256 hookAdjustedAmountCalculatedRaw);

Called after a swap to give the Pool an opportunity to perform actions once the balances have been updated by the swap.

Parameters:

NameTypeDescription
paramsAfterSwapParamsSwap parameters

Returns:

NameTypeDescription
successboolTrue if the pool wishes to proceed with settlement
hookAdjustedAmountCalculatedRawuint256New amount calculated, modified by the hook

onComputeDynamicSwapFeePercentage

function onComputeDynamicSwapFeePercentage(
    PoolSwapParams calldata params,
    address pool,
    uint256 staticSwapFeePercentage
) external view returns (bool success, uint256 dynamicSwapFeePercentage);

Called after onBeforeSwap and before the main swap operation, if the pool has dynamic fees.

Parameters:

NameTypeDescription
paramsPoolSwapParamsSwap parameters
pooladdressAddress of the pool
staticSwapFeePercentageuint256Value of the static swap fee, for reference

Returns:

NameTypeDescription
successboolTrue if the pool wishes to proceed with settlement
dynamicSwapFeePercentageuint256Value of the swap fee