Requirements

Internet connection

Code editor

Browser

Difficulty

Intermediate

Tutorial Contents

To get started, you will have to install NPM so we can use the packages from both Zepellin and Truffle without having to manually download and copy, you can get NPM from herehttps://www.npmjs.com/get-npm

To check if you already have node/npm on your computer, you can run this two commands on the terminal to see what version you have, if you have any of them you will get an error, then, check the previous step

node -v
npm -v

Use the terminal/command prompt to navigate to the folder where you want to start the project

If you do not have truffle installed, you can get it by simply running the command below

npm install -g truffle

The above will install truffle globally, which will allow you to create new projects in the future

Now, initiate the truffle and npm development environments on the folder by running the following commands on the terminal

truffle init
npm init

You will be prompted to add a few personalized settings but they are not necessary at this stage, so just press enter until it finishes

Now we will import the zeppelin libraries so that we can use their contracts as the base for our apps

npm install openzeppelin-solidity

You can see that without touching a single line of Solidity we already have the bases of a lot of contracts, which opens up many doors for personalization and allows for better security.

Starting our version of ERC20

Inside the directory contracts, create a file a file ourErc20.sol

Now we will leverage the base contracts from the Zeppelin project to create our own ERC20 token.

Here is the structure of the zeppelin directories
Inside the token contracts, directory is the base of the ERC20 token.
We will use the libraries from ERC20.sol and from ERC20Detailed.sol to achieve, automatically, the same results from the second tutorial

Also, notice on the code above, we pass into the constructor of our own contract the variables (that will be input from another file) that will then be forwarded to ERC20Detailed
The image above is just to show where the ERC20Detailed will receive the variables from the constructor

Compiling on truffle

You can compile the code on your computer by running the command

truffle compile

Launching the local testnet

Now let us get started with launching our local test environment, for this we will use the package ganache
To install it, run the command

npm install -g ganache-cli

Now start the ganache local node with

ganache-cli

This will start a local ethereum blockchain that is for testing purposes only, and ganache will also provide you with accounts filled with fake Ethereum so you can do your testings
Notice that after initiating the ganache-cli, in the end, you will see the port of your local network, usually it is 8545, but if you are already a developer or you are using a future version or whatever other reason, if it is different, take note of it because we will use this port on a configuration file later in this class

Setting up the migration

Now, on the migrations directory
Migration means the same as "deploying" because it is actually migrating the code to the blockchain, you could think of it like that.
There is the file 1_migrations_token.sol that is the standard
create a second file named 2_migrations_token.sol

You can copy the code from the 1 to the 2 and just add the new variables, this are the variables that will be sent to the ourErc20.sol as arguments for the constructor, below is the code from the second migrations file with comments on lines that have been changed

Updating the setting with the local testnet

Now, the last thing is to update the truffle-config.js, that is on the root of the project, with the local network settings
Opening the file you will see that all configurations are commented, so you can just uncomment the ones you want to use, in our case:

Deployment

To deploy the contract you just need to do

truffle migrate

The contract is ready to deploy, it is not optimized. If you want your gas cost to be reduced in the future you can

One more thing

If you want to further reduce your gas expenses when deploying a contract, you can use the optimizer settings on the truffle-config.js. On this Stack Exchange post you can read a little bit more about the optimizer. It is something that is easy to use, but hard to understand.