Foundations · Exchanges, DEX & DeFi

Decentralized Exchanges (DEX)

A decentralized exchange is a set of smart contracts on a blockchain that lets people trade tokens directly from their own wallets, with no company holding their funds.

  • Beginner
  • 5 min read
  • Updated

Why it matters

A decentralized exchange lets two tokens change hands with no company in the middle. Nobody holds the trader’s funds, opens an account or approves a withdrawal.

That removes some of the risks of a centralized exchange and replaces them with others. The custodian is gone, and so is the help desk. Knowing which risks leave and which arrive is the purpose of this page.

How it works

What happens in a DEX swap
  1. Trader sends token AInto the liquidity pool.
  2. Pool rebalancesFormula sets how much B comes out.
  3. Trader receives token BMinus a fee paid to liquidity providers.
There is no order book and no counterparty waiting. The trader trades against the pool.

A decentralized exchange, or DEX, is a set of smart contracts: programs stored on a blockchain that run as written. A trader connects a wallet, chooses two tokens and signs a transaction. In that single transaction, one token leaves the wallet and the other arrives. If any part fails, the whole transaction is undone and the tokens stay where they were, although the network fee is still spent.

Most DEXs do not use an order book. They use an automated market maker: a pool of two tokens and a formula that sets the price, explained in AMMs and liquidity pools. Some DEXs do run order books, fully or partly on-chain.

In plain words

A vending machine has no shopkeeper. Coins go in, a mechanism follows fixed rules, and a product drops out. Nobody asks who you are, and nobody is there to help if you press the wrong button.

The parts of a DEX

What a DEX is made of
InterfaceA website or app. Convenient, replaceable, and able to be copied by a fake.
Your walletHolds the keys and signs every action.
Exchange contractsCode on the blockchain that holds the pools and performs the swaps.
BlockchainPuts transactions in order, records them and charges gas.
The exchange is the contract layer. The interface on top of it is only one way of reaching it.

The website most people call “the DEX” is only an interface. The exchange itself is the contract code on the blockchain, which can be reached through other interfaces too.

This matters for security. A copied website can look identical while asking the wallet to sign something quite different, which is a common form of phishing.

Permission before trading

How a token approval works
  1. You approveYou let a contract spend a token from your wallet.
  2. Allowance is storedOften unlimited, and it stays until revoked.
  3. Contract can spendUp to the allowance, without asking again.
An approval is not a payment, but it is standing permission. A malicious or hacked contract can use it later.

A contract cannot take a token from a wallet without permission. Before the first swap of a given token, the wallet signs an approval that lets the exchange contract spend it. The approval is a separate transaction, it is often set to an unlimited amount, and it stays in force until it is revoked. Token approvals covers why that matters.

What a swap costs

Cost What it is
Swap fee A percentage of the trade, paid mostly to the people who supplied the pool’s tokens
Price impact The trade itself moves the pool’s price, more so in a small pool
Slippage The price can change between the quote and the moment the transaction is processed
Gas The network fee, paid in the chain’s own coin, even if the swap fails

Swaps and routing explains each of these in detail.

For the people who supply a pool, the swap fee is what they are paid. That payment is variable, is not guaranteed, is sometimes topped up with a token whose price can fall, and comes with the risks of the pool.

Going deeper

Anyone can list anything. Creating a pool needs no permission, so there is no listing review. Tokens that copy the name and symbol of a known asset are common. A token’s identity is its contract address, not its name. See rug pulls and honeypots.

Everything is public. Each swap is recorded permanently against a wallet address. Transactions that are waiting to be processed can often be seen by others, who may trade ahead of them.

Decentralization is a matter of degree. Some exchange contracts cannot be changed by anyone. Others can be upgraded or paused by a small group holding special keys, and the interface is usually run by a company.

Cost depends on the chain. Gas on a busy base chain can exceed the value of a small swap. The same design on a Layer 2 network usually costs less, with that network’s own trade-offs.

A hypothetical first swapHypothetical illustration

Made-up numbers. A wallet holds 500 of a stablecoin S and a little of the chain’s own coin for gas. Token T is priced at 100 S in the pool.

  1. Approval. The wallet lets the exchange contract spend S. Gas: worth 0.40 S.
  2. Swap. 200 S goes in. The swap fee is 0.3% of 200 = 0.60 S, which leaves 199.40 S. At a price of 100 that would be 1.994 T. Price impact of about 0.2% reduces it to about 1.99 T. Gas: worth 0.90 S.

In total the trader gave up 200 S, plus gas worth 0.40 + 0.90 = 1.30 S, and received about 1.99 T. The all-in cost is 201.30 / 1.99 = about 101.16 S per T, against a displayed price of 100.

Had the wallet held no coin for gas, neither transaction could have been sent.

What it does NOT mean

  • Decentralized does not mean safe. Removing the custodian removes one kind of risk. Contract bugs, fake tokens and user error remain.
  • Non-custodial does not mean nobody else can move your tokens. An approval gives a contract standing permission to spend them.
  • A token being tradable on a DEX is not an endorsement. Anyone can create a pool for any token.
  • No account does not mean no trace. Every swap is public and permanently linked to a wallet address.
  • “DEX” is not one design. Pools, order books and hybrids behave differently, and so do the chains they run on.

Common misunderstandings

  1. Identifying a token by its name. Names and symbols can be copied freely. Only the contract address is unique.
  2. Forgetting gas. Every action costs a network fee, including approvals and failed swaps.
  3. Treating the website as the exchange. The interface can be imitated, taken offline or changed while the contracts stay the same.
  4. Leaving old approvals in place. Permission given once remains after the swap is finished.
  5. Expecting someone to fix a mistake. A confirmed transaction cannot be reversed, and there is no support desk with the power to do so.

Risks and limits

  • Contract failure. A bug or exploit can drain a pool. See smart contract risk.
  • Thin pools. A small pool gives large trades a poor price and makes the price easy to move.
  • Transaction ordering. Others can see a pending swap and trade around it at the trader’s expense.
  • Irreversibility. Tokens sent to the wrong contract or swapped for a worthless token are usually gone.
  • Network conditions. Congestion can raise gas sharply or delay a swap until its price is stale.
  • Legal status. How DEXs and their users are treated differs between jurisdictions and is still changing.

Key takeaway

A decentralized exchange is code on a blockchain that swaps tokens straight from a user’s wallet. It replaces reliance on a company with reliance on the code and on the user’s own care.

Sources and further reading