The only course you need to make your own cryptocurrency

Photo by Kanchanara on Unsplash

The only course you need to make your own cryptocurrency

What are we learning šŸ¤” ?

Hello Builders šŸ˜Ž

Here in this brand new step by step self-paced course, we will be launching our own token with a name and symbol of our choice and then we will deploy the token on a test net and list it on uniswap so that users can trade it with other tokens and then we will build an awesome frontend so that people can purchase our token.

Fun fact : A meme token named Shiba Inu built on ERC 20 token standard has a current market cap of 12 billion USD

So who knows your own token can also achieve this status and can become one of the largest cryptocurrencies in the world & we will be doing everything to achieve that.

The step by step guide is as follows :

  1. Setting up our development environment

  2. Writing code for our token

  3. Deploying the token on testnet

  4. Getting your token on metamask

  5. Listing the token on uniswap

  6. Launching the token

So, are you ready to hop on this crazy ride , let's go !


Understanding ERC 20 standard šŸ¤“

Before moving further we need to understand about the backbone, powering the cryptocurrencies on ethereum which is erc 20 standard.

  • ERC 20*

ERC stands for ethereum request for comment and ERC 20 is the standard for making tokens on ethereum virtual machine. It is a set of functions that every token needs to follow in order to be called an erc 20 token. ERC 20 standard is used for having uniformity in different tokens to get a seamless experience within the ethereum ecosystem like they can be exchanged with one another .

Wallets like metamask are designed to expect these functions in a smart contract while accepting a new cryptocurrency.

ERC 20 was mentioned in Ethereum Improvement Proposal -20 or EIP-20. You can read more about that here : https://github.com/ethereum/EIPs/blob/f98a60e1b1590c9cfa0bfe66c4e6a674e54cd386/EIPS/eip-20.md

ERC-20 standard describes six functions that maintain some functionalities and features of Ethereum-based digital tokens. These functions include the way tokens are transferred between addresses and some important data related to the token smart contract, such as symbol, name, and supply.

The main functions for making your token šŸ‘€

Letā€™s see and understand about the main functions of ERC-20 for making our token

  1. totalSupply - This function will return the total number of tokens which can ever exist .In this function we have to specify the total supply of tokens.
  2. balanceof - This function returns the amount of tokens owned by the account.
  3. transfer - This function transfers _value amount of tokens to address _to
  4. transferFrom - This function transfers _value amount of tokens from address _from to address _to
  5. allowance - This function returns the remaining number of tokens that spender will be allowed to spend on behalf of owner through [transferFrom](https://docs.openzeppelin.com/contracts/2.x/api/token/erc20#IERC20-transferFrom-address-address-uint256-). This is zero by default.
  6. approve - This function allows _spender to withdraw from your account multiple times, up to the _value amount. If this function is called again it overwrites the current allowance with _value.

Setting up basic project šŸ§

Now, Letā€™s start coding our tokenā€™s smart contract.

For coding we will be using remix . Remix is a browser based IDE with built in solidity compiler so it will be easy and convenient to use.

Head over to remix.ethereum.org and in the contracts directory make a new file named token.sol

.sol is the extension for solidity files.

pic_1.png

Make a new contract and give it a name you want .

Now we made a contract NewToken and inside the token letā€™s declare the name and symbol of our token .

Here we are naming our token Zcoin and the symbol ZCN .

Give your token a different name, Play with it around and give your token a unique name it can be pizzacoin or frytoken . It can be anything you want ,no restrictions .

You can see the list of all the available crypto tokens in the markets here : https://coinmarketcap.com/

pic_2.png

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

contract NewToken {
    string NAME = "ZCoin";
    string SYMBOL = "ZCN";
}

Letā€™s understand the code line by line

  1. In line 1 we have declared license identifier which is mandatory for all solidity files.
  2. In line 2 we have written the pragma command which tells the compiler the version of solidity we are using .
  3. In line 3 we have declared our contract, contracts in solidity are same like classes in C++ or java.
  4. Under the contract we have declared two strings which are our tokenā€™s name and symbol.

. Naming the coin šŸ‘“

Letā€™s make functions to retrieve our tokenā€™s name and symbol from our contract .

The strings that we declared inside our contract are private ,so as to access them we have to make functions for them.

pic_3.png


function name() public view returns (string memory){
        return NAME;
    }

    function symbol() public view returns (string memory) {
        return SYMBOL;
    }

The name and symbol functions are public.

Public functions in solidity can be called from anywhere.

These functions returns string memory.

Memory keyword in Solidity tells the compiler that after returning the variable, it can erase it from the memory stack.

Deciding the total supply of coin āš–ļø

Now comes the most important part of our token smart contract ,which is to decide on the total supply of our own token.

As we know in crypto currencies are of two types : inflationary and fixed.

We can take the example of bitcoin for fixed and eth for inflationary currency.

In Inflationary currency the number of coins in circulation keeps increasing with time which means that the function totalSupply() returns a higher number each time we call it.

On the other hand fixed currencies define how many coins will ever be in circulation. This creates scarcity, and drives up the prices on the exchanges when demand for the coins increases.

Bitcoin is not an ERC-20 token but COMP (Compound) is a fixed supply erc 20 token which have a fixed supply for 10 M coins

In this tutorial we are making a fixed supply token so letā€™s write the functions for that

pic_4.png

function totalSupply() public view returns (uint256) {
        return 10000000 * 1e8; //10M * 10^8 because decimals is 8
    }
function decimals() public view returns (uint8) {
        return 8;
    }

In the function we have declared our tokenā€™s supply as 10 million coins .

You are also seeing an additional function decimals but what is this for , Letā€™s understand

This function tells that how divisible our coin can get .

For example an eth can be divided into wei and gwei .

Similarly, how divisible our token can get , we will decide it with the help of decimal function .

Here our function returns 8 it means that our token is divisible to the value of 10 raise to the power of 8 .

1 token can be divided upto 0.00000001 tokenā€™s value.

Events šŸ’¼

Events in solidity are a way to communicate with a client application or front-end website that something has happened on the blockchain.

pic_5.png

  • event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
    

In our smart contract we have have two events which are transfer and approval

The transfer event gets triggered whenever there is a transfer to be made of our token from one address to another.

The approval event is for the owner of the contract to approve the spending of the spender of our token.

These two events are also in the EIP-20 and are mandatory in an ERC-20 token.

Getting the money matters right šŸ’°

In this section , we will understand the functions to get balance, transferring the coins

Now we will make the function to get the total balance of our coins in an address

For this we first have to declare a mapping for balances.

Mapping in solidity are used to store the data in the form of key-value pairs. Mappings are mostly used to associate the unique Ethereum address with the associated value type.

pic_6.png

mapping(address => uint) balances;
function balanceOf(address _owner) public view returns (uint256 balance) {
        return balances[_owner];    
    }

The function here returns the balance of the contract .

pic_7.png

function transfer(address _to, uint256 _value) public returns (bool success) {
        assert(balances[msg.sender] > _value);
        balances[msg.sender] = balances[msg.sender] - _value;
        balances[_to] = balances[_to] + _value;

        emit Transfer(msg.sender, _to, _value);

        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        if(balances[_from] < _value)
            return false;

        if(allowances[_from][msg.sender] < _value)
            return false;

        balances[_from] -= _value;
        balances[_to] += _value;
        allowances[_from][msg.sender] -= _value;

        emit Transfer(_from, _to, _value);

        return true;
    }

The transfer function is used to transfer a value of tokens from our smart contract to another address .

This function checks if the contract has the amount more than it is sending to the address and also check the allowance means that if the caller of the contract have the permission to spend the amount on the contractā€™s behalf or not.

Then after checking it reduces that much amount of tokens from our contract and emits a transfer event and completes the process.

Similarly the transferfrom function is used to transfer the tokens from one address to another .

This function checks if the sender of tokens has sufficient amount of tokens in his/her account and then it completes the transaction by emitting the transfer event.

pic_8.png

mapping(address => mapping(address => uint)) allowances;


    function approve(address _spender, uint256 _value) public returns (bool success) {
        allowances[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value);
    }

    function allowance(address _owner, address _spender) public view returns (uint256 remaining) {
        return allowances[_owner][_spender];
    }

For the approve and allowance functions we have declared a mapping of mapping to give an address the permission to spend tokens on behalf of tokenā€™s smart contract.

The approve function gives the contractā€™s caller the allowance to spend tokens and then it completes the final process by emitting the approval event .

Similarly,the approve function declares the definition of allowances.

Constructor is the gold mine šŸ‘‘

We have made the token ,we are the creator so we deserve to be rewarded and we can reward ourselves by giving us 1 million tokens out of 10 M tokens.

But how can we do that,

We can do that with the help of constructor.

The constructor will get executed only once in the lifetime of contract at the time of deployment.

pic_9.png

address deployer;

    constructor(){
        deployer = msg.sender;
        balances[deployer] = 1000000 * 1e8;
    }

For constructor we will declare a field deployer of the type address and then in the constructor we will update the balance of the sender of the contract that is the deployer of the contract by 1 M coins .By this method we got 1 M of our tokens .

If you want more of your tokens in your wallet you can update the value in the function.

Deploying the coin šŸ¤–

Now is the time to finally deploy our token.

We are going to deploy our token on ropsten testnet so before deploying you should have some ropsten test eth in your metamask wallet .

You can get your test eth from here : https://faucet.egorfine.com/

Just go to this website ,Enter your address and you will get your test eth easily.

Now letā€™s come to the main part

In the remix workspace click on solidity compiler tab and then press the compile token.sol button .

pic_10.png

After compilation you can get some warnings in yellow ,ignore them for now and then go to the deploy tab.

From the dropdown menu change your environment from javascript vm to injected web3.

A new metamask popup will appear to enter your password. Enter your metamask password and then it will connect your wallet to remix.

Now press the deploy button ,

pic_11.png

A metamask popup will appear to confirm the transaction.

Click confirm and then the transaction will get executed

pic_12.png

you will see a green tick sign it means that your token is deployed in the testnet

click on view on etherscan and you can see the address of your deployed contract

this is also the address of your token copy this address and letā€™s go forward and add our token in our metamask wallet.

pic_13.png

Getting our new coin on metamask šŸ¦Š

Now is the time to get our tokens in our metamask wallet

Follow the steps below to do it ā†’

  1. Open metamask
  2. In the assets section , Click on import tokens

pic_14.png

  1. Paste the contract address that we have copied from etherscan ,metamask will automatically detect the name and symbol of the token.

pic_15.png

  1. Click on add custom token ,now you have your token in your metamask wallet.Go to the assets section and you can see your token and the balance that we rewarded ourselves with šŸ˜œ.

pic_16.png

  1. We did it , celebrate šŸ¤© because you have successfully deployed your first coin on blockchain and you have your crypto coin in your wallet. šŸ˜Ž

Getting our coin on uniswap šŸ¦„

Letā€™s list our coin on uniswap to make it trading ready

To list the token on uniswap, follow the steps below :

  1. Go to app.uniswap.org
  2. Connect your wallet and click on pool tab.

pic_17.png

  1. In the pool tab click on add v2 liquidity

pic_18.png

  1. Set the liquidity , like here we have set 1 eth equal 10000 ZCN it means we have set the initial value of 10000 ZCN equal to 1 eth ,and now we will create a liquidity pool for it .The uniswap will further take care of its price as the demand for the token rises or falls

pic_19.png

  1. Click on create liquidity pool, confirm the metamask transaction ,it will take some time.

pic_20.png

  1. Voila ,we have listed our token successfuly on uniswap and now it is ready to be traded in crypto market. Now when you check your wallet you will see that 10000 tokens are deducted to create the liquidity pool .

pic_21.png

pic_22.png

The easy way

We did it ! šŸ˜¤

We successfully built our token from scratch deployed it and listed on it uniswap.

You can get the full code of this course here :

https://github.com/siddhantsrivastava2002/TokenCourse/blob/main/NewToken.sol

I hate to break it to you,but we took the hard way particularly to learn about the process to make an ERC-20 token and to enhance our learning .

There is an easy way to make your token which is with the help of openzeppelin

We can make our ERC-20 token by inheriting openzepelling ERC-20 interface in our contract and with just a few lines of code and there is no problem in taking this path.

You can learn more about this method here :

https://docs.openzeppelin.com/contracts/4.x/erc20#constructing-an-erc20-token-contract

We hope that you learned and have fun in making your token with us in this course.

We will see you in our next course .

Until then Keep Learning

Signing off šŸ˜„

Ā