Concentrated liquidity, introduced by Uniswap v3 on Ethereum and now used on most EVM chains, lets each liquidity provider choose a price range, so their capital only backs trades between those prices. Prices are divided into ticks (each tick is a 0.01% price step, price = 1.0001^tick), and at any moment only positions whose range includes the current price are active, earn fees and absorb trades. Uniswap v4, launched in January 2025, keeps the same concentrated-liquidity maths but puts every pool inside one PoolManager contract with flash accounting, and lets each pool attach a hook: an external contract that the pool calls at fixed points (before and after initialise, liquidity changes, swaps and donations) to add custom logic such as dynamic fees, limit orders or access control.
What problem did v3 solve?
In Uniswap v2, every pool follows x * y = k across all prices from zero to infinity. Liquidity is spread evenly over that whole curve, so in a USDC/ETH pool most of it sits at prices ETH may never trade at. Only a small fraction actually serves trades near the current price, which means high slippage per dollar deposited.
v3 lets an LP say "I'll provide liquidity only between 2,800 and 3,600". Inside that range the position behaves like a v2 pool with much larger virtual reserves. The same capital provides far deeper liquidity where trading happens, which is why v3 pools can quote tighter prices with less total value locked.
How do ticks and liquidity work?
A v3 pool tracks three things:
sqrtPriceX96: the square root of the price (token1 per token0, in raw units) as a Q64.96 fixed-point number. Working in square roots keeps the swap maths simple and precise.- The current tick:
floor(log_1.0001(price)), from -887272 to 887272. liquidity(L): the sum of liquidity of all positions whose range contains the current price.
Each position has a lower tick, an upper tick and a liquidity amount. Ticks where positions start or end are initialised and store a net liquidity change. When a swap pushes the price across an initialised tick, the pool adds or removes that net amount from L and continues. A large swap may cross many ticks, each crossing costing extra gas.
For a position with range [pa, pb] and the current price P inside it, the tokens it holds are:
amount0 = L * (1/sqrt(P) - 1/sqrt(pb))
amount1 = L * (sqrt(P) - sqrt(pa))
If the price moves above pb, the position is entirely token1; below pa, entirely token0. Out of range, it earns nothing and simply holds one asset, having sold the other on the way out. That is the trade-off: higher fee income per dollar while in range, and amplified impermanent loss when the price moves.
Fees are charged on the input amount of each swap and credited to active liquidity pro rata, tracked through per-tick fee growth counters so the contract never loops over positions. v3 uses fixed fee tiers, each with a tick spacing that limits which ticks positions may use:
| Fee tier | Tick spacing | Typical use |
|---|---|---|
| 0.01% | 1 | Stablecoin pairs |
| 0.05% | 10 | Correlated or major pairs |
| 0.30% | 60 | Most pairs |
| 1.00% | 200 | Exotic or volatile pairs |
Positions are unique, so v3 represents them as NFTs through the NonfungiblePositionManager rather than fungible LP tokens.
Reading a pool's price is a common first task. For the mainnet USDC/WETH 0.05% pool, token0 is USDC (6 decimals) and token1 is WETH (18):
import { createPublicClient, http, parseAbi } from "viem";
import { mainnet } from "viem/chains";
const POOL = "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"; // USDC/WETH 0.05%
const abi = parseAbi([
"function slot0() view returns (uint160 sqrtPriceX96, int24 tick, uint16, uint16, uint16, uint8, bool)",
]);
const client = createPublicClient({ chain: mainnet, transport: http() });
const [sqrtPriceX96, tick] = await client.readContract({ address: POOL, abi, functionName: "slot0" });
// raw price (token1 units per token0 unit) = sqrtPriceX96^2 / 2^192
const Q192 = 2n ** 192n;
const rawTimes1e18 = (sqrtPriceX96 * sqrtPriceX96 * 10n ** 18n) / Q192;
const wethPerUsdc = (Number(rawTimes1e18) / 1e18) * 10 ** (6 - 18); // adjust for decimals
console.log({ tick, usdPerEth: 1 / wethPerUsdc });
A pool's spot price is easy to move within one transaction, so never use it as an oracle for lending or liquidations. Use a TWAP or an external feed such as a Chainlink price feed read safely; flash loan attacks usually begin with exactly this mistake.
What changed in Uniswap v4?
v4 keeps the v3 pricing maths but rebuilds the architecture:
- Singleton PoolManager. All pools live in one contract. Creating a pool is a state update, not a contract deployment, and multi-hop swaps no longer move tokens between pool contracts.
- Flash accounting. Callers
unlockthe PoolManager and do their work inunlockCallback. Each swap or liquidity change records a delta (what the caller owes or is owed) in transient storage (EIP-1153). Only the net amounts are settled at the end, and the call reverts unless every delta is zero. A three-hop swap moves tokens twice, not six times. - Native ETH as a pool currency, without wrapping to WETH.
- ERC-6909 claims, so frequent traders and LPs can keep balances inside the PoolManager instead of transferring tokens each time.
- Any fee and any tick spacing, including dynamic fees set by a hook.
- Hooks, attached at pool creation. A pool's identity, the
PoolKey, is(currency0, currency1, fee, tickSpacing, hooks), so the same pair can have many pools with different hooks.
Liquidity positions in v4 are managed through a PositionManager that still issues NFTs, and swaps typically go through the Universal Router, with approvals handled by Permit2.
How do v4 hooks work?
A hook is a contract the PoolManager calls at specific points. Which callbacks a hook implements is encoded in the lowest 14 bits of its address, so the PoolManager can tell from the address alone what to call. You deploy hooks with CREATE2 and search for a salt that produces an address with the right bits (v4-periphery's HookMiner utility does this).
| Callbacks | Typical uses |
|---|---|
beforeInitialize / afterInitialize | Validate pool parameters, set up per-pool state |
beforeAddLiquidity / afterAddLiquidity | Allowlists, lock-ups, liquidity mining accounting |
beforeRemoveLiquidity / afterRemoveLiquidity | Withdrawal fees, vesting |
beforeSwap / afterSwap | Dynamic fees, limit orders, oracles, MEV protection |
beforeDonate / afterDonate | Reward distribution |
Four ...ReturnDelta flags | Let the hook change the amounts owed, enabling custom curves |
Here is a hook that charges 0.05% on small swaps and 0.30% on large ones, using OpenZeppelin's uniswap-hooks base contract. The pool must be created with the dynamic fee flag for the override to apply:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import {BaseHook} from "@openzeppelin/uniswap-hooks/src/base/BaseHook.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {LPFeeLibrary} from "@uniswap/v4-core/src/libraries/LPFeeLibrary.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {SwapParams} from "@uniswap/v4-core/src/types/PoolOperation.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";
contract SizeBasedFeeHook is BaseHook {
using LPFeeLibrary for uint24;
uint24 public constant SMALL_FEE = 500; // fees are in hundredths of a bip: 500 = 0.05%
uint24 public constant LARGE_FEE = 3000; // 0.30%
uint256 public constant LARGE_SWAP = 100e18; // in units of the specified token
error MustUseDynamicFee();
constructor(IPoolManager _poolManager) BaseHook(_poolManager) {}
function getHookPermissions() public pure override returns (Hooks.Permissions memory p) {
p.beforeInitialize = true;
p.beforeSwap = true;
}
function _beforeInitialize(address, PoolKey calldata key, uint160) internal pure override returns (bytes4) {
if (!key.fee.isDynamicFee()) revert MustUseDynamicFee();
return IHooks.beforeInitialize.selector;
}
function _beforeSwap(address, PoolKey calldata, SwapParams calldata params, bytes calldata)
internal
pure
override
returns (bytes4, BeforeSwapDelta, uint24)
{
int256 amt = params.amountSpecified; // negative = exact input, positive = exact output
uint256 size = amt < 0 ? uint256(-amt) : uint256(amt);
uint24 fee = size >= LARGE_SWAP ? LARGE_FEE : SMALL_FEE;
return (IHooks.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, fee | LPFeeLibrary.OVERRIDE_FEE_FLAG);
}
}
BaseHook's external beforeSwap is restricted to the PoolManager and forwards to _beforeSwap; its constructor also checks that the deployed address matches getHookPermissions(), so a wrongly mined address fails at deployment rather than silently skipping callbacks.
What should developers watch for?
For hook authors:
- Only the PoolManager may call callbacks. If you write a hook without
BaseHook, enforce this yourself, or anyone can call your hook with fake parameters. senderis the router, not the user. The first callback argument is whoever called the PoolManager, usually a router contract. Don't use it as the trader's identity without a trusted router passing it inhookData.hookDatais caller-supplied. Treat it as untrusted input and authenticate anything that grants privileges, for example with a signature.- Return-delta hooks can move value. A hook with
beforeSwapReturnDeltacan take or give tokens. That is powerful and is exactly where audits should focus. - One hook serves many pools. Key per-pool state by
PoolId, and decide whether anyone may create a pool with your hook. - Gas and reverts affect every swap. A hook that reverts bricks its pool; one that loops over storage makes every trade expensive. Gas-cutting techniques for Solidity apply directly.
For integrators and LPs: a v4 pool is only as trustworthy as its hook. Check the hook's code, its permissions, whether it is upgradeable and who controls it before routing users' funds or adding liquidity. Swaps in any version are still exposed to sandwich attacks and other MEV, so set slippage limits.
Test hooks with v4-core's test utilities and Foundry fuzz and invariant tests, covering random swap sizes and directions, since bugs often hide at range edges and tick crossings.
Summary
| Uniswap v2 | Uniswap v3 | Uniswap v4 | |
|---|---|---|---|
| Liquidity | Full range, x * y = k | Concentrated in chosen tick ranges | Same as v3 |
| LP position | Fungible ERC-20 | NFT | NFT via PositionManager |
| Fees | 0.30% | Fixed tiers (0.01% to 1%) | Any static fee, or dynamic via hook |
| Architecture | One contract per pool | One contract per pool | Singleton PoolManager, flash accounting |
| ETH | WETH | WETH | Native ETH supported |
| Custom logic | None | None | Hooks at 10 callback points plus 4 delta flags |
Get the weekly commit
New blockchain deep dives every week.