Day 7: Swap some tokens on Polygon

CORE CONCEPTS

Today you’ll learn how to swap tokens on a decentralized exchange. Token swaps are an important primitive in the fast-growing world of Decentralized Finance, aka DeFi. Because we’re performing our transactions on the Polygon network, gas fees will be minimal, so relax and have fun here.

What are decentralized exchanges?

On Day 1, you purchased some Ethereum on a centralized exchange. You swapped US Dollars or your home country’s fiat currency for cryptocurrency. The centralized exchange you used (eg, Coinbase) charged a small fee for performing this service.

Today you’re going to perform a similar type of transaction, except it will be on a decentralized exchange. And, instead of swapping fiat for crypto, you’ll be swapping crypto for crypto.

Decentralized exchanges are built on open-source code, enabling any user or other interested party to see how they work. They can also fork the codebase or add new functionality to it.

Decentralized exchanges use a smart contract known as an Automated Market Maker (AMM). AMMs allow digital assets to be traded in a permissionless and automatic way through liquidity pools rather than traditional markets of buyers and sellers. AMM users supply liquidity pools with crypto tokens, whose prices are determined by a constant mathematical formula. Liquidity providers earn a share of fees whenever a token swap is performed. (We’ll learn more about liquidity pools on Day 12.)

Different token swap combinations have different levels of liquidity. Some new or uncommon pairings many not have much liquidity, meaning that a single trade may have a significant impact on price. Price slippage occurs when trades have a large and immediate impact on price. Slippage is a problem for big trades or small liquidity pools.

Uniswap is the largest decentralized exchange, with more than $1 billion in daily transactions. Sushi Swap, originally a fork of Uniswap, is another leading exchange. Both are built on Ethereum and have support for Polygon. Decentralized exchanges operate 24/7.

To keep things familiar, we’re going to use Zapper to perform our first swaps.

Make your first token swap

Go to the Zapper website (zapper.fi) on your MetaMask-enabled browser. Switch the network from Ethereum to Polygon. MetaMask should pop up asking you to confirm that you want to Switch network.

Switch over to Polygon
Switch over to Polygon

Then click the Swap link from the sidebar. A little widget will appear that looks similar to the bridging widget you used yesterday.

Get ready to swap your first tokens
Get ready to swap your first tokens

Our first swap will exchange WETH (Wrapped Ethereum) for MATIC. MATIC is Polygon’s native token and the token used to pay gas fees on the network.

Follow these steps to make your first swap:

  1. In From, select WETH. This should appear at the top of your list of options, since it’s the only token you currently have on the Polygon network.
  2. In To, type in MATIC and select it from the list. You’ll also see WMATIC and a few other tokens with the word MATIC in them, but just ignore them.
  3. Where the Number is, enter 0.005 WETH. This equates to about 10 MATIC or $15 at current rates of exchange.

Then, click the Approve WETH button. MetaMask will pop-up and… Oops! What’s this??? It says we cannot proceed with the swap because we don’t have enough MATIC to cover the gas fee (of about $0.01).

MetaMask can't complete the transaction because we can't pay the gas fee
MetaMask can't complete the transaction because we can't pay the gas fee

OK, to proceed, we’ll need to get some MATIC from a different source…

Get some MATIC

Fortunately, there’s a simple and easy way to get some MATIC directly from the Polygon dapp, without needing to go back to the drawing board.

Follow this link to access the Polygon dapp: wallet.polygon.technology.

Click Connect to a Wallet and sign the transaction when MetaMask pops up.

Connect to a Wallet
Connect to a Wallet

Now you’re connected to the site. Click on the Polygon Wallet icon. This will bring you to a page that looks like the screenshot below. Once again, you’ve brought your data with you: the Polygon dapp knows that you have some Wrapped ETH in your wallet.

Your digital assets on the Polygon network
Your digital assets on the Polygon network

Click the Swap for gas token link on the left side of the page. Note the small print, showing that 1 MATIC will pay for approximately 1000 transactions. Gas is cheap on the Polygon network!

Swap for gas token (MATIC token)
Swap for gas token (MATIC token)

Select the smallest amount - 1 MATIC - and select the ETH token from the dropdown. You should see the widget indicate that the transaction is ready for approval. Also note the fine print saying that this is a gas-less transaction. Click Approve and then sign the transaction when MetaMask pops up.

Approve and sign the transaction
Approve and sign the transaction

Polygon transactions are much faster than Ethereum. You should see your approval transaction execute in seconds.

That was part one. Now you need to click the Swap button to complete the transaction. MetaMask will pop-up again for you to sign.

Swap and sign the transaction
Swap and sign the transaction

You’ll be notified when the transaction completes.

Success!
Success!

Now we’re ready to proceed with our swap.

Complete the swap on Zapper

You can close your tab with the Polygon dapp and go back to your tab with Zapper. Hopefully, the transaction you set up previously is still on the screen. (If not, go back to the earlier section and set-up your swap again. If there’s an issue, just refresh your browser and set-up the swap again.)

Once gain, click the Approve WETH button. MetaMask will pop-up and this time we are able to Confirm the transaction.

Approve WETH and confirm the transaction in MetaMask
Approve WETH and confirm the transaction in MetaMask

Once you’ve approved the interaction, click the Exchange button and then Confirm on MetaMask. Your swap will execute now and should be complete in under a minute.

Complete your exchange of WETH for MATIC
Complete your exchange of WETH for MATIC

If you go to the Home page on Zapper, you should see your portfolio updated to include MATIC and a reduction in your WETH balance.

Your MATIC tokens are in your wallet now
Your MATIC tokens are in your wallet now

If you click on the History tab on your Zapper homepage, you can view each of your transactions on the Polygon network. We made a total of three transactions - and spent about 0.03 MATIC (about $0.05) in gas fees.

Gas is much cheaper on Layer 2
Gas is much cheaper on Layer 2

We’ll be spending the next few days making transactions on Layer 2. Feel free to experiment by making your own token swaps if you like. Because gas is cheap, you can play around and always swap back to a more familiar token like WETH or MATIC.

Further reading:

Subscribe to Carl Cervone
Receive the latest updates directly to your inbox.
Verification
This entry has been permanently stored onchain and signed by its creator.