Eidode

Eidode / Simulator / Uniswap V4

Uniswap V4 — PoolManager Singleton & Hooks

V4 ubah arsitektur dasar: satu contract menampung semua pool, dan hooks memungkinkan developer suntik logika kustom.

🧠 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.

Pertanyaan yang Sering Diajukan

+How is Uniswap V4 different from V3?

Two main changes: (1) all pools are stored in a single contract called PoolManager (singleton) — saving ~99% of pool-creation gas; (2) hooks let developers inject custom code before/after pool operations (swap, add liquidity, etc.) to build dynamic fees, oracles, MEV protection, or any 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 that handles 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 can raise the fee to 1% in a market storm and drop it to 0.05% when calm.

+Why put every pool in one contract?

Gas. V3 pool creation deploys a new contract (~500k gas). V4 pool creation just writes to PoolManager storage (~50k gas). Multi-pool swaps can also flash-account within a single transaction — no intermediate transfers needed.

+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.