Skip to main content
Helpful?

IHooks

Git Source | Generated with forge doc

V4 decides whether to invoke specific hooks by inspecting the least significant bits of the address that the hooks contract is deployed to. For example, a hooks contract deployed to address: 0x0000000000000000000000000000000000002400 has the lowest bits '10 0100 0000 0000' which would cause the 'before initialize' and 'after add liquidity' hooks to be used. See the Hooks library for the full spec.

Should only be callable by the v4 PoolManager.

Functions

beforeInitialize

The hook called before the state of a pool is initialized

function beforeInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96, bytes calldata hookData)
external
returns (bytes4);

Parameters

NameTypeDescription
senderaddressThe initial msg.sender for the initialize call
keyPoolKeyThe key for the pool being initialized
sqrtPriceX96uint160The sqrt(price) of the pool as a Q64.96
hookDatabytesArbitrary data handed into the PoolManager by the initializer to be be passed on to the hook

Returns

NameTypeDescription
<none>bytes4bytes4 The function selector for the hook

afterInitialize

The hook called after the state of a pool is initialized

function afterInitialize(
address sender,
PoolKey calldata key,
uint160 sqrtPriceX96,
int24 tick,
bytes calldata hookData
) external returns (bytes4);

Parameters

NameTypeDescription
senderaddressThe initial msg.sender for the initialize call
keyPoolKeyThe key for the pool being initialized
sqrtPriceX96uint160The sqrt(price) of the pool as a Q64.96
tickint24The current tick after the state of a pool is initialized
hookDatabytesArbitrary data handed into the PoolManager by the initializer to be be passed on to the hook

Returns

NameTypeDescription
<none>bytes4bytes4 The function selector for the hook

beforeAddLiquidity

The hook called before liquidity is added

function beforeAddLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
bytes calldata hookData
) external returns (bytes4);

Parameters

NameTypeDescription
senderaddressThe initial msg.sender for the add liquidity call
keyPoolKeyThe key for the pool
paramsIPoolManager.ModifyLiquidityParamsThe parameters for adding liquidity
hookDatabytesArbitrary data handed into the PoolManager by the liquidity provider to be passed on to the hook

Returns

NameTypeDescription
<none>bytes4bytes4 The function selector for the hook

afterAddLiquidity

The hook called after liquidity is added

function afterAddLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
BalanceDelta delta,
BalanceDelta feesAccrued,
bytes calldata hookData
) external returns (bytes4, BalanceDelta);

Parameters

NameTypeDescription
senderaddressThe initial msg.sender for the add liquidity call
keyPoolKeyThe key for the pool
paramsIPoolManager.ModifyLiquidityParamsThe parameters for adding liquidity
deltaBalanceDeltaThe caller's balance delta after adding liquidity; the sum of principal delta, fees accrued, and hook delta
feesAccruedBalanceDeltaThe fees accrued since the last time fees were collected from this position
hookDatabytesArbitrary data handed into the PoolManager by the liquidity provider to be passed on to the hook

Returns

NameTypeDescription
<none>bytes4bytes4 The function selector for the hook
<none>BalanceDeltaBalanceDelta The hook's delta in token0 and token1. Positive: the hook is owed/took currency, negative: the hook owes/sent currency

beforeRemoveLiquidity

The hook called before liquidity is removed

function beforeRemoveLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
bytes calldata hookData
) external returns (bytes4);

Parameters

NameTypeDescription
senderaddressThe initial msg.sender for the remove liquidity call
keyPoolKeyThe key for the pool
paramsIPoolManager.ModifyLiquidityParamsThe parameters for removing liquidity
hookDatabytesArbitrary data handed into the PoolManager by the liquidity provider to be be passed on to the hook

Returns

NameTypeDescription
<none>bytes4bytes4 The function selector for the hook

afterRemoveLiquidity

The hook called after liquidity is removed

function afterRemoveLiquidity(
address sender,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
BalanceDelta delta,
BalanceDelta feesAccrued,
bytes calldata hookData
) external returns (bytes4, BalanceDelta);

Parameters

NameTypeDescription
senderaddressThe initial msg.sender for the remove liquidity call
keyPoolKeyThe key for the pool
paramsIPoolManager.ModifyLiquidityParamsThe parameters for removing liquidity
deltaBalanceDeltaThe caller's balance delta after adding liquidity; the sum of principal delta, fees accrued, and hook delta
feesAccruedBalanceDeltaThe fees accrued since the last time fees were collected from this position
hookDatabytesArbitrary data handed into the PoolManager by the liquidity provider to be be passed on to the hook

Returns

NameTypeDescription
<none>bytes4bytes4 The function selector for the hook
<none>BalanceDeltaBalanceDelta The hook's delta in token0 and token1. Positive: the hook is owed/took currency, negative: the hook owes/sent currency

beforeSwap

The hook called before a swap

function beforeSwap(
address sender,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
bytes calldata hookData
) external returns (bytes4, BeforeSwapDelta, uint24);

Parameters

NameTypeDescription
senderaddressThe initial msg.sender for the swap call
keyPoolKeyThe key for the pool
paramsIPoolManager.SwapParamsThe parameters for the swap
hookDatabytesArbitrary data handed into the PoolManager by the swapper to be be passed on to the hook

Returns

NameTypeDescription
<none>bytes4bytes4 The function selector for the hook
<none>BeforeSwapDeltaBeforeSwapDelta The hook's delta in specified and unspecified currencies. Positive: the hook is owed/took currency, negative: the hook owes/sent currency
<none>uint24uint24 Optionally override the lp fee, only used if three conditions are met: 1. the Pool has a dynamic fee, 2. the value's 2nd highest bit is set (23rd bit, 0x400000), and 3. the value is less than or equal to the maximum fee (1 million)

afterSwap

The hook called after a swap

function afterSwap(
address sender,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
BalanceDelta delta,
bytes calldata hookData
) external returns (bytes4, int128);

Parameters

NameTypeDescription
senderaddressThe initial msg.sender for the swap call
keyPoolKeyThe key for the pool
paramsIPoolManager.SwapParamsThe parameters for the swap
deltaBalanceDeltaThe amount owed to the caller (positive) or owed to the pool (negative)
hookDatabytesArbitrary data handed into the PoolManager by the swapper to be be passed on to the hook

Returns

NameTypeDescription
<none>bytes4bytes4 The function selector for the hook
<none>int128int128 The hook's delta in unspecified currency. Positive: the hook is owed/took currency, negative: the hook owes/sent currency

beforeDonate

The hook called before donate

function beforeDonate(address sender, PoolKey calldata key, uint256 amount0, uint256 amount1, bytes calldata hookData)
external
returns (bytes4);

Parameters

NameTypeDescription
senderaddressThe initial msg.sender for the donate call
keyPoolKeyThe key for the pool
amount0uint256The amount of token0 being donated
amount1uint256The amount of token1 being donated
hookDatabytesArbitrary data handed into the PoolManager by the donor to be be passed on to the hook

Returns

NameTypeDescription
<none>bytes4bytes4 The function selector for the hook

afterDonate

The hook called after donate

function afterDonate(address sender, PoolKey calldata key, uint256 amount0, uint256 amount1, bytes calldata hookData)
external
returns (bytes4);

Parameters

NameTypeDescription
senderaddressThe initial msg.sender for the donate call
keyPoolKeyThe key for the pool
amount0uint256The amount of token0 being donated
amount1uint256The amount of token1 being donated
hookDatabytesArbitrary data handed into the PoolManager by the donor to be be passed on to the hook

Returns

NameTypeDescription
<none>bytes4bytes4 The function selector for the hook
Helpful?