Skip to content

How to create your own ERC-20 token

openzeppelin erc20

In this easy, step-by-step guide we’ll be creating an ERC-20 token on the Rinkeby test network involves 8 of the following steps! But before that, what’s an ERC-20 token?

ERC-20 is a standard for tokens on the Ethereum blockchain. It defines a set of rules that all ERC-20 tokens must follow, which makes it easier for developers to create and interact with these tokens in a predictable and consistent way.

⭐️ Some of the key features of ERC-20 tokens

  • Transferability: can be transferred from one Ethereum address to another.
  • Fungibility: are interchangeable, meaning that any two tokens of the same type are equivalent and can be exchanged for one another.
  • Divisibility: can be divided into smaller units, up to a certain number of decimal places.
  • Token balance: have a balance associated with each Ethereum address, which represents the number of tokens that the address holds.
  • Token approval: can be “approved” for transfer by one Ethereum address to another, allowing for the use of third-party smart contracts to facilitate token transfers.

Overall, ERC-20 tokens are a popular choice for creating digital assets on the Ethereum blockchain, and have been used for a wide range of applications, including fundraising, loyalty programs, and gaming.

🎫 How to create an ERC-20 token in 8 easy steps

Course: Create your own Ethereum ERC-20 token

1. Set up a development environment

  • Install Node.js and the npm package manager.
  • Install Truffle, a popular development framework for the Ethereum Blockchain. You can do this by running the following command:
npm install -g truffle

2. Create a new Truffle project

  • Create a new directory for your project and navigate to it.
  • Run the following command to create a new Truffle project:
truffle init

This will create a basic Truffle project structure with the following directories:

  • contracts/: This directory will contain your Solidity contract files.
  • migrations/: This directory will contain scripts for deploying your contracts to the Ethereum network.
  • test/: This directory will contain your test files for testing your contracts.

3. Define your ERC-20 token

  • Create a new file in the contracts/ directory and name it MyToken.sol.
  • Paste the following code into the file. This is the basic structure of an ERC-20 token:
pragma solidity ^0.6.0;

import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/SafeERC20.sol";

contract MyToken is SafeERC20 {
  // Define your token's name, symbol, and decimals
  string public name;
  string public symbol;
  uint8 public decimals;

  // Define the total supply of your token
  uint256 public totalSupply;

  // Define a mapping to store the balances of each account
  mapping(address => uint256) public balances;

  // Define the constructor function to initialize the variables
  constructor() public {
    name = "My Token";
    symbol = "MTK";
    decimals = 18;
    totalSupply = 1000000000 * (10 ** uint256(decimals));
    balances[msg.sender] = totalSupply;
  }

  // Define a function to allow users to transfer tokens to other accounts
  function transfer(address _to, uint256 _value) public {
    require(balances[msg.sender] >= _value && _value > 0, "Insufficient balance");
    balances[msg.sender] -= _value;
    balances[_to] += _value;
  }
}

This code defines a contract named MyToken that inherits from the SafeERC20 contract from the OpenZeppelin library. It has the following features:

  • A name, symbol, and number of decimals for the token.
  • A total supply of tokens that is defined in the constructor function.
  • A mapping to store the balances of each account.
  • A function to allow users to transfer tokens to other accounts.

4. Compile the contract

  • Open a terminal and navigate to your project directory.
  • Run the following command to compile your contract:
truffle compile

5. Set up your Rinkeby test network

  • In the terminal, navigate to the root of your Truffle project directory.
  • Run the command truffle network --clean to reset any previous network configurations.
  • Run the command truffle network --network rinkeby to set up the Rinkeby test network.

6. Write the smart contract code

  • Create a file called MyToken.sol in the contracts directory of your Truffle project.
  • Copy and paste the following code into the file:
pragma solidity ^0.6.0;

import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/SafeERC20.sol";

contract MyToken is SafeERC20 {
    constructor() public {
        _mint(msg.sender, 1000000);
    }
}

This is the code for a simple ERC-20 token with a constructor that mints 1,000,000 tokens and assigns them to the contract owner.

7. Compile and migrate the smart contract

  • In the terminal, navigate to the root of your Truffle project directory.
  • Run the command truffle compile to compile the smart contract.
  • Run the command truffle migrate --reset --network rinkeby to deploy the contract to the Rinkeby test network.

8. Test the contract

  • In the terminal, navigate to the root of your Truffle project directory.
  • Run the command truffle console --network rinkeby to open the Truffle console.
  • In the console, run the following commands to test the contract:
truffle(rinkeby)> MyToken.deployed().then(inst => { token = inst })
truffle(rinkeby)> token.balanceOf(web3.eth.accounts[0])

The first command will retrieve the deployed contract instance and assign it to a variable called token. The second command will display the balance of the contract owner (i.e., the first account in Ganache). You should see the balance output as 1000000.

That’s it! You have successfully created an ERC-20 token on the Rinkeby test network. 🎉🤠

Note: These instructions are just a basic outline and there are many more steps involved in creating and deploying a production-ready ERC-20 token. It is highly recommended to thoroughly test and debug your contract before deploying it to the main network.