废话不少,先上完整代码:% K/ r& g6 H. u1 Q0 n
- pragma solidity ^0.4.24;# G1 y2 V) Z5 | t9 q
- library SafeMath { /**
- * @dev Multiplies two numbers, throws on overflow.
- */% q# |, z: P/ \. g) d' f
- function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c)
- { if (_a == 0) { return 0;
- }, Q" L2 q/ G$ Z+ \! t2 B+ ?
- c = _a * _b;" S4 m4 M2 J! z% o: n* I5 B; J3 k
- assert(c / _a == _b); return c;, ~+ u7 A& p& p- {$ q
- } /**7 h. ?; M/ Y, W6 W
- * Integer division of two numbers, truncating the quotient. C! v5 o8 K" @' X
- */4 o- P. q$ P3 @; S7 @3 t
- 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).
- */. d# Z: N- P9 P
- function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
- assert(_b = _a); return c;) d( d2 i1 V9 J$ ~. r
- }
- }- E: G# A' Y( ], `0 \
- contract ERC20 {
- uint256 public totalSupply; function balanceOf(address _who)/ L6 z: _) a/ j! I
- public view returns (uint256); 4 A% R5 g; V+ S' C& E( J- p3 p- Q8 O. j
- function allowance(address _owner, address _spender) public view returns (uint256); 7 l: q5 p7 u7 i, { g& J
- function transfer(address _to, uint256 _value) public returns (bool); 0 p9 S6 m7 W6 k2 X0 T' o: i+ x( x
- function approve(address _spender, uint256 _value) public returns (bool);
- function transferFrom(address _from, address _to, uint256 _value) public returns (bool); 8 I$ p6 L K: N3 Q9 y9 w
- event Transfer( address indexed from, address indexed to, uint256 value); ' f) Z6 G" S! h5 u; r3 K
- event Approval(address indexed owner, address indexed spender, uint256 value);
- event Burn(address indexed from, uint256 value);1 V. w6 d, z9 A4 U( {7 ~
- }contract StandardToken is ERC20 {
- using SafeMath for uint256;
- mapping(address => uint256) balances;
- 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];" o* j% }: n& M9 k
- } /**7 O7 e$ h* ^( V" y) U
- * 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.
- * @return A uint256 specifying the amount of tokens still available for the spender." N" S5 P. j) A8 |
- */
- function allowance(address _owner, address _spender) public view returns# N/ z! Z& a6 j; t$ K* B8 X. w. d
- (uint256){ return allowed[_owner][_spender];5 s0 _+ u; Z; M+ b
- } /**6 ~9 Y( v) G# I, ^
- * Transfer token for a specified address6 Q+ N6 x J9 m/ t3 }3 \
- * @param _to The address to transfer to.3 I7 o8 n0 F8 h* E: Y
- * @param _value The amount to be transferred./ }( A- ~; @6 R! q1 M6 F' x
- */. b. }& T0 M9 D
- function transfer(address _to, uint256 _value) public returns # Y+ M$ g6 d1 g0 r: i: s
- (bool) { require(_value = oldValue) {4 y# L: m* ^. B T1 M6 \. x
- allowed[msg.sender][_spender] = 0;
- } else {
- allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
- }
- emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); return true;3 Q* b9 G! ~% Y$ Z, @) ?) Y
- }
- /**
- * Destroy tokens3 c1 v- B& }( y5 A
- *2 Y5 w2 p1 B. [7 y( k& h
- * Remove `_value` tokens from the system irreversibly0 p6 Z" N. t1 k$ e) ^) s, I0 u; B
- *2 T8 ~2 T, b7 @7 O# e0 J
- * @param _value the amount of money to burn
- */1 ~# _8 ^* e q1 S, d
- function burn(uint256 _value) public returns (bool success)* C0 w! U6 F/ L: g( ]5 F6 l5 b
- { require(balances[msg.sender] >= _value); - I+ u3 q* d- d
- balances[msg.sender] = balances[msg.sender].sub(_value); 6 d1 x0 ~, w) `9 H
- totalSupply = totalSupply.sub(_value);
- emit Burn(msg.sender, _value); return true;
- } /*** @ p2 z$ f; }# w' A* s0 Z
- * Destroy tokens from other account O! y2 n' Y4 ~6 e* F* j h
- *0 H4 Q1 [2 ], J0 T2 k3 { s, R$ C
- * Remove `_value` tokens from the system irreversibly on behalf of `_from`.; }' ~. Y$ L# C2 c3 N" f9 Y
- *
- * @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) - e8 o5 g- c4 {. y
- { require(balances[_from] >= _value); 6 \( P/ e$ x1 g: s1 ~
- require(_value
这些代码是什么意思呢?9 q: Y' e, Z5 T
一、ERC20(EIP-20)说明- ~! p" k+ R3 E: ^
ERC20是一个标准的token接口规范:A standard interface for tokens; X; }( v/ k7 z- Q
该接口的官网说明在下面的链接:https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
我们来简单介绍下这个接口规范,规范里面定义了9个方法和2个事件- Z' A+ a/ i; ]0 B/ Q
9个方法包括:
返回token的名字( h8 a4 H2 r6 ~' _1 X" s
function name() view returns (string name)
返回token的代码
function symbol() view returns (string symbol)) ]4 {8 Z3 Z; Z( T5 N5 m
返回token的小数点位数4 W6 a4 ~: P4 J1 Z
function decimals() view returns (uint8 decimals). s8 Z6 G$ m+ x3 }7 ` x0 I
返回供应总量
unction totalSupply() view returns (uint256 totalSupply)7 c. M1 `# J6 B9 V/ x
查询某个地址的余额3 z6 m# h% r8 ?* c
function balanceOf(address _owner) view returns (uint256 balance)
给某个地址转账,如果余额不足,该方法必须抛出异常而不是返回false6 B! h O' ^$ x; T! e+ T1 i
function transfer(address _to, uint256 _value) returns (bool success)
从from地址转账到to地址,该方法用于委托给其他合约代理你转账, }$ V R- S( [1 x
function transferFrom(address _from, address _to, uint256 _value) returns (bool success)
允许多次从您的帐户委托到_spender地址,最高_value金额。如果再次调用此函数,则会覆盖当前允许值_value
function approve(address _spender, uint256 _value) returns (bool success)
返回_spender仍允许退出的金额_owner; U% i0 t* E7 g
function allowance(address _owner, address _spender) view returns (uint256 remaining)2 n4 e# o& {" p8 B
2个事件包括:
转移令牌时必须触发,包括零值转移,注意:创建新token的合约应该触发Transfer事件,即从_from地址设置为0x0创建token的时候8 ^ k: X! \- v0 S; C. G
event Transfer(address indexed _from, address indexed _to, uint256 _value)' m# \. @ S8 E9 P: f: ~4 @6 j
委托转账必须触发
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
二、代码解释
代码包括几部分:9 ~: h* d- y! w( H/ Q+ U% t
安全的数学计算库
2 V' d) l" m4 H [
contract类 ERC20,主要针对ERC20规范定义方法和事件, x2 J, l- ^. U3 d" W" p# b
contract类StandardToken继承ERC20,定义具体的方法实现- _* E% T+ E( V6 {9 V# A! q! s5 X5 ?
4 u7 q8 C) `3 v4 T4 m9 j# i
contract 类MyTokenERC20集成StandardToken,定义名词,代号,总供应量和创建token