How to Create A Basic ERC20 Token Using Solidity And Remix

As far as blockchain technology goes, Ethereum, being the second most popular network, has emerged as one of the most popular platforms for creating decentralized applications (also known as Dapps).

Conceived in 2013 and eventually created and released in 2015 by Canadian-Russian programmer, Vitalik Buterin, Ethereum's native cryptocurrency, Ether (ETH), is also one of the most popular and most widely acknowledged cryptocurrencies in the world.

Consequently, the standard on which Ether was created is called the ERC20 standard, and is the most widely adopted token standard in the world. Over the years, developers have created a variety of other tokens using this standard, to represent different assets and use cases.

In greater detail before we delve in, the ERC20 standard is a set of properties that fungible tokens on the Ethereum network must have. In this article, we do a quick walkthrough of the process of creating an ERC20 token using Solidity; the programming language used for Ethereum smart contracts.

And by the time you are done reading, you would have had the necessary skills and knowledge to create and configure your own basic ERC20 token.

What is an ERC20 token?

The definition I gave earlier of what an ERC20 token is is sufficient, but we need a lot more than vague definitions before we proceed.

To understand the technical aspects of creating an ERC20 token, a few concepts have to be understood, including ERCs themselves, fungibility, solidity and everything in between.

ERCs

ERC stands for Ethereum Request for Comment, which is a process used for proposing technical standards and improvements for the Ethereum network. The 20 in ERC20 refers to the unique ID number assigned to this particular standard.

With that being said, ERC20 tokens are fungible, meaning that each token is interchangeable with any other token of the same type. They can be used to represent any asset or utility, such as a cryptocurrency, a share in a company, or a vote in a governance system. Because ERC20 tokens follow a common set of rules, they can be easily integrated into other applications and platforms that support the standard.

That is not to say that ERC20s are the only kinds of token standards in existence.

ERC721s for example are the token standard for NFTs, while ERC1155s are the token standard for NFT tokens, but with royalties.

There are several others including ERC1400s, ERC223s, and ERC777s among others (more on these in a later article).

Creating an ERC20 token using Solidity

To create an ERC20 token, you'd probably need to know a little bit of Solidity. There are a few no-code solutions out there with which you can do this, writing litle to no code. However, the purpose of this tutorial is to get our hands dirty with some solidity and build something out.

Solidity is a high-level programming language used for creating smart contracts on the Ethereum blockchain, and is very similar to JavaScript and C++. Other blockchain

If you aren't familiar with the programming language or would like to brush up on your knowledge, you can read the official solidity documentation here.

If you are familiar with solidity and are ready to proceed, then read on!

Here are the steps for creating an ERC20 token using Solidity and the Remix text editor:

Setting Up the Development Environment

To get started, we need to set up a development environment that allows us to write, compile, and deploy Solidity contracts. There are several options for setting up a Solidity development environment, but for this tutorial, we will be using Remix, a web-based Solidity IDE.

To get started with Remix, simply navigate to remix.ethereum.org in your web browser. Once you're on the Remix IDE, create a new file by clicking on the "+" icon in the file explorer on the left-hand side of the screen.

Writing the ERC20 Token Contract

Now that we have our development environment set up, we can start writing our ERC20 token contract. For this tutorial, we will be creating a basic ERC20 token contract that allows users to transfer tokens to each other.

Here's the Solidity code for our ERC20 token contract:

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

contract MyToken {
    string public name = "My Token";
    string public symbol = "MTK";
    uint256 public totalSupply = 1000000;
    mapping(address => uint256) balances;

    constructor() {
        balances[msg.sender] = totalSupply;
    }

    function balanceOf(address account) public view returns (uint256) {
        return balances[account];
    }

    function transfer(address recipient, uint256 amount) public returns (bool) {
        require(amount <= balances[msg.sender], "Insufficient balance");

        balances[msg.sender] -= amount;
        balances[recipient] += amount;

        return true;
    }
}

Let's walk through each of the sections of this code to understand what it does.

First, we have the SPDX-License-Identifier comment, which specifies the license under which the code is released. In this case, we're using the MIT license.

Next, we have the pragma statement, which specifies the version of Solidity that we're using. In this case, we're using Solidity version 0.8.0.

After that, we have the contract declaration for our ERC20 token. We've named our contract "MyToken". Within the contract, we've defined three variables:

  • "name", which is a string that represents the name of our token.

  • "symbol", which is a string that represents the symbol of our token.

  • "totalSupply", which is a uint256 that represents the total supply of our token.

We've also defined a mapping called "balances", which maps addresses to token balances. This mapping will be used to keep track of how many tokens each address has.

Next, we have the constructor for our contract. The constructor is called when the contract is deployed to the network, and it initializes the balances mapping by setting the balance of the contract deployer (msg.sender) to the total supply of tokens.

After the constructor, we have two functions:

  • "balanceOf", which takes an address as an argument and returns the balance of that address.

  • "transfer", which takes a recipient address and an amount as arguments and transfers the specified amount of tokens from the sender's address to the recipient's address.

In the "transfer" function, we've added a require statement that checks if the sender has sufficient tokens to transfer. If the sender doesn't have enough tokens, the function will revert with an error message.

If the sender has enough tokens, the function will deduct the transferred amount from the sender's balance and add it to the recipient's balance. Finally, the function returns true to indicate that the transfer was successful.

Compiling and Deploying the Contract

Now that we've written our ERC20 token contract, we need to compile it and deploy it to the Ethereum network. To do this in Remix, follow these steps:

  1. Click on the "Compile" button in the left-hand panel to compile the contract. This will generate an ABI (Application Binary Interface) and bytecode for the contract.

  2. Click on the "Deploy & Run Transactions" button in the left-hand panel to deploy the contract to the network. This will open a new panel on the right-hand side of the screen.

  3. In the new panel, select "Injected Web3" as the environment. This will connect Remix to your Ethereum wallet, allowing you to deploy the contract using your wallet address.

  4. Click on the "Deploy" button to deploy the contract to the network. You will be prompted to confirm the transaction in your Ethereum wallet.

  5. Once the transaction is confirmed, you will see the contract address in the panel on the right-hand side of the screen. Copy this address, as we will need it later.

Testing the Contract

Now that we've deployed our ERC20 token contract, we can test it by using the functions we defined in the contract.

To test the "balanceOf" function, we can call it with an address as an argument to check the balance of that address. For example, if we want to check the balance of the contract deployer (msg.sender), we can call the function like this:

MyToken myToken = MyToken(contractAddress);
uint256 balance = myToken.balanceOf(msg.sender);

In this code, we're creating a new instance of our contract using the contract address that we copied earlier. We're then calling the "balanceOf" function with the msg.sender address to get the balance of the contract deployer.

To test the "transfer" function, we can call it with a recipient address and an amount as arguments to transfer tokens from the sender's address to the recipient's address. For example, if we want to transfer 100 tokens to a recipient with the address "0x123456789", we can call the function like this:

MyToken myToken = MyToken(contractAddress);
bool success = myToken.transfer(0x123456789, 100);

In this code, we're creating a new instance of our contract using the contract address that we copied earlier. We're then calling the "transfer" function with the recipient address and the amount of tokens to transfer.

If the transfer is successful, the function will return true. If the transfer fails (for example, if the sender doesn't have enough tokens), the function will revert with an error message.

Conclusion

Creating an ERC20 token using Solidity and the Remix text editor is a great way to get started with building decentralized applications on the Ethereum network. In this article, we walked through the process of creating an ERC20 token step-by-step, from setting up the development environment to deploying the token on the network.

While our ERC20 token contract is relatively simple, it provides a good starting point for building more complex smart contracts on the Ethereum network. By understanding the basics of Solidity and ERC20 tokens, you'll be well on your way to creating powerful decentralized applications that can revolutionize industries and change the world.

I hope to see you again in a later article. Until next time, dear reader.