here i come with a new tutoral. how to create an erc20 cryptocurrency. it took me a while to write it.
How to create your own cryptocurrency simply?
The goal of this article is to show how to create an ERC20-like cryptocurrency in as little time as possible.
Let's start with the basics: what is an ERC20 token? In recent years, the ERC20 token specification has become the de facto standard for Ethereum tokens (or Ethereum-derived blockchains). In other words, most of the Ethereum contracts available today are ERC20 compliant.
This guide details how you can create your own ERC20-like token on the Ethereum blockchain, but before we get started, let's take a closer look at the ERC20 standard.
What makes ERC20 tokens so attractive and successful?
There are several factors at play: ERC20 tokens are simple and easy to deploy, as you will see in this tutorial.
The ERC20 standard solves an important problem, as blockchain-based markets and crypto-wallets need a single, standardized set of commands to communicate with the range of tokens they manage. This includes the rules of interaction between the different tokens, as well as the rules of token exchanges.
It was the first popular specification to propose the standardization of tokens on the Ethereum blockchain. It was by no means the first, but thanks to its popularity, it quickly became the industry standard. Just like other Ethereum tokens, ERC20 tokens are implemented as smart contracts and run on the Ethereum virtual machine (EVM) in a decentralized manner (on the blockchain). Solidity: the programming language of "Smart Contract" or Smart Contracts Ethereum smart contracts are written in Solidity. Although there are alternative languages, almost no one uses them for this purpose. Solidity is similar to JavaScript, so if you have some knowledge of JavaScript, or even Java and other C-type languages, you shouldn't have a hard time understanding what a piece of code in Solidity does, even before you master Solidity enough to use it.
This is where the fun begins, as you should be able to start creating a simple ERC20 contract in no time. This is a simple task, simple enough for this article to explain how to write and deploy an ERC20 token in less than an hour. The token we will create in this demonstration will be a simple ERC20 implementation, without too many bells and whistles. However, I have seen many similar simple tokens in the real world, and they tend to work well.
Presentation of the ERC20 token standard What is an ERC20? Simply put, the ERC20 standard defines a set of functions to be implemented by all ERC20 tokens in order to allow integration with other contracts, wallets or cryptocurrency markets. This set of functions is rather short and basic.
function totalSupply() public view returns (uint256);
function balanceOf(address tokenOwner) public view returns (uint);
function allowance(address tokenOwner, address spender) public view returns (uint);
function transfer(address to, uint tokens) public returns (bool);
function approve(address spender, uint tokens) public returns (bool);
function transferFrom(address from, address to, uint tokens) public returns (bool);
The functions of ERC20 tokens allow an external user, for example a crypto-wallet application, to know a user's balance and transfer funds from one user to another with appropriate authorization. The smart contract defines two specifically defined events:
event Approval(address indexed tokenOwner, address indexed spender,
uint tokens);
event Transfer(address indexed from, address indexed to,
uint tokens);
These events will be invoked or issued when an account is granted the right to withdraw tokens from an account, and after the tokens are actually transferred. In addition to the functions of standard ERC20 tokens, many ERC20 tokens also have additional fields and some have become a de facto part of the ERC20 standard, if not in writing, in practice. Here are some examples of these fields.
string public constant name;
string public constant symbol;
uint8 public constant decimals;
Here are some points about ERC20 and Solidity: A public function is accessible outside the contract itself, view means essentially constant, that is, the internal state of the contract will not be modified by the function. An Event is how Solidity allows customers, e.g. your app, to be notified of specific events in the contract. Most Solidity language constructs should be clear if you already possess the essential Java/JavaScript skills.
Write an ERC20 token in Solidity Now that we've outlined the basics and explained what it takes to create an ERC20 token, it's time to start writing logic. First, we need to define two mapping objects. Here is the notion of Solidity for an associative or key / value array:
mapping(address => uint256) balances;
mapping(address => mapping (address => uint256)) allowed;
Write an ERC20 token in Solidity Now that we've outlined the basics and explained what it takes to create an ERC20 token, it's time to start writing logic. First, we need to define two mapping objects. Here is the notion of Solidity for an associative or key / value array:
As you can see, the value field of the authorized mapping is in itself a mapping of the tracing account address with its approved withdrawal sum. These mappings as well as all other contract fields will be stored in the blockchain and will be exploited, which will cause the changes to spread to all user nodes in the network. Blockchain storage is expensive and the users of your contract will have to pay, one way or another. Therefore, you should always try to minimize the storage size and writes in the blockchain. Now that we have the required data structures in place, we can start writing the ERC20 logic in the appropriate functions.
https://www.toptal.com/ethereum/create-erc20-token-tutorialThe goal of this article is to demonstrate how to create an ERC20 token in as little time as possible.
Let’s start with the basics: What is an ERC20 token?
In recent years, the ERC20 token specification has become the defacto standard for Ethereum tokens. In other words, most Ethereum contracts out there today are ERC20-compliant. This article will detail how you can create your own Ethereum token, but before we get started, let’s take a closer look at the ERC20 standard.
ERC20 token illustration
What makes ERC20 tokens so attractive and successful? There are several factors in play:
ERC20 tokens are simple and easy to deploy, as you will see in this tutorial.
The ERC20 standard solves a significant problem, as blockchain-based marketplaces and crypto-wallets need a single, standardized set of commands to communicate with the range of tokens they manage. This includes interaction rules between different tokens, as well as token purchase rules.
It was the first popular specification to offer Ethereum token standardization. It was not by any means the first, but thanks to its popularity, it quickly became the industry standard.
Just like other Ethereum tokens, ERC20 tokens are implemented as smart contracts and executed on the Ethereum Virtual Machine (EVM) in a decentralized manner.
Solidity: the Smart Contract Programming Language
Ethereum smart contracts are written in Solidity. While there are alternative languages, hardly anyone uses them for this purpose. Solidity is similar to JavaScript, so if you have some knowledge of JavaScript, or even Java and other C-like languages, you should have no trouble figuring out that a piece of code in Solidity does, even before you actually master Solidity enough to use it.
This is where the fun starts, as you should be able to start creating a simple ERC20 contract in no time. This is a straightforward task, simple enough that this article will demonstrate how you can write and deploy an ERC20 token in under an hour.
The token we will be creating in this demonstration will be a bare-bones ERC20 implementation, without too many bells and whistles. However, I have seen many similarly simple tokens in the real world, and they tend to do quite well.
Overview of ERC20 Token Standard
What is ERC20?
Put simply, the ERC20 standard defines a set of functions to be implemented by all ERC20 tokens so as to allow integration with other contracts, wallets, or marketplaces. This set of functions is rather short and basic.
function totalSupply() public view returns (uint256);
function balanceOf(address tokenOwner) public view returns (uint);
function allowance(address tokenOwner, address spender)
public view returns (uint);
function transfer(address to, uint tokens) public returns (bool);
function approve(address spender, uint tokens) public returns (bool);
function transferFrom(address from, address to, uint tokens) public returns (bool);
ERC20 functions allow an external user, say a crypto-wallet app, to find out a user’s balance and transfer funds from one user to another with proper authorization.
The smart contract defines two specifically defined events:
event Approval(address indexed tokenOwner, address indexed spender,
uint tokens);
event Transfer(address indexed from, address indexed to,
uint tokens);
These events will be invoked or emitted when a user is granted rights to withdraw tokens from an account, and after the tokens are actually transferred.
In addition to standard ERC20 functions, many ERC20 tokens also feature additional fields and some have become a de-facto part of the ERC20 standard, if not in writing then in practice. Here are a few examples of such fields.
string public constant name;
string public constant symbol;
uint8 public constant decimals;
Here are a few points regarding ERC20 and Solidity nomenclature:
A public function can be accessed outside of the contract itself
view basically means constant, i.e. the contract’s internal state will not be changed by the function
An event is Solidity’s way of allowing clients e.g. your application frontend to be notified on specific occurrences within the contract
Most Solidity language constructs should be clear if you already possess essential Java/JavaScript skills.
Writing an ERC20 Token in Solidity
ERC20 tokens in solidity
Now that we’ve outlined the basics and explained what it takes to create an ERC20 token, it is time to start writing some logic.
First, we need to define two mapping objects. This is the Solidity notion for an associative or key/value array:
mapping(address => uint256) balances;
mapping(address => mapping (address => uint256)) allowed;
The expression mapping(address => uint256) defines an associative array whose keys are of type address— a number used to denote account addresses, and whose values are of type uint256 — a 256-bit integer typically used to store token balances.
The first mapping object, balances, will hold the token balance of each owner account.
The second mapping object, allowed, will include all of the accounts approved to withdraw from a given account together with the withdrawal sum allowed for each.
As you can see, the value field of the allowed mapping is by itself a mapping plotting account address to its approved withdrawal sum.
These mappings together with all other contract fields will be stored in the blockchain and will be mined resulting in changes being propagated to all network user nodes.
Blockchain storage is expensive and users of your contract will need to pay for, one way or another. Therefore you should always try to minimize storage size and writes into the blockchain.
Now that we have the required data structures in place, we can start to actually write the ERC20 logic into the appropriate functions.