New to Rust? Grab our free Rust for Beginners eBook Get it free →
What Is a Decentralized Exchange (DEX)? How DEX Swaps Work

A decentralized exchange (DEX) lets you swap tokens from a self-custody wallet through transactions on a blockchain, while pool depth, trade size, and fees can change the quote before the transaction is confirmed.
What is a decentralized exchange?
A DEX is a marketplace where crypto trades are settled by blockchain transactions rather than an exchange operator matching every account in an internal ledger. Coinbase describes a DEX as a peer-to-peer marketplace, while protocols can use smart contracts, onchain order books, or liquidity pools to carry out the trade.
Because you approve the transaction from your wallet, you must verify the token, network, recipient contract, and permission before signing.
How a DEX swap works
A swap begins with a wallet request and ends when the network confirms a transaction, while the interface can only estimate the output before the contract applies the protocol rules.
}
The wallet signs an approval or swap
Your wallet signs a transaction that can grant a token allowance, call a router contract, or both, depending on the protocol and token.
The interface shows a route and expected output
The interface can route a swap through one pool or several pools. Inspect the input token, output token, network, expected output, minimum received amount, price impact, and gas estimate before confirming.
The pool or order book settles the trade
An order-book DEX records bids and asks, while an automated market maker (AMM) lets you trade against reserves held by a pool contract with a pricing formula that changes as reserves change.
Uniswap documents this AMM model with a constant-product rule, so a larger trade relative to the reserves changes the price more and a quote needs price-impact and slippage controls.
See AMM price impact with a small calculation
The following Python example models a constant-product pool with 100 ETH and 300,000 USDC. It is a teaching model, not a production quote engine, because deployed pools can use concentrated liquidity, routing, and protocol-specific fee logic.
from decimal import Decimal
def quote_swap(reserve_in, reserve_out, amount_in, fee_rate=Decimal('0.003')):
amount_in_after_fee = amount_in * (Decimal('1') - fee_rate)
amount_out = reserve_out * amount_in_after_fee / (reserve_in + amount_in_after_fee)
return amount_in_after_fee, amount_out
reserve_eth = Decimal('100')
reserve_usdc = Decimal('300000')
amount_in = Decimal('10')
net_input, amount_out = quote_swap(reserve_eth, reserve_usdc, amount_in)
spot_price = reserve_usdc / reserve_eth
execution_price = amount_out / amount_in
price_impact = (spot_price - execution_price) / spot_price * Decimal('100')
print(f'Input after 0.3% fee: {net_input:.2f} ETH')
print(f'Output: {amount_out:.2f} USDC')
print(f'Spot price: {spot_price:.2f} USDC per ETH')
print(f'Execution price: {execution_price:.2f} USDC per ETH')
print(f'Price impact: {price_impact:.2f}%')
Run the file with python amm_math.py. Its output returns 27,198.33 USDC for a 10 ETH input after a 0.3% fee, while the execution price falls below the starting spot price because the trade changes the pool balance.
DEX versus centralized exchange
A centralized exchange can offer a familiar account workflow and may provide support tools, fiat rails, or deeper liquidity for some markets. A DEX puts transaction approval and wallet security closer to you.
| Question | DEX | Centralized exchange |
|---|---|---|
| Who approves the trade? | You sign a wallet transaction. | You place an order through the exchange account. |
| Where are assets held before the trade? | Usually in your wallet or a protocol contract during execution. | Usually in an exchange-controlled account. |
| How is price formed? | Order books, pool formulas, or routing across pools. | Usually an exchange order book and internal matching system. |
| What must you check? | Wallet permissions, contract, token, network, price impact, and gas. | Account security, order type, fees, withdrawal rules, and custody terms. |
Neither structure removes the need to assess the asset and the venue. FINRA warns that crypto assets can be volatile and that fraud can involve impersonation or unsafe storage requests.
What to check before confirming a DEX swap
A swap screen is a transaction preview, not a guarantee that every detail is safe. Use it to verify the transaction boundaries before your wallet signs.
- Confirm the token contract and network from an official project source, not from a search ad or unsolicited message.
- Read the minimum received amount and price impact. High price impact can mean the trade is large for the available liquidity.
- Review the allowance request. Remove unused allowances when your wallet or an approval-management tool supports it.
- Check the gas estimate and keep enough native network token to complete the transaction.
Slippage tolerance is a boundary, not a prediction. Uniswap explains that it defines how far the price may move before the trade fails, so a setting that is too loose can accept a worse result while a setting that is too tight can cause the transaction to revert.
When a DEX is useful
A DEX can fit when you need to swap supported onchain tokens from your own wallet and can inspect the transaction details. It is not a substitute for evaluating an asset, protecting wallet credentials, or understanding smart-contract risk.
If the pool calculation feels opaque, start by comparing the quoted output with the minimum received amount and price impact before signing. That single check connects the visible number in the interface to the pool mechanics behind it.




