Ethereum Tokens Development (ERC20 standard)

Source: Internet
Author: User
Ethereum ERC20 Development first need to understand the basic concepts of ethereum, tokens, ERC20, smart contracts and other ethereum tokens. Based on our sample code, we can issue our own ethereum tokens. # # What is ERC20 can simply understand ERC20 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 (Stri Ng 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); } ' name returns the name of the ERC20 token, such as "My test token". Symbol returns the abbreviation of the token, for example: MTT, which is also the name we see in the token exchange generally. Decimals returns the number of decimal points used by token. For example, if set to 3, that is, support 0.001 means. Totalsupply returns the total supply of tokens balanceof returns the account balance of an address (account) transfer the number of tokens to the address _to from the caller address of the token contract, and the transfer event must be triggered. Transferfrom the transfer event must be triggered by sending a token-to-address _to of _value from address _from. 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 allows _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, it is also necessary to add that approve is authorized third parties (such as a service contract) transfer tokens from the sender's account, and then through the Transferfrom () function to perform the specific transfer operation. Account A has 1000 eth and wants to allow the B account to call his 100 eth at will, the process is as follows: 1. A account calls the Approve function approve (b,100) 2 in the following form. The B account would like to use the 10 eth in the 100 eth to give the C account, calling Transferfrom (A, C, 10) 3. Call allowance (A, b) to see the B account can also be able to call the A account number of tokens followed by two is an event, the event is provided for easy access to the log. The former is triggered when tokens are transferred, which is triggered when the approve method is called. # # based on ERC20 written a token contract ' ' pragma solidity ^0.4.16; Contract token{uint256 public totalsupply, function balanceof (address _owner) public constant Returns (uint256 balance); function transfer (address _to, uint(_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 tokens {string public name;//name, such as "My test Token" uint8 public decimals;//return Token used several decimal points. 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 * * * UINT256 (_decimalunits);//Set initial total Balances[msg.sender] = TotalS upply; 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 the maximum value (2^256-1).//If there will be new T over time Oken generated, you can use this sentence to avoid overflow exception require (Balances[msg.sender] >= _value && balances[_to] + _value > 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 Transfer ( 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 a Transfer Transaction event return true; } function balanceof (address _owner) public constant Returns (uint256 balance) {return balances[_owner];} function Appro ve (address _spender, uint256 _value) pubLIC returns (bool success) {Allowed[msg.sender][_spender] = _value; Approval (Msg.sender, _spender, _value); return true; } function allowance (address _owner, address _spender) public constant Returns (uint256 remaining) {return Allowed[_owner ][_spender];//allows _spender to transfer tokens from _owner} mapping (address = uint256) balances; Mapping (address = mapping (address = uint256)) allowed; } ' The code does not have to be interpreted too much and the comments are clearly written. There may be some people in doubt, name,totalsupply these standards should not be the method, how to define the property variables? 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 section requires that your browser has installed the Metamask plugin, as to what is metamask and how to install and use it, please search your own query. 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 as shown in the Click Create creation contract, the parameters can be set in the way. Note that the environment is selected injected WEB3, which opens the browser plugin metamask for test deployment. [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-76ca0470349abd97?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240) Click Create will pop up the contract confirmation screen, click Submit directly, wait for the contract confirmation. [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-9f0963b48973360a?imagemogr2/auto-orient/strip%7cimageview2/2/w/1240) We can click on the details of the contract submitted in Metamask, we will jump to the Ethereum browser, you can see the various information of the contract:! [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-551682f17e2cc078?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240) as shown, 1 indicates the hash value of the transaction (the contract is also a trade), 2 is the current position of the contract (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. [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-e315016c12adfc5d?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240) 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 the purse before the transfer! [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-473f0d2b67a78c29?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240)! [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-69c224dee233e09c?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240) Note In, the environment we choose is also a test environment and is consistent with the environment in Metamask. Click Add Custome token, enter the token address and other information can see tokens, and then transfer operations. [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-d685754e716f4713?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240) We randomly transferred to an address, after the transfer was completed, we found that the token balance did decrease. [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-fc716210c0e20a40?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240) # # # Ethereum Wallet Mist+geth Private environment deployment test my personal development with this environment is more, but this environment installs more troublesome, concrete process can look at my previous article. Open the Mist wallet, enter the contract interface, then click on the Deploy new contact and copy the code to compile. [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-aa0ce9f5b8246c10?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240) and then click deploy! [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-1bcb0bca5813e985?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240) Enter the account password to start the deployment. With the mining, the contract is deployed to my Geth private environment! [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-90bfc192168d35dc?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240) back to the wallet of the contract interface can already see the contract,  ! [Ethereum Development Tokens] (httP://upload-images.jianshu.io/upload_images/11831773-736d54a419a325c1?imagemogr2/auto-orient/strip%7cimageview2 /2/w/1240) Click Transfer Ether&tokens to enter the transfer interface to transfer funds. [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-914b405f54e7b5d1?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240)! [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-410ab6d48a5c7a81?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240) After success you can see that the balance has been reduced and the balance of the transferred account has increased. [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-3fe57b09895b28c1?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240)! [Ethereum Development Tokens] (http://upload-images.jianshu.io/upload_images/11831773-96784bef44ef8892?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240) If you are interested in ethereum development, two tutorials are recommended: 1. Ethereum Dapp for new blockchain developers and smart contracts, token development tutorials: [Ethereum Tokens development] (http://xc.hubwiz.com/course/ 5a952991adb3847553d205d1/?affid=624studygolang) 2. Blockchain, Interstellar file System (IPFs), node. js, and MongoDB to build the Ethereum Dapp e-commerce platform Tutorial: [Ethereum Combat] (http ://xc.hubwiz.com/course/5abbb7acc02e6b6a59171dd6/?affid=624studygolang) 191 reads &NBsp

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.