Create an ERC-20 token on Polygon

tedtecch

tedTecch

Posted on May 16, 2022

Create an ERC-20 token on Polygon

The adoption of cryptocurrencies and block-chain technology has been amazing in the past few years, leading to more coins being created with different use-cases from Defi to exchanges etc.
In this article, I would teach you how to create an ERC-20 token using solidity and deploy it on the polygon block-chain network.
But before we start, Let’s define some of the terms we will come across in this article.

What is ERC-20?

Erc-20 is a technical standard used in a smart contract on the Ethereum blockchain to provide and implement a list of rules that all Ethereum based tokens must follow according to Investopedia.

What is a smart contract?

A “contract” simply means an agreement between two or more people and the word “smart” means automated or independent, therefore a smart contract is an automated document or instruction that executes an agreement running independently on a block-chain network.

According to Wikipedia, a Block-chain is a growing list of records called blocks that are securely linked together using cryptography, each block contains a cryptographic hash of the previous block, time and transaction data.

What is Polygon?
Polygon is a secondary layer solution that is compatible with and complements the Ethereum block-chain. Polygon complements the Ethereum block-chain by providing additional features relating to security, block-chain sovereignty, fast and cheap transactions.

Getting Started

To begin, we must first download and install MetaMask. So, go ahead and click this link to add the MetaMask extension to your browser (preferably Chrome).
After that, create and configure your cryptocurrency wallet. Because this is a learning exercise, click here, scroll down and click “add Mumbai network” to add the polygon test network and switch your network from Ethereum Mainnet to Matic Mumbai as seen in the image below.

Image description
The next thing is to get some test Matic to handle gas fees when performing write operations to our smart contract. Click this link and paste your wallet address to get some as seen in the images below.

Image description

Step 1: Create Remix File
To write the code we would use Remix by clicking on remix.ethereum.org, Remix is an online integrated development environment(IDE) for solidity. Click on the contract folder by the left and create a file called token.sol, the “sol” here stands for solidity.

Step2: Using OpenZeppelin
OpenZeppelin provides the ERC-20 token standard we discussed above and other templates for different tokens. Therefore we can utilise the wizard feature on their website to create the token even without prior knowledge of solidity.
In the Wizard page here, we add the name “First Token”, and the symbol “FRT” for our token, we also make it mintable meaning you can create a supply for the token. you can see this in the image below.

copy the generated code and head over to remix.

Explanation:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
Enter fullscreen mode Exit fullscreen mode

In the first line, we added the license type, followed by the second line that declares the version of solidity we are using, which is 0.8.7 in this file.

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable
Enter fullscreen mode Exit fullscreen mode

Lines 4 and 5 import the openZeppelin’s ERC-20 and ownable packages respectively, Then declare a contract followed by the name “First Token” we also specify that the contract is ER20 and it’s ownable.

constructor() ERC20("First Token", "FRT") {}
Enter fullscreen mode Exit fullscreen mode

next, we declared the constructor to initialize the ERC20 name and symbol whenever we create an instance.


function mint(uint256 amount) public onlyOwner {
        _mint(msg.sender, amount* 10**uint(decimals()));
    }
Enter fullscreen mode Exit fullscreen mode

Final we have a function that collects the address the token will be minted to and the amount to be minted, then a public modifier which means the function can be called outside the contract and another modifier only owner indicating only the owner(the address that deployed the contract) can call the function, it then passes this input details to the _mint() function.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract FirstToken is ERC20, Ownable {
    constructor() ERC20("First Token", "FRT") {}
    function mint(uint256 amount) public onlyOwner {
        _mint(msg.sender, amount* 10**uint(decimals()));
    }
}
Enter fullscreen mode Exit fullscreen mode

Step4: Compile the contract
Using remix makes it so easy to compile our smart contract, you can do this by clicking on the solidity compiler which is the third icon on the left navbar. Next, we ensure that our solidity version in the second line in our contract is the same as our compiler version to prevent any errors, then you click the “compile token.sol” button, subsequently you can just “CTRL + S” to compile which saves time.

Step5: Deploy Contract
Now that our contract is compiled we can deploy it to Ethereum, polygon blockchain, Binance etc. Here we would deploy on the polygon Mumbai testnet because this would cost some gas fees to deploy, also testnet is a great way for developers to learn without any cost.

To deploy the contract, we move to the last icon on the left navbar. First, we change the deployment environment JavaScript VM(london) to injected web3, and approve the "connect” metamask wallet address to remix. The next thing we check is if the contract we are about to deploy is the “token.sol” you can do this by checking the above the deploy button.

Finally, we can now click the deploy button, confirm the transaction on metamask and wait for the traction to be approved which would take some seconds.
Now that our token has been successfully deployed we are going to mint 1000 FirstToken and confirm the transaction, we can also check the total supply of this token feel free to play around with other functions. Note the orange buttons are written operations that would cost a gas fee and the blue buttons are read operations which won't cost any gas fees.

View Deployed Token In Wallet
Now, that we have our token deployed on the polygon network, it would be amazing to view this token in our wallet just like any other token right?
To view, this token opens up metamask, click on the assets tab and
“import tokens” at the bottom of our MetaMask app. On the provided page we can fill out some empty fields but we would need only the contract address and the rest would the filled with the help of MetaMask.
You can get the contract address by clicking on the copy icon in the remix as seen in the image below.

Image description
Click on the “add Custom Token” button and we have our token imported successfully. As you can see from the image below, we have “1000 FRT” which we minted earlier and you can also see the history of the transaction, we can also transfer this coin to your friends with just their address.

Image description

Conclusion

In this article, we have covered some blockchain terminologies, solidity, how to create an erc20 token and also viewing this token in your MetaMask wallet.
I hope this article has been helpful to you in your tech journey, remember there is no learning without practice so make sure your practice.
Thank you.

In the second part of this article, I will be integrating this smart contract into a flutter application.
You can read the second article here

💖 💪 🙅 🚩
tedtecch
tedTecch

Posted on May 16, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related