Skip to main content

Decentralized Exchanges, Liquidity Pools and Swap Costs

12 min · Free

A decentralized exchange, or DEX, uses on-chain contracts to execute trades. This lesson compares that model with a centralized venue, works through liquidity-pool pricing and adds up swap costs. Learn what the tool does and when its technical responsibilities fit your purpose.

A constant-product pool starts with 10 WBTC. After a 37,500 USDC input, 9.52381 WBTC remain and 0.47619 WBTC leave the pool.750,000 USDC → 787,500 USDC10Before: WBTC9.52381After: WBTC0.47619Output: WBTC
The product stays at 7,500,000 before fees. USDC reserves rise from 750,000 to 787,500, giving an average paid price of 78,750 USDC per WBTC.

What changes on a DEX

A centralized exchange maintains customer balances and matches orders. A DEX executes through blockchain contracts. In a noncustodial swap you retain control of your wallet keys and authorize the transactions that move assets into and out of the contract.

Many protocols require no conventional account registration, although interfaces and services can impose access restrictions. You must check addresses, permissions and transactions. Support teams cannot normally undo a finalized on-chain transfer.

The model shifts responsibility toward the user and the contract code. It can enable on-chain services and token access, but each protocol has its own execution and custody design.

  • DEX trades execute using blockchain contracts.
  • Noncustodial wallets keep signing keys under the user's control.
  • Review every address and permission you authorize.
  • Finalized transactions generally cannot be reversed by support.

Liquidity pools and automated market makers

Many DEX designs use pools rather than a conventional order book. Liquidity providers deposit assets such as WBTC and USDC and earn a share of fees. In the simplified constant-product AMM, reserves x and y satisfy x × y = k before fees. Other DEX designs use different formulas or order books.

Consider 10 WBTC and 750,000 USDC. The initial marginal price is 75,000 USDC per WBTC and k = 7,500,000. Add 37,500 USDC: the USDC reserve becomes 787,500 and WBTC becomes 7,500,000 ÷ 787,500 = 9.52381. The output is 0.47619 WBTC. Average paid price is 37,500 ÷ 0.47619 = 78,750, 5% above the initial reference, ignoring fees.

This difference is price impact caused by the trade itself. For a given order size, deeper usable liquidity usually reduces it. Pool depth matters especially for thinly traded tokens.

  • Many AMMs derive a quote from pool reserves.
  • In this example the reserve product stays constant before fees.
  • A 37,500-USDC input against 750,000 USDC gives a 78,750 average price, 5% higher.
  • Compare order size with available liquidity.

A noncustodial wallet is the connection

Use a compatible wallet such as MetaMask to control your keys and sign transactions. Its recovery phrase or other backup method protects control over the wallet, as covered in the keys lesson.

Connecting a wallet usually shares the address and permits a site to request signatures. It does not itself grant token-spending rights. Subsequent approvals and signatures can grant those rights, so a fake interface can still cause serious loss.

Keep long-term funds separate from experimental connections. Use a different wallet with a deliberately small balance for unfamiliar applications so its permissions do not expose the main reserve.

  • A noncustodial wallet controls its own signing keys.
  • Connection and token-spending approval are separate actions.
  • Verify the official domain and read each request.
  • Use a separate low-balance wallet for experiments.

Swaps, price impact and slippage tolerance

A swap exchanges one asset for another. The quote can change before execution. Distinguish the impact already included in the quote from additional movement between quoting and execution, commonly called slippage.

In a simplified $500 example, a 3% reduction in quoted output represents about $15 at the reference price. A 0.5% reduction is about $2.50. The actual minimum received is set in token units. If execution would fall below it, a correctly configured exact-input swap reverts, though an on-chain attempt can still cost gas.

Inspect liquidity first and then set tolerance with reference to the minimum output you will accept. Splitting a trade can change timing and cost, but sequential chunks in an unchanged constant-product pool do not remove aggregate price impact.

  • Slippage is movement between quoted and executed outcomes.
  • Tolerance sets a minimum acceptable token output.
  • A 3% reduction on a $500 reference output is about $15.
  • Evaluate depth, size and costs before deciding whether to split.

Gas is separate from trade size

Ethereum transactions consume gas for computation and storage. The amount depends on what the transaction does. A teaching estimate of 150,000 gas can apply to a swap regardless of whether its input is $100 or $100,000, although actual consumption varies by contract and route.

At an effective price of 20 gwei, 150,000 × 20 = 3,000,000 gwei = 0.003 ETH. With ETH at the assumed $3,000, that is $9. Add a $15 adverse output change on the $500 example and the combined effect is $24, or 4.8%, before any uncounted pool fees.

Compare supported networks and layer-two routes, including bridge costs, security and token availability. Use the current wallet estimate instead of assuming a fixed fee. Pool trading fees also affect output and must be included without counting them twice.

  • Gas depends on execution work and fee conditions, not simply on input dollars.
  • Under the assumptions, 150,000 gas × 20 gwei = 0.003 ETH = $9.
  • A small swap can lose a significant fraction to fixed execution costs.
  • Compare supported routes and current fee estimates.

When a DEX fits the task

A DEX can fit access to a particular on-chain token or DeFi service, including lending or liquidity provision. It also allows noncustodial execution where the user understands the operational and contract risks and meets applicable access rules.

For a first small purchase of a liquid asset, compare the whole process with a permitted centralized venue, including withdrawals. A $50 swap on the illustrative Ethereum route pays $9 in gas, or 18%, before other costs. An Ethereum pool normally trades a wrapped representation of Bitcoin, not native BTC.

The course begins with the simpler purchase workflow and returns to DeFi in later levels. Add complexity when it serves a specific objective and the security checks have become routine.

  • Consider a DEX for a defined token or on-chain service.
  • Account for irreversible mistakes and technical responsibility.
  • Start with the basic purchase workflow and progress deliberately.

Counterfeit tokens, thin pools and rug pulls

A token name and logo can be copied. Compare the chain and contract address with a trusted official source. A correct address identifies the intended token, but it does not establish that its issuer or contract is safe. Thin liquidity increases the effect of your order and other traders' orders.

In a rug pull, insiders can withdraw liquidity or use contract powers to make exit difficult. Chainalysis reported more than $2 billion attributed to rug pulls in 2021. Anonymous control, unaudited code and unilateral withdrawal powers merit scrutiny.

Review audits, administrator permissions, liquidity-lock scope and expiry. None guarantees safety. Keep unfamiliar-token exposure small and resist pressure to act immediately.

  • Verify chain and contract address against a trusted source.
  • Examine usable pool liquidity.
  • Rug pulls accounted for more than $2 billion in the cited 2021 report.
  • Review audits, liquidity restrictions, privileged powers and exposure size.

Key points

  • A DEX executes through contracts, requiring the user to manage signing and permission risks.
  • In the constant-product example, exchanging against reserves changes the pool's price.
  • Adding 37,500 USDC to a pool with 750,000 USDC gives an average purchase price 5% above its initial reference.
  • For a $500 reference output, a 3% adverse change is about $15. Check minimum output in tokens.
  • At 150,000 gas and 20 gwei, the cost is 0.003 ETH, or $9 with the assumed ETH price.
  • Check contract identity, liquidity, privileged permissions and liquidity locks for token and pool risks.
  • Use separate wallets for long-term storage and experimental application connections.

Lesson quiz

Sign in to enroll and save progress.