How DEXs Work
What is a DEX
A DEX (Decentralized Exchange) is a platform that exchanges tokens through smart contracts without a centralized intermediary. Unlike centralized exchanges (CEX) like Binance or Coinbase, no one holds custody of user assets and no one can censor trades.
Since Uniswap launched in 2018, DEXs have become core DeFi infrastructure. Daily trading volume now reaches billions of dollars, and some tokens are only traded on DEXs.
CEX vs DEX
| Category | CEX (Centralized Exchange) | DEX (Decentralized Exchange) |
|---|---|---|
| Asset Custody | Exchange holds | Stays in user wallet |
| Account Required | KYC needed | Just connect wallet |
| Listing | Exchange decides | Anyone can create pool |
| Trade Execution | Order book matching | Smart contracts |
| Speed | Very fast | Needs block confirmation time |
| Fees | Low (~0.1%) | Medium (0.3%) + gas |
| Liquidity | High | Varies by pool |
| Risk | Exchange bankruptcy, hacks | Smart contract bugs |
When CEX is Better
- Large trades (millions of dollars or more)
- Fiat currency on/off ramps
- Advanced margin/futures features
- Trading requiring fast order execution
When DEX is Better
- Want direct asset custody
- Trading new/small cap tokens
- Privacy priority (no KYC)
- Integration with DeFi protocols
AMM (Automated Market Maker)
Order Book vs AMM
Traditional exchanges use the order book method. Buyers and sellers place orders at desired prices, and trades execute when prices match.
Most DEXs use the AMM (Automated Market Maker) method. Instead of order books, trades can happen anytime using assets deposited in liquidity pools. Prices are automatically determined by mathematical formulas.
Liquidity Pool
A liquidity pool is a smart contract where two types of tokens are deposited as a pair.
ETH-USDC Pool Example:
- Pool has 1,000 ETH + 3,000,000 USDC
- You can buy ETH or buy USDC from this pool
- The counterparty is not another person but the pool itself
Constant Product Formula
The most basic AMM formula used by Uniswap V2:
x * y = k
- x: Quantity of token A in pool
- y: Quantity of token B in pool
- k: Constant (maintained before and after trades)
When a trade occurs, one token decreases and the other increases, but the product (k) stays constant. This formula automatically determines the price.
Price Determination Mechanism
Pool Ratio = Price
If a pool has 1,000 ETH and 3,000,000 USDC:
- 1 ETH = 3,000 USDC
- 1 USDC = 0.000333 ETH
Price is determined by the ratio of the two tokens.
Trades and Price Changes
When someone swaps 100 ETH for USDC:
- 100 ETH added to pool (1,000 -> 1,100 ETH)
- To maintain k, USDC must decrease
- New USDC balance: k / 1,100 = 2,727,273 USDC
- User receives: 3,000,000 - 2,727,273 = 272,727 USDC
- Effective price: 272,727 / 100 = 2,727 USDC per ETH
The trade executed at a lower price than the original (3,000). This is price impact.
Price Impact
The larger the trade relative to the pool, the greater the price impact.
| Trade Size (vs Pool) | Approximate Price Impact |
|---|---|
| 0.1% | ~0.2% |
| 1% | ~2% |
| 5% | ~10% |
| 10% | ~20%+ |
Always check price impact when making large trades. Trading large amounts in low-liquidity pools can result in very unfavorable prices.
Slippage
What is Slippage
Slippage is the difference between expected price and actual execution price. There are two causes:
- Price impact: My trade itself moves the price
- Price movement: Price changes from other trades while my transaction is pending
Setting Slippage Tolerance
When swapping on a DEX, you set slippage tolerance.
- Too low (0.1%): Transaction fails with slight price movement
- Too high (5%+): Risk of executing at unfavorable price
- Typical setting: 0.5~1%
Higher slippage settings may be needed in volatile markets or for low-liquidity tokens.
Slippage Protection
Major DEXs like Uniswap have slippage protection. If the set tolerance is exceeded, the transaction automatically cancels, preventing execution at worse-than-expected prices.
Front-running and MEV
Front-running
Front-running is seeing another user's transaction and cutting in front to profit.
Scenario:
- User A submits large ETH buy transaction
- Bot spots this in mempool (pending transactions)
- Bot pays higher gas to buy ETH first
- User A's trade pushes price up
- Bot immediately sells for profit
User A buys at a higher price, and the bot takes the difference.
Sandwich Attack
A variation of front-running that inserts trades before and after a user's transaction.
- Bot buys before user (price rises)
- User's trade executes (buys at higher price)
- Bot sells right after user (takes profit)
MEV (Maximal Extractable Value)
MEV is the maximum value that block producers can extract by manipulating transaction order. Front-running, sandwich attacks, etc. are all forms of MEV.
MEV Protection Methods
- Use MEV protection RPC: Flashbots Protect, MEV Blocker
- Private transactions: Bypass public mempool
- Lower slippage tolerance: Reduces attack profitability
- Use DEX aggregators: 1inch, Cowswap (MEV protection features)
Major DEX Types
Uniswap Style (General Purpose AMM)
- Trade any token pair
- Based on constant product formula
- Uniswap, SushiSwap, PancakeSwap
Curve Style (Stable Swap)
- Optimized for stablecoin trades
- Uses low slippage formula
- Curve, Ellipsis
Concentrated Liquidity
- LPs specify price ranges
- Improved capital efficiency
- Uniswap V3, Trader Joe V2
Order Book DEX
- Traditional order book style like regular exchanges
- Supports more sophisticated order types
- dYdX, Vertex
DEX Aggregators
What is an Aggregator
A DEX aggregator is a service that compares liquidity across multiple DEXs to find the optimal trading route.
You can often get better prices by splitting trades across multiple DEXs rather than executing on a single DEX. Aggregators automatically perform this optimization.
Major Aggregators
| Aggregator | Chains | Features |
|---|---|---|
| 1inch | Multi-chain | Most popular, Fusion mode |
| Jupiter | Solana | Largest on Solana, limit orders |
| Paraswap | Multi-chain | Institutional features |
| Cowswap | Ethereum | MEV protection, batch auctions |
Benefits of Using Aggregators
- Better prices (optimal route finding)
- Gas savings (efficient routing)
- MEV protection features (some)
- Convenience (single interface)
DEX Usage Precautions
Token Verification
Anyone can list tokens on DEXs. There are many scam tokens, so:
- Verify contract address: Get address from official sources
- Use CoinGecko/CoinMarketCap links: Verified addresses
- Check liquidity: Very low liquidity is a warning sign
- Honeypot check: Verify selling isn't blocked
Liquidity Check
Check the pool's liquidity before trading. If liquidity is too low:
- Price impact is very large
- Rug pull (liquidity removal) risk
- Unfavorable price when selling
Price Impact Check
Always check price impact numbers for large trades. Over 5% is very unfavorable - consider splitting the trade or trying a different DEX.
Summary
A DEX is a decentralized exchange that exchanges tokens through smart contracts without intermediaries. Most DEXs use the AMM method, where prices are automatically determined by liquidity pools and mathematical formulas. Understanding slippage and price impact and setting them appropriately is important, and MEV protection measures are also necessary. Using DEX aggregators helps you trade at optimal prices.
Next article: AMM and Liquidity Pools - How Prices Are Determined