Create your first ERC20 token


 

Hi  there ! Hope you're doing great 😃

 

In this brand new section centered around blockchain and more particularly around the ethereum project we will dive more precisely in this burgeoning ecosystem and learn how to build project and extract values from the growing DeFi space aiming to revolutionize the financial market as we know it today.

 

⚠️⚠️⚠️ Important note ⚠️⚠️⚠️

  • No prior coding knowledge is require to follow along those tutorials. So don't worry if you don't have any idea about Solidity, Javascript I will explain everything as we go and you'll see you'll be hable to get a hang of it very quickly as goes the saying "we all have to start somewhere " 😉
  • Set-up : These tutorials will be based on the set up that I'm currently using which run on macOS and as you 'll see as you go deeper and deeper in the blockchain space most of the tools and resources are first developed for mac so if you're on Windows or linux don't worry the core coding scripts will be the same but be aware that you'll  have to overcome some little challenges from time to time when dealing with packages and all 

But enough talking already, let's get down to it and see how we can create our very first own ERC20 token.

 

First we will have to create a new folder on our computer, to build our project. To do so, go into your application folder, and double clic on Utilities. There you should see Terminal, double click on it to open it. 

 

Now that we're in the terminal it's time to ask our computer to create our new directory and the different folder and files that we'll need but don't worry I already  did all the hard work for you so just go over to this github page, copy the url link and paste it in the terminal as follows :

git clone "URL LINK"

 

 

This way we'll skip all the setting up part of the project which appears to be a bit complex for only an introduction tutorial and focus solely on the coding and testing part 😌

 

Now before being able to code and all you'll need a text editor, personally I'm using Sublime text so if you already have your own text editor I let you see by yourself how you can input the solidity syntax in it. Else you can download Sublime Text here and find a quick and easy explanation  on how to input the solidity syntax here.

 

Now that we're all set up let's go and take a quick look at our project saved in the root directory. To do so, go into the finder and press (Shift + Cmd + g) you should see a window pop up

 

 

Press enter and you'll be redirected automatically into the root directory, find the ERC20_project and double click on it.

 

As you can see many folders are present, each serving a specific purpose :

  • contracts : folder regrouping all the Solidity coding file for our token
  • migration : folder regrouping the JavaScript files controlling the migration details of our token on the blockchain during its deployment
  • test : folder regrouping our JavaScript test files relative to our token 
  • truffle-config.js : file describing the details of the different networks where we want to deploy our token

Now that we made a quick tour of our project, it's time to start coding our ERC20 token. So, go into the contracts folder and open  the token.sol file with Sublime Text.

 

 

The first thing that we have to do is to specify the version of solidity that we're using and initiate our Token by creating the relative contract and specify its basic information.

Code explanation :

  • The pragma solidity statement allows us to specify the version  of Solidity that we want to use which here is the 0.6.7
  • The contract statement here act similarly to a class object in other object oriented language and allow us to create our token.
  • The 2 following statements create the name and symbol of our token that's why we declare those as a "string" or text elements that are "public" meaning that they  can  be called inside the contract or via message on top of being attributed an automatic getter function.
  • The next 2 "uint" or number variables creates the  total supply and the number of decimals to take into account. By definition  the number of decimals for ERC20 token is 18 that's why we'll use 18 decimals here.
  • The constructor statement which is special method invoked whenever a contract is created is automatically created by Solidty during the deployment of the smart contract but it is good practice to code it explicitely especially since in our case we need to to attribute to the message sender or here the address used to deploy the token contract the entirety of the token supply.
  • Finally, we create a transfer function allowing us to transfer from the message sender address a specified amount of tokens  and emitting an event in order to inform all  users on the blockchain the source, the destination and the token amount of the transaction. (see here  for the ERC20 standard guidelines)

Alright, so now that we created the basis of our  contract let's test it. To do so, go back to the project in the finder and open the token_text.js file in the test folder with Sublime Text and copy the below code :

 

Code explanation :

 

The code here in JavaScript can be divided in 3 major parts :

  • 1st part : we upload our token contract from our project
  • 2nd part : we call the library that we need and create our helper function
  • 3rd part : we deploy our contract and operate our first basic test of the token contract

⚠️⚠️ Important note ⚠️⚠️ : We are here dealing with javascript promises (see here for more details) so in order to be able to test our contract we have to take into account the asynchronous nature of those and wait to receive the response before going further and test the response that we got that's why we use the "async( )" term.

 

Now that we have coded the basic structure of our ERC20 token and the javascript code to test it, it's time to deploy our smart contract locally and run our test file in order to see if everything is fine. To do so, you will need to first download Ganache which will enable you to run sort of a local blockchain in order to test your code (download link) . Once you downloaded it, open it and click on "quickstart" you should normally end up with the following interface

 

Now let's go back to the console. To be able to run your test you''ll need to install node.js and the truffle suite in your virtual environment. So first go other this link and download node.js on your computer. Once it's done got over the console and type  node-v and press enter to make sure that everything is correctly install and running smoothly.

 

 

You may not have the same version as me but don't worry as long as you download the version proposed by node.js on their website you should be fine 😉

 

Alright, now let's install the truffle framework as follows :

npm install -g truffle

 

Once it's done, you  are now up and running to test your contract. So let's do just that. First we need to migrate our contract onto our local blockchain powered by blockchain by typing the following command :

  • truffle migrate --reset

 

 

Normally if everything went fine, you should see something looking like that and the balance  of the first address in Ganache should have diminish by the amount of the amount of the final cost as Ganache automatically considered this address as the one with which it deploys the smart contract.

 

Alright, and now that our contract is deployed we can finally test it by using the following command in the console :

 

truffle test

 

 

And that's it !! We successfully deployed and test our token on a local blockchain environment 🥳🥳🥳

 

So congrats to you , if you follow along and succeed to make this little test file work, it's indeed a nice first step toward more and more interesting things to come 😀

 

By the way, in the next coming up tutorials, we will build on top of what we did today to finish the implementation of our ERC20 token and step by step develop first a full back end solution in order to create a dummy ICO for our token and then move on to see how to create the client side in order to allow daily internet user to buy our token through the use of the MetaMask wallet both on local network with Ganache and public network  with Ropsten so stay put and see you on the next one ✌️