Blog

How Decentralized Exchanges Work: A Deep Dive into Automated Market Makers

Exploring the automated market maker systems powering today's leading decentralized exchanges like Uniswap, PancakeSwap, and Curve.
Cryptocurrency
September 25, 2023
How Decentralized Exchanges Work: A Deep Dive into Automated Market Makers

Decentralized exchanges (DEXs) are one of the most popular applications running on blockchain networks like Ethereum and Solana. DEX trading volume surpassed $1 trillion in 2021, as users sought alternatives to centralized exchanges like Binance and Coinbase.

But how do decentralized exchanges actually work under the hood? In this deep dive, we'll explore the automated market maker (AMM) model that the vast majority of DEXs use for trading cryptocurrencies in a permissionless, non-custodial way.

Automated Market Makers Explained

Unlike traditional order book exchanges, DEXs like Uniswap, PancakeSwap and Curve don't match individual buyers and sellers. Instead, they rely on AMMs to provide liquidity and facilitate trading algorithmically.

AMMs utilize smart contracts that create pools of tokens. For example, a pool could contain 10 ETH and 5000 DAI tokens. Based on the ratio of tokens in the pool, the smart contract can determine exchange rates between the assets.

As trades occur, token ratios shift. If 1 ETH is swapped for DAI, the ETH pool balance drops to 9 and DAI increases to 5100. This changes the exchange rate accordingly.

AMMs don't require counterparties for trades. The smart contracts hold pooled reserves that provide liquidity. Anyone can become a market maker by funding pools with an equal value of both assets. In exchange for providing liquidity, they earn trading fees from swap volumes.

AMM Formula - Constant Product Market Makers

The first AMMs like Uniswap used a simple constant product formula:

x * y = k

Where:

x = quantity of token Xy = quantity of token Yk = constant value of the product

For example, if a pool starts with 10 ETH and 5000 DAI, k equals 50,000. If 1 ETH is traded, x becomes 9. To keep k constant at 50,000, y (DAI) must equal ~5555.

This elegant formula allows prices to respond organically to trades while ensuring liquidity providers earn fees. Other AMM designs have since emerged that improve capital efficiency for certain use cases.

Advantages of Automated Market Makers

AMMs bring unique benefits over order book exchanges:

  • No counterparty needed - Anyone can trade against liquidity pools at any time. No need to wait for a buyer/seller match.
  • Low barriers to become a market maker - Anyone can provide liquidity and earn fees proportional to their share of the pool.
  • Transparent pricing - Formulas are open source so anyone can verify exchange rates.
  • Censorship resistance - No single entity controls asset listings or who can trade.
  • Composability with DeFi - AMM pools integrate seamlessly as lego-like building blocks within broader DeFi ecosystems.

However, AMMs also come with limitations compared to order books:

  • Higher slippage on large trades - Token ratios in pools can shift rapidly with large swaps, creating price impacts.
  • Impermanent loss for liquidity providers - If one asset changes value disproportionately, LP holdings can lose value versus just HODLing the tokens.
  • Front running risks - Smart contracts are transparent, allowing arbitrage bots to monitor transactions and trade ahead of them.

Overall though, AMMs present a breakthrough model for non-custodial, decentralized trading. Their liquidity pools aggregate capital on-chain without intermediary risk.

Next, let's look at some leading DEX protocols leveraging AMM architectures.

Uniswap - The Pioneer of AMM-Based Decentralized Exchanges

Uniswap pioneered the constant product AMM model on Ethereum in 2018. It quickly became the leading DEX by trading volume, unlocking decentralized trading of Ethereum-based tokens for the first time.

Some key aspects of Uniswap include:

  • Ethereum based - Uniswap v1-v3 have all run on Ethereum, helping cement its spot as the dominant DeFi blockchain.
  • Constant product formula - Uniswap v1-v2 use the simple x * y = k price mechanism. v3 introduced concentrated liquidity around specific price ranges.
  • ETH base pair - All trades are against ETH, rather than direct token/token pairs. This simplifies liquidity provision.
  • Automated liquidity pools - Anyone can use smart contracts to easily add or remove liquidity to earn fees with no central gatekeeper.
  • Transparent - Anyone can view exchange rates and trading activity on Uniswap's open-source interfaces.

Uniswap governance is controlled by UNI token holders who can propose and vote on upgrades. UNI also incentivizes liquidity providers along with collected trading fees.

Uniswap demonstrated the massive potential of AMMs. But as Ethereum congestion and fees rose, competitors emerged aiming to improve the model.

PancakeSwap - Uniswap on Binance Smart Chain

PancakeSwap replicated Uniswap's v2 design but built on Binance Smart Chain instead of Ethereum. Launched in late 2020, it quickly overtook Uniswap in daily trading volume due to BSC's lower transaction fees.

Here's why PancakeSwap gained so much traction:

  • BSC compatibility - Built on Binance Smart Chain using BEP-20 token standard, avoiding Ethereum's network congestion and high gas costs.
  • Forked codebase - Copied Uniswap v2's proven constant product AMM model and open-source frontend.
  • CAKE token incentives - Introduced CAKE yield farming rewards distributed to liquidity providers and SYRUP pools to bootstrap new pairs.
  • Lottery/Gamification - Addedgamified elements like a CAKE lottery jackpot to incentivize platform usage.
  • Lower fees - Avg. transaction fees are just $0.10 on BSC vs $20+ on Ethereum, making PancakeSwap cheaper for traders.

PancakeSwap showed the power of cloning and iterating on existing DeFi protocols cross-chain. Its simplified incentive structures and focus on user acquisition fueled massive growth built atop Uniswap's AMM foundation.

Curve Finance - Specialized AMM for Stablecoins/Stable Assets

Curve Finance pioneered the "stableswap" AMM model optimized for minimizing slippage between assets of equivalent value, like stablecoins.

Curve deploys multiple AMM pools, each containing liquidity between assets pegged to similar values. For example:

  • USD Coin (USDC), DAI, Tether (USDT)
  • renBTC, wBTC

This specialization allows Curve to minimize price impacts for large swaps between these assets. Key innovations include:

  • Multi-coin pools - Each pool aggregates liquidity between assets of equivalent value, rather than just a single pair.
  • Algorithmic market making - Curve uses a more complex formula than just x * y = k to manage inventories and minimize slippage.
  • Low-slippage swaps - By keeping assets at near 1:1 ratios, swaps between them have minimal price impacts.
  • CRV token rewards - CRV incentivizes liquidity providers per pool based on swap volume and staked CRV governance power.

Curve optimized AMMs for stablecoin and stable value swaps. This made it perfect for applications like Convex's tokenized leverage yield aggregation. Curve's liquidity is essential to many DeFi protocols.

Conclusion

In the early days of crypto, order book exchanges like MtGox and BitStamp powered trading using the traditional exchange model. A decade later, automated market makers like Uniswap have proved to be a superior paradigm for trustless, non-custodial, transparent digital asset exchange.

AMMs have paved the way for the explosive growth of decentralized finance applications by enabling automated, programmatic liquidity. Developers can easily integrate DEX price oracles and swapping functions to build modular DeFi services.

Despite challenges around impermanent loss, slippage, and front running, AMMs represent a landmark primitive for the crypto ecosystem. Just like serverless computing abstracted infrastructure backends, AMMs abstract exchange infrastructure from the financial applications built on top.

The pioneer spirits pushing the envelope of exchange design like Uniswap, PancakeSwap, and Curve have only just begun to reveal the potential of decentralized trading.