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.
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.
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.
Then click the Swap link from the sidebar. A little widget will appear that looks similar to the bridging widget you used yesterday.
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:
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).
OK, to proceed, we’ll need to get some MATIC from a different source…
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.
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.
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!
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.
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.
You’ll be notified when the transaction completes.
Now we’re ready to proceed with our swap.
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.
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.
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.
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.
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.