当前位置:网站首页>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;
}
}边栏推荐
猜你喜欢

Intelligent trash can (VII) -- Introduction and use of sg90 steering gear (Pico implementation of raspberry pie)

ZABBIX 5.0 uses its own redis template for monitoring

Connection pool - return connection details (Part 2)

Powercl batch creates and manages virtual switches

Real time data warehouse: Netease strictly selects the practice of real-time data warehouse based on Flink

Control fillet stroke materialshapedrawable

Sword finger offer 41. median in data flow

IDEA报错Error running ‘Application‘ Command line is too long解决方案

Interpretation of ISO 13400 (doip) standard

Leetcode64. Minimum path sum
随机推荐
1-5 类式组件
【C】喝汽水,找单身狗问题
CMake 基础学习
Okaleido ecological core equity Oka, all in fusion mining mode
mysql索引失效的常见9种原因详解
Detailed principle explanation and verification results of digital clock based on FPGA
【C】 Replace spaces and realize binary parity bit exchange of integers by macros
What do you need to bring with you for the NPDP exam? Stationery carrying instructions
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
Websocket heartbeat mechanism (keep alive mechanism)
Install MySQL using Yum for Linux
Introduction and solution of common security vulnerabilities in web system CSRF attack
Compilation principle research study topic 2 -- recursive descent syntax analysis design principle and Implementation
Field injection is not recommended solution
Leetcode62. Different paths
【C】替换空格,宏实现整数的二进制奇偶位交换
“Method Not Allowed“,405问题分析及解决
Leetcode 763. partition labels divide alphabetic intervals (medium)
The failure rate is as high as 80%. How to correctly complete the strategic planning of digital transformation?
Please briefly describe the respective characteristics of list, set and map type sets (briefly describe three different inheritance methods)