Book a free call

How to Build a DEX Like Uniswap, Step by Step

A DEX like Uniswap swaps tokens in liquidity pools priced by formula, not an order book. It covers the AMM model, parts, fees, v4 hooks and mistakes.

Hertiv engineering teamReviewed by a senior engineer
8 min read
How to Build a DEX Like Uniswap, Step by Step
On this page

Short answer

A DEX like Uniswap is a decentralised exchange that prices trades with an automated market maker instead of an order book. Traders swap against liquidity pools held in smart contracts, and liquidity providers earn a share of the trading fees for supplying the tokens in those pools.

What is a DEX like Uniswap?

A DEX like Uniswap is a decentralised exchange where trades run against liquidity pools held in smart contracts instead of an order book. Anyone with a wallet can swap one token for another, and anyone can add tokens to a pool and earn a share of the trading fees.

Uniswap launched on Ethereum in November 2018, created by Hayden Adams, and it made the automated market maker (AMM) model the standard way to trade on chain. Instead of matching buyers with sellers, as an order book does, the protocol prices every trade from the ratio of the two tokens sitting in a pool.

A Uniswap-like exchange has no accounts, no deposits and no central operator. Listing a new token needs no permission: anyone can create a pool for a pair, add liquidity and start trading. That openness is also the risk, because anyone can list a worthless or fake token, so traders must check what they are buying.

How does the AMM price a trade?

The AMM prices a trade with the constant product formula, written as x times y equals k. The two numbers are the quantities of the two tokens in the pool, and their product must stay the same before and after a trade, apart from the fee.

Take a pool holding 20,000 TOKEN and 40,000 USDC. The price of one TOKEN is 40,000 divided by 20,000, which is 2 USDC. A trader who sends in 4,000 USDC receives about 1,818 TOKEN, because the pool must keep the same product: 44,000 USDC times 18,182 TOKEN equals the original 800,000,000.

The trader paid an effective price of about 2.20 USDC per TOKEN, 10 percent above the starting price. That gap is price impact, and it grows with the size of the trade relative to the pool. Deep pools keep the gap small, while thin pools make even modest trades expensive.

VersionLaunchedPricingFees
v2May 2020Full-range constant productFixed 0.30%
v3May 2021Concentrated liquidity rangesTiers of 0.05%, 0.30%, 1%
v4January 2025Concentrated ranges plus hooksDynamic fees via hooks

What parts does a Uniswap-like DEX need?

A Uniswap-like DEX is a small set of contracts plus a frontend. Each part has one job, and the parts only talk to each other through narrow interfaces, which keeps the system easier to test and audit.

PartWhat it does
FactoryCreates one pool per token pair
PoolHolds reserves, runs the pricing formula, tracks LP shares
RouterTakes a swap request, finds the path, moves the tokens
QuoterEstimates what a trade returns before it is sent
OracleReports a time-weighted average price other contracts can read
FrontendWallet connection, swap form and pool pages

The pool contract is the part that must be right. A general DEX build guide covers the same parts in less detail. It holds the funds, enforces the pricing rule and records who owns what share of the pool. A bug here can drain the pool, so teams test it hardest and audit it first.

The frontend is the only part users see, but it holds no funds. It reads pool data, builds the swap transaction and hands it to the user's wallet to sign. Keeping funds out of the frontend means a website bug cannot steal deposits.

How do liquidity providers earn fees?

Liquidity providers earn a share of the trading fees paid on every swap through their pool. The share matches the share of the pool they supplied, so a provider who supplied a quarter of the pool earns a quarter of the fees.

Uniswap v3 introduced fee tiers of 0.05 percent, 0.30 percent and 1 percent, so a pool can match its fee to the pair. Stable pairs with low price movement use the lowest tier, while volatile pairs use higher tiers to pay providers for the extra risk.

Concentrated liquidity lets a provider pick a price range instead of spreading capital across every possible price. Capital inside the active range earns fees, while capital outside the range earns nothing until the price moves back. The trade-off is impermanent loss: if the price moves far, the pool sells the token that rose, and the provider can end up with less value than simply holding both tokens.

How to build a DEX like Uniswap in 6 steps

Building a DEX like Uniswap means copying the model, not the code line for line. Clone-style guides such as the Aster-like DEX build follow the same pattern. The steps below follow the order teams usually work in.

  1. Choose the model first. A v2-style full-range pool is simpler to build, while a v3-style concentrated pool earns more per unit of capital but needs range management.
  2. Write the factory and pool contracts. The factory creates one pool per token pair, and the pool holds the reserves, enforces the pricing formula and tracks each provider's share.
  3. Add the router and quoter. The router moves tokens between trader and pool, and the quoter shows the expected output before the trader signs.
  4. Add the price oracle and fee logic. A time-weighted average price feed lets other contracts read a fair price, and the fee tier should match the pair: low for stable pairs, higher for volatile ones.
  5. Test on a testnet, then get an audit. Run swaps, liquidity changes, failed trades and edge cases on a test network, and have an independent auditor review the pool contract before real funds arrive.
  6. Seed liquidity and open trading. The team or early providers add the first liquidity so the first trades have a fair price, then the pool opens to everyone and fees start flowing to providers.

Launch with one or two pairs, not twenty. Deep pools give traders better prices than many thin ones, and the team can watch the first weeks before adding more.

How do hooks change what a pool can do?

Uniswap v4, launched on 31 January 2025, keeps the concentrated liquidity of v3 but adds hooks: small plug-in contracts that run custom logic at set points in a pool's life, before or after a swap, or when liquidity is added or removed.

A hook can set a dynamic fee that rises when the market is volatile, run on-chain limit orders, return part of the value from fast trades to providers, or restrict trading to wallets that pass a check. More than 150 hooks were already built when v4 launched, according to Uniswap's launch post.

Version 4 also holds every pool inside a single contract instead of one contract per pair, and settles trades by netting balances at the end of a transaction. That design cuts the gas cost of creating a pool sharply, and Uniswap's own estimate puts the saving at roughly 99 percent, while swaps through several pools also get cheaper.

Hooks add power, and they add risk. Each hook is extra code that can hold or move funds, so a hook needs the same testing and audit as the pool itself. A team that copies v4 should start with no hooks, then add one at a time.

Example (hypothetical): a first-week liquidity pool

Example (hypothetical): a team launches a TOKEN-USDC pool with 50,000 TOKEN and 100,000 USDC, so one TOKEN starts at 2 USDC. The team supplies 25 percent of the pool, and three early providers supply the rest.

In the first week the pool handles 200 trades worth 300,000 USDC in total. The fee tier is 0.30 percent, so the pool earns 900 USDC in fees. The team's 25 percent share is 225 USDC for the week.

One of those trades shows the pricing rule in action. A trader swaps 4,000 USDC into the pool and receives about 1,923 TOKEN, because the product of the reserves must stay fixed at 5,000,000,000. The effective price is about 2.08 USDC per TOKEN, so the trader accepts roughly 4 percent price impact on a trade that size.

By the end of the week the pool holds more USDC and fewer TOKEN than at launch, because traders bought TOKEN on balance. The providers' fees are the reward for that drift: the pool sold TOKEN as its price rose, which is the impermanent loss every provider accepts.

What are the common mistakes?

The most common mistakes when building a DEX like Uniswap are shipping unaudited pool code, even when a team offers DEX development help, launching with too little liquidity, and copying v3 or v4 features the team does not fully understand.

  • Skipping the audit. The pool contract holds the funds, so an independent audit before launch is not optional, and the audit should cover the router and any hooks too.
  • Launching with thin liquidity. A pool with little capital gives traders large price impact, which drives them to a deeper pool and leaves the new exchange with no volume.
  • Copying concentrated liquidity without a plan. Ranges need active management, and providers who set a narrow range and walk away can stop earning fees when the price moves out of it.
  • Trusting a spot price from one pool. A price read from a single small pool can be pushed by one large trade, so use a time-weighted average price or an outside oracle for anything that depends on the price.
  • No plan for bad trades. Failed swaps, stuck transactions and front-running need handling in the frontend and the router, or users pay for gas on trades that never settle.

Key takeaways

  • A Uniswap-like DEX prices trades with an AMM formula, not an order book: the pool's token ratio sets the price
  • The constant product rule (x times y equals k) means large trades move the price, and that gap is price impact
  • The core parts are a factory, a pool, a router, a quoter, an oracle and a frontend
  • Liquidity providers earn fees in proportion to their share of the pool, and v3 fee tiers match the fee to the pair
  • Uniswap v4 adds hooks and a single-contract design, but every hook needs the same audit as the pool

Frequently asked questions

Is Uniswap a company or a protocol?

Uniswap is a protocol: a set of smart contracts that anyone can use without permission. Uniswap Labs is the company that builds the protocol and the web app, but the contracts run on Ethereum and keep working even if the company stops.

Do I need a licence to launch a DEX like Uniswap?

It depends on where you operate and what the exchange lists. Some jurisdictions treat exchange operators as regulated businesses, and listing certain tokens can add securities rules. Get legal advice for your markets before launch, and do not rely on the contracts being decentralised as a defence.

How much liquidity does a new DEX need?

There is no fixed number. The pool must be deep enough that a normal trade for your users moves the price by only a small amount. Start with one or two pairs, seed them well, and add pairs only when the first pools hold their depth.

What is the difference between Uniswap v2 and v3?

Version 2 spreads each provider's capital across the whole price range at a fixed 0.30 percent fee. Version 3 lets providers pick a price range and choose a fee tier of 0.05, 0.30 or 1 percent, so capital earns more inside the range and nothing outside it.

What are hooks in Uniswap v4?

Hooks are plug-in contracts that run custom logic at set points in a pool's life, such as before or after a swap. A hook can set a dynamic fee, run limit orders, return value to providers, or restrict who can trade. Each hook is extra code that needs its own audit.

Can a DEX like Uniswap run on a chain other than Ethereum?

Yes. Uniswap v4 launched on ten chains, including Arbitrum, Base, Polygon, BNB Chain and Avalanche, and the contracts work on any chain that runs the Ethereum Virtual Machine. The model is the same; only the chain, the gas costs and the available tokens change.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: blockchain systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Planning a DEX like Uniswap? Talk to Hertiv about the build.

A senior engineer replies within one business day. Or see our DEX Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to contact@hertiv.com.

Prefer email? Write to contact@hertiv.com
Estimate costBook a call