In this article we will go over how to create your very own token using ERC-20 standard on the ethereum blockchain. You will only need a metamask account.

So, how do we go over it ?

The very first thing will be to create a metamask wallet in your broswer. As mentioned on their website (https://metamask.io/) : MetaMask is a bridge that allows you to visit the distributed web of tomorrow in your browser today. It allows you to run Ethereum dApps right in your browser without running a full Ethereum node. It is a browser extension that lets you interact with the Ethereum network as a Ethereum Node.

Here is how it looks like :

You can chose in top section of the wallet right next to the little fox which network you want to use. Currently my wallet is connect to the mainnet (the “productive” ethereum network), to create our token and to avoid paying real ethereum as fees we will use the Ropsten testnet. Simply select the testnet and done :

Alright now we will feed our test account with some test Ether that we will use to fund our deployment and our transaction fees. Simply go to the ropsten faucet : https://faucet.ropsten.be/ and then send to your wallet 1 ETH, it should arrive in less than a minute

Once you have your test ETH in your test wallet your are all done and can actually start to create your own token.

Each token which is released on the Ethereum blockchain is created from a smart contract, a smart contract is a glorified script containing code, it is neither a contract nor smart but it permits to write and deploy a set of instructions and functions to the ethereum blockchain. Once deployed the content of a smart contract cannot be changed, which has it benefits but also its drawbacks obviously. Before deploying a “real” smart contract, it should be well tested and audited. Most of the hacks in DeFi are usually caused by bugs in the smart contract that can be leveraged by malitious actors.

The programming language which is usually used to code smart contract is solidity. To actually code, compile and deploy directly on the blockchain our smart contract we will leverage remix : https://remix.ethereum.org/

Remix is a tool that helps you write Solidity contracts straight from your own browser. It also supports testing, debugging and deploying of smart contracts and as such is a great end to end tool to manage the creation of your smart contract and your very own Token.

This is what Remix looks like, head over to the first section (first icon on the left menu) and simply create two new files that you will call :

  • IERC20.sol
  • ERC20.sol

Ethereum tokens can be created leveraging several pre-created standards or frameworks. To name the most common ones (source : eattheblocks token cheatsheet)

ERC-20

  • Suitable for: Fungible assets (asset A can be exchanged with asset B)
  • Use cases: company shares, event ticket, virtual currency, productized service
  • Implementation: https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC20
  • Specification: https://eips.ethereum.org/EIPS/eip-20

ERC721

  • Suitable for: non-fungible assets (asset A cannot be exchanged with asset B)
  • Use cases: cryptocollectibles, art items, real estate
  • Implementation: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol
  • Specification: https://eips.ethereum.org/EIPS/eip-721

ERC777

  • Suitable for: Same as ERC20, except it prevents locked coins in contracts + provide function “hooks” in receiving contracts
  • Use cases: Same as ERC20
  • Implementation: https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC777
  • Specification: https://eips.ethereum.org/EIPS/eip-777

ERC1400

  • Suitable for: Same as ERC20, except it contains additional functions such as the controller function
  • Use cases: Assets and Securities
  • Implementation: https://github.com/ConsenSys/UniversalToken/blob/master/contracts/ERC1400.sol
  • Specification: check consensys

Anyway, for our token we will use the reference standard which is ERC-20.

To use it we need two things :

The interface, that will go to the IERC20 file. This file will list all the different functions available for our smart contract. you can find it here :

https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC20

ALL the ERC-20 token need to have at least the functions listed in the IERC20.sol script. Here is the code :

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

And then we need to also fill the second file, ERC20.sol which contains the actual content of the functions.

You can find it here : https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol

I use a slightly different “lightweight” version, here is the code :

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.0;

import "IERC20.sol";

contract ERC20 is IERC20 {
    
    string public _name;
    string public _symbol;
    uint256 _totalSupply;
    uint _decimals = 18;
    
    mapping(address => uint) balances;
    mapping(address=> mapping(address=> uint)) allowances;
    
    constructor (string memory name_, string memory symbol_, uint initialSupply) {
        _name = name_;
        _symbol = symbol_;
        _totalSupply = initialSupply;
        balances[msg.sender] = initialSupply;
    }
    
    function name() external view returns (string memory) {
        return _name;
    }
    
    
    
    function totalSupply() external override view returns (uint256) {
            return _totalSupply;
        }

    
    function balanceOf(address _account) external override view returns (uint256){
        return balances[_account];
    }


    function transfer(address recipient, uint256 amount) external override returns (bool){
        require(amount <= balances[msg.sender], "not enough balance");
        balances[msg.sender] -= amount;
        balances[recipient] += amount;
        return true;
        
    }
    
    
    function allowance(address owner, address spender) external override view returns (uint256){
        return allowances[owner][spender];
    }


    function approve(address spender, uint256 amount) external override returns (bool){
        allowances[msg.sender][spender] = amount;
        return true;
    }

    
    function transferFrom(address sender, address recipient, uint256 amount) external override returns (bool){
        require(allowances[sender][msg.sender] > amount, "no right");
        balances[sender] -= amount;
        balances[recipient] += amount;
        return true;
    }


}

Once both files are done we click on the second icon and we reach the solidity compiler, we select the 0.8.0 compiler (or whatever version you use, in my case it is 0.8.0 as specified in the second line of my scripts : pragma solidity ^0.8.0;) , the language is solidity and we need to select IERC20.sol as the interface of our contract :

Click on the blue button if your “Auto compile” is not enabled. You should see a green check appear on the compiler, this is the sign that everything went well.

Now it is time to deploy the smart contract, go to the third icon “Deploy & run transactions”

Select Injected Web3 to deploy it to the blockchain, double check that you are indeed on the Ropsten testnet, check that the account is your metamask account (holding your fake ETH), fill the information in the deploy drop down menu :

  • Name of your token (in my case : BRAXV2)
  • Symbol of your token (in my case : BR2)
  • Initial supply of your token (in my case : 1000000000)

And finally click on transact, this will open up metamask, it will ask you to confirm the deployment of the smart contract and show you an estimation of the gas fee that you will have to pay for the deployment :

Accept it, you are using fake ETH anyway. Your contract will be in processing :

Once finished, it should only take a few seconds, you will see your deployed contract appear in remix in the deploy & run transactions :

Click on it to expend it, copy past the adress of the smart contract (right next to ERC20) and go check it out on the ropsten etherscan, in my case it is :

https://ropsten.etherscan.io/address/0x7ab9a11f1348e65c4d624f9d108a1716b96de6b9

Add the new token to your metamask wallet, click on “add new token” in the lower section of the wallet

Click on the custom token and fill out the adress of the contract and the name of the token you created :

Select your token that will appear and add it to your metamask :

Congratulations, you just created your own token on the Ethereum testnet ! you can now send it to your friends !


Brax

Dude in his 30s starting his digital notepad