Create your own token on the Ethereum platform
- Tram Ho
After writing articles about decentralized applications and preparing to guide you to do practical examples. I want to write some important topics in detail so that people can understand them better. And today will be a way to create a token on the Ethereum platform. Why is a token and not a coin? Everyone with me find out
1. Token and Coin
When it comes to something to circulate in the Blockchain system, people often think of Bitcoin or Altcoin immediately (Alternative Cryptocurrency Coins). So what is token and what does it do on Blockchain?
Altcoin is referred to as variable electronic currencies from Bitcoin. There are forked branches from Bitcoin such as Namecoin, Peercoin, Litecoin, Dogecoin, … These coins were developed from the original Bitcoin protocols to meet the needs that developers want different from Bitcoin. . Many people believe it is a reform of Bitcoin. But people see that, never on Coin Market Cap has any other name in the number one position besides Bitcoin. Some other Altcoin ways are rewritten to separate protocols and used in separate Blockchain systems such as Ethereum, Ripple, Omni, Bitshares, NEO, …
=> So conclusions, Coin and Altcoin have their own independent Blockchain system and are used in their original Blockchain system protocols.
A token is a representation of a specific asset or utility, usually located on an existing blockchain. The token code seems more versatile when it can represent the value of goods, expressing the uniqueness of an Object, the points of members, can be used as electronic currency for circulation.
Creating tokens is a much easier process because you don't have to modify the code from a specific protocol or create a blockchain from scratch. All you have to do is follow a standard pattern on blockchain – such as the Ethereum platform or Waves – allowing you to create your own token. The function of creating your own token is done through the use of smart contracts; Computer code can be programmed to execute itself and does not need any third party to function.
=> So creating a decentralized application with tokens using the Blockchain system is sure to be more easily accessible than creating a separate Blokchain system and coin for it.
2. Standards to create a token on Ethereum
As mentioned above, you can create your own token but follow the standards available. Why does it need to be standard? Let's take a look at some concepts:
Ethereum Request for Comment (ERC) is a form of research release and requires comment in the Ethereum source code community. ERC is a form of EIP, or Ethereum Improvement Proposal – Proposal to improve Ethereum. Just like Bitcoin Improvement Proposals (BIPs), only a handful of proposals are actually implemented. The following figures can be interpreted as version registration numbers to distinguish them from other ERC standards. Common ERC standards such as ERC20, ERC777, ERC223, ERC721, ERC827, ERC948, ERC884.
In particular, ERC20 is the most commonly used token Ethereum standard. It can be said that almost any ICO project uses this standard ERC20 token. A token using ERC20 standard will be available
The following 6 functions:
1 2 3 4 5 6 | totalSupply () public view returns (uint256 totalSupply) balanceOf (address _owner) public view returns (uint256 balance) transfer (address _to, uint256 _value) public returns (bool success) transferFrom (address _from, address _to, uint256 _value) public returns (bool success) approve (address _spender, uint256 _value) public returns (bool success) allowance (address _owner, address _spender) public view returns (uint256 remaining) |
and 2 events:
1 2 | Transfer (address indexed _from, address indexed _to, uint256 _value). Approval (address indexed _owner, address indexed _spender, uint256 _value) |
Before using this standard, if you send a token to the wrong address, the token will be permanently lost. These standards can be considered as patches or improvements to the token. And more importantly, using an ERC standard helps the system understand and interact better with your token.
An e-wallet will know the interactions with your token if you know you're using an ERC20 standard. If you don't use any standard, your token is a new thing without any electronic wallet, so it's hard for developers to do something to store it for you or to exchange between Your token with other tokens.
Maybe you are interested
Learn Blockchain technology located under Bitcoin Protocol [Part 1]
The roadmap to become a programmer after 9 months of self-study (Part 1)
3. Practice only
Using the Token standard has your code ante jailed, no longer being created? No, as is known, solidity is an object-oriented language. So you only need to implement this ERC20 class token to develop your token. Keep using Remix IDE.
The code of ERC20 you can get here
I will use solidity version 0.4.25 familiar to me
There are some problems when you compile with solidity versions, some versions will require you to use pure
or view
instead of constant
so I fix a bit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | pragma solidity ^ 0.4.25; // ------------------------------------------------ ---------------------------- // ERC Token Standard # 20 Interface // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md // ------------------------------------------------ ---------------------------- interface ERC20 { function balanceOf (address who) public view returns (uint256); function transfer (address to, uint256 value) public returns (bool); function allowance (address owner, address spender) public view returns (uint256); function transferFrom (address from, address to, uint256 value) public returns (bool); function approve (address spender, uint256 value) public returns (bool); event Transfer (address indexed from, address indexed to, uint256 value); event Approval (address indexed owner, address indexed spender, uint256 value); } |
You create the file ERC20.sol
and then add the other code. Function description of the function can be viewed here
Next is the file SafeMath.sol
. As its name implies, this file helps to check the condition of parameters before doing calculations with them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | pragma solidity ^ 0.4.25; library SafeMath { function mul (uint256 a, uint256 b) internal pure returns (uint256) { if (a == 0) { return 0; } uint256 c = a * b; assert (c / a == b); return c; } function div (uint256 a, uint256 b) internal pure returns (uint256) { // assert (b> 0); // Solidity automatically throws when dividing by 0 uint256 c = a / b; // assert (a == b * c + a% b); // There is no case in which this doesn't hold return c; } function sub (uint256 a, uint256 b) internal pure returns (uint256) { assert (b <= a); return a - b; } function add (uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; assert (c> = a); return c; } } |
And finally, import the above files and implement the contracts into the Token.sol
file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | pragma solidity ^ 0.4.25; import 'browser / SafeMath.sol'; import 'browser / ERC20.sol'; contract Token is ERC20 { Using SafeMath for uint; internal string _name; _symbol internal string; uint8 internal _decimals; uint256 internal _totalSupply; mapping (address => uint256) internal balances; mapping (address => mapping (address => uint256)) internal allowed; constructor (string name, string symbol, uint8 decimals, uint256 totalSupply) public { _symbol = symbol; _name = name; _decimals = decimals; _totalSupply = totalSupply; balances [msg.sender] = totalSupply; } function name () public view returns (string) { return _name; } function symbol () public view returns (string) { return _symbol; } function decimals () public view returns (uint8) { return _decimals; } function totalSupply () public view returns (uint256) { return _totalSupply; } function transfer (address _to, uint256 _value) public returns (bool) { require (_to! = address (0)); require (_value <= balances [msg.sender]); balances [msg.sender] = SafeMath.sub (balances [msg.sender], _value); balances [_to] = SafeMath.add (balances [_to], _value); Transfer (msg.sender, _to, _value); return true; } function balanceOf (address _owner) public view returns (uint256 balance) { return balances [_owner]; } function transferFrom (address _from, address _to, uint256 _value) public returns (bool) { require (_to! = address (0)); require (_value <= balances [_from]); require (_value <= allowed [_from] [msg.sender]); balances [_from] = SafeMath.sub (balances [_from], _value); balances [_to] = SafeMath.add (balances [_to], _value); allowed [_from] [msg.sender] = SafeMath.sub (allowed [_from] [msg.sender], _value); Transfer (_from, _to, _value); return true; } function approve (address _spender, uint256 _value) public returns (bool) { allowed [msg.sender] [_ spender] = _value; Approval (msg.sender, _spender, _value); return true; } function allowance (address _owner, address _spender) public view returns (uint256) { return allowed [_owner] [_ spender]; } function increaseApproval (address _spender, uint _addedValue) public returns (bool) { allowed [msg.sender] [_ spender] = SafeMath.add (allowed [msg.sender] [_ spender], _addedValue); Approval (msg.sender, _spender, allowed [msg.sender] [_ spender]); return true; } function decreaseApproval (address _spender, uint _subtractedValue) public returns (bool) { uint oldValue = allowed [msg.sender] [_ spender]; if (_subtractedValue> oldValue) { allowed [msg.sender] [_ spender] = 0; } else { allowed [msg.sender] [_ spender] = SafeMath.sub (oldValue, _subtractedValue); } Approval (msg.sender, _spender, allowed [msg.sender] [_ spender]); return true; } } |
After everything is ok (that is, there will be no errors in the code line) then proceed to compile.
There are some warning on the other side. But okay, there is no error when compiling. Go to the Run tab and try deploying the code.
Since the last Smart Contract I use is Token, I will choose it to deploy. You should note that the constructor part I am making my token initialization parameters can be entered. And those parameters are:
- Name of the token
- Code of token
- The number of decimal places after the comma of the token
- Total number of tokens to issue
For example, we will create the token as follows:
- Name:
- Code: VBL
- Number of decimal digits after comma: 0
- Total issued tokens: 1000
Remember to log into the metamask to use the remix as your guide in the previous post. You can review it here
Enter everything like your image and click on transact, confirm the transaction that the metamask notified and wait a bit.
Delicious! Successfully deploying and I tested the functions ok.
You can view your token at https://ropsten.etherscan.io/ has it matched the five world powers. Type a name or a Token symbol to search. It will take some time to find it because the tokens deployed on the testnet network are quite numerous and continuous.
Or you can go to the Token section on Metamask to add the token. Click on the latest transaction in Metamask's transaction history and you will see a Smart Contract address that token deploy on. Use that address to add to Add Token in the metamask we can check if the Token number has been deployed yet.
Source : Viblo