当前位置:网站首页>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;
}
}边栏推荐
- Do you know any formal part-time platforms?
- 实时数仓:美团基于Flink的实时数仓建设实施
- Leetcode59. 螺旋矩阵 II
- Sword finger offer 64. find 1+2+... +n, logical operator short circuit effect
- Websocket heartbeat mechanism (keep alive mechanism)
- What do you need to bring with you for the NPDP exam? Stationery carrying instructions
- ZABBIX 5.0 uses its own redis template for monitoring
- 【MySQL 8】Generated Invisible Primary Keys(GIPK)
- 软件设计师的错题汇总
- Leetcode61. 旋转链表
猜你喜欢

ISO 13400(DoIP)标准解读

【C】 Introduction and Simulation Implementation of ATOI and offsetof

The failure rate is as high as 80%. How to correctly complete the strategic planning of digital transformation?

"Method not allowed", 405 problem analysis and solution

【C】喝汽水,找单身狗问题

#{}和${}的区别

Worthington RNA determination detailed introduction

Leetcode62. 不同路径

GhostNets on Heterogeneous Devices via Cheap Operations

【C】 Drink soda and find a single dog
随机推荐
【C】替换空格,宏实现整数的二进制奇偶位交换
1-8 basic use of props
Leetcode61. rotating linked list
Network traffic monitoring tool iftop
Doip communication of canoe application case
Worthington RNA determination detailed introduction
[MySQL series] MySQL database foundation
Interpretation of ISO 13400 (doip) standard
实时数仓:网易严选基于Flink的实时数仓实践
AutoCAD -- import excel tables into CAD and merge CAD
Worthington - chemical properties and related studies of Worthington trypsin
Android studio connects to MySQL and completes simple login and registration functions
Using recursion and chain header interpolation to realize the group turnover of linked lists -- leetcode25 K group turnover linked lists
After SAP Oracle replicates a new instance, the remote connection of the database reports an error ora-01031
ISO 13400(DoIP)标准解读
"Method not allowed", 405 problem analysis and solution
Add build dependency error
Servlet运行原理_API详解_请求响应构造进阶之路(Servlet_2)
以JSP为视图解析器搭建SSM项目
Cmake basic learning