Teach you how to create ERC20 tokens step-by-step

Source: Internet
Author: User
Tags constant

Ext.: https://blog.csdn.net/pony_maggie/article/details/79588259

Reading this article requires a basic understanding of the concepts of Ethereum, smart contracts, tokens, and so on. What is ERC20

ERC20 can be simply understood as a token agreement on Ethereum, and all tokens based on Ethereum are subject to this agreement. The tokens that follow these agreements are considered to be standardized tokens, and the benefits of standardization are good compatibility. These standardized tokens can be supported by various ethereum wallets for different platforms and projects. Frankly speaking, if you want to issue token financing on Ethereum, you must abide by the ERC20 standard.

The standard interface for ERC20 is this:

  Contract ERC20 {
      function name () constant returns (string name)
      function symbol () constant returns (string symbol)
      function decimals () constant Returns (Uint8 decimals)
      function totalsupply () constant Returns (UINT totalsupply );
      function balanceof (address _owner) constant returns (uint balance);
      function transfer (address _to, uint _value) returns (bool success);
      function Transferfrom (address _from, address _to, uint _value) returns (bool success);
      function approve (address _spender, uint _value) returns (bool success);
      function allowance (address _owner, address _spender) constant returns (uint remaining);
      Event Transfer (address indexed _from, address indexed _to, uint _value);
      Event Approval (address indexed _owner, address indexed _spender, uint _value);
    }
1 2 3 4 5 6 7 8 9 10 11 12 13

Name

Returns the name of the ERC20 token, such as "My test token".

Symbol

Returns the abbreviation for tokens, for example: MTT, which is also the name that we generally see on the token exchange.

Decimals

Returns the number of tokens used after the decimal point. For example, if set to 3, that is, support 0.001 means.

Totalsupply

Returns the total amount of token supply

Balanceof

Return an account balance for an address (account)

Transfer

Transfer the number of _value tokens from the caller address of the token contract to the address _to, and the transfer event must be triggered.

Transferfrom

The transfer event must be triggered from the address _from to send the token to address _to of _value.

The Transferfrom method is used to allow a contract to proxy someone to transfer tokens. The condition is that the from account must be approve. This is illustrated later.

Approve

Allow _spender to retrieve your account multiple times, up to _value amount. If you call this function again, it will overwrite the current margin with _value.

Allowance

Returns the amount that _spender is still allowed to extract from _owner.

The following three methods are not good to understand, here also need to add a note,

Approve is authorizing a third party (such as a service contract) to transfer tokens from the sender's account and then perform a specific transfer operation through the Transferfrom () function.

Account A has 1000 eth and wants to allow the B account to call his 100 eth at random, as follows:

A account calls the Approve function approve (b,100) in the following form

b account would like to use the 10 eth in these 100 ETH to C account, call Transferfrom (A, C, 10)

Call allowance (A, B) to view the B account and also be able to invoke the a account number of tokens

In addition, I recommend this article, the concept of this part of the explanation is relatively clear.

Https://mp.weixin.qq.com/s/foM1QWvsqGTdHxHTmjczsw

The next two are events, and events are provided for easy access to the logs. The former is triggered when tokens are transferred, which is triggered when the approve method is called. A token -based contract written in ERC20

pragma solidity ^0.4.16;

    Contract token{uint256 public totalsupply;
    function balanceof (address _owner) public constant Returns (uint256 balance);
    function transfer (address _to, uint256 _value) public Returns (bool success);

    function Transferfrom (address _from, address _to, uint256 _value) public Returns (bool success);

    function approve (address _spender, uint256 _value) public Returns (bool success);

    function allowance (address _owner, address _spender) public constant Returns (uint256 remaining);
    Event Transfer (address indexed _from, address indexed _to, uint256 _value);
Event Approval (address indexed _owner, address indexed _spender, uint256 _value);                   } contract Tokendemo is Token {string public name;               Name, such as "My test token" uint8 public decimals; Returns the number of tokens used after the decimal point.
    For example, if set to 3, that is, support 0.001 means.               string public symbol; token abbreviation, like MTT function Tokendemo (uint256 _initialamount, String _tokenname, Uint8 _decimalunits, string _tokensymbol) public {totalsupply = _initialamount * * * UINT25         6 (_decimalunits); Set initial total Balances[msg.sender] = totalsupply;                   
        The initial token quantity is given to the message sender because it is a constructor, so this is also the creator of the contract name = _tokenname;          
        decimals = _decimalunits;
    symbol = _tokensymbol; } function Transfer (address _to, uint256 _value) public Returns (bool success) {//default totalsupply does not exceed maximum value (2^2
        56-1). If there is a new token generation over time, you can avoid overflow exceptions with this sentence require (Balances[msg.sender] >= _value && balances[_to] + _valu
        e > Balances[_to]);
        Require (_to! = 0x0); Balances[msg.sender]-= _value;//Subtract the token amount from the message sender's account _value balances[_to] + = _value;//Increase the number of tokens to the receiving account _value T
    Ransfer (Msg.sender, _to, _value);//Trigger Currency transaction event return true;
        } function Transferfrom (address _from, address _to, uint256 _value) public Returns (bool success) {Require (Balances[_from] >= _value && Allowed[_from][msg.sender] >= _value); BALANCES[_TO] + = _value;//Receive account increase the number of tokens _value balances[_from]-= _value; Expense account _from minus token quantity _value allowed[_from][msg.sender]-= _value;//message sender can reduce the number of transfers from account _from _value Transfer (_
    From, _to, _value);//Trigger Currency transaction event return true;
    } function balanceof (address _owner) public constant Returns (uint256 balance) {return balances[_owner]; } function approve (address _spender, uint256 _value) public Returns (bool success) {allowed[msg.se
        Nder][_spender] = _value;
        Approval (Msg.sender, _spender, _value);
    return true; } function allowance (address _owner, address _spender) public constant Returns (uint256 remaining) {return a
    llowed[_owner][_spender];//allows _spender to transfer tokens from _owner} mapping (address = uint256) balances;
Mapping (address = mapping (address = uint256)) allowed; }
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

The code does not have to be interpreted too much, and the comments are clearly written.

Some people here may have doubts, name,totalsupply these standards should not be a method, how to define this is a property variable. This is because solidity automatically generates a getter interface with the same name for the public variable. deployment Test

I will provide two environment deployment test process, are pro-measured, you can choose according to their preferences. I usually use more than the latter. remix+metamask Environment Deployment test

This part requires that your browser has installed the Metamask plugin, as to what is metamask and how to install and use please search queries on your own. Metamask we use a test environment network, in the test network can apply for some of the etheric currency to test.

We copy the code to remix compile, no problem if you click on the Create contract as shown below, the parameters can be set in the following figure. Note that the environment is selected injected WEB3, which opens the browser plug-in Metamask for test deployment.

Click Create will pop up the contract confirmation screen, click Submit directly, wait for the contract confirmation.

We can click on the details of the contract submission in Metamask and we will jump to the Ethereum browser where we can see the various information about the contract:

As shown in the figure above, 1 indicates the hash value of the transaction (the contract is also a trade), 2 is the current contract location (of course, the test environment) and the number of blockchain has been confirmed, 3 is the creation address of the contract, 4 is the address of the contract province.

The concepts of 3 and 4 are easy to confuse and to understand.

Enter the token interface of Metamask, click Add Token, and then we copy the address of the contract to the previous submission to see our tokens. You can also click on the token icon to open the browser to view tokens details.

Here you have completed the development and deployment of tokens. Next we also want to see how to transfer tokens, this is a token more commonly used operations. Transfer we need to combine Ethereum wallet Myetherwallet, an ethereum web-based lightweight wallet that makes it easy to manage our etheric coins and other tokens.

We must first add tokens to our wallets before we make a transfer.

Note In the image above, the environment we selected is also a test environment and is consistent with the environment in Metamask. Click Add Custome token, enter the token address and other information to see the tokens, and then transfer the operation.


We randomly transferred to an address, when the transfer was completed, we found that the token balance did decrease.

Ethereum wallet mist+geth Private Environment Deployment test

My personal development with this environment is more, but this environment installed more trouble, the specific process can look at my previous article.

Open the Mist wallet, enter the contract interface, then click on Deploy new contact and copy the code to compile.

Then click Deploy

Enter your account password to begin deployment.

With the mining, the contract was deployed to my Geth private environment,

Back to the wallet's contract interface, you can see the contract,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.