Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize ERC20 token contract

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to realize the ERC20 token contract". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to realize the ERC20 token contract.

ERC20 specification

ERC20 specifies an interface to be implemented in a token contract. For more information, please see ERC20.

/ / API standard contract ERC20 {function totalSupply () constant returns (uint totalSupply); / / Total circulation function balanceOf (address _ owner) constant returns (uint balance); / / token distribution (note that only contract Creator can be called) function transfer (address _ to, uint _ value) returns (bool success) / / here is the token transfer between owner and owner 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); / / Token information string public constant name = "4FunCoin"; string public constant symbol = "4FC"; uint8 public constant decimals = 18; / / the precision of token is mostly 18}

The above code is a standard ERC20 standard code, the specification gives the framework, we only need to implement the corresponding function, here is the function description.

Interface function description

The formal parameters of the function are locally valid, so underscores are used in front of them to distinguish them from other variables. Such as _ owner.

The totalSupply () function returns the total circulation of this Token

BalanceOf () queries the number of Token of an address, which is implemented with mapping.

Transfer () owner uses this to send tokens

The owner of transferFrom () token is used to send token

Allowance () controls the trading of tokens, such as tradable accounts and assets, and controls the circulation of Token

The number of tokens that approve () allows the user to spend

Event function description

Here two Event are key events that can be captured by the front-end js code and handled accordingly:

Money transfer event of event Transfer () Token

Event Approval () allow events

Implementation of ERC20 token contract

After understanding the above function and the following code, we realize the function filling of the Token contract.

Pragma solidity ^ 0.4.16: the recipient of the interface tokenRecipient {function receiveApproval (address _ from, uint256 _ value, address _ token, bytes _ extraData) public;} / / token declares the interface here, and the contract TokenERC20 {/ * Token attribute description * / string public name = 4FunCoin; string public symbol = 4FC; uint8 public decimals = 18 will be included in our ABI / / 18 is the recommended default uint256 public totalSupply; / / circulation / / establish mapping address corresponds to uint' is his balance mapping (address = > uint256) public balanceOf; / / address corresponds to balance mapping (address = > mapping (address = > uint256)) public allowance; / / event, which is used to notify the client that event Transfer (address indexed from, address indexed to, uint256 value) occurs in the Token transaction. / event, which is used to notify the client that the token is consumed (here is not transferred, but token is gone) event Burn (address indexed from, uint256 value); / / here is the constructor, which executes function TokenERC20 (uint256 initialSupply, string tokenName, string tokenSymbol) public {totalSupply = initialSupply * 10 * * uint256 (decimals) when the instance is created; / / the total circulation balanceOf [msg.sender] = totalSupply is determined here / / this is more important, which is equivalent to the implementation of giving all token to the contract's Creator name = tokenName; symbol = tokenSymbol;} / / token's sending function function _ transfer (address _ from, address _ to, uint _ value) internal {require (_ to! = 0x0); / / non-zero address require (balanceOf [_ from] > = _ value) / / there is enough balance to send require (balanceOf [_ to] + _ value > balanceOf [_ to]); / / it is also interesting here that you cannot send a negative value (hhhh) uint previousBalances = balanceOf [_ from] + balanceOf [_ to]; / / this is for verification to avoid process errors, and the total amount remains the same, right? BalanceOf [_ from]-= _ value; / / not to say balanceOf [_ to] + = _ value; Transfer (_ from, _ to, _ value); / / for the event that triggered the transfer, see event assert (balanceOf [_ from] + balanceOf [_ to] = = previousBalances). / / determine whether the total amount is consistent, and avoid process errors} function transfer (address _ to, uint256 _ value) public {_ transfer (msg.sender, _ to, _ value) / / the information of the contract creator has been stored here. This function can only be used by the contract creator} function transferFrom (address _ from, address _ to, uint256 _ value) public returns (bool success) {require (_ value = _ value); / / there must be so much balanceOf [msg.sender]-= _ value; totalSupply-= _ value; Burn (msg.sender, _ value) Return true;} / / this is the user who destroys the token. Function burnFrom (address _ from, uint256 _ value) public returns (bool success) {require (balanceOf [_ from] > = _ value); / / there must be so many require (_ value)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report