How to Create Your Own ERC-20 Cryptocurrency

by Ben Lewis, Co-Founder

In the rapidly evolving world of blockchain technology, creating your own cryptocurrency has become an intriguing endeavor for developers and enthusiasts alike. While this blog post will outline the process and provide basic code for creating an ERC-20 token on EVM-compatible chains like Ethereum, Polygon, and Base, it is important to note that this is not a production-ready tutorial. Essential topics, including security, scalability, and optimization, will not be covered here.

Creating your own coin can be a rewarding learning experience, but actually launching one to the public involves many considerations, including legal. This post is purely technical, and will not touch on any of these aspects.

Top tip

Need help with blockchain projects? Our experienced team is here to assist you.

What is a cryptocurrency?

Before diving into the technical aspects of creating a crypto coin, it's crucial to understand what a cryptocurrency is. In essence, a cryptocurrency is a digital or virtual token that uses cryptography for security. Unlike traditional currencies issued by governments, cryptocurrencies are decentralized and typically operate on blockchain technology.

At the core of a cryptocurrency is a smart contract, which is a self-executing contract with the terms of the agreement directly written into code. Smart contracts are responsible for controlling how tokens are issued, burned, and transferred. They set the rules for the market and ensure that transactions are executed automatically when certain conditions are met.

It's important to note that while you can set the initial price at which you're willing to sell the coin, you have no control over its actual market value. The market value of a coin is determined by supply and demand dynamics, as well as the value and utility it delivers within its ecosystem.

Solidity & OpenZeppelin

Smart contracts can be written in several programming languages, but this tutorial will focus on Solidity, which is the most widely used language for developing smart contracts on Ethereum and EVM-compatible chains. Solidity is a high-level, strictly-typed language with similarities to JavaScript and C++.

To simplify the process of creating a crypto coin, we will rely heavily on OpenZeppelin, a library of pre-vetted code components that can be assembled and reused in smart contracts. OpenZeppelin provides a collection of secure and tested code templates that follow best practices, allowing developers to focus on their project's unique features and functionality.

Setting Up Your Development Environment

Before you can start writing code, you'll need to set up your development environment. The recommended tool for this tutorial is the Remix IDE, a web-based integrated development environment (IDE) specifically designed for Ethereum smart contract development. To ensure your security, always use the official website at https://remix.ethereum.org.

While Remix is open source and freely available, be cautious of other websites hosting copies of Remix. Using unauthorized sites can be risky, as attackers could modify the code to perform malicious actions.

Here is step-by-step guide on getting started with Remix:

  1. Accessing Remix IDE: Open your web browser and navigate to https://remix.ethereum.org. This site hosts the official version of Remix and ensures that you are using a safe and reliable environment for your development.
  2. Familiarizing with the Interface: Once you open Remix, you'll see an interface divided into several sections, including a file explorer, code editor, and console output. Spend some time exploring the features and tools available.
  3. Creating a New File: In the file explorer, create a new Solidity file by clicking on the "New File" button. Give your file a descriptive name, such as MyToken.sol.

Time to look at some code!

Creating your ERC-20 Contract

The ERC-20 standard defines a set of rules that a token contract must implement to be compatible with the Ethereum ecosystem. These rules include functions for transferring tokens, checking balances, and approving token allowances. By adhering to the ERC-20 standard, your token can be easily integrated with wallets, exchanges, and other dApps.

To create an ERC-20 token, you'll need to write a smart contract in Solidity that implements the ERC-20 standard. We'll use the OpenZeppelin library to simplify this process and ensure that our contract is secure and compliant with best practices.

Here is a basic example of an ERC-20 token contract using OpenZeppelin:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";

contract MyToken is ERC20, ERC20Burnable {
    uint256 public constant INITIAL_SUPPLY = 1_000_000 * 10**18;

    constructor() ERC20("MyToken", "MTK") {
        _mint(msg.sender, INITIAL_SUPPLY);
    }
}

Let's look at what this is doing, line by line.

// SPDX-License-Identifier: MIT

The SPDX license identifier specifies the license for the contract. In this case, we are using the MIT License, which makes it clear that this code is open source and can be reused or modified under the terms of the MIT License.

This line is recommended, as you will need to publish the source code for your contract, so other parties can verify that you're not doing anything malicious. It tells people reading the code whether they're allowed to copy it for use in their own projects.

pragma solidity ^0.8.19;

The pragma directive specifies the Solidity compiler version required for the contract. Using ^0.8.19 ensures that the contract is compatible with Solidity version 0.8.19 and any minor version updates, which include important safety checks, optimizations, and features that help reduce gas fees, such as:

  • Arithmetic Safety: Starting from version 0.8.0, Solidity includes built-in overflow and underflow checks for arithmetic operations, reducing the risk of integer overflows without needing external libraries.
  • Custom Errors: Solidity 0.8.4 introduced custom errors, which are cheaper than strings in require statements, helping reduce gas costs in error handling.
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";

These lines import the OpenZeppelin libraries for the ERC20 and ERC20Burnable contracts. OpenZeppelin provides well-tested, community-reviewed implementations of ERC-20 token contracts, allowing you to leverage battle-tested code for token operations and burn functionality. Importing these libraries simplifies the development process and ensures your contract adheres to best practices.

Imports from where? Good question. Remix will automatically detect imports like this, and look for the source code on npm. You can read the source code of these imports here.

contract MyToken is ERC20, ERC20Burnable {

This line declares a new contract named MyToken that inherits from the ERC20 and ERC20Burnable contracts provided by OpenZeppelin. Inheritance allows MyToken to access and utilize the functions and features of the parent contracts, enabling standard ERC-20 behavior and token burning capabilities.

uint256 public constant INITIAL_SUPPLY = 1_000_000 * 10**18;

Here, a constant named INITIAL_SUPPLY is defined to represent the initial token supply. By multiplying by 10**18 (10 followed by 18 zeros), the supply is specified in the smallest unit of the token.

The underscores in 1_000_000 are purely cosmetic, to help with readability. They will be removed by the compiler.

constructor() ERC20("MyToken", "MTK") {

The constructor function is executed once when the contract is deployed. It initializes the token with a name ("MyToken") and a symbol ("MTK") by calling the parent ERC20 constructor. This name and symbol can be queried by 3rd-parties, providing metadata to marketplaces and other applications.

_mint(msg.sender, INITIAL_SUPPLY);

This line mints the initial supply of tokens, INITIAL_SUPPLY, and assigns them to the address that deployed the contract (msg.sender). The _mint function is a part of the OpenZeppelin ERC20 contract, which securely increases the total supply and allocates the tokens to the specified address.

Baby Steps

This is an example of the bare minimum ERC-20 contract. It will create a token, mint some initial supply, and transfer it to the deployer.

In the real world, you'd want to add features specific to your use-case, but this is all we need to learn.

Deploying Your Contract

After writing and testing your ERC-20 token smart contract, the next step is deploying it to a blockchain network. Deployment is the process of broadcasting your contract to the network, where it will reside at a specific address and be accessible for interaction. For this tutorial, we'll focus on deploying the contract using the Remix IDE and MetaMask wallet.

Prerequisites for Deployment

Before deploying your contract, ensure you have the following prerequisites in place:

  1. MetaMask Wallet: MetaMask is a browser extension that acts as a cryptocurrency wallet and interface to interact with Ethereum and other EVM-compatible blockchains. You can install it from the official MetaMask website.
  2. Test Ether: To deploy your contract on a test network, you need test Ether to cover transaction fees (also known as gas fees). You can obtain test Ether from a faucet corresponding to the network you choose, such as Ropsten or Goerli.
  3. Remix IDE: If you haven't already, set up your development environment as outlined earlier

Deploying

  1. Load your Contract: In Remix, ensure your ERC-20 token contract (MyToken.sol) is loaded. If you haven't already, create a new file, copy your contract code, and paste it into the editor.

  2. Compile your Contract:

    • Click on the "Solidity Compiler" tab on the left panel of Remix.
    • Select the appropriate compiler version (e.g., 0.8.19) that matches your pragma directive.
    • Click the "Compile MyToken.sol" button. Ensure there are no errors or warnings during compilation.
  3. Connect Remix to MetaMask:

    • Open your MetaMask extension and ensure you are logged in.
    • Switch to the desired test network, such as Sepolia. You can change networks by clicking the network dropdown at the top of the MetaMask window.
    • In Remix, go to the "Deploy & Run Transactions" tab.
    • Select "Injected Web3" as the environment. This option allows Remix to use MetaMask as the transaction signer. Remix will prompt you to connect your MetaMask wallet.
  4. Deploy your Contract:

    • Ensure that the "Contract" dropdown in Remix is set to MyToken.
    • Click the "Deploy" button. MetaMask will open a window asking you to confirm the transaction.
    • Review the transaction details in MetaMask and click "Confirm" to proceed.
  5. Wait for Confirmation: Once confirmed, the deployment transaction will be sent to the network. You can view the transaction status in MetaMask. After the transaction is mined and confirmed, your contract will be deployed on the test network.

  6. Interact with Your Deployed Contract:

    • After deployment, the contract's address will appear under the "Deployed Contracts" section in Remix.
    • You can interact with your deployed contract directly from Remix by expanding the contract interface, where you can call functions such as balanceOf, transfer, or burn.

Final Thoughts

Creating your own ERC-20 token provides valuable insights into blockchain technology and the inner workings of smart contracts. While this tutorial offers a technical guide for setting up a simple token, launching a public cryptocurrency requires careful consideration of legal, security, and business implications.

If you're looking to develop a more complex blockchain project or need assistance with deployment, our agency provides comprehensive blockchain development services with a personalized touch. We have a team of experienced professionals who are well-versed in the latest blockchain technologies and trends. If you need help hiring a team for your blockchain project, contact us today.

More articles

How to Hire Blockchain Developers (in 2024)

Learn how to hire blockchain developers in 2024. Explore effective strategies and tips for finding skilled developers for your blockchain projects.

Read more

We can't wait to bring your vision to life

Our offices

  • Jersey
    3rd Floor, Forum 4
    Grenville Street