This article is part of a structured DeFi learning series.
In previous articles we covered:
- what DeFi is
- how wallets work
- how blockchain transactions work
- what tokens are and why approvals exist
- how stablecoins work and where stablecoin risks begin
Now we move to one of the most common actions inside DeFi:
swapping assets on a decentralized exchange.
Many beginners think of a DEX as simply a crypto website where you convert one asset into another. That description is not completely wrong, but it is very shallow.
A DEX is not just a website.
A DEX is not just a button that says swap.
A DEX is a trading system that runs on smart contracts.
If you do not understand how a DEX works, you do not fully understand how value moves inside DeFi.
Because whenever you swap assets, rebalance a portfolio, enter a position, exit a position, or move from one asset to another, you are usually interacting with DEX infrastructure.
Why This Topic Matters
For many users the first real action inside DeFi is a swap.
They buy a crypto asset.
They convert a stablecoin into something like ETH.
They move from one asset to another.
They open a protocol interface and press confirm.
And that moment is usually when theory turns into something real.
So far the series has built a few key ideas:
- self custody means your wallet is your authority
- transactions are signed instructions that change on chain state
- tokens live inside smart contracts
- approval allows contracts to move tokens on your behalf
- stablecoins often act as the pricing unit inside DeFi
A DEX brings all of these ideas together in one place.
When you perform a swap on a DEX:
- your wallet signs a transaction
- a smart contract executes logic
- tokens move according to contract rules
- liquidity determines how easily the trade executes
- price depends on the market structure you interact with
If you misunderstand DEXs, you will also misunderstand things like:
- why prices change during a trade
- why approvals are required
- why low liquidity can be dangerous
- why the interface and the protocol are not the same thing
- why a quoted price is not always guaranteed
DEX knowledge is not an optional side topic.
It is one of the structural pillars of DeFi.

What Is a DEX?
DEX stands for decentralized exchange.
At its simplest, a DEX is a system that allows users to exchange one crypto asset for another directly on chain through smart contracts, without handing control of their funds to a centralized company.
There are a few important ideas inside this definition.
First, a DEX is an exchange.
An exchange is simply a market structure where assets can be priced and traded against each other.
In other words, a DEX is an on chain trading mechanism that allows one crypto asset to be exchanged for another.
Second, it is decentralized because the trading logic does not live inside a private company database. Instead it lives inside smart contracts deployed on a blockchain.
Third, most DEX interactions happen directly from your wallet.
You usually do not deposit funds into an exchange account and trade inside that system.
In the wallet article we learned that a wallet is not storage but authority.
In the transaction article we saw that on chain actions happen when you sign something.
In the token article we saw that smart contracts update token balances and approvals allow contracts to move tokens.
In the stablecoin article we saw that many DeFi actions are priced in stablecoins.
A DEX is where all of these concepts start working together inside a real market system.
CEX vs DEX
Before going deeper, it helps to clarify what a CEX is.
CEX means centralized exchange.
Platforms like Binance, Coinbase, or OKX are examples.
On these platforms users usually deposit their assets into an account controlled by the exchange and trade inside that system.
For example, if you hold BTC in a Binance account and convert it to ETH, that is usually not a DEX swap.
Trading BTC/USDC on Binance is also not a DEX transaction. It is a trade happening in the exchange's internal spot market.
In that situation you are not swapping assets from your own wallet on chain. The exchange processes the trade inside its own infrastructure.
So when does a DEX actually enter the picture?
DEX activity begins when assets stay in your own wallet and you interact directly with a smart contract protocol instead of a company account.
For example, swapping USDC for ETH on Uniswap from your own wallet is a DEX swap. The transaction settles on chain through smart contracts.
A centralized exchange typically works like this:
- you deposit funds into the platform
- the platform holds custody of those assets
- trades happen inside the exchange system
- balances are updated in the exchange database before any on chain settlement
Behind the scenes this usually involves:
- an order book that stores buy and sell orders
- a matching engine that pairs those orders
- an internal ledger that tracks user balances
When you trade BTC/USDC on a platform like Binance, the trade usually executes inside that order book system and your balance changes in the exchange database.
Some platforms also offer a simplified Convert feature where users do not see the order book and simply accept a quoted price. Even in that case the execution usually happens through the exchange's own liquidity and internal systems.
In this model the user trusts the company.
The company runs the interface, the databases, the matching engine, and the custody system.
A DEX works differently:
- assets stay in your wallet
- you sign transactions to trade
- smart contracts execute the trading logic
- settlement happens on chain
This does not mean DEX is automatically better.
It simply means the tradeoffs are different.
Centralized exchanges usually provide:
- easier onboarding
- customer support
- fast internal execution
- simpler user experience
DEXs usually provide:
- self custody
- transparent on chain settlement
- easier integration with other DeFi protocols
- composability with the broader ecosystem
But DEXs also require more responsibility.
There is no support ticket to undo a bad signature.
There is no password reset after a bad approval.
Transparency does not automatically mean safety.
This aligns with one of the core principles of this series:
transparency does not equal security.

Where Do You Actually Use a DEX?
What matters is not the brand name of the platform but how the transaction works.
DEX activity therefore often happens through protocols such as Uniswap, Curve, Balancer, or PancakeSwap.
But the key factor is not the name. It is the structure of the transaction.
If you swap assets by signing a transaction from your own wallet that interacts with a smart contract, you are using a DEX.
If you trade inside Binance or Coinbase accounts, even if the interface looks similar, that is not a DEX.
The difference is custody, execution, and settlement.
Why Do DEXs Exist If Centralized Exchanges Already Work?
At first glance this is a reasonable question.
The answer is that CEX and DEX may look similar on the surface but represent different trust models.
A centralized exchange focuses on speed, convenience, and support.
But users give up custody and rely on the company's infrastructure.
A DEX requires more responsibility but offers different advantages:
- self custody of assets
- direct interaction with protocols
- transparent settlement on chain
- composability with other DeFi systems
- easier access to on chain opportunities
DEXs are not just conversion tools.
They are entry points into the broader DeFi ecosystem.
Once assets are in the right form through a DEX, they can move easily into lending protocols, liquidity pools, yield strategies, and other DeFi applications.
The real difference is not simply swapping assets.
The real difference is the trust model behind the system.
The Core Mental Model
A DEX is not just a website. It is a market mechanism built on liquidity, pricing rules, and smart contract execution.
The website you see is only the interface.
The real system exists underneath.
Beginners often imagine DEX trading like this:
"I pressed a button and the application found my trade."
But the structure is deeper than that.
A functioning DEX requires:
- tradable assets
- pricing rules
- smart contracts that execute logic
- liquidity providers who make trading possible
A DEX cannot magically create liquidity.
If liquidity is weak, trades may become expensive, inefficient, or impossible.
So the correct mental model is not:
DEX = token swap website
The correct model is:
DEX = an on chain trading system built on liquidity pools, pricing formulas, and smart contract execution.
Do All DEXs Use the Same Code?
No.
From the outside three different DEX interfaces may look identical. You might see "swap BTC for USDC" on multiple platforms.
But that does not mean they run the same code.
Several different possibilities exist:
- the sites may be entirely different protocols
- they may run different versions of the same protocol
- one may be a fork of another codebase
- some may be aggregators that route trades through multiple DEXs
- different interfaces may connect to the same underlying contracts
Even when the result looks similar, the pricing logic, routing, fee structure, and liquidity design can differ.
There is another subtle point.
When you see "BTC" inside a DEX environment it is usually not native Bitcoin from the Bitcoin network.
Instead it is often a representation such as WBTC or another wrapped form of Bitcoin.
So even a BTC/USDC swap across different DEXs may involve different representations of Bitcoin.
That is why a swap that looks identical may still carry different risks depending on the protocol and asset representation.
How a DEX Actually Works
There are different ways exchanges can function.
Two high level models are useful to understand.
Order book model
In an order book system buyers and sellers place orders.
One side says "I want to buy at this price".
Another side says "I want to sell at this price".
When compatible orders meet, the trade executes.
This model is common in traditional finance and centralized exchanges.
AMM model
To understand AMMs properly we need to answer an important question.
Where does the mechanism actually run?
Is it on a server?
No.
AMM logic does not live on a private server. The rules exist inside smart contracts deployed on the blockchain.
That means pricing logic, pool balances, and swap rules all exist in contract code.
When a user performs a swap:
- the wallet signs a transaction
- the transaction is sent to the network
- validators include it in a block
- the smart contract executes
- the contract reads pool balances
- the pricing formula runs
- input tokens are received and output tokens are sent
So the AMM is not a server watching your order.
There is no traditional order book.
Instead the contract executes a mathematical rule.
In simplified terms:
- in a CEX, pricing and matching happen on company servers
- in a DEX, pricing and execution happen inside smart contracts on chain
That is what allows decentralized markets to function.
Most beginner DeFi swaps happen through an AMM, an automated market maker.
Instead of matching with another visible trader, you trade against a liquidity pool.
How AMMs Determine Price
Many AMM designs rely on a simple relationship known as the constant product formula.
x * y = k
Where:
- x is the amount of one asset in the pool
- y is the amount of the other asset
- k is a value that remains constant
When you swap assets you change the balance of the pool.
Imagine a pool that contains:
- 100 ETH
- 200000 USDC
If you add USDC to buy ETH, the pool receives USDC and releases ETH.
The ratio between the two assets changes, and the price adjusts accordingly.
Price is therefore not set by a server.
It emerges automatically from the pool balance.
This explains why:
- small trades barely move the price
- large trades move the price more
- price changes as pool ratios change

Liquidity Pools
Liquidity pools also raise an important question.
What happens if you try to swap more than the available liquidity can comfortably support?
A DEX cannot create assets out of thin air. If the trade size is very large compared to the pool, several things may happen:
- price impact becomes significant
- execution price becomes worse
- slippage limits may cause the trade to fail
- the trade may be impractical due to insufficient liquidity
DEX interfaces therefore often display information such as:
- price impact
- minimum received
- route
- liquidity depth
These numbers matter.
They describe the quality of execution.
Large trades often need:
- deeper pools
- routing through multiple pools
- splitting trades into smaller pieces
Liquidity pools are smart contracts that hold reserves of assets for traders to swap against.
For example a pool may contain:
- ETH
- USDC
A trader who wants to convert USDC into ETH interacts with that pool.
They add one asset and remove the other according to the protocol's pricing logic.
Liquidity pools exist because liquidity providers deposit assets into them.
Later in the series we will explore how they earn fees and why impermanent loss matters.

What Happens When You Click Swap?
Let us walk through the process step by step.
Imagine you want to swap USDC for ETH.
Step 1: Connect your wallet
The interface can now read:
- your public address
- your balances
- your current network
This connection is usually read only.
Step 2: Choose input and output assets
You select:
- input token: USDC
- output token: ETH
The interface queries the protocol and calculates an estimated output.
This estimate is called a quote.
Step 3: Grant approval if necessary
If the token is an ERC20 asset like USDC, the contract usually requires permission to move it.
Approval allows the contract to spend a defined amount of the token on your behalf.
Step 4: Review the quote
The interface shows values such as:
- estimated output
- route
- fee
- price impact
- minimum received
These numbers describe the execution environment of the trade.
Step 5: Review the transaction
Before signing you should understand what you are doing.
Are you granting approval?
Are you executing the swap?
Are you interacting with the correct contract?
Are you on the correct network?
Step 6: Sign the swap
At this point users usually encounter two types of costs.
- gas, the cost of executing the transaction on the network
- swap fee, the trading fee paid to the protocol and liquidity providers
These are different mechanisms.
Gas is paid to the blockchain validators.
Swap fees go to the protocol or liquidity providers.
Different smart contract designs may consume different amounts of gas. A simpler or more optimized contract may cost less to execute than a complex one.
Step 7: Contract execution
If all conditions are satisfied, the smart contract:
- transfers the input asset
- applies the pricing logic
- sends the output asset
- updates on chain state
Step 8: Receive the output asset
After execution completes your wallet reflects the new balance.
Why Prices Change During a Swap
A common beginner question is why the received amount differs from the quoted price.
Two concepts explain this:
- quoted price, the estimate before execution
- execution price, the actual result after the transaction executes
Factors that affect the difference include:
- trade size
- liquidity depth
- market movement during confirmation
- routing across multiple pools
Slippage
Slippage is the difference between the expected price and the final execution price.
Causes may include:
- market movement
- trade size
- low liquidity
- state changes in the pool before execution
Even stablecoin swaps can experience slippage under stressed conditions.
Why Approval Matters
Approval is one of the most misunderstood parts of DeFi.
ERC20 tokens require permission before a contract can move them.
DEX usage therefore often involves two layers:
- approval
- execution
Connecting a wallet is not the same as granting spending permission.
Interface vs Protocol
The website you see is the interface.
The smart contracts on chain are the protocol.
A polished interface may feel trustworthy, but security depends on the contracts you interact with.
Network Selection
Many DEX protocols exist on multiple networks such as:
- Ethereum
- Arbitrum
- Base
- BNB Chain
The wallet ultimately determines the network where the transaction executes.
Choosing the wrong network may mean:
- the pool does not exist
- the asset version differs
- the transaction fails
For example USDC on Ethereum and USDC on Arbitrum share the same symbol but are not the same asset.

Safety Checklist Before Every Swap
Before executing a swap:
- verify the network
- verify the token contract address
- confirm whether you are approving or swapping
- review the expected output
- check price impact and minimum received
- start with small test transactions
- do not blindly trust the interface
- keep enough native tokens for gas
Summary For Lazies
A DEX is an on chain trading system that allows users to exchange assets directly from their wallets using smart contracts.
Most swaps happen through AMMs and liquidity pools.
Liquidity determines execution quality.
Quotes are estimates. Execution prices may differ.
Approval and swap are separate actions.
And most importantly:
The interface is not the protocol.
DEXs are where the abstract ideas of DeFi become practical.
Self custody, smart contracts, approvals, stablecoins, liquidity, and execution all come together in a single user action.
Understanding how DEXs work provides the foundation for understanding liquidity provision, lending flows, yield strategies, and execution risks across the broader ecosystem.
In the next article we will explore slippage, price impact, and why swap prices change deeply.
Originally published on X · 2026-03-06