Book a free call

What Is a DEX, and How Does It Work?

A DEX is a crypto exchange that runs on smart contracts, so you trade from your own wallet. Learn how pools set prices, what it costs, and the risks.

Hertiv engineering teamReviewed by a senior engineer
7 min read
What Is a DEX, and How Does It Work?
On this page

Short answer

A DEX, or decentralised exchange, is a crypto exchange that runs on smart contracts instead of a company holding your funds. You trade from your own wallet, prices are set by liquidity pools, and the trade settles on the blockchain.

What is a DEX?

A DEX, or decentralised exchange, is a crypto exchange that runs on smart contracts instead of a company holding your funds. You trade from your own wallet, and the trade is settled by code on a blockchain, not by an exchange matching orders behind closed doors.

The key difference is custody. On a centralised exchange, you deposit coins and the exchange holds them, so you trust the company to keep them safe and to pay you back. On a DEX, your coins stay in your wallet until the trade happens, and the smart contract moves them only when the swap is ready.

FeatureDEXCentralised exchange
Who holds your fundsYou, in your own walletThe exchange, in its accounts
How trades happenSmart contract and liquidity poolsOrder book run by the company
Sign-upConnect a wallet, no account neededAccount, email and ID checks
New tokensListed by anyone who adds liquidityListed by the exchange team
If the service failsYour funds stay in your walletYour funds may be frozen

A DEX is not a company, a website or an app in the usual sense. It is a set of contracts on a chain, and the website you use is just one front door to those contracts. For the steps involved in building one, see how to build a DEX.

How does a DEX work?

A DEX works through liquidity pools: pairs of tokens locked in a smart contract that anyone can trade against. When you swap token A for token B, the contract takes your A, adds it to the pool, and sends you B from the same pool, with the price set by the ratio of the two tokens in the pool.

The price moves with every trade. If many people buy B, the pool holds less B and more A, so the price of B rises. This is called an automated market maker, or AMM, and it replaces the order book that a normal exchange uses. No one sets the price; the pool ratio does.

People who add tokens to a pool are called liquidity providers, and they earn a small fee from every trade in that pool. Their share of the fee depends on how much of the pool they own. If the pool earns $1,000 in fees in a day and you own 10% of it, you earn $100 that day, minus any loss from price moves.

Your trade also needs a wallet and a small network fee. You connect a wallet, pick the two tokens, check the price and the fee, and confirm. The contract runs the swap, and the new tokens arrive in your wallet in the same transaction.

What can you trade on a DEX?

On a DEX you can trade any pair of tokens that has a liquidity pool, which usually means thousands of pairs. Popular pairs such as ETH and stablecoins have deep pools and tight prices, while new or small tokens have thin pools where a single trade can move the price a lot.

Beyond simple swaps, many DEXs offer limit orders, lending and staking through linked protocols. A limit order waits until the price reaches your target, while lending lets you earn interest on tokens you are not trading. These features are built from the same contracts, which are written through smart contract development, so they work without an account.

New tokens appear on DEXs first, often within hours of launch, because anyone can create a pool. That is both the strength and the risk: you can buy a token early, but you can also buy a token that has no real project behind it. Check the contract and the team before you trade a new token.

Some tokens cannot be traded on a DEX at all, such as stocks or fiat currencies, because they do not exist as tokens on the chain. A DEX only trades what lives on its chain, so a token on Ethereum cannot be swapped directly for a token on Solana without a bridge.

What are the fees on a DEX?

DEX fees have two parts: the pool fee and the network fee. The pool fee is a small share of each trade, often 0.3%, which goes to the liquidity providers. The network fee, called gas, pays the chain to run the contract, and it changes with how busy the network is.

Gas is the part that surprises new users. A swap that costs $0.30 in pool fees can cost $5 or $50 in gas on a busy chain, and the gas is paid even if the trade fails. On cheaper chains or layer-2 networks, the same swap can cost a few cents, which is why many traders moved there.

Slippage is a third cost. If the pool is thin, your trade moves the price, so you receive less than the quoted price. Most DEXs let you set a slippage limit, such as 1%, and the trade fails instead of going through at a bad price. Set the limit low on large trades, or split the trade into smaller ones.

Compare the total cost, not just the pool fee. A DEX with a 0.05% fee on an expensive chain can cost more than a DEX with a 0.3% fee on a cheap chain, once gas is counted. Check both numbers before you trade. For the wallet side of trading, see crypto wallet development use cases.

Example (hypothetical): swapping $1,000 of ETH for a stablecoin

Example (hypothetical): a trader wants to swap $1,000 of ETH for USDC on a DEX. The pool charges a 0.3% fee, which is $3, and the network fee is $2, so the total cost is $5. The trader receives about $995 of USDC, and the swap settles in under a minute.

The same trader tries a thin pool for a new token. They swap $1,000, but slippage is 5%, so they receive only $950 of the token, and the network fee is another $2. The total cost is $52, more than ten times the first trade, because the pool was too small for a $1,000 order.

The lesson is to check the pool size before a trade. If the pool holds $10,000, a $1,000 trade moves it by about 10%, while a $100 trade moves it by about 1%. Split large trades, or use a pool with more liquidity, and the cost stays close to the quoted price.

What are the risks of using a DEX?

The risks of a DEX start with smart contract bugs. The contracts hold real money, and a bug can let someone drain a pool. Stick to DEXs that have been audited and used for a long time, and be careful with new pools that have no audit.

A second risk is fake tokens. Anyone can create a token with the same name as a real one, so check the contract address against the project's own website before you trade. Buying the wrong token is one of the most common ways people lose money on a DEX.

A third risk is impermanent loss for liquidity providers. If the two tokens in a pool change price relative to each other, the provider can end up with less value than if they had simply held the tokens. The trading fees may cover the loss, but not always, so check the numbers before you add liquidity.

Finally, there is no support desk. If you send tokens to the wrong address or approve a bad contract, no one can reverse it. Double-check the address, the token and the amount before you confirm, and keep most of your funds in a separate wallet.

What are the common mistakes?

The most common DEX mistakes are trading in thin pools, approving unlimited spending, and trusting a token name instead of its contract address. Each one is easy to avoid with a short checklist.

  • Trading in a thin pool. Check the pool size first; split large trades if the pool is small.
  • Unlimited approvals. Approve only the amount you need, and revoke old approvals.
  • Trusting the token name. Always check the contract address on the project's own site.
  • Ignoring gas. A cheap pool fee means little on a busy chain; check the total cost.
  • No slippage limit. Set a limit so a bad price fails the trade instead of going through.

Key takeaways

  • A DEX settles trades with smart contracts; you keep custody of your funds
  • Prices come from the ratio of tokens in a liquidity pool, not an order book
  • Liquidity providers earn pool fees but face impermanent loss
  • Total cost is pool fee plus gas plus slippage, so check all three
  • Check the contract address, the pool size and the audit before you trade

Frequently asked questions

Do I need an account to use a DEX?

No. You connect a wallet and trade from it, with no account, email or ID check. The trade is settled by a smart contract, and your funds stay in your wallet until the swap runs.

Is a DEX safer than a centralised exchange?

It depends on the risk. A DEX removes the risk of an exchange freezing your funds, but it adds smart contract risk and fake tokens. Use audited DEXs and check contract addresses before you trade.

What is a liquidity pool?

A liquidity pool is a pair of tokens locked in a smart contract that anyone can trade against. Traders swap against the pool, and the people who supplied the tokens earn a share of the trading fees.

What is impermanent loss?

Impermanent loss is the loss a liquidity provider faces when the two tokens in a pool change price relative to each other. The provider can end up with less value than if they had simply held the tokens, though fees may cover the gap.

Can I trade any token on a DEX?

You can trade any pair that has a liquidity pool, which is usually thousands of pairs. New tokens appear on DEXs first, but thin pools mean high slippage, so check the pool size before a large trade.

What happens if a DEX trade fails?

A failed trade does not move your tokens, but you still pay the network fee for the attempt. Failed trades often come from slippage limits or low gas, so check both before you retry.

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

Building a decentralised exchange for your users?

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 [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call