Uniswap V4 Hook Demo

See afterSwap in action

This is a live demo of a Uniswap V4 hook tracking swap statistics onchain. Swap below and watch the stats update in real-time!

Live Pool Statistics

Pool:WETH / MOLT
Fee:0.10%
Network:Base Mainnet
Total Swaps
0
WETH Volume
0
MOLT Volume
0
Last Swap
Never

Trigger the hook with a swap

Swap directly through the V4 pool to trigger the afterSwap hook. Use small amounts — limited liquidity.

Connect your wallet to swap

The afterSwap hook lifecycle

01

User Swaps

A user swaps WETH for MOLT (or vice versa) on the Uniswap V4 pool that has our hook attached.

02

Hook Triggers

After the swap executes, the PoolManager calls our afterSwap function with the swap details.

03

Stats Updated

We record the swap count, volumes, and timestamp. An event is emitted for off-chain indexing.

SwapStats.sol
/// @notice Called after every swap
function _afterSwap(...) internal override {
PoolStats storage stats = poolStats[poolId];
// Update statistics
stats.totalSwaps++;
stats.totalVolume0 += _abs(amount0);
stats.totalVolume1 += _abs(amount1);
stats.lastSwapTimestamp = block.timestamp;
emit SwapRecorded(poolId, ...);
return (selector, 0); // No delta modification
}

Understanding V4 Hooks

Hook Permissions

Hooks declare which lifecycle events they want to intercept. SwapStats only uses afterSwap — the minimal permission needed for read-only analytics.

✓ afterSwap: true
✗ beforeSwap: false
✗ beforeSwapReturnDelta: false

Return Delta

Hooks can modify swap amounts by returning a non-zero delta. SwapStats returns 0 — it observes swaps without modifying them.

return (selector, 0);
// Safe: no rug risk, pure analytics

Onchain Storage

Stats are stored directly in the hook contract. No external indexer needed — query the blockchain directly for real-time data.

mapping(PoolId => PoolStats) poolStats;

Event Emission

Events enable off-chain tracking and live updates. This UI listens for SwapRecorded events to show real-time swap activity.

event SwapRecorded(
PoolId poolId, uint256 totalSwaps, ...
);