DeFi Basics
What Is a Decentralized Exchange (DEX)?
Learn how decentralized exchanges (DEXs) work, how they differ from centralized exchanges, and the role of automated market makers and liquidity pools in DeFi trading.
Understand how automated market makers work, the role of liquidity pools in decentralized trading, how AMMs differ from order books, and the trade-offs including slippage and impermanent loss.
An automated market maker is one of the foundational innovations that made decentralized finance possible. Before AMMs, decentralized trading was clunky and illiquid because it tried to replicate the order book model used by centralized exchanges. AMMs threw out the order book entirely and replaced it with something radically different: mathematical formulas and liquidity pools.
The result was a breakthrough that enabled anyone to trade any token at any time without needing a counterparty on the other side of the trade. AMMs like Uniswap, Curve, and Balancer now facilitate billions of dollars in daily trading volume and serve as critical infrastructure for the entire DeFi ecosystem, including lending and borrowing platforms.
On a traditional exchange, whether it is the New York Stock Exchange or a centralized crypto exchange like Coinbase, trading works through an order book. Buyers place bids (the price they are willing to pay), and sellers place asks (the price they are willing to accept). When a bid and an ask meet, a trade is executed.
Professional market makers play a crucial role in this system. They continuously place both buy and sell orders, providing liquidity and earning the spread between bid and ask prices. Without market makers, many assets would have thin order books, wide spreads, and poor execution for traders.
Early attempts to build decentralized exchanges used on-chain order books, but they ran into several problems:
AMMs solved all of these problems with an entirely new approach.
Instead of an order book, an AMM uses a liquidity pool: a smart contract that holds reserves of two (or more) tokens. Anyone can deposit tokens into the pool and become a liquidity provider (LP). In return, they receive LP tokens that represent their share of the pool.
When a trader wants to swap Token A for Token B, they do not need to find a counterparty. They simply interact with the pool's smart contract, depositing Token A and receiving Token B. The pool's algorithm determines the exchange rate.
The most iconic AMM formula is the constant product formula, popularized by Uniswap:
x * y = k
Where:
When a trader buys Token A, they add Token B to the pool and remove Token A. This changes the ratio of x to y, which changes the price. The formula ensures that as one token becomes scarcer in the pool, its price rises, and as the other token becomes more abundant, its price falls.
This creates a self-adjusting pricing mechanism that requires no central authority, no order matching engine, and no professional market makers.
Imagine a pool with 100 ETH and 200,000 USDC. The constant product k = 100 * 200,000 = 20,000,000. The implied price of ETH is 200,000 / 100 = 2,000 USDC.
Now a trader wants to buy 1 ETH. After the trade, the pool must maintain k = 20,000,000. With 99 ETH remaining, the USDC balance must be 20,000,000 / 99 = 202,020.20 USDC. The trader pays 2,020.20 USDC for 1 ETH, slightly more than the 2,000 USDC spot price. This premium is the slippage.
Slippage is an inherent property of AMMs. Every trade moves the price because it changes the token ratio in the pool. The larger the trade relative to the pool size, the more the price moves. A trade that represents 1% of the pool will cause noticeably more slippage than a trade representing 0.01%.
This is why pool size matters enormously. Large liquidity pools offer better execution with less slippage, which is why major AMMs aggressively compete for liquidity.
The classic x * y = k formula provides a simple, general-purpose AMM that works for any token pair. It is the most widely deployed AMM model and works well for volatile asset pairs. However, it is capital-inefficient because liquidity is spread across the entire price range from zero to infinity.
Uniswap v3 introduced concentrated liquidity, allowing LPs to provide liquidity within specific price ranges rather than across the entire curve. This dramatically improves capital efficiency because liquidity is concentrated where trading actually occurs. An LP providing liquidity in the $1,900-$2,100 range for ETH/USDC can earn the same fees as an LP providing 4,000x more capital across the full price range.
The trade-off is complexity. LPs must actively manage their positions, adjusting price ranges as the market moves. If the price moves outside their range, they stop earning fees and may suffer greater impermanent loss.
Curve Finance designed an AMM formula optimized for assets that should trade near a 1:1 ratio, such as different stablecoins (USDC/USDT/DAI) or different wrapped versions of the same asset (wBTC/renBTC). The StableSwap curve allows extremely low slippage for trades between similar assets, often achieving swap rates within a few basis points of 1:1.
Balancer extends the constant product formula to support pools with more than two tokens and non-equal weighting. Instead of a 50/50 pool, you could create an 80/20 pool or even a pool with eight different tokens at various weights. This opens up use cases like self-rebalancing index funds and liquidity bootstrapping pools.
AMMs serve as a critical price discovery mechanism for the DeFi ecosystem. When arbitrageurs trade on AMMs to bring prices in line with centralized exchanges, the AMM pool prices converge with broader market prices. This on-chain price data can then be used by other protocols, including lending platforms and derivatives markets.
AMMs and lending protocols are deeply intertwined parts of the DeFi stack. This relationship operates on several levels:
The borrowing leg of that strategy is exactly what Borrow is built for — comparing offers from Aave v3, Morpho Blue, and supported CeFi lenders so the borrower can secure the cheapest USDC before deploying it anywhere else.
AMMs are a prime example of DeFi composability. They serve as a base layer that other protocols build upon. Yield aggregators route trades through AMMs to rebalance portfolios. Options protocols use AMMs for hedging. Aggregators like 1inch combine multiple AMMs to find the best execution path for a trade.
Impermanent loss is the most discussed risk of providing liquidity to an AMM. It occurs when the price of tokens in the pool diverges from the price at which the LP deposited them. The AMM's constant product formula means that as one token's price rises, the pool automatically sells that token and accumulates the other, resulting in the LP holding less of the appreciating asset than they would have if they simply held both tokens.
The loss is called "impermanent" because it reverses if prices return to their original ratio. However, if an LP withdraws while prices have diverged, the loss becomes permanent.
For traders, slippage is a real cost. Large trades in shallow pools can result in execution prices significantly worse than the quoted mid-price. Experienced DeFi users learn to check slippage estimates before confirming trades and may split large orders across multiple pools or use aggregators to minimize price impact.
Like all DeFi protocols, AMMs are only as secure as their underlying smart contracts. Bugs in AMM code have historically led to exploits where attackers drain liquidity pools. Well-audited, battle-tested AMMs like Uniswap and Curve have strong security track records, but risks can never be fully eliminated.
Sandwich attacks are a form of front-running where an attacker detects a pending AMM trade, places a buy order before it to push up the price, lets the victim's trade execute at the worse price, and then sells immediately after for a profit. These attacks extract value from regular traders and are a significant concern in AMM design.
The AMM space has evolved rapidly since Uniswap v1 launched in 2018. Each generation has addressed specific limitations:
Layer 2 scaling solutions have been transformative for AMMs. Lower transaction costs on L2s make it viable for smaller traders to swap tokens and for LPs to actively manage concentrated liquidity positions. AMMs on L2s like Arbitrum, Optimism, and Base have seen rapid growth as gas costs on Ethereum mainnet priced out smaller participants.
Common Questions
An automated market maker (AMM) is a type of decentralized exchange protocol that uses mathematical formulas and liquidity pools to facilitate token swaps instead of traditional order books. Instead of matching buyers with sellers, AMMs allow users to trade against a pool of tokens locked in a smart contract. The price of each token is determined algorithmically based on the ratio of tokens in the pool.
Related Questions
DeFi Basics
Learn how decentralized exchanges (DEXs) work, how they differ from centralized exchanges, and the role of automated market makers and liquidity pools in DeFi trading.
DeFi Basics
Learn what yield farming is in DeFi, how it works, the strategies involved, and the risks and rewards of farming crypto yields across decentralized protocols.
DeFi Basics
Learn what liquidity pools are in DeFi, how they work, why they matter for decentralized trading and lending, and the risks and rewards of providing liquidity.
DeFi Basics
Learn what a DeFi protocol is, how decentralized finance protocols work, the different types of DeFi protocols, and how they are governed by their communities.