Free sample lesson ยท Introduction to Uniswap v4 Hooks

Architecture Overview

What if a single contract could manage every liquidity pool simultaneously?

Lesson 1 of the Wondering course Introduction to Uniswap v4 Hooks

The Shift to a Singleton Model

Uniswap v4 introduces a radical change in how decentralized exchanges work.

In previous versions like v2 and v3, every new liquidity pair required its own unique smart contract. This made the system fragmented.

In v4, all pools live inside one single contract called the PoolManager. This centralized design is known as the Singleton pattern.

Uniswap Contract Architecture: v3 vs v4

Lowering Gas Costs for Everyone

Creating a new pool is now significantly cheaper. Developers do not need to deploy a whole new contract for every pair.

Swapping across multiple pairs is also more efficient. In older versions, tokens moved between different contracts for every step of a trade.

In the Singleton model, tokens stay put. The contract simply updates internal balances, saving users money on gas fees.

How the Singleton Model Saves Gas
  • No New Deployments: Developers do not need to deploy a whole new contract for every pair.
  • Internal Balances: Tokens stay put while the contract simply updates internal balances.
  • Reduced Gas Fees: Users save money on gas by eliminating multi-contract token transfers.

Efficiency Through Flash Accounting

Uniswap v4 uses a method called flash accounting to manage trades.

Instead of transferring tokens at every step of a multi-hop swap, the system tracks what is owed. It only requires a final transfer at the very end of the transaction.

This ensures the net balance of tokens owed to or by the contract is zero before the call finishes. It is a lean, high-speed way to handle liquidity.

Flash Accounting Execution Flow

A Foundation for Custom Logic

By centralizing the state of all pools, Uniswap v4 becomes a flexible foundation for developers.

This architecture makes it easier to build custom liquidity logic using Hooks.

Because the PoolManager handles the core accounting, developers can focus on creating unique features without worrying about the complex underlying transfers.

Uniswap v4 Architecture Layers

Putting It All Together: Designing a Hook with Singleton Architecture

Putting It All Together: Designing a Hook with Singleton Architecture
  • Centralize State: Rely on the centralized PoolManager contract instead of deploying custom pair contracts.
  • Track via Flash Accounting: Execute multi-hop swaps by tracking internal balances rather than moving tokens at every step.
  • Attach Custom Hook: Focus solely on writing custom hook logic while the singleton handles the heavy lifting of accounting and transfers.

Check yourself

In Uniswap v4, all liquidity pools are centralized inside a single contract called the _____.

  • PoolManager
  • FactoryManager

Answer: PoolManager

The Singleton pattern in Uniswap v4 centralizes all liquidity pools inside one contract named the PoolManager, reducing deployment and swap gas costs.

In older Uniswap versions, tokens _____ at every trade step, whereas in the v4 Singleton model, tokens remain put and the system updates _____.

Answer: In older Uniswap versions, tokens transferred between contracts at every trade step, whereas in the v4 Singleton model, tokens remain put and the system updates internal balances.

By avoiding intermediate token transfers between different contracts and simply updating internal balances, Uniswap v4 significantly reduces gas costs.

How does flash accounting reduce transaction costs during multi-hop swaps in Uniswap v4?

  • It deploys a temporary smart contract for each hop in the trade.
  • It tracks token deltas during trade steps and settles only the final net amount at the end.
  • It executes each hop as an independent, isolated blockchain transaction.
  • It eliminates gas fees completely by shifting computational execution off-chain.

Answer: It tracks token deltas during trade steps and settles only the final net amount at the end.

Flash accounting tracks balance deltas (what is owed) throughout intermediate steps of a multi-hop swap and only transfers physical tokens at the very end, provided the net balance owed is zero.

Keep going

This is lesson 1 of 9 in Introduction to Uniswap v4 Hooks. Next up:

  1. Hook Basics
  2. Lifecycle Events
  3. PoolManager Interaction
  4. Dynamic Fees
  5. Custom Oracles

Start this course free

Wondering turns any topic into short lessons you can read, watch, or listen to, with practice questions and an AI tutor.