废话不少,先上完整代码:
- pragma solidity ^0.4.24;' v& e1 x* H+ [5 [% h
- library SafeMath { /**. l; A( a; V6 i+ c
- * @dev Multiplies two numbers, throws on overflow." u3 Q$ d% ?+ A7 y7 C3 C/ @
- */% \8 p- Y6 E! |, x% ^, l' s$ O9 K1 `. E
- function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c)3 `, w2 `+ h# M& T: n) A9 [4 j
- { if (_a == 0) { return 0;
- }5 C; P. _8 F' M. `
- c = _a * _b;
- assert(c / _a == _b); return c;7 U- {: b. g) w4 G; e$ }$ p3 a
- } /**
- * Integer division of two numbers, truncating the quotient.
- */
- function div(uint256 _a, uint256 _b) internal pure returns (uint256) { return _a / _b;/ y" W) ?; C$ D d6 ]
- } /**
- * Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).4 X, O9 l/ ]( p
- */
- function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
- assert(_b = _a); return c;
- }
- }- g* K6 C+ ~2 l& U
- contract ERC20 {; @2 W0 T* j' `. R0 p1 Z* K
- uint256 public totalSupply; function balanceOf(address _who)
- public view returns (uint256); d. o+ A+ S# X8 g) `& C1 a
- function allowance(address _owner, address _spender) public view returns (uint256); $ w) m* g" z4 j$ ?5 j r6 Z
- function transfer(address _to, uint256 _value) public returns (bool); 2 G" O' h, {: n+ ]$ r
- function approve(address _spender, uint256 _value) public returns (bool); & O" I2 j( O1 r
- function transferFrom(address _from, address _to, uint256 _value) public returns (bool); ! p% \- E( x* \6 w' I
- event Transfer( address indexed from, address indexed to, uint256 value);
- event Approval(address indexed owner, address indexed spender, uint256 value);
- event Burn(address indexed from, uint256 value);; B! @# i# d( y3 f+ ]
- }contract StandardToken is ERC20 {- U; p4 f8 l) X0 w1 l; p
- using SafeMath for uint256;
- mapping(address => uint256) balances;9 x, K) ?7 ?8 c! C
- mapping (address => mapping (address => uint256)) internal allowed; /**# z- P) n" t& d
- * Gets the balance of the specified address.
- * @param _owner The address to query the the balance of.3 M' ~' o/ ~/ q9 l Q5 Z
- * @return An uint256 representing the amount owned by the passed address. B: H Z$ Z9 U
- */. t4 I* t: m: l1 J9 @% W
- function balanceOf(address _owner) public view returns (uint256)
- { return balances[_owner];$ `- t- G4 l1 F1 |
- } /**
- * Function to check the amount of tokens that an owner allowed to a spender.
- * @param _owner address The address which owns the funds.7 T( r1 P/ y$ x- p3 Q; }% A
- * @param _spender address The address which will spend the funds.% z* P! F' u; E! Z, M* Q) a
- * @return A uint256 specifying the amount of tokens still available for the spender.
- */+ e6 R: o- B& |( z0 J
- function allowance(address _owner, address _spender) public view returns- a2 x% b8 A. H: I- V. a
- (uint256){ return allowed[_owner][_spender];! h! n& X6 Q, u5 j
- } /**
- * Transfer token for a specified address9 Y+ M, B2 J+ J1 ?1 U, e
- * @param _to The address to transfer to.
- * @param _value The amount to be transferred.
- */
- function transfer(address _to, uint256 _value) public returns 8 J; u( H+ E$ K1 j7 E0 o% B
- (bool) { require(_value = oldValue) {4 O0 D* h# [! U# N; o7 r9 ?* |
- allowed[msg.sender][_spender] = 0;4 Q! ]% [% B$ ?) N i: B& R
- } else {
- allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
- }
- emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); return true;
- }
- /**
- * Destroy tokens" y7 c8 ?- k) G
- *
- * Remove `_value` tokens from the system irreversibly: r ~2 ?0 g: e
- *0 l) Z- h6 A$ M+ @& I
- * @param _value the amount of money to burn! H F% X( b- X
- */- w9 ?- d# C: z5 U! ]7 J4 x
- function burn(uint256 _value) public returns (bool success)/ m! Q5 C) a# H
- { require(balances[msg.sender] >= _value); - X9 S+ U7 C3 ]9 K* Y+ n
- balances[msg.sender] = balances[msg.sender].sub(_value); 2 X; Y' H" @/ A, k1 j
- totalSupply = totalSupply.sub(_value);
- emit Burn(msg.sender, _value); return true;5 I9 J& }3 o1 m+ x/ m$ }2 }# B3 ?
- } /**
- * Destroy tokens from other account. a7 M/ g, _% D: }! x+ B
- *3 [6 K& \9 r- J) k' m
- * Remove `_value` tokens from the system irreversibly on behalf of `_from`.
- *3 w/ ^) Y1 w! E, k# o
- * @param _from the address of the sender
- * @param _value the amount of money to burn
- */
- function burnFrom(address _from, uint256 _value) public returns (bool success)
- { require(balances[_from] >= _value);
- require(_value
这些代码是什么意思呢?6 a. q5 ]& p& L( v' I" }
一、ERC20(EIP-20)说明
ERC20是一个标准的token接口规范:A standard interface for tokens$ Y0 d! R# {0 R3 ?1 Z/ Y+ Z0 R
该接口的官网说明在下面的链接:https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
我们来简单介绍下这个接口规范,规范里面定义了9个方法和2个事件+ c0 ?$ K; y5 p) [9 S- v
9个方法包括:5 \: {4 t, j# @1 p, J9 R9 e$ J
返回token的名字
function name() view returns (string name)! m8 F* w/ l, V: C
返回token的代码
function symbol() view returns (string symbol)
返回token的小数点位数2 D8 H" ]; N/ N. b% f% }' V- H
function decimals() view returns (uint8 decimals)- n; h3 y$ z' A; {; ]' ~ x s
返回供应总量( t& ~# g5 N- }' g' q% U
unction totalSupply() view returns (uint256 totalSupply)
查询某个地址的余额
function balanceOf(address _owner) view returns (uint256 balance)' T; D+ |* v, M: g- J
给某个地址转账,如果余额不足,该方法必须抛出异常而不是返回false) X8 K8 n4 |" p. g
function transfer(address _to, uint256 _value) returns (bool success)/ }1 {9 A9 T d, q, n
从from地址转账到to地址,该方法用于委托给其他合约代理你转账
function transferFrom(address _from, address _to, uint256 _value) returns (bool success)
允许多次从您的帐户委托到_spender地址,最高_value金额。如果再次调用此函数,则会覆盖当前允许值_value6 \" w/ c; @$ Y, N$ r
function approve(address _spender, uint256 _value) returns (bool success)+ n6 r1 `' B4 O1 F2 C9 _8 X
返回_spender仍允许退出的金额_owner
function allowance(address _owner, address _spender) view returns (uint256 remaining)
2个事件包括:9 C+ G" l7 k6 o* F1 M
转移令牌时必须触发,包括零值转移,注意:创建新token的合约应该触发Transfer事件,即从_from地址设置为0x0创建token的时候
event Transfer(address indexed _from, address indexed _to, uint256 _value)) N' y2 Z- E* c5 c9 W+ l
委托转账必须触发
event Approval(address indexed _owner, address indexed _spender, uint256 _value)7 A# C: ]0 _6 R; f. i
( ~- y% }; O6 L* U1 }& T) J
二、代码解释) v- ^8 a/ B6 E. t* j! u
代码包括几部分:3 s$ f2 {9 q, V
6 N' a# J& e( w7 |; a K" e
安全的数学计算库
( H7 X7 F7 t7 b6 q( w8 _6 A
contract类 ERC20,主要针对ERC20规范定义方法和事件
# s0 k) P/ A L: n
contract类StandardToken继承ERC20,定义具体的方法实现
contract 类MyTokenERC20集成StandardToken,定义名词,代号,总供应量和创建token