废话不少,先上完整代码:- \; {( m5 e- |6 g, m5 S, E
- pragma solidity ^0.4.24;
- library SafeMath { /**; H e7 h- k! h# S. m c
- * @dev Multiplies two numbers, throws on overflow.
- */
- function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c)/ b! A5 ]6 |8 Q# e
- { if (_a == 0) { return 0;3 l" C: c2 C( V A, _6 W
- }( [- Y1 j0 b* o6 h* x
- c = _a * _b;) Y+ ]* h8 a% i* }
- assert(c / _a == _b); return c;
- } /**1 S+ w. Z3 `3 q, Q h
- * Integer division of two numbers, truncating the quotient.5 I1 i7 {# K$ ~, U
- */
- function div(uint256 _a, uint256 _b) internal pure returns (uint256) { return _a / _b;
- } /**
- * Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).2 [5 H7 h" b9 _9 d; Z5 _3 Z
- */- D I( W W0 p- G* g9 q2 j
- function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
- assert(_b = _a); return c;
- }
- }
- contract ERC20 {& R3 j7 [% K' i3 X( O4 m# M6 Q& ^
- uint256 public totalSupply; function balanceOf(address _who)! P( X. b8 Y4 n" e( }; b2 F
- public view returns (uint256); / W' S; D# v: E4 D7 w7 C
- function allowance(address _owner, address _spender) public view returns (uint256); ( d1 Z( }. z$ A1 C
- function transfer(address _to, uint256 _value) public returns (bool); 9 [2 h4 i6 ~( W% S# r2 |
- function approve(address _spender, uint256 _value) public returns (bool); - ?$ F4 m3 x8 t/ R
- function transferFrom(address _from, address _to, uint256 _value) public returns (bool); : O7 P3 q9 E) G& L" {3 F& U, L
- event Transfer( address indexed from, address indexed to, uint256 value); $ |- l9 o, f' A# K2 f
- event Approval(address indexed owner, address indexed spender, uint256 value);
- event Burn(address indexed from, uint256 value);+ O) l# R& p9 D. N1 u7 z. A
- }contract StandardToken is ERC20 {% S8 V2 }; M* I
- using SafeMath for uint256;
- mapping(address => uint256) balances;1 N8 g% b" c& D" Q
- mapping (address => mapping (address => uint256)) internal allowed; /**
- * Gets the balance of the specified address.
- * @param _owner The address to query the the balance of.
- * @return An uint256 representing the amount owned by the passed address.
- */
- function balanceOf(address _owner) public view returns (uint256)
- { return balances[_owner];: J! T+ `3 n! B
- } /**
- * Function to check the amount of tokens that an owner allowed to a spender.
- * @param _owner address The address which owns the funds.
- * @param _spender address The address which will spend the funds.2 d& ~4 c. Y5 E9 V p
- * @return A uint256 specifying the amount of tokens still available for the spender.+ i3 l/ K C7 l" D/ o9 C
- */
- function allowance(address _owner, address _spender) public view returns
- (uint256){ return allowed[_owner][_spender];. K# w6 z5 J/ ?+ H
- } /**
- * Transfer token for a specified address
- * @param _to The address to transfer to.; v5 s- I# |5 h
- * @param _value The amount to be transferred.# ^& v( a' E1 s: Y
- */0 j. G- n' Y2 m* R+ P m2 x
- function transfer(address _to, uint256 _value) public returns 4 I1 c1 }5 H. {: E
- (bool) { require(_value = oldValue) {
- allowed[msg.sender][_spender] = 0;
- } else {
- allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
- }, ~; \$ X" _9 H5 R
- emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); return true;8 e" s' I. U8 n" e5 q
- } * @9 e9 M: ?( Y+ ?& J \4 x! c, [
- /**
- * Destroy tokens& \, w/ p$ X) ]- n) [
- *
- * Remove `_value` tokens from the system irreversibly
- *
- * @param _value the amount of money to burn3 y6 S% g) p6 { P
- */
- function burn(uint256 _value) public returns (bool success)( W ?5 E% R, |! p) c: A9 S
- { require(balances[msg.sender] >= _value);
- balances[msg.sender] = balances[msg.sender].sub(_value);
- totalSupply = totalSupply.sub(_value);
- emit Burn(msg.sender, _value); return true;
- } /**8 o1 R6 P" F+ A" |
- * Destroy tokens from other account
- *; j) x1 i- D" y) b/ Y# R
- * Remove `_value` tokens from the system irreversibly on behalf of `_from`.
- *
- * @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) % Y0 ]1 [* C# K
- { require(balances[_from] >= _value); * H7 `# E7 E9 Q2 X" Q
- require(_value
这些代码是什么意思呢?' q2 N3 C0 R/ ^5 A( b$ Y2 v
一、ERC20(EIP-20)说明3 R% E( r6 e( K7 I3 @
ERC20是一个标准的token接口规范:A standard interface for tokens7 b/ }5 J6 ?7 i
该接口的官网说明在下面的链接:https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md/ D; m5 R4 ~! p+ h" E
我们来简单介绍下这个接口规范,规范里面定义了9个方法和2个事件
9个方法包括:
返回token的名字
function name() view returns (string name)* v3 i, v- k( T5 O# Y# h) }
返回token的代码 e* o- o$ b" }: x, m
function symbol() view returns (string symbol)
返回token的小数点位数& K- s9 D" H0 o+ X3 _2 w6 ]
function decimals() view returns (uint8 decimals)
返回供应总量
unction totalSupply() view returns (uint256 totalSupply)
查询某个地址的余额& C( }$ t$ w; R9 T1 D2 m
function balanceOf(address _owner) view returns (uint256 balance)' O. b3 [" o: j
给某个地址转账,如果余额不足,该方法必须抛出异常而不是返回false8 g1 `& o6 V) F$ t
function transfer(address _to, uint256 _value) returns (bool success)
从from地址转账到to地址,该方法用于委托给其他合约代理你转账
function transferFrom(address _from, address _to, uint256 _value) returns (bool success)& |+ Y: k9 o- S1 y+ y8 B+ y2 n
允许多次从您的帐户委托到_spender地址,最高_value金额。如果再次调用此函数,则会覆盖当前允许值_value
function approve(address _spender, uint256 _value) returns (bool success)
返回_spender仍允许退出的金额_owner
function allowance(address _owner, address _spender) view returns (uint256 remaining)
2个事件包括:! x: s2 R7 x' Q
转移令牌时必须触发,包括零值转移,注意:创建新token的合约应该触发Transfer事件,即从_from地址设置为0x0创建token的时候( U6 }2 R* k, t" `9 ~/ s* N8 z
event Transfer(address indexed _from, address indexed _to, uint256 _value)
委托转账必须触发+ \7 h- q" L* q# T! l* q% m
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
! L8 ~* }8 z- [6 |) z* O& g
二、代码解释7 W. s) K6 d& Z
4 }) k$ @* }, I8 r3 D" P7 ~
代码包括几部分:& A o Y! O1 ]& D4 o$ |
6 Z, D/ V, Y! Q/ [1 i2 g% \. X( v
安全的数学计算库8 }. T' m8 o: m: s; {: Y
5 W( |2 G+ }( z7 v1 C4 t( x
contract类 ERC20,主要针对ERC20规范定义方法和事件
9 J2 }' f/ o6 k1 I
contract类StandardToken继承ERC20,定义具体的方法实现
contract 类MyTokenERC20集成StandardToken,定义名词,代号,总供应量和创建token