DEV Community

Cover image for ERC 20 and Solidity: A Starting Point (Part 1)
Mayank Vats
Mayank Vats

Posted on • Updated on

ERC 20 and Solidity: A Starting Point (Part 1)

Things you will learn:

  • Intro to solidity and smart contracts
  • What and Why of ERC20
  • Remix - an online code editor

      If you have been following the world of blockchain you may have come across some terms such as cypto currencies, tokens, digital assets etc. Thanks to Ethereum, since 2015, there have been a rise in number of projects over Ethereum blockchain and since May 2020, DeFi(Decentralized Finance) based projects are skyrocketing.

      But on every blockchain based project you may have come across(or you will come across) you will notice that each project have their own token, a fungible asset for example COMP, BAL, LINK or Steve Woznaik's project Efforce token WOZX and many more.

      Each of these tokens have one thing in common, that is, they are based on ERC20 token standards and are a fungible asset. In this series we will be diving into the know how of a Smart Contract, Solidity, ERC20 token, and also how you can build and deploy your own token, so let's get started.

What is a Smart Contract ?

        It is a source code which is written in Solidity, Vyper, Go and many more languages, compiled to byte code and deployed over Ethereum blockchain and executed by Ethereum Virtual Machine(EVM).

An example (Using Solidity):

pragma solidity ^0.6.0;

contract myContract {
    uint public a = 1;

    function setA(uint _a) public returns (uint) {
        a = _a;
        return a;
    }
}
Enter fullscreen mode Exit fullscreen mode

Compile and deploy smart contract

    To try the code above in Remix, the online solidity's code editor, follow the given steps below:

  1. Go here and create a new file with .sol as extension, as shown Alt Text

  2. Copy and paste the code in the example above in the file you created, and compile the code as shown (see the left side) Alt Text

  3. Now we need to deploy the compiled code to interact with it, you can deploy the code as shown in the picture (see the left side) Alt Text

    Interacting with the contract

        After deploying the contract if you scroll down the left side you will see something like this

Alt Text

As you can see in the image above, Remix has generated two buttons for the corresponding functions in the contract setA() and a().
Note: Since variable 'a' is declared public the getter function for its value is automatically generated with same name as variable.

So, What is ERC20?

    Now you have the idea of what smart contract is, how to write and deploy one and how you can interact with it, you are ready to understand the concept of ERC20 and its tokens.
    Every token on the Ethereum blockchain is just a smart contract with some specific functions and variables and runs on Ethereum Virtual Machine(EVM) and follows a specific rules set by Ethereum Requests for Comments(ERC). These rules state that every smart contract written for the development of an ERC20 token must have the following functions defined by the ERC:-

  • function name() public view returns (string)
  • function symbol() public view returns (string)
  • function decimals() public view returns (uint8)
  • function totalSupply() public view returns (uint256)
  • function balanceOf(address _owner) public view returns (uint256 balance)
  • function transfer(address _to, uint256 _value) public returns (bool success)
  • function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)
  • function approve(address _spender, uint256 _value) public returns (bool success)
  • function allowance(address _owner, address _spender) public view returns (uint256 remaining)

But why does the smart contract must adhere to these rules? Why can't we make our own functions?

    Well, we can make our own functions and there is nothing wrong in doing so. But, if every developer makes their own functions for the token's smart contract then to interact with those contracts you would first need to know about name and signature of each function, which may be an inefficient way, since the idea behind every token's smart contract is same, that is to:

  • keep track of and check total supply.
  • keep track of and check each user's balance.
  • transfer the tokens to other account.
  • allow someone else to transfer them on your behalf.

Thus, ERC20 standard makes our life easier and we don't have to know about the name and signature of particular token's smart contract functions for total supply, balance, transfer and allowing someone else to transfer those tokens.

Make your own token

You can make your own token right now, by following the steps mentioned above for compiling and deploying the contract.
The contract's code can be found here which you can copy paste into Remix.

Thank you for your valuable time. I will be writing the Part 2 of this blog in a short while, where I will explain each and every function of the ERC20 Token smart contract.

Top comments (0)