Learn / OpenZeppelin
last updated 2026-08-27
Build with OpenZeppelin hooks
OpenZeppelin/uniswap-hooks is an MIT Solidity library for secure, modular Uniswap v4 hooks. Prefer it (or Uniswap’s BaseHook) over a blank contract so caller checks and permission validation are already there.
The library is experimental and not a substitute for an audit. Use it as the correct starting shape — then read secure v4 hooks before you ship.
Install (Foundry)
forge install OpenZeppelin/uniswap-hooks
# remappings.txt
@openzeppelin/uniswap-hooks/=lib/uniswap-hooks/src/Hardhat is not currently supported upstream because v4 core/periphery are not published as npm packages.
What to inherit
| Path | Use for |
|---|---|
src/base/ | BaseHook, BaseCustomAccounting, BaseCustomCurve, BaseAsyncSwap |
src/fee/ | Dynamic fee / after-fee building blocks |
src/general/ | AntiSandwichHook, LimitOrderHook, LiquidityPenaltyHook, ReHypothecationHook |
src/oracles/ | Oracle-oriented hooks |
src/utils/ | CurrencySettler and helpers |
Minimal BaseHook pattern
External callbacks are gated with onlyPoolManager. You implement getHookPermissions and the matching _beforeSwap / _afterSwap internals. Full excerpt on the BaseHook cut sheet.
modifier onlyPoolManager() {
if (msg.sender != address(poolManager)) revert NotPoolManager();
_;
}
function beforeSwap(...) external onlyPoolManager returns (bytes4, BeforeSwapDelta, uint24) {
return _beforeSwap(...);
}Ready-made patterns in the library
- AntiSandwichHook — top-of-block price checkpoint; resists sandwiching on one swap direction.
- LimitOrderHook — library-shaped fill-on-cross with cancel and fee accounting (compare Uniswap’s example limit-order).
- LiquidityPenaltyHook — JIT fee withhold and donate on early remove.
- ReHypothecationHook — idle LP in yield sources, JIT inject on swap.
- BaseDynamicFee — afterInitialize +
_pokeforupdateDynamicLPFee.
Agent workflow
- Match the job on v4hooks and open the cut sheet.
- If the pattern is “start from a base,” use OpenZeppelin BaseHook, not a blank IHooks.
- Copy the full file from the listing’s
source.url, not only the excerpt. - Run the security checklist before deploy.