How To Create Your Own ERC20 Token In 30 Mins + Contract Verification

How To Create Your Own ERC20 Token In 30 Mins + Contract Verification

In this guide, we will be creating an ERC20 Token and deploying it to Polygon using Remix IDE, OpenZeppelin Wizard, Metamask and Polygonscan.

·

10 min read

Introduction

Have you ever heard of tokens like Shiba Inu, Dai, and Usdt listed on popular exchanges and wondered how they were created? Or probably just getting started with smart contract development. In this guide, we are going to use a step-by-step approach to create and deploy our token to Polygon.

We will first write the smart contract using Remix IDE, an online IDE made for smart contract development, and then deploy it. Here is a more detailed summary of what we gonna do:

  • What is ERC20?

  • Getting familiar with OpenZeppelin Wizard.

  • Write our Token contract in solidity with Remix IDE.

  • Deploy our smart contract to Polygon

  • Interact with our deployed smart contract from Remix

  • Add our custom token to Metamask Wallet

  • Verify our Contract on Polygonscan

  • Conclusion

What is ERC20?

ERC-20 is quite famous these days. Many Defi protocols, Nft projects and DAOs are launching tokens following that standard as they play a major role in most of these platforms. Tokens have real-world uses. You can literally use them for several different purposes, which may vary from on-chain voting, payment of services within specific ecosystems, a unit of account and exchange of goods and services, etc.

ERC-20 represents fungible tokens which means they have the properties of uniformity and divisibility. Every fungible token is interchangeable as each unit of token possesses the same value. Just like every dollar bill has the same value as one dollar.

Overview of ERC20 Token Standard Functions

ERC20 standard defines a set of functions to be implemented by all ERC20 tokens so as to allow integration with other contracts, wallets, or marketplaces. This set of functions defines two events and 9 methods (including 3 optional methods) as highlighted below:

function name() public view returns (string) // optional
function symbol() public view returns (string) // optional
function decimals() public view returns (uint8) // optional
function totalSupply() public view returns (uint256)
function balanceOf(address _owner) public view returns (uint256 balance)
function transfer(address _to, uint256 _value) public returns (bool success)
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)
function approve(address _spender, uint256 _value) public returns (bool success)
function allowance(address _owner, address _spender) public view returns (uint256 remaining)

event Transfer(address indexed _from, address indexed _to, uint256 _value)
event Approval(address indexed _owner, address indexed _spender, uint256 _value)

Given the following interface above, developers may customize tokens by adding new features and logic and deploy on the Polygon Mumbai network.

Getting familiar with Openzeppelin Wizard

OZ Wizard is an interactive generator that helps bootstrap your smart contract and learn about OpenZeppelin Contracts. With OZ Wizard, you can build your smart contract for free in minutes.

OWizard.png

So that's what OZ Wizard looks like? Yes, that's what it looks like.

As seen in the picture above. There are different tabs for respective token standards so you can customize them to your taste. In this guide, we would be focusing more on ERC20 of course.

In the settings tab, there are quite a number of configurations to make. From the token name, symbol, decimal and pre-mint. All these are meant to be filled. The features tab helps us to configure some of the functionality of the token.

Let's customize our token!

So that means, we are customizing:

  • The token name

  • The token symbol (I'd go no more than 4 characters here)

  • The token decimal places

  • The pre-mint (amount of tokens to be minted to yourself)

  • The mintable feature

newOz.png

The image above shows our newly customized ERC20 token contract. You can always customize it to your taste but for the sake of this guide, we will not be considering other features except the mintable feature.

Some things to keep in mind:

The amount you're minting to yourself should correlate to the amount of decimal places that you set. For example, if you want a token that has 0 decimal places to have 50 tokens, then the supply would be 50.

But if you have a token with 18 decimal places and you want 50 of them, then the supply would be 50000000000000000000 (18 zeros added to the amount).

The pre-mint enables you to receive the amount set as the creator of the contract. That’s what this line of code is:

_mint(msg.sender, 10000000000 * 10 ** decimals());

Whatever you set here will be sent to the wallet of wherever you deployed the contract. While the mint function enables new tokens to be created and sent to the address provided. Note this is only callable by the creator of the contract.

Once we are done customizing, we can use that in our IDE.

Write our Token contract in solidity with Remix IDE

Let's navigate to Remix, open the contacts folder, and create a new file called “Gemini.sol”. Copy&Paste your already customized token contract like the one below.

token.png

Note we changed our pragma solidity to ^0.8.0. We had to because it would be important while verifying our contract which we would get to in a few minutes.

Deploy our smart contract to Polygon

For this guide, we would be deploying our smart contract to a test network.

There are what we call Testnets (Test networks). Polygon mumbai is Polygon's mainnet test network. A testnet is an environment that is pretty much like Mainnet, despite the fact that it’s here to act as a sandbox.

It’s a testing environment, that can be used to run your smart contract in almost real conditions, without involving real assets (we will see in a bit that we will still use MATIC, but fake MATIC).

Note that the steps to deploy to Polygon Mainnet are exactly the same as the ones that we follow here.

Install Metamask

We would need Metamask to sign and send transactions, so make sure it is installed. Don't know how to go about it, you can read this guide here and learn how to add polygon network here.

Select the Polygon Mumbai network

Metamask allows us to change the environment in one click. By default, it is connected to Ethereum Mainnet, let’s switch it to Polygon Mumbai.

metamask.png

Where to get MATIC to deploy our smart contract

As we now know, Polygon Mumbai works as Polygon Mainnet. Meaning that we still need to pay gas to deploy our contract. And to pay for the gas, we need MATIC… “Test MATIC”.

We can get some MATIC to play with from different faucets.

Here is a faucet to get some Test MATIC in your wallet.

Let’s deploy our smart contract

Head back to Remix, and go to the Deploy and Run Transactions page. Let's quickly change the execution environment to injected web3 as seen below

iweb3.png

connectedIweb3.png

Head over to the contract section and select your contract as seen below

contractSec.png

Note you should notice the network, compiler version and compiler configuration when you deployed the contract as they would be needed for contract verification.

Finally, let's deploy our smart contract!!

Click on deploy!!

After a few seconds, there is a metamask pop-up notification for transaction confirmation and your terminal displays a success message as seen below

metamaskNot.png

contractCreation.png

To view the transaction on polygonscan, copy the address of the already deployed contract and paste it into the block explorer in my case 0xB8b18A839F1968D5fcA9d37d38074E29Fd171725, then click on the transaction hash.

gemVeri.png

Congratulations 🎉🎉, we’ve just created our first ERC20 token contract on Polygon Mumbai. Check it here!

Interact with our deployed token contract from Remix

Let's head back to our IDE. There should now be a deployed contracts panel in the Deploy & Run Transactions tab:

geminiDep.png

Awesome! Let's examine two functions that we created in our token contract: transfer, balanceOf

Transfer and BalanceOf Function

To transfer tokens, we need to fill in the address of the recipient of the token and the amount to be deposited as done below. Note: the address transferring must have sufficient tokens. In our case, the address transferring has 5000000 pre-minted tokens.

transferGem2.png

What this does is send some tokens(100,000) out of the sender's to the recipient's address. We can see the state changes below:

gemBalB4.png

gemBalAf.png

And the balance of the recipient increased from 0 to its current balance as seen below:

receipientGemBal.png

Add your custom token to Metamask Wallet

In other to add your tokens to your wallet, you need to readily have your token contract address. In this case, I'll be using the address of Gemini Token (0xB8b18A839F1968D5fcA9d37d38074E29Fd171725)

Now let's head back to Metamask in order to import our tokens as seen below

metamaskAdd.png

The next step is to add our contract address in the fields provided as seen below

importGem.png

Then finally click on import to view your token in the list of your assets as seen below addedGemToken.png

Verify our contract on Polygonscan

We’re going to be validating our token source code, which is going to be important for various reasons, mainly verifying the validity of your token is to let the public know you’re not doing anything shady.

Okay, next let us go through it step by step

  • Flatten your contract

We'll use the Remix Flattener plugin to flatten all our contracts into a single file. Click the Plugin Manager on the left, find the Flattener plugin, and click Activate to use it. I already have it activated as seen below

flatenner.png

Then the plugin FLATTENER will show on the left side of the page, click it and you will find the blue button Flatten contracts/Gemini.sol, click it, and then click the bottom button Save Gemini_flat.sol to save the flattened file, as seen below:

stepsFlatten.png

It will ask your permission to save the file. After you accept, it will show that the file was saved. This means you can find the flattened file at the bottom of the list of files, so click to open it.

flattensol.png

Now we are done flattening our file, all we have to do now is to add the SPDX license manually as seen below:

spdx.png

  • Verify on the Polygonscan

Nice! we are only one step away from success. Goto the token contract you have deployed on polygonscan, click the option Contract and then click Verify and Publish, as seen below:

verify&Publish.png

Next is to do the following:

Choose the right Compiler Type : Solidity(Single file)

Choose the right Compiler Version : YOUR_DEPLOYED_VERSION

Choose a right License Type.

Note: The contract address field is automatically filled.

Fill these fields accordingly as seen below:

veriNext.png

To get to the last phase, click the button Continue to enter the next page, click the Optimization if you use it when you deploy, and then paste the flattened source code we generated in step 1.

Note we did not fill in the constructor argument because we did not pass it in at deployment because it was hardcoded in our code. It is important to note that generally, Polygonscan will upload constructor arguments automatically, but sometimes it will upload the wrong constructor arguments due to the complexity of constructor arguments, so in this case, you can encode arguments by this tool:

Finally, complete the captcha and click the button Verify and Publish as seen below:

nextveri1.jpg

It takes us to this page

verification.png

Note it now has a verified icon

verfiedTicker.png

Congratulations 💃💃 you have just successfully verified your token contract.

Conclusion

What a ride! Big ups for making it until the end and for writing, deploying and verifying your token contract on Polygon Mumbai with Remix IDE, OZ wizard and Polygonscan.

I hope this guide to creating and deploying your token smart contract was an eye-opener. If you have successfully created your token and verified it, you can send me the polygonscan link here. Also if you have any issues while verifying your contract, you can check out this guide

If you have any questions, suggestions or want some Gem Tokens you can drop your address in the comment section below, or reach out to me on Twitter!

Stay Buidling 💪💪