Eidode / Mô phỏng / Uniswap V4
Uniswap V4 — PoolManager Singleton & Hooks
V4 thay đổi kiến trúc nền tảng: một contract duy nhất chứa toàn bộ pool, và hooks cho phép developer chèn logic tùy chỉnh.
PoolManager Singleton
PoolManager.sol
Một contract giữ toàn bộ pool
Khác V2/V3 mỗi pool là contract riêng → V4 gom tất cả vào một singleton. Giảm ~99% gas khi deploy pool mới + cho phép .
10 Hook points — bật/tắt để xem
Hover từng hook để xem giải thích
Quy trình swap với các hooks bạn đã bật
- 1User gọi swap()Bắt đầu — bạn click 'Swap' trên frontend
- 2beforeSwap hookChỗ thay đổi fee động, kiểm tra điều kiện, hoặc dùng curve tùy chỉnh
- 3Core swap math (x·y=k hoặc tick)PoolManager tính output dựa trên reserves và range
- 4afterSwap hookUpdate oracle, log analytics, distribute reward
- 5Settle & return deltaTransfer net amount cuối cùng — tiết kiệm gas nhờ flash accounting
Ví dụ Hook thật — chọn để xem code
Dynamic Fee Hook
Tính phí động dựa trên volatility — pool dao động mạnh tăng phí lên 1%, ổn định giảm còn 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.
Câu hỏi thường gặp
+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.