How a DeFi DEX is actually put together

May 23, 20265 min readBy Harman Kamboj
Web3Engineering

People hear DEX and picture a single smart contract that swaps tokens. That contract exists and it is the smallest part of the system. The real DeFi DEX architecture is a stack of pieces that have to agree with each other across very different reliability models, from immutable on-chain code to a frontend you redeploy ten times a day. I have built parts of this and the interesting engineering is almost never where newcomers expect.

The on-chain core is small on purpose

The contracts that hold funds and execute swaps are deliberately minimal. Every line on chain costs gas, cannot be easily changed after deploy, and is a target for anyone looking to drain it. So the core does the least it can: hold liquidity, run the pricing math, move tokens, emit events. That is roughly it. The pricing math itself, whether a constant product curve or something more involved, is a few formulas. The discipline is in keeping everything else out of the contract.

This minimalism is a feature. The smaller the on-chain surface, the less there is to audit, the less there is to exploit, and the less you can break with an upgrade. Anything that does not absolutely need to be on chain belongs off chain, and most things do not need to be on chain.

The indexer is the unsung half

A DEX contract emits events and forgets them. It does not keep a list of your trades, your liquidity positions over time, or the price history of a pair. The chain has that information buried in its logs, but reading it directly for every UI request would be hopelessly slow. So you build an indexer that watches the contract events and turns them into a queryable database.

This is where a large share of the actual code lives. The indexer reconstructs pool reserves, tracks each provider's share, computes price candles, totals volume, and serves it all fast enough to feel instant. Tools like The Graph and Subsquid exist precisely for this job. Without a solid indexer you have a contract that works and an interface that cannot show anyone what is happening, which is not a usable product.

  • Pool state over time, so charts and analytics have history to draw from
  • Per-user positions and trade history, which the chain does not store in any convenient form
  • Derived metrics like volume, fees earned, and total value locked, precomputed so reads stay cheap
  • A clean API the frontend can hit without ever talking to a node directly for this data

The frontend carries the trust

The interface is where users decide whether to believe you with their money, so it does far more than render buttons. It quotes the expected output of a swap before they commit, calculates price impact and slippage, warns when a trade will move the market against them, and builds the transaction the wallet will sign. Get any of that subtly wrong and a user loses funds in a way that is technically their signature but really your interface's fault.

The frontend also has to handle the gap between clicking swap and the transaction confirming. Prices move in that window. A quote that was fair when shown can be unfair by the time it lands, which is why slippage protection is not a nice extra, it is the thing standing between your users and getting sandwiched. I treat the swap UI as safety-critical code, not as a form.

Where the pieces meet is the hard part

Each layer is manageable alone. The difficulty is the seams between them. The frontend quotes a price using indexed data, but the actual swap executes against live on-chain reserves that may have shifted since the index last updated. The indexer must stay close enough to the chain head that its numbers are trustworthy, while handling reorgs that can rewrite recent history out from under it. The contract is immutable while everything around it ships constantly.

Reconciling those different clocks and reliability guarantees is the real work. An indexer a few blocks behind shows slightly stale prices. A frontend that trusts stale prices quotes swaps that fail or execute at bad rates. The whole thing only feels solid when each layer accounts honestly for the freshness and trust level of the data it gets from its neighbors.

What I would tell someone starting

Do not start with the contract. Start by mapping the data flow: what happens on chain, how it gets indexed, how the frontend reads it, and how a transaction travels back the other way. Once that picture is clear, the contract is the easy part and the indexer plus the frontend safety logic is where you should spend your attention. That is the inversion most people get wrong, and it is the difference between a DEX that demos and one you would actually put your own money through.

Building something where this matters?

I am open to senior full-stack, Web3, or AI engineering roles, fully remote and any timezone. If the hard part of your product is fighting you, that is the work I like.

Get in touch →