How to create your own ERC20 token?

Jeffrey Hancock
9 min readApr 24, 2020

--

This Ethereum tutorial demonstrates how you can create and deploy your own ERC20 token in under an hour.

Your own Ethereum token creation is a process standardized and detailed by platform developers. The creators intentionally sought to ensure that anyone, even those with no programming knowledge, could launch their coin using a ready-made blockchain network. Ethereum developers, in fact, created a digital constructor, in which the source token standard ERC20 need to change a few lines and using the program upload the result into the network. Let’s take a closer look at how to create a token on Ethereum network.

The basics

Ethereum is an environment created by a huge number of connected nodes (nodes). This union works as a single machine, which is also called Ethereum Virtual Machine (short for EVM). There is also a network-based smart contract system.

The essence of “smart contracts” is that both parties make some kind of operational deal, bypassing intermediaries. Two conditional persons without a third party form the terms of the contract, which are “uploaded” to the system (general ledger) in the form translated into code. The program automatically determines compliance with the contract rules. If the conditions are met, EVM will confirm it and conduct the transaction. If the conditions are violated, the system will automatically fine the “culprit” (if provided for in the rules) and either return the asset or perform other actions written in the code. And it is impossible to rewrite the contract unilaterally as it is stored in a decentralized chain.

Forming your own smart contract system on the basis of the Ethereum blockchain, you can create your own project or a decentralized application (dApps), the settlement unit in which will be your token. Examples of an internal currency are coins from SIA systems, libraries, etc. In the Ethereum network itself, these tokens do not give any rights and privileges, but may have independent value.

Selected project branches create an entire Ethereum ecosystem in which potentially the only unit of account could be the main ETH coin. But this is very inconvenient. Individual projects and applications can live their own “life”, develop at different rates, change the configuration and all these changes are easier and more natural to do with a special crypto equivalent.

However, fully “inventing” the cryptocurrency equivalent from scratch means embedding it in the system at all levels and sublevels, making it “understandable” for various exchanges, wallets, services and dApp. And it is difficult and more often economically inexpedient.

Therefore, in 2015 DevCon 1 introduced the so-called Initial Standards Token, or standard for Ethereum tokens. After accepting its Mist wallet, testing and adding the necessary requirements, the current standard ERC-20 was formed. The standard is not mandatory, but following it makes it much easier for the user to create his own coin, and makes it understandable for applications and third-party services.

Creation process

Smart token contract consists of two groups of elements:

  • Data structures that link network addresses to the number of tokens, for which there is a set of public variables;
  • A set of functions that provide asset transfer between addresses (transfer and _transfer functions), coin transfer from someone else’s address (transferFrom), access of other users to wallet funds (approve), token write-off (burn), etc.

Activity in the network may take the form of free access to a smart contract to read data from its structure, or it may take the form of a paid data record on behalf of the wallet belonging to the user. The fee is deposited to the account of the network members, who provide the operation of calculation and recording in the Ethereum blockchain.

The ERC20 standard defines a set of functions that all ERC20 tokens must perform to ensure integration with other contracts, wallets or markets. This set of functions is quite short and simple.

The ERC20 functions allow an external user, say a crypto wallet application, to know the balance of the user and transfer funds from one user to another with proper authorization.

The smart contract defines two specific events (Confirmation and Transfer):

These events will be triggered or published when the user is granted the rights to receive tokens from the account and after the tokens are actually transferred.

In addition to the functions of the ERC20 standard, many ERC20 tokens also define additional fields, and some of them have become an actual part of the ERC20 standard. Below are a few examples of such fields.

Here are a few things regarding the ERC20 and the Solidity features:

  • Access to an open function (public function) may be outside the contract itself;
  • View basically means constant, i.e. the internal state of the contract will not be changed by the function;
  • Event is a way of Solidity that allows clients, for example, to be notified of specific events within the contract to the external interface of your application.

Most Solidity language constructs should not cause problems with understanding if you already have the necessary Java/JavaScript skills.

Now that we have laid out the basics and explained what is needed to create an ERC20 token, it is time to move on to specific logic. First, we need to define two projection objects. This is the concept of Solidity for an associative row, or in other words a key/value row.

The projection expression (address => uint256) defines an associative row, the keys of which belong to the address type — a number used to denote the address account and whose values belong to the uint256–256-bit integer, usually used to store the token remainders.

The first object in the projection will contain information about the token balance of each account owner. The second projection object, allowed, will include all accounts that are allowed to exit this account together with the withdrawal of the amount for each account.

As you can see, the value projection object allowed itself is a projection of the account address bound to the withdrawal amount approved for it.

These projections, along with all other fields of the contract, will be stored on the block and will be minimized, resulting in changes that will apply to all network users (nodes). Storage is expensive, and users of your contract will have to pay one way or another. So you should always try to minimize the size of the storage and write it to a blockchain.

Now that we have the necessary data structures on hand, we can start writing ERC20 logic to the appropriate functions.

Token supply

How to determine the number of tokens for ICO? Well, there are several ways to set the maximum number of tokens, and this question itself deserves a long discussion.

In the context of our manual on ECR20 we will use the simplest approach: to set the total number of tokens at the time of contract creation and initially assign them to the “contract owner”, i.e. the account that has deployed the smart contract:

Constructor is a special function automatically called by the Ethereum immediately after the contract is deployed. It is typically used to initialize the state of a token using parameters passed to the account deploying the contract.

msg is a global variable declared and filled by the Ethereum itself. It contains important data for contract execution. The field that we use here: msg.sender contains data about the Ethereum account that is performing the function of the current contract.

Only the account that deploys the contract can be entered by the contract constructor. When starting the contract, this function allocates available tokens to the account “contract owner”.

Get the token supply:

This function returns the number of all tokens allocated under this contract, regardless of the owner.

Get the balance of tokens from the owner’s account:

balanceOf will return the current balance of tokens from an account identified at the address of its owner.

Transfer tokens to another wallet:

As the name implies, the transfer function is used to transfer the amount of tokens from one owner’s account to another owner’s or recipient’s account. The account holder who makes the transfer is referred to as msg.sender, i.e. it is the person who performs the function, which implies that only the token holder can transfer funds to other members.

The way to approve a predicate in Solidity is require. In this case it means that the transferring account has a sufficient balance to make the transfer. If the request cannot be fulfilled, the transaction is automatically closed without any changes to a blockchain.

Right before exit, the function initiates the ERC20 event “Transfer”, thus allowing registered listeners to react to the completion of the event.

Approve delegate for token listings:

This function is most commonly used in token market scenarios. The purpose of approve is to allow the owner, i.e. msg.sender, to approve the delegate account — possibly the market itself — to withdraw tokens from their account and transfer them to other accounts.

As you can see, this function is used for those scenarios where account holders offer tokens in the market. This allows the market to finalize the transaction without waiting for prior approval.

At the end of a transaction, this function initiates an Approval event.

Other functions

Get the number of tokens confirmed for withdrawal from the account:

This function returns the number of tokens currently confirmed by the account holder for a specific delegate as set by the approve function.

Transfer tokens by delegate:

The transferFrom function is the node of the approve function that we talked about earlier. It allows the delegate approved for withdrawal to transfer funds from the account holder to a third party account.

Two require commands are required at the time the function is launched to confirm that the transaction is legal, i.e. that the account holder has sufficient tokens to transfer and that the delegate has permission to withdraw (at a minimum) numTokens.

In addition to transferring the amount of numTokens from the account holder to the buyer, this function also deducts the numTokens from the delegate’s allowance. This basically allows the delegate, given the amount of money allowance given, to divide it into several separate amounts for withdrawal, which is characteristic of the market.

Deploying everything

It’s time to deploy our contract. Once deployed, our contract will be sent out to all nodes that participate in the network. All changes in the contract will be distributed to all participating nodes.

The developers of Ethereum usually use deployment tools such as Truffle. But even Truffle is an overshoot for this article with a limited set of needs, so a simple online tool such as Remix will suffice.

To use it, you need to install the MetaMask plug-in on your browser and have an account on Rinkeby (the Ether Test Network) with at least a minimum amount of Rinkeby ETH in it. These are relatively simple steps, so we will not go into details.

If you don’t have ETH, visit our crypto lotto Ethex.bet! It rewards lucky players who are able to guess the last symbols of the next block hash of Ethereum blockchain. The hashes generate on basis of a pure randomness so lottery is completely fair and transparent.

Now that everything is in place, we will go to Remix and paste the above code. Then we will go to the second tab on the right called Run and click on Deploy. You will see a MetaMask pop-up window asking you to confirm the transaction. Of course, we will confirm it.

You’ve just deployed your first ERC20 token, just like a true Ethereum developer. The project is simple and lightweight but fully functional, meeting the ERC20 standard. Github contain many examples of projects to release their coins on the Ethereum platform. You can find the sources of a certain token at its address in the “Source Code” tab on etherscan.io. Developers recommend that the authors publish the source code when launching their project, as transparency is supposed to increase the user’s trust in the token.

--

--

Jeffrey Hancock
Jeffrey Hancock

Written by Jeffrey Hancock

Blockchain enthusiast developer and writer. I love video games, blockchain and the hot symbiosis of these two worlds.

Responses (1)