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 ⚠️⚠️⚠️
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 :
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 :
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 :
⚠️⚠️ 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 :
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 ✌️