In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to develop and create ERC20 ethernet tokens, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
What is ERC20?
ERC20 can be understood simply as a token agreement on ethernet place, and all token contracts based on ethernet place abide by this agreement. Tokens that abide by these agreements can be regarded as standardized tokens, and the advantage of standardization is good compatibility. These standardized tokens can be supported by a variety of Ethernet Square wallets for different platforms and projects. To put it bluntly, if you want to issue token financing on Ethernet Square, you must comply with the ERC20 standard.
The standard interface for ERC20 is as follows:
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);}
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 usually see in the token exchange.
Decimals
Returns a few places after the decimal point used by token. For example, if it is set to 3, it supports 0.001 representation.
TotalSupply
Returns the total supply of token
BalanceOf
Returns the account balance of an address (account)
Transfer
The address _ to to which the number of _ value token is transferred from the caller address of the token contract, and the Transfer event must be triggered.
TransferFrom
The Transfer event must be triggered when the number of token sent from the address _ from to the address _ to is _ value.
The transferFrom method is used to allow a contract agent to transfer token. The condition is that the from account must pass through approve. This will be illustrated later.
Approve
Allow _ spender to retrieve your account multiple times, up to the _ value amount. If this function is called again, it will overwrite the current margin with _ value.
Allowance
Returns the amount that _ spender is still allowed to withdraw from _ owner.
The latter three methods are difficult to understand, which need to be supplemented here.
Approve authorizes a third party (such as a service contract) to transfer tokens from the sender's account and then performs the specific transfer operation through the transferFrom () function.
Account A has 1000 ETH. You want to allow account B to call his ETH at will. The procedure is as follows:
An account calls the approve function approve (BMagne 100) in the following form
B account wants to use 10 of these ETH to C account, call transferFrom (A, C, 10)
Call allowance (A, B) to see how many token accounts B can also call An accounts.
The last two are events, which are provided for easy access to logs. The former is triggered when the token is transferred, and the latter is triggered when the approve method is called.
A token contract pragma solidity ^ 0.4.16 based on ERC20: 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 decimal places used by token. For example, if set to 3, 0.001 is supported. String public symbol; / / token abbreviation, like MTT function TokenDemo (uint256 _ initialAmount, string _ tokenName, uint8 _ decimalUnits, string _ tokenSymbol) public {totalSupply = _ initialAmount * 10 * * uint256 (_ decimalUnits); / / set the initial total balances [msg.sender] = totalSupply / / the initial number of token is given to the message sender. Since it is a constructor, it is also the contract creator name = _ tokenName; decimals = _ decimalUnits; symbol = _ tokenSymbol;} function transfer (address _ to, uint256 _ value) public returns (bool success) {/ / default totalSupply will not exceed the maximum (2 ^ 256-1). / / if a new token will be generated over time, you can use the following exception require to avoid overflow (balances [msg.sender] > = _ value & & balances [_ to] + _ value > balances [_ to]); require (_ to! = 0x0); balances [msg.sender]-= _ value;// subtract the number of token from the sender account _ value balances [_ sender] + = _ value / / increase the number of token to the receiving account _ value Transfer (msg.sender, _ to, _ value); / / trigger the 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 / / increase the number of token in the receiving account _ value balances [_ from]-= _ value;// expenditure account _ from minus the number of token _ value allowed [_ from] [msg.sender]-= _ value;// the sender can reduce the number of value Transfer transferred from the account _ from by _ value Transfer (_ from, _ to, _ value); / / trigger the exchange 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.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]; / / number of token allowed _ spender to be transferred from _ owner} mapping (address = > uint256) balances; mapping (address = > mapping (address = > uint256)) allowed;}
The code does not need to be explained too much, and the comments are clearly written.
Here some people may wonder, name,totalSupply these should not be methods according to the standard, why are attribute variables defined here? This is because solidity automatically generates a getter interface with the same name for the public variable.
Deployment test
I will provide deployment and testing processes for both environments, both of which have been tested in person, and you can choose according to your preferences. Personally, I usually use the latter more.
Deployment testing of Remix+MetaMask environment
This section requires that your browser has installed the MetaMask plug-in, as for what is MetaMask and how to install and use, please search for your own query. MetaMask We use the network of the test environment, in the test network, we can apply for some etheric coins for testing.
We copy the code to remix to compile. If there is no problem, click create to create the contract as shown in the following figure. The parameters can be set as shown in the following figure. Note that the environment selects injected web3, which opens the browser plug-in MetaMask for test deployment.
After clicking create, the contract confirmation screen will pop up, click submit directly, and wait for the contract confirmation.
We can click on the details of the contract submission in MetaMask and jump to the browser of Ethernet Square, where we can see all kinds of information about the contract:
As shown in the figure above, 1 represents the hash value of the transaction (the contract is also a transaction), 2 is the block location of the current contract (the test environment, of course) and the number of block chains that have been confirmed, 3 is the creation address of the contract, and 4 is the address where the contract province is located.
The concepts of 3 and 4 are easy to be confused. Pay attention to understanding.
Go to MetaMask's token interface, click add token, and then we copy the address of the contract to the past submission to see our tokens. You can also click the token icon to open a browser to view the details of the token.
You have completed the development and deployment of tokens here. Next, we will look at how to transfer money with tokens, which is also a common operation of tokens. Transfer we need to combine the etheric square wallet MyEtherWallet, this is an etheric square web version of the lightweight wallet, it can be very convenient to manage our etheric coins and other tokens.
Before transferring money, we must first add tokens to our wallets.
Notice in the figure above that the environment we selected is also the test environment and is consistent with the environment in MetaMask. Click add custome token, enter the token address and other information to see the token, and then transfer the money.
We transferred to a random address, and after the transfer was completed, we found that the balance of tokens had indeed decreased.
Ethernet Square Wallet mist+geth Private Environment deployment Test
My personal development uses this environment more, but this environment installation is more troublesome, the specific process can take a look at my previous article.
Open the mist wallet, enter the contract screen, click deploy new contact, and copy the code into the compilation.
Then click deploy
Enter the account password to start the deployment.
As the mining went on, the contract was deployed to my geth private environment.
Back to the contract interface of the wallet, you can already see the contract.
Click transfer ether&tokens to enter the transfer screen to transfer money.
After success, you can see that the balance has decreased and the balance transferred to the account has increased.
After reading the above, do you have any further understanding of how to develop and create ERC20 ethernet tokens? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.