Create Your ERC20 Token In Just A Few Clicks

Kerala Blockchain Academy
6 min readJan 31, 2024

--

By Lekshmi P G, Research Engineer Kerala Blockchain Academy

Tokens play a great role in the Web3 era. Considering Web3/Decentralised applications, tokens are indeed multi-talented. They serve as an entry pass for one application, as reward points in other applications, as an incentive mechanism for some other applications, and so on. If you are new to tokens, don’t worry; just go through this article before you start.

This article will help you generate tokens seamlessly. Here we are going to see how to generate a fungible token. So, if you are new to fungible and non-fungible tokens, just go through this. As said earlier, we will deal with fungible tokens with the token standard of ERC20.

About ERC20, it is a proposal by Vitalik Buterin to introduce a standard for fungible tokens. A standard to deal with generating new tokens, buying & selling of tokens, and destruction of generated tokens. To put it, the ERC20 standard is an interface that contains a set of function declarations that should be implemented for generating a token. Openzeppelin has already implemented this ERC20 interface; you can visit their GitHub repository and look through their ERC20 implementation.

Openzeppelin has made our job one step easier. Here we are going with Openzeppelin’s implementation. Now we have to use Openzeppelin’s ERC20 implementation in our contract. To write, compile, and deploy the contract, we are using RemixIDE.

Open RemixIDE using this link. Now we are going to write our Token Contract.

Now we are going to write our Token Contract.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

On RemixIDE, currently, you will be in default_workspace; click on that and select Create a new workspace.

On selecting that, another tab will open, where there is an option to choose a template; Basic by default.

Click on that, and you get an option Openzeppelin ERC20;

Select.

Now, you will move to the option to select Features like Mintable, Burnable & Pausable. We are going with Mintable.

Click OK.

Here comes a list of folders like contracts, scripts, and tests created. Let’s concentrate on the contracts folder, inside this folder resides our Token Contract. The name of the contract is MyToken.sol.

MyToken.sol

Here you can see the MyToken contract inheriting Openzeppelin’s ERC20 contract and also some other contracts like Ownable, to assign an owner for this contract, and also ERC20Permit, to manage a spender or a third party. To inherit, we need to import the necessary contracts. Then, inside the MyToken contract, first comes the constructor, which is invoked only once, ie at the time of deployment. Here, we also have to initialise the inherited contract’s constructors. To the MyToken contract constructor, we have to pass an address, which we want to assign as the owner of this contract. And then to the ERC20 constructor, we have to pass the ‘token-name’ & ‘symbol’. Here it is “MyToken” & “MTK”. Then to the Ownable constructor, we pass the contract’s owner address’ and to the ERC20Permit constructor we pass the ‘token name’, “MyToken”.

In the MyToken contract, you can make any changes to make your customised contract.

In the constructor function, we assign the Owner of the contract and also set the name & symbol of the token. Then comes the mint function, which creates a new token. Inside the mint function, OpenZeppelin’s _mint function is called. To the _mint function, we are passing the ‘to’ address to which the minted token should get credited and the ‘amount’ of tokens to be minted.

Now, we can compile the contract using the compile option on Remix, and then we can deploy the contract. So let’s deploy in Sepolia Testnet. For that, you have to select Injected Provider on Deploy & Run Transaction on Remix and switch to Sepolia on your wallet. To deploy, we have to pass one more thing: the owner address for the constructor. And now Deploy. Since we are using a wallet(Metamask) for deploying & interacting, for every transaction except for read/call operations, metamask will give a pop-up for confirmation of spending ethers.

If you want to know in depth about what actually happens when we deploy a contract, Be free to refer to this article.

On deployment, a new interface tab for interacting with the contract will be opened on Remix, which contains our mint function along with other functions like approve, transfer, transferFrom, allowance, balanceOf, and so on; these functions are functions present in the inherited ERC20, Ownable, ERC20Permit contract.

So let’s use the mint function of our MyToken contract.

To the mint function, we have to pass two parameters: the ‘to’ address, the address to which the newly minted token is to be given, and the ‘amount’ of the token to be minted.

Here on amount, if you want to generate 100 tokens then you have to put 100 followed by 18 zeros (100,000000000000000000), since on Openzeppelin’s ERC20 contract the denomination is specified as 18.

Openzeppelin’s ERC20

Once the transaction is successful, you can see a green tick followed by transaction details on the Remix log.

Now you can check the balance using the balanceOf function, by providing the ‘to’ address.

So we have minted 100 tokens which we can use in our decentralized application. Now if you want to send the token to any address, you have to import it to the wallet. Here, we will see how to import it to Metamask wallet, go to Metamask, and then to the Sepolia account into which the newly minted token got credited. On the account, there is an option called Tokens.

Select Import tokens option, you will get a new tab.

Then, enter the MyToken contract address, which can be copied from Remix, in the Deployed contracts field.

Once you enter the contract address correctly, the remaining fields will be auto-filled.

Now click next.

Now Metamask is asking permission to import the newly minted 100 MTK tokens. So click on Import. Then, you can see the 100 MTK tokens listed in the token field.

Now click on this token, and you will get an interface to send the token like any other crypto.

So here is the happy ending.. we have created our fungible tokens in a few clicks. You can actually use this token for any of your Decentralized Applications, or you can send it to any other address.

Go on, create, and play with your token…..

References

  1. https://ethereum.org/en/
  2. https://github.com/OpenZeppelin/openzeppelin-contracts
  3. https://ethereum.org/en/developers/docs/standards/tokens/erc-20/
  4. https://github.com/ethereum/ercs/blob/master/ERCS/erc-20.md

--

--

Kerala Blockchain Academy
Kerala Blockchain Academy

Written by Kerala Blockchain Academy

One-stop solution for quality blockchain education and research. Offers best in class blockchain certification programs in multiple blockchain domains.

Responses (1)