当前位置:网站首页>Erc20 Standard Code
Erc20 Standard Code
2022-07-29 00:15:00 【LEVI_ one hundred and four】
pragma solidity ^0.4.16;// Version number
// Interface :tokenRecipient
interface tokenRecipient {
function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public;
}
contract TokenERC20 {
string public name;// Name of currency
string public symbol;// Abbreviation of coin name , such as : Bitcoin is BTC
// digit 18 Is the recommended default .ether and wei The transformation is 10^18
// If set to 0, Then tokens are indivisible . This is equivalent to : Bitcoin can only be traded one coin at a time
uint8 public decimals = 18;
uint256 public totalSupply;// Set the total coin issuance
// You transfer money to others yourself : Each address corresponds to a balance
mapping (address => uint256) public balanceOf;
// Send your authorization to A,A You have the right to transfer your money to others ,allowance Is the authorized limit
// You are the first address, Then you authorize everyone 【mapping (address => uint256)】
//【mapping (address => uint256)】 Equivalent to an authorization table , Record your authorization
mapping (address => mapping (address => uint256)) public allowance;
// event : Transfer situation
event Transfer(address indexed from, address indexed to, uint256 value);
// event : Authorization
event Approval(address indexed owner, address indexed spender, uint256 value);
// event : Destruction of coins
event Burn(address indexed from, uint256 value);
// Constructors : Determine the total number of coins , Name of currency , Abbreviation of currency
function TokenERC20(uint256 initialSupply, string tokenName, string tokenSymbol) public {
totalSupply = initialSupply * 10 ** uint256(decimals);
balanceOf[msg.sender] = totalSupply;
name = tokenName;
symbol = tokenSymbol;
}
// Inside the contract : Currency transfer
function _transfer(address _from, address _to, uint _value) internal {
require(_to != 0x0);// It can't be transferred to 0 Address ( Equivalent to destroying coins )
require(balanceOf[_from] >= _value);// There should be enough money to transfer out
require(balanceOf[_to] + _value > balanceOf[_to]);// Prevent upward overflow
//(*) The purpose of the code : Before and after transfer , The amount of money transferred between the two remains the same , This is for safety
uint previousBalances = balanceOf[_from] + balanceOf[_to];(*)
balanceOf[_from] -= _value;// Transfer out money
balanceOf[_to] += _value;// Transfer in money
emit Transfer(_from, _to, _value);// Record events
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);(*)
}
// External call : Currency transfer
function transfer(address _to, uint256 _value) public returns (bool) {
_transfer(msg.sender, _to, _value);
return true;
}
// User authorized transfer out :_from It's the client ( Authorized person ),msg.sender Is the principal
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
// Ensure that the money to be transferred out is less than or equal to the authorized money
require(_value <= allowance[_from][msg.sender]);
allowance[_from][msg.sender] -= _value;
_transfer(_from, _to, _value);
return true;
}
// to grant authorization : The principal gives the principal an authorized amount
function approve(address _spender, uint256 _value) public
returns (bool success) {
allowance[msg.sender][_spender] = _value;// The principal gives the principal an amount authorization value
emit Approval(msg.sender, _spender, _value);// Record events : Authorization
return true;
}
// Implementation interface : Authorize and call : Authorized person msg.sender, Authorized to _spender,
function approveAndCall(address _spender, uint256 _value, bytes _extraData) public returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
}
// Destroy currency
function burn(uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value);// You should have enough money to destroy
balanceOf[msg.sender] -= _value;// Set the coin to 0
totalSupply -= _value;// The amount of currency issued is set to 0
emit Burn(msg.sender, _value);// Record the coin destruction event
return true;
}
// Authorize others to destroy money
function burnFrom(address _from, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value);// The amount of money destroyed is enough
require(_value <= allowance[_from][msg.sender]);// The authorized amount of money destruction meets the authorization range
balanceOf[_from] -= _value;
allowance[_from][msg.sender] -= _value;
totalSupply -= _value;
emit Burn(_from, _value);// Record events
return true;
}
}边栏推荐
- Worthington RNA determination detailed introduction
- Android studio connects to MySQL and completes simple login and registration functions
- @Transactional 注解使用详解
- Sword finger offer 41. median in data flow
- With this, your messages can't be monitored
- After SAP Oracle replicates a new instance, the remote connection of the database reports an error ora-01031
- Review of categories 1-4
- Oracle super full SQL, details crazy
- What do you need to bring with you for the NPDP exam? Stationery carrying instructions
- Do you know any formal part-time platforms?
猜你喜欢

GhostNets on Heterogeneous Devices via Cheap Operations

Is the declarative code of compose so concise?

Field injection is not recommended solution

【C】 Introduction and Simulation Implementation of ATOI and offsetof

JS advanced ES6 ~ es13 new features

SAP temporary tablespace error handling

curl (7) Failed connect to localhost8080; Connection refused

feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r

Leetcode62. 不同路径

Leetcode59. Spiral matrix II
随机推荐
实时数仓:滴滴的实时数仓落地实践
SQL实现将多行记录合并成一行
Oracle create tablespaces and users
研发效能的道法术器
Event extraction and documentation (2018)
MySQL安装配置教程(超级详细、保姆级)
curl (7) Failed connect to localhost8080; Connection refused
EN 12101-8:2011 smoke dampers for smoke and heat control systems - CE certification
Virtual lab basic experiment tutorial -8. Fourier transform (1)
Sword finger offer 41. median in data flow
JS advanced ES6 ~ es13 new features
Real time data warehouse: Didi's real-time data warehouse landing practice
Add build dependency error
Field injection is not recommended solution
Doip test development practice
Feign call fails. JSON parse error illegal character ((ctrl-char, code 31)) only regular white space (R
Concurrency in go
1-4 类的复习
Eight performance analysis indicators of effective supply chain management (Part 1)
Laptop external display