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).Documentation Index
Fetch the complete documentation index at: https://defibyexample.com/llms.txt
Use this file to discover all available pages before exploring further.
DEXs operate without intermediaries. Trades happen directly between users through smart contracts, not through a centralized authority.
With DEXs, you maintain full custody of your funds. No need to deposit assets to a centralized platform that could be hacked or frozen.
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. 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.
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.