How to build hooks using BaseHook
mainWhen developing custom hooks for Uniswap v4, it is recommended to inherit from the BaseHook contract provided by the v4-hooks-public repository. You can implement custom logic by overriding specific hook callbacks (e.g., _beforeAddLiquidity). Ensure that your overridden functions return the appropriate selector from the BaseHook to maintain correct hook execution flow.
contract CoolHook is BaseHook {
// Override the hook callbacks you want on your hook
function _beforeAddLiquidity(
address,
PoolKey calldata key,
ModifyLiquidityParams calldata params,
bytes calldata hookData
) internal override returns (bytes4) {
// hook logic
return BaseHook.beforeAddLiquidity.selector;
}
}