DEV Community

Seymour1948
Seymour1948

Posted on

Deploy a ERC20 compliant contract. Part 1.

The other day I read about how programming is not about code anymore and more about putting together to work different technologies. It's kind of similar to my experience under the lockdown, one of the things I decided I could occupy my time was to assembly a pc. I discover the hard way than sometimes it's not that easy to put together the different pieces to obtain something that work even if they should be compatible, the reality sometimes is different.
In this post I will try to put together all the pieces for a new assembly being now software instead of hardware. Being frameworks, languages, networks instead of cpu, motherboards, power units.
I will be using visual studio code, hardhat, polygon network, open zeppelin contracts, git, github and maybe to add a very basic front end some React.
Create your project folder. Open the terminal navigate to your projects folder and type:

mkdir polygon-eth20 && cd polygon-eth20
Enter fullscreen mode Exit fullscreen mode

I'm working with VSC so to open a new VSC window with my project as folder I will do the following

code .
Enter fullscreen mode Exit fullscreen mode

Let's open the terminal inside VSC with ctrl + ` and let's install hardhat with our initial dependencies. You can use yarn o npm to do this.

npm install --save-dev @nomiclabs/hardhat\-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers
Enter fullscreen mode Exit fullscreen mode

Next thing we should do is to start our project with

npx hardhat
Enter fullscreen mode Exit fullscreen mode

In the terminal we will be asked to chose what kind of project we want to create. We chose a basic sample project, the same folder as destiny and we create a .gitignore file. This file is capital if you plan to don't upload later on private info to Github. Having this file also means that hardhat create a git repository for us for free.

Let's create a contract provided file.

touch contract\Glass.sol
Enter fullscreen mode Exit fullscreen mode

Open it in VSC using its interface or using this from terminal

code contract\Glass.sol -r
Enter fullscreen mode Exit fullscreen mode

-r makes the file open in the same VSC instance.
Now let's create our contract.

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

contract Glass {
//
}
Enter fullscreen mode Exit fullscreen mode

So now we wont our contract to be ERC-20 complian so to do that we make our contract inherit from open zeppeling contract
to do that we need to intall this contracts, from terminal

npm install @openzeppelin/contracts
Enter fullscreen mode Exit fullscreen mode

after that we can import it in our solidity contract file and make our contract inherit from ERC20 contract.

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";


contract Glass is ERC20 {
    constructor(uint256 initialSupply) ERC20 ("Glass", "GLSS"){
        _mint(msg.sender, initialSupply);
    } 
}
Enter fullscreen mode Exit fullscreen mode

If you take a look to the following to ERC20 you can check the functions we are inheriting from it. I encourage you greatly to do so. In your career as developer one of the things you will do the most is reading docs and when they are not available reading code written by others so better get used to it. I don't wanna digress but write your code as if someone have to read it sooner or later even that one is the yourself of the future. Anyway, if you look we have set a constructor inside of our contract. This is a special function that is only executed once in the moment the contract is deployed, there we are setting two parameters like the name and the symbol, they are part of the standard. We can also set another parameter like decimals, if we don't the default value will be 18. Decimals are a whole subject by itself, so for now just now that in solidity we don't have anything like 1.5 and are this decimals what allows to "fraction" the units of our token.
Another important thing that happen in our constructor is we call a private function call _mint, note the underscore before the the name, just a convection but really helpful to establish at a first glance what methods will be accessible from outside and what not.
This function do something really important creates and initial amount of the tokens we are creating and in this case assign it to the contract creator (If you want to dig deeper in this subject about supply, I encourage you to do so take a look to this doc).
Msg is a global variable in solidity, we have a few of them, and msg.sender return the address of the external account (in this case) who call the contract.
All calls to contracts have to be initiated from a external account, even if in the middle we have a contract calling functions of another contract but these things are far away from the scope of this post so let's continue with our process. So our in this first transaction _mint function transfer the given amount, which we specify in the contract constructor parameters to the contract creator. And that's it we have a bare-bone ERC20 compliant contract and from here we could add more functionalities from here but what I pretend if walk for the whole process of a contract deployment in a net so let's continue.
At this point we could compile our contract and find out if we get any errors from the process so let's do it. From the terminal execute this.

npx hardhat compile
Enter fullscreen mode Exit fullscreen mode

We should get a message that the process went just fine. So we were able to convert our sol file to code than the EVM (Ethereum Virtual Machine) can work with.

We end here our first part of this article in next article we will see how to deploy our contract. We'll do it first to our local network and also we will be introduce to testing in hardhat and will try to do it from a TDD (Test driven development) approach.
To be honest I'm the main target of this article, I know things but I'm new to this Web3 thing so I could be incurring in some errors and misconceptions. I hope not I'm the first interested in not mislead anyone including myself. Feel free to point out any error you could find in this or future articles and if somehow you find this useful to you great, I'm glad for it.

Top comments (0)