Swapping tokens is great, and the experience feels like the funds are from thin air but they’re not. The swaps we made in the previous tutorial are executed on a decentralized exchange (DEX).
DEXs operate without intermediaries. Trades happen directly between users through smart contracts, not through a centralized authority.
Think of a DEX (decentralized exchange) like a big vending machine for crypto. Instead of a shopkeeper stocking it, you and other users fill it up with different snacks (tokens). When you put your tokens into these machines, you become a liquidity provider (LP). You’re providing the “stock” people trade from, and in return, you earn fees every time someone makes a purchase (swap). A DEX is a crypto exchange that lets you trade directly with other people, without intermediaries (like banks or centralized exchanges). Unlike banks, they don’t hold your money. You trade from your own wallet with other traders through the platform.
With DEXs, you maintain full custody of your funds. No need to deposit assets to a centralized platform that could be hacked or frozen.
You keep control of your assets. You trade with no sign-up or KYC, and they are open 24/7 globally.

Understanding AMMs

Most DEXs work using something called an AMM (Automated Market Maker) In a traditional exchange (like a stock market), there’s an order book: buyers say, “I’ll buy at this price,” and sellers say, “I’ll sell at this price.” The exchange matches them up. However, AMMs make it significantly easier and more transparent. Instead of matching buyers and sellers one by one, AMMs use liquidity pools.
AMMs use mathematical formulas (like x*y=k) to automatically determine prices based on supply and demand in the pool.
Here’s how it works:
  • People called liquidity providers (LPs) put pairs of tokens into a pool (for example, USDC and ETH).
  • When you swap, you’re not trading with another person directly — you’re trading with the pool.
  • The AMM uses a mathematical formula to decide the price automatically based on how much of each token is in the pool.
Because of this, you can swap any time, without waiting for someone else to be on the other side of the trade. That’s why it feels like the tokens appear “out of thin air” but really, they come from the pool funded by LPs.

Main Version of AMMs

There are two main versions of AMMs you need to be conversant with. Both pioneered by Uniswap, a AMM DEX on the Ethereum blockchain.

V2 AMMs

In V2 AMMs, when you provide liquidity (tokens), you earn profits based on the total pool’s returns. This is because your tokens are available across all the price ranges even if everything might never get used.
V2 AMMs spread your liquidity across all price ranges, which means lower capital efficiency but simpler management.

V3 AMMs

This version allows you to provide your liquidity (tokens) within specific price ranges that you choose. You can focus and specify the prices where you’ll earn money. This makes your money work harder and you will earn harder fees.
V3 AMMs offer higher returns but require active management. If prices move outside your range, you stop earning fees.
Why do people provide liquidity? Simple: they earn trading fees every time someone swaps tokens in that pool. It’s like earning passive income by allowing others to use your tokens for trading. Learn about providing liquidity in the next tutorial and how you can set yourself up for passive