Crypto Newbie

Crypto Newbie / Simulators / Uniswap V4

Uniswap V4 — PoolManager Singleton & Hooks

V4 changes the underlying architecture: one contract (PoolManager) holds every pool, and hooks let developers inject custom logic at 10 points in a pool's lifecycle. The interactive diagram below walks through how hooks work, with sample code for the 4 most common hook types.

🧠 Picture a V4 pool as a with one entrance and one exit. A is like a security guard — it can check or modify the transaction before it passes through, or after it completes. Toggle each hook below to see where it sits in the swap lifecycle.

PoolManager Singleton

PoolManager.sol

One contract holds every pool

ETH/USDC
WBTC/ETH
...100s pools

Unlike V2/V3 where each pool was its own contract → V4 gathers all of them inside one singleton. ~99% cheaper to create a new pool + enables .

10 hook points — toggle to explore

Hover a hook to see the explanation

Swap path with the hooks you've enabled

  1. 1User calls swap()Start — you click 'Swap' on the frontend
  2. 2beforeSwap hookWhere dynamic fees, condition checks, or custom curves run
  3. 3Core swap math (x·y=k or tick)PoolManager computes output from reserves and range
  4. 4afterSwap hookUpdate oracle, log analytics, distribute rewards
  5. 5Settle & return deltaTransfer the final net amount — gas savings come from flash accounting

Real hook examples — pick one to view the code

Dynamic Fee Hook

Set a dynamic fee based on volatility — a wildly swinging pool charges 1%, a calm one drops to 0.05%.

function beforeSwap(...) {
  uint24 dynamicFee = isVolatile() ? 10000 : 500;
  poolManager.updateDynamicLPFee(key, dynamicFee);
  return BaseHook.beforeSwap.selector;
}

Comparing V2 → V3 → V4

V2 used a pool-factory architecture with full-range x·y=k liquidity and a single 0.3% fee. V3 added concentrated liquidity in ranges plus three fee tiers, but still deployed a new contract per pool. V4 collapses everything into a singleton PoolManager contract that holds every pool (~10× cheaper pool creation), supports hooks that inject custom logic at 10 points in a pool's lifecycle, supports dynamic fees, and handles native ETH without wrapping.

The four most common hook types

1) Dynamic fee — adjust fee by volatility/volume so the pool adapts to market conditions. 2) Oracle — every swap updates an on-chain oracle that other apps can read for trusted prices. 3) Limit order — on-chain limit orders without a market maker; the hook self-executes when price is hit. 4) MEV protection — commit-reveal or batch auction to defend against sandwich attacks.

Risks of swapping through hooked pools

Hooks have full authority over the pool — bugs can drain liquidity. Hooks may block swaps for any reason (KYC, blacklists, etc.). Gas costs are higher than for hookless pools. Before using a hooked pool, verify the hook address has a public audit.

Frequently asked questions

+How is Uniswap V4 different from V3?

Two big changes: (1) all pools share a single PoolManager contract (singleton) — about 99% cheaper to create a pool; (2) hooks let developers inject custom code before/after operations (swap, add liquidity, …) to build dynamic fees, oracles, MEV protection, or any other logic.

+What is a hook in V4?

A hook is a smart contract attached to a pool at creation. Each pool can have a hook handling 10 lifecycle points: beforeInitialize, afterInitialize, beforeAddLiquidity, afterAddLiquidity, beforeRemoveLiquidity, afterRemoveLiquidity, beforeSwap, afterSwap, beforeDonate, afterDonate.

+How does a dynamic-fee hook work?

The beforeSwap hook can read current volatility/volume and call poolManager.updateDynamicLPFee() to change the fee before the swap executes. Example: an ETH/USDC pool that lifts fees to 1% in a market storm and drops back to 0.05% when calm.

+Why put every pool in one contract?

Gas. In V3, creating a pool deploys a new contract (~500k gas). In V4, creating a pool just writes to PoolManager storage (~50k gas). Multi-pool swaps can also flash-account in one transaction without intermediate transfers.

+Does V4 replace V3?

Not immediately. V3 already holds huge TVL and will run in parallel for years. V4 fits pools that need custom logic (institutional, dynamic-fee, oracle-integrated). Simple pools like USDC/USDT should stay on V3 until V4 has matured.

+Can I write my own hook?

Yes. V4 lets anyone deploy a hook contract and attach it to a new pool. But hooks have full authority over the pool — a bug can drain liquidity. Audit carefully before using a pool with an unknown hook.