ERC 20 and Solidity: A Starting Point (Part 1)
Published: 2021-01-12 16:30:04

>Things you will learn:
* Intro to solidity and smart contracts
* What and Why of ERC20
* Remix - an online code editor

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](https://www.efforce.io/) token [WOZX](https://www.coingecko.com/en/coins/efforce) and many more.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 ?
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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;
    }
}
```
### Compile and deploy smart contract
&nbsp;&nbsp;&nbsp;&nbsp;To try the code above in [Remix](http://remix.ethereum.org/), the online solidity's code editor, follow the given steps below:
1. Go [here](http://remix.ethereum.org/) and create a new file with .sol as extension, as shown ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a9fdupece7icmdditefj.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/i/mxp5k3fqx55gwugpyc5y.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/i/sr0aywyuc9pq12ur9mbz.jpg)
### Interacting with the contract
&nbsp;&nbsp;&nbsp;&nbsp;After deploying the contract if you scroll down the left side you will see something like this

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/inm66hcc25avv2aibvrx.jpg) 

As you can see in the image above, Remix has generated two buttons for the corresponding functions in the contract <i>setA()</i> and <i>a()</i>.
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?
&nbsp;&nbsp;&nbsp;&nbsp;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.
&nbsp;&nbsp;&nbsp;&nbsp;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? 
&nbsp;&nbsp;&nbsp;&nbsp;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](https://github.com/MayankVats/smart-contracts/blob/main/myToken.sol) which you can copy paste into [Remix](http://remix.ethereum.org/).

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.