DeFi and On-Chain Trading
DEX Mechanics
Trading Without an Order Book
On traditional exchanges, trading happens through an order book. Buyers place bids and sellers place asks, and the exchange's central system matches them up. But what if there's no central matchmaker? This is the world of decentralized exchanges (DEXs), which run on automated smart contracts.
Instead of an order book, most DEXs use an Automated Market Maker (AMM). An AMM is a protocol that facilitates trading using pools of assets supplied by other users. Think of it like a self-service currency exchange machine. You put in one currency, and it gives you another based on a predetermined rate. The 'machine' itself is a smart contract holding reserves of both currencies.
Automated market makers (AMMs) are pricing mechanisms utilized by decentralized exchanges (DEX).
The Constant Product Formula
The 'predetermined rate' in an AMM isn't set by a person; it's calculated by a simple but powerful mathematical formula. The most common one is the constant product formula. It ensures that the pool's assets remain in balance regardless of trading activity.
The formula is straightforward: the quantity of Token X in the pool multiplied by the quantity of Token Y must always equal a constant value, K.
Let's see it in action. Imagine a pool contains 10 ETH (x) and 40,000 USDC (y). The constant, k, would be . According to the pool, the price of 1 ETH is 4,000 USDC (40,000 / 10).
Now, a trader wants to sell 1 ETH to the pool. They add 1 ETH, so the pool now has 11 ETH. To keep k at 400,000, the amount of USDC must change. The new amount of USDC (y) will be . The difference, USDC, is what the trader receives. Notice they received less than the initial 4,000 USDC price. This effect is known as price slippage and is more pronounced in pools with low liquidity. The larger the trade relative to the size of the pool, the more the price moves.
Who Provides the Liquidity?
The tokens in these pools don't appear out of thin air. They are supplied by users called Liquidity Providers (LPs). Anyone can become an LP by depositing an equivalent value of both tokens in a trading pair into the pool's smart contract. For our ETH/USDC pool, you'd need to deposit both ETH and USDC.
Why would anyone do this? In return for providing liquidity, LPs earn a small percentage of the fees from every trade that happens in their pool. When you perform a swap on a DEX, a portion of what you pay goes directly to the LPs. This incentivizes people to fund the pools, ensuring there's always a market for traders.
When LPs deposit funds, they receive special LP tokens that represent their share of the pool. They can redeem these tokens at any time to withdraw their portion of the pool's assets, including any fees they've earned. This entire process is automated and permissionless.
Different Pools for Different Pairs
The constant product formula works well for most token pairs, but it's not always the most efficient. For assets that are supposed to trade at a 1:1 ratio, like two different stablecoins (e.g., USDC and DAI) or a token and its staked version (e.g., ETH and stETH), the standard AMM can result in unnecessary price slippage.
To solve this, specialized AMMs were created. A popular variant is the stable-swap pool, pioneered by Curve Finance. These pools use a hybrid formula that is much flatter than the constant product curve around the 1:1 price point. This allows for very large trades between pegged assets with minimal slippage. Only when the pool becomes severely imbalanced does the price start to change significantly.
Standard pools are for volatile asset pairs like ETH/USDC, while stable-swap pools are optimized for pegged asset pairs like USDC/DAI.
By understanding these different pool types, you can make more informed trading decisions. Choosing the right pool for your trade is key to minimizing costs and getting the best possible execution price. DEX aggregators often handle this automatically, searching across multiple DEXs and pool types to find the most efficient trading path for you.
This system of automated liquidity and algorithmic pricing allows DEXs to operate 24/7 without any central oversight, providing a truly open and accessible financial market.
Let's check your understanding of these core DEX concepts.
What is the primary mechanism most decentralized exchanges (DEXs) use for trading, in contrast to the order books used by traditional exchanges?
In a liquidity pool using the constant product formula (), there are 50 ETH and 150,000 USDC. A trader sells 10 ETH to the pool. Approximately how much USDC will the trader receive?
This combination of liquidity pools and automated formulas forms the backbone of decentralized trading, enabling anyone to swap assets directly from their wallet.
