close
close
LearnDeFi
How to swap tokens?

How to swap tokens?

April 13, 2023
3
 min read

A decentralized exchange (DEX) lets people exchange any two tokens. In this web3 guide, we’ll cover how DEXs work and how to swap tokens.

By Alan, Peter

A decentralized exchange (DEX) lets people exchange any two tokens.

In this guide, we’ll cover:

  1. A simple analogy
  2. How do DEXs work?
  3. How to swap tokens?

What follows is not investment advice.

A simple analogy

Suppose you have some apples that you want to trade for oranges. You visit the grocery store:

  1. You sell 10 apples to the store at $1 each. The store takes your apples but doesn’t pay yet.
  2. Your neighbor buys 10 apples from the store at $2 each. You and the store both get $10.
  3. You use your $10 to buy 5 oranges at $2 each from the store.

One day, you realize that the oranges you’ve been buying are actually from your neighbor. You both decide to cut out the middleman (store) as follows:

  1. You and your neighbor pool 10 apples and oranges in a basket.
  2. To take 1 apple out, you need to put 1 orange in the basket.
  3. After a while, the basket only has 5 oranges vs. 15 apples. Because there are fewer oranges, you agree to put in 2 apples for every orange that you take out.

Keep this analogy in mind as we discuss how decentralized exchanges work.

How do DEXs work?

Centralized exchanges (CEX), like the store, use the order book method:

  1. You put in an order to sell 10 AAPL shares for $100 each 
  2. The CEX fills your order when someone else agrees to buy the shares at the same price

Decentralized exchanges (DEX), like the basket, are automated market makers (AMM) that use liquidity pools. Let’s define a few terms:

  1. A liquidity pool is a pair of tokens locked in a smart contract to facilitate trading.
  2. Liquidity providers (LPs) are users that fund a liquidity pool with a pair of tokens. 
  3. Liquidity is the ability to buy and sell a token without affecting its market price.

DEXs want a large liquidity pool to reduce price volatility. Consider these two examples:

  1. An ETH-DAI pool has 10 tokens each. If you swap 1 ETH for DAI, the pool’s supply will decline by 10%, and it’ll become much more expensive to swap ETH for DAI. 
  2. An ETH-DAI pool has 100,000 tokens each. If you swap 1 ETH for DAI, the pool’s supply won’t really be affected and prices will remain stable.

So to recap, people can use DEXs to:

  1. Swap tokens through a pool, paying some transaction fees.
  2. Provide liquidity to a pool, getting a cut of the transaction fees along with extra tokens.

We’ll cover providing liquidity later. Let’s walk through how to swap tokens first.

How to swap tokens?

In the example below, we’ll swap ETH and DAI tokens using Uniswap (a popular DEX) on Polygon. Polygon is an Ethereum L2 scaling solution that offers faster and cheaper transactions. To try this example yourself, you need to have:

  1. A crypto wallet (see How to set up a wallet?)
  2. Polygon WETH and MATIC tokens (see How to use L2 chains?)

1. Visit Uniswap and connect your wallet and network of choice

Visit Uniswap and click “Connect Wallet” in the top-right corner. Switch to the “Polygon” network.

Connect your wallet

2. Choose the tokens that you want to swap

Select the “Swap” tab and choose to swap an amount of WETH for DAI.

Select your tokens and enter the amount that you are willing to swap

3. Check the exchange rate

Check the exchange rate to prevent ugly surprises. Cryptocurrency prices are volatile and market prices can differ from the quoted swap price in the pool.

In this example, we’re swapping WETH (Wrapped Ether) with DAI

Slippage is the difference between your expected exchange rate and the actual rate. Volatile tokens are more likely to experience slippage than ETH and DAI. You can also set the slippage tolerance in Uniswap:

  1. Click the gear icon and set the slippage tolerance. 
  2. In the example below, the transaction will not go through if the price changes by more than 0.5% during a swap.
Click the gear icon to see options

4. Click ‘Swap’ 

Click the ‘Swap’ button to initiate a swap and confirm the transaction using your wallet. Note that you’ll need to pay a transaction fee (e.g., a small amount of MATIC). 

After a few minutes, you should see some DAI in your wallet.

Now that you know how to swap tokens, let’s cover lending and borrowing crypto.

Up next: How to lend and borrow assets?

Learn more

Subscribe to our newsletter
Oops! Something went wrong.
On this page

A simple analogy

How do DEXs work

How to swap tokens

Learn more

Related articles

Browse all

Prev Article

🤷

There are no previous articles in this Pathway

Check out other Learning Paths!

Next Article

🤷

There are no more articles in this Pathway

Check out other Learning Paths!