Decentralized Exchanges
What is a Decentralized Exchange?
Decentralized Exchanges (DEXs) are on-chain marketplaces that enable Explorers to securely exchange cryptocurrency with other users while maintaining self-custody of their wallet funds. These peer-to-peer trades are facilitated using publicly accessible smart contracts that connect users with large communal vaults of tokens. These vaults are called liquidity pools. DEXs can be found on almost any blockchain, and are present on Ethereum Layer 1 and 2.
Exchanging tokens is an essential part of using DeFi. In DeFi you’ll find a greater range of token variety and utility than on any other type of exchange. Some users buy tokens to access on-chain products and services. Others purchase tokens as an investment. Some tokens grant holders voting power used to determine project direction, much like holding shares in a traditional corporation! Regardless of your motivation, you’ll be visiting DEXs on a regular basis in DeFi.
Let's learn how they work and how they can best serve you.
Centralized and Decentralized Exchanges
Let’s cover the differences between the technology used for a Centralized Exchange (like Coinbase, Binance, Kraken) and that of a Decentralized Exchange (like Uniswap, PancakeSwap).
Centralized Exchanges (CEXs) allow users to trade and invest in cryptocurrency, without engaging in the blockchain ecosystem itself. As your account is registered on the CEX, your private keys and funds are in their custody: you are subject to their management, rules, and business model risks.
Decentralized Exchanges (DEXs) enable users to trade cryptocurrency entirely in self-custody: the original intended purpose of blockchains. The peer-to-peer model allows you to act as both the consumer and the provider, accessing financial opportunities previously available only to the financial class. The blockchain system is both transparent and censorship-resistant: no one can freeze your access or reverse your trades. Hacks remain a risk, as we'll cover later in this lesson.
Knowledge Check 1
Which of the following is true regarding cryptocurrency exchanges?
-
[ ] There is no team behind a DEX.
-
[ ] The only way you can lose funds on a CEX is due to a bad trade.
-
[ ] DEXs let you trade in self-custody, while CEXs do not.
Decentralized Applications
DEXs are a type of dApp, a decentralized application running on a blockchain. For an internet application to be considered ‘decentralized’ it must indiscriminately allow anyone to use it, process their interactions without need for another person, and be written in publicly transparent code.
dApp services are facilitated using smart contracts, lines of code that take a user’s on-chain action and return a predictable on-chain response. The Ethereum Foundation compares smart contracts to vending machines, where the user inputs the number corresponding to the item they’d like to receive, plus the appropriate amount of money, and they receive an expected output (their snack) without the need for another human to facilitate the transaction.
DEX smart contracts handle a variety of commands, like token swapping, voting, or adding and removing liquidity.
Decentralized Applications (continued)
DEXs follow the same logic as the vending machine: they take a user’s input token and output the desired token. Other dApp examples include:
🎟️ Voting dApps: allocating a user’s vote to a specified entity.
📦 Bridge dApps: transferring a user’s cryptocurrency from one blockchain network to another.
🤝 Lending/Borrowing dApps: granting loans to users who meet specified requirements.
Smart contracts are accounts on Ethereum: they have an address and balance, performing automated actions when prompted by a transfer and command. A DEX is a programmed Ethereum account with several available functions.
dApps will typically use a website as a visual interface to help users interact with the underlying smart contracts. If the website is down, you can still access the smart contract with some experience!
Knowledge Check 2
Which properties are needed for a dApp to be considered decentralized?
-
[ ] Permissionless: open access to all users.
-
[ ] Autonomous: user interactions do not need an intermediary.
-
[ ] Transparent: the smart contract code is publicly available.
-
[ ] All of the above.
Automated Market Makers
In traditional markets and CEXs, your custodian uses an order book: a database filled with buy and sell offers. The CEX connects your trade offer with another person’s. You are usually charged a base/scaling commission on your trade, and you’re also left wondering if the undisclosed matching method found you the best possible deal.
Most DEXs use ‘Automated Market Maker’ (AMM) technology, the most common design for token swaps: a system that prices your trade using a public algorithm. Some newer DEXs use order books or intent-based systems instead. As the AMM algorithm is open-source, anyone can understand, clone, and improve it, leading to healthy competition and constant innovation.
AMMs route user trades through liquidity pools, rather than directly matching user bids and asks. These communal token vaults accumulate and dispense tokens according to user interactions, with every step visible on the public blockchain.
Knowledge Check 3
What is a benefit of using AMMs compared to a traditional order book?
-
[ ] The AMM trade is faster than an order book trade.
-
[ ] AMMs connect you directly to the other user.
-
[ ] You can detect and prevent other parties creating one-sided trades.
Token Swaps
Cryptocurrency trades on the blockchain are called token swaps. These smart contract interactions convert one cryptocurrency to another using AMM liquidity pools. By forming a trade route, a pathway through the appropriate liquidity pools, a DEX smart contract exchanges your input token for your desired output token. Since pools usually hold only two tokens, and not every token pair has a pool, a route may pass through several pools to fulfil your swap.
To let a smart contract access our wallet, we grant it permission to withdraw funds up to a specified (or unlimited) amount. These token allowances let trusted contracts carry out transactions without our private key. Granting costs gas, so permissions stay open for future use: one reason to trade from one wallet and hold in another. We monitor and revoke allowances in our Managing Token Allowances lesson!
Token Swaps (continued)
Let’s take a look at an example swap to understand the permission and exchange process. The example is a swap between USDC to OP on Velodrome, a large DEX on the Optimism network. This trade is often routed through two pools, as the USDC/OP liquidity pool isn’t as cost-efficient:
- First, you grant the appropriate Velodrome smart contract the permission to make USDC withdrawals from your wallet.
- You submit your swap transaction request to Velodrome.
- The transaction is accepted: Velodrome withdraws the specified amount of USDC from your wallet, into the USDC/ETH liquidity pool. The equivalent amount of ETH exits this first liquidity pool and is transferred to the ETH/OP liquidity pool. Lastly, OP is transferred from the second liquidity pool to your wallet address.
The swap transaction is complete. Your USDC tokens have been swapped for OP, via ETH!
Knowledge Check 4
AMMs can route a trade through multiple liquidity pools, all in a single transaction.
-
[ ] True
-
[ ] False
What is Liquidity?
Liquidity in the crypto space refers to a marketplace’s ability to facilitate digital asset buys and sells at fair prices. When liquidity is high, prices are more stable; when liquidity is low, prices are more volatile. As users are generally attracted to fairer prices, DEXs aim to have high liquidity across all of their liquidity pools.
High liquidity means there is a high quantity of tokens in the liquidity pool, generally a 50/50 valuation split of the two tokens users are trading in and out of the pool. For example, a USDC/ETH pool facilitates all trades between this token pair on the host platform.
When there are more tokens, users making trades through the pool have less impact on the 50/50 asset balance, which helps prices remain stable. The amount by which any trade throws off this balance is known as price impact.
As an Explorer, you want the lowest price impact on your trades as possible, in order to receive the best deal! That means you want high, balanced liquidity.
Liquidity Providers
Optimizing for high liquidity is integral to the success of a DEX, but because there’s only so much liquidity in the cryptocurrency ecosystem, each DEX is in competition to capture as much liquidity as possible. So where does this liquidity come from?
In a decentralized ecosystem, DeFi citizens are incentivized to provide liquidity to a given pool to raise the TVL (total value locked) on a platform. Fees gathered from users making trades through the pool are distributed to the LPs (liquidity providers) based on the amount of liquidity provided. You heard that right: by lending your tokens to a DEX liquidity pool, you can generate passive income.
There are a variety of considerations when becoming an LP, and we’ll cover this in future content. For now, know that the large APRs (annual percentage rates) displayed across DEX liquidity pools aren’t guaranteed, and there can be losses.
Knowledge Check 5
Finish the statement: “When liquidity is __________.”
-
[ ] high, volatility is high.
-
[ ] low, volatility is low.
-
[ ] low, volatility is high.
Knowledge Check 6
How do DEXs incentivize users to provide liquidity?
-
[ ] Insurance for trade losses.
-
[ ] A share of platform fees and/or bonus tokens.
-
[ ] Access to private liquidity pools.
-
[ ] All of the above.
Platform Fees
Both CEXs and DEXs charge fees for their services, and interacting with the blockchain is not free either. Here are five common costs to consider when choosing a platform.
🏷️ Platform fees: CEXs set their own trading commissions, while DEX pool fees vary by pool (commonly a fraction of a percent). The key difference: DEX fees are visible on-chain for anyone to check.
🌐 Network fees: Blockchains charge gas fees on top of the dApp transaction. You can minimize these costs by using the network during quiet periods. Etherscan.io has a real-time gas estimate tool for Ethereum Mainnet: Etherscan.io. On Layer 2s, fees are far cheaper; compare networks at growthepie.
📦 Bridge fees: Both CEXs and blockchain bridges charge for transferring cryptocurrency from one blockchain network to another. For CEXs, refer to their on-site information. Bridge dApps show a fee estimate before you confirm the transfer.
💹 Exchange rates: When buying cryptocurrency directly with fiat on a CEX or DEX, be wary of exchange rates that don’t reflect the market rate.
🧊 Slippage: Prices move fast, so DEXs leave room for fluctuation on a swap: this is called slippage (customizable, usually 0.5-2%). You may lose up to that value on a trade, but too low a setting can get your trade rejected.
Always do your own research before trading, so you understand a platform’s costs and trade-offs.
DEX Advantages
We’ve covered a lot of theory in this lesson, but you might still be wondering if DEXs are for you. Generally speaking, you are probably going to benefit from Decentralized Exchanges if:
- 🔑 You want to retain custody over your digital assets.
- 🔒 You want to secure your assets on the blockchain, avoiding CEX collapses.
- ⌛ You want 24/7 access to the cryptocurrency market.
- 👛 You want access to a wider range of cryptocurrencies.
- 🤑 You are interested in providing liquidity.
- 🛂 You don’t want to register and
KYCon every platform you interact with. - ⚔️ You seek the additional risks and rewards of exploring Decentralized Finance.
With that said, almost every DeFi user has an account on a Centralized Exchange. This is because CEXs have easy on/off ramp features to the traditional banking world; you can easily get money from your bank account onto the blockchain and vice-versa. Ryan Sean Adams compares this to using a public bathroom: “You go in, you do your business, you get out.”
This is great because it means you can start with a CEX account and slowly transition to DeFi as you become more confident in navigation.
DEX Risks
Using a DEX also comes with risk. Here are a few of the most impactful:
🐞 Smart contract risk: Audits reduce the chances of smart contract bugs, but they don't eliminate them: in 2025, a major DEX that had been audited by multiple firms lost $128M to a subtle code bug. In a worst-case scenario, you could lose up to your trade amount. Favor trusted, heavily audited smart contracts.
💰 Self-custody risk: Sole responsibility for your private keys means you could lose an entire wallet to theft, scams, or a misplaced seed phrase. This is why it is important to mitigate risk with a multi-wallet strategy, and to always keep a copy of your seed phrases backed up in a secure, real-world location.
🥪 Sandwich attacks: Setting your swap slippage high increases the likelihood of trade frontrunners coordinating sandwich attacks against you. In a sandwich attack, you could lose up to your slippage amount on a trade. We’ll cover how to protect yourself from this style of attack in future content.
With these advantages and risks considered, a CEX might be a better fit for you if:
- 🎓 You’re still early in your cryptocurrency journey, working to understand the risks and rewards.
- ⚖️ Your trade frequency and volume are small, making blockchain fees an unrealistic cost.
- 🏰 You’d prefer to trust an exchange to watch your funds, rather than being responsible for them.
Some users take a hybrid approach to lower their overall risk, using a CEX to buy and sell their cryptocurrency, while storing it on the blockchain itself.
Knowledge Check 7
Why would you use a Decentralized Exchange over a Centralized Exchange?
-
[ ] You want to access tokens not listed on a Centralized Exchange.
-
[ ] You want to retain full custody over the exchanged funds.
-
[ ] You want access to tools and opportunities not usually available.
-
[ ] All of the above.
Choosing a DEX
There are many Decentralized Exchanges in DeFi, and some are better than others. Consider these five key factors when deciding on which DEX to use:
🥇 Legitimacy: Is the entity well known for its trustworthiness, quality, and longevity?
⛲ Liquidity: Is the liquidity pool TVL high enough to minimize price impact?
🖱️ Ease of use: Is the user interface easy to interact with?
🔐 Security: Have the smart contracts been audited by multiple auditors?
🎁 Rewards and Features: Are there loyalty rewards for using the exchange or providing liquidity? Can you vote in governance?
Notable names that score highly in these areas include Uniswap, Curve, Velodrome, and PancakeSwap. You can easily move from one DEX to another until you find a few favorites! For the lesson quest, we’re going to use Velodrome, a well-established DEX on the Optimism network. It’s easy to use, and because it’s on a Layer 2 the fees are far more reasonable!
DEX Best Practices
Before you interact with a dApp, there are some best practices you should follow to keep your funds safe:
👩💻 Verify a dApp’s link via the official project X (Twitter) account (gold check mark) or a trusted third party, then bookmark it. Many DeFi scams begin with a fake link, even on popular search engines.
🔓 When granting on-chain token allowances, limit the allowance to your trade amount. Many DEXs now use signature-based approvals covering just your trade: see Managing Token Allowances.
♟️ Don’t interact with dApps from your HODL wallet; use a separate wallet just for dApps. Our Web3 Security lesson covers wallet strategies.
Now you’re ready to interact with a Decentralized Exchange!
Knowledge Check 8
How can you be sure you’ve chosen a reputable DEX?
-
[ ] By checking its reputation and only using URLs from trusted sources.
-
[ ] By performing a small test interaction on your first use.
-
[ ] Both of the above.