Social media

How I created my first ERC-20 token on the ethereum testnet?

Author: LB 2021-03-06 142

The ethereum blockchain brought us many interesting and convenient functionalities. Among these are the so called smart contracts that enable any user in the ethereum ecosystem to create executable codes and deploy them onto the blockchain. These executable codes can represent conditional relationships between the users who are willing to interact with the smart contract. With the aid of these objects one can implement functions to represent classical instruments of traditional finance. The most straightforward example is a coin and it turns out to be relatively easy to issue completely new coins with a few lines of code. ERC-20 tokens are very similar to coins and they can also be implemented using the ethereum community’s new programming language. In the following article, I’m going to demonstrate the necessary steps with the corresponding source code to create new ERC-20 tokens on the ethereum blockchain.

Smart contracts can be implemented using solidity and the contracts can be deployed by either using an online development environment (Remix in this case) or Truffle Suite. If you have gone through my previous article, I don’t have to show you all the necessary steps, because there is a huge overlap between implementing a new coin and an ethereum token.

So first of all, since you don’t want to waste your hard earned money on transaction fees, the best is to initialize an ethereum testnet, and you can achieve this by downloading the ganache command line interface. For this use the following command: $ sudo npm install -g ganache-cli. As soon as you have the application on your computer, you can launch it by simply evoking “ganache-cli”. Now you have a running ethereum test net and ganache generates you 10 ethereum accounts with 100 ETH on each of them. You can use these accounts to test your dapp you’re about to develop here.

In order to implement your first ERC-20 token on the ethereum blockchain, you can download a sample code from here. There is nothing special about ERC-20 tokens. They are smart contracts with a few methods prescribed by the ERC-20 standard. An ERC-20 token must have a name, a symbol, a number of decimals used and a total supply. In addition to these, the smart contract representing the token must implement the following functions: balanceOf(), transfer(), transferFrom(), approve() and allowance(). The contract should also have two events: Transfer and Approval. As long as your smart contract implements these methods, you can call it an ERC-20 token.

I think most of the methods don’t require explanation. But let’s talk about approve() and allowance(), because it took some time for me to understand the function of these. Obviously no user is allowed to withdraw tokens from you if you don’t give them a permission. Token transfers can only take place if a user enables another user to withdraw tokens from him. This is called the approval mechanism in the world of ethereum tokens. So you always have to call the approve() method from the source wallet and authorize a certain number of tokens to be withdrawn by another user from the source wallet before you can evoke the transferFrom() function.

Now you have to deploy the smart contract representing your new ERC-20 token. In my previous tutorial, in which I implemented a coin on the ethereum blockchain, I used Remix to deploy the contract onto my ethereum testnet. This was a very convenient way, because you only have to choose the right option from the dropdown menus and click on deploy. I guess, if you’re about to write a much more dynamic application, Remix is not going to be the best solution so I searched for a command line deployer.

The command line deployer can be installed with the following command: $ sudo npm install -g truffle. You have to create a new directory for your truffle project and execute $ truffle unbox webpack in the directory. This will initialize a new truffle project for you. I found the Truffle Suite very easy to use, however I had to overcome some obstacles until I could finally compile and deploy my contract. Let’s talk about this a bit.

The first problem I found was the following. When you initialize a new project truffle creates a few sample contracts under the contracts directory. You have to delete them if you’re messing around with your own contract.

The next thing is to set up your migrations under “migrations”. You’ll find two files here and you have to rename the variables in these files so that they reflect your own new token.

You can try to deploy your contract using $ truffle migrate --network development, however it will most likely fail. The first thing I had to resolve was the conflicting solidity versions in the configuration file and in the smart contract. So make sure the solidity version in the first line of your smart contract matches the solidity version written in "truffle-config.js" under the compilers section.

You also have to set the network you’re about to deploy onto. This can be achieved by setting your network variable in "truffle-config.js". If you’re using the ganache testnet, you only have to uncomment the host, port and network id part.

Also, don’t forget to set the number of ERC-20 tokens to be issued on the ethereum testnet. If you read the smart contract carefully enough, you can discern that the token’s constructor takes a parameter, and this will tell the contract the number of tokens to be issued. You have to set this amount when you are creating your Token object in the "1_initial_migration.js" file under migrations.

If you are done with these settings, you can retry the deployment process with $ truffle migrate --network development. If there is no typo in the migration files, the smart contract should get deployed on the test net and you’re ready to use it.

These smart contracts can be conveniently interacted with using a web3 framework. In the previous guide, I have already shown you how to write a simple NodeJs web application to interact with a smart contract on the ethereum blockchain. These steps are almost identical, because smart contracts are smart contracts, no matter whether you’re implementing a coin or an ERC-20 token.

In order to avoid integer overflows, be sure to use the SafeMath library and this will protect your smart contract against certain malicious attacks.

When you’re writing your NodeJs application, you need to somehow generate the ABI (Application Binary Interface) data of your ethereum smart contract. Luckily, truffle generates this data for your during the deployment process, and you can find this in the "build/contracts/[your token name].json" file.

Now you can issue your own ERC-20 token on the ethereum blockchain.

Good luck!


Interesting entries


Russian miners are already testing the Nvidia CMP 30HX chip for mining


Technical details and the ROI of Chia Network Farming


Investigating monero mining on a notebook


Optimizing the mining of monero on a multithreaded CPU


On game theory on cryptocurrency mining with increasing DAG size