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 ERC20 token contract". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize ERC20 token contract".

ERC20 is the improvement proposal of Ethernet Square put forward by Fabian Vogelsteller at the end of 2015, and it is the standard followed by many popular contracts. ERC20 makes license smart contracts behave very much like traditional cryptocurrencies, such as sending and receiving between different accounts, looking at the total supply of permits, or looking at the balance of permits available at an address, just like Bitcoin or Ethernet coin. This is similar to sending and receiving Ethernet coins with Taifang wallets, checking the total amount of etheric coins in circulation, checking the currency balance of specific wallets, and so on.

ERC20 interface

ERC20 defines some standard interface functions: balanceOf, totalSupply, transfer, transferFrom, approve, and allowance. And some optional fields, such as pass name, symbol, and the number of decimal places reserved. You can view the full text of ERC-20 at github.

The following code declares a concise ERC20 smart contract:

Contract ERC20 {function totalSupply () constant returns (uint theTotalSupply); 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 _ address) spender (spender); constant returns (spender _ constant returns, constant returns _ constant returns, uint remaining _) Event Approval (address indexed _ owner, address indexed _ spender, uint _ value);}

The description and examples of each field in the contract are as follows.

TotalSupply ()

Although, like Bitcoin, the total supply of permits can be easily fixed, this function allows the contract instance to calculate and return the total amount of permits that exist in circulation.

Contract MyERCToken {/ / in this example, the permit supply is fixed, but it can also be set to a modifiable uint256 _ totalSupply = 1000000; the return variable theTotalSupply theTotalSupply = _ totalSupply; return theTotalSupply;}} balanceOf () is defined in the function totalSupply () constant returns (uint256 theTotalSupply) {/ / function declaration.

This function allows the smart contract to return the card balance of the specified account address. This function takes an address as an argument, so the permit balance of any address is public.

Contract MyERCToken {/ / create a mapping table to record the account balance mapping (address = > uint256) balances; / / Owner of this contract / / contract owner address public owner; function balanceOf (address _ owner) constant returns (uint256 balance) {/ / return the permit balance of the specified address return balances [_ owner];}} approve ()

The caller of this function authorizes the given address to withdraw money from its address.

Here, and later in the code snippet, you may see a variable msg. This is an implied field provided by an external application, such as a wallet, to better interact with the contract. The Ethernet Square Virtual Machine (EVM) allows us to use this field to store and process data provided by external applications.

In this example, msg.sender is the address of the caller of the contract method.

Contract MyERCToken {/ / create a mapping table to record permit holders, grantees, and number of authorizations mapping (address = > mapping (address = > uint256)) allowed; function approve (address _ spender, uint256 _ amount) returns (bool success) {allowedding [msg.sender] [_ spender] = _ amount; / / trigger Approval events Approval (msg.sender, _ spender, _ amount) when authorization occurs; return true;} transfer ()

This function allows the caller to send a specified number of passes to another address, just like a cryptocurrency transaction.

Contract MyERCToken {mapping (address = > uint256) balances / / if the return value is true, function transfer (address _ to, uint256 _ amount) returns (bool success) {/ / if the sender has sufficient funds and the quantity sent is not 0, send it to the specified address if (balances [msg.sender] > = _ amount & & _ amount > 0 & & balances [_ to] + _ amount > balances [_ to]) {balances [msg.sender]-= _ amount Balances [_ to] + = _ amount; / / trigger Transfer event Transfer (msg.sender, _ to, _ amount); return true;} else {return false;}} transferFrom ()

This function allows intelligent contracts to automate the transfer process and send a given number of passes on behalf of the owner.

When you see this, you may be a little confused: why do you need transferFrom () when you have transfer ()?

Go back to paying your bills by transferring money in your daily life. Usually you need to do your own transfer to pay the bill, which is like using transfer (): you need to do it yourself without the help of others.

In another case, you can sign an automatic payment agreement with the bank. It's like using transferFrom (): the bank's machine will automatically transfer money in your name. With this function, the contract can automatically send a pass to another address on your behalf without your intervention.

Contract MyERCToken {mapping (address = > uint256) balances; function transferFrom (address _ from, address _ to, uint256 _ amount) returns (bool success) {if (balances [_ from] > = _ amount & & allowed [_ from] [msg.sender] > = _ amount & & _ amount > 0 & balances [_ to] + _ amount > balances [_ to]) {balances [_ from]-= _ amount; balances [_ to] + = _ amount Transfer (_ from, _ to, _ amount); return true;} else {return false;}} name-pass name

Name is an optional field, but many popular passports define this field so that wallets like Mist and MyEtherWallet can recognize them:

Contract MyERCToken {string public constant name = "My Custom ERC20 Token";} symbol-pass symbol

Symbol is another optional field used to identify a pass, usually with an acronym of three to four letters, like BTC, ETH, AUG, or SJCX

Contract MyERCToken {string public constant symbol = "MET";} decimals-decimal places

This is an optional field that determines the number of decimal places for the number of passes. The most common decimal place is 18.

Contract MyERCToken {uint8 public constant decimals = 18;}

The source code for this ERC20 pass can be found at Github.

The ERC20 standard opens up a new set of smart contracts that can be created and distributed like Bitcoin or Ethernet Square, and these tokens can be hosted on exchanges and traded like other assets, so investors can easily buy and sell them.

Thank you for your reading, the above is the content of "how to achieve ERC20 token contract". After the study of this article, I believe you have a deeper understanding of how to achieve ERC20 token contract, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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