废话不少,先上完整代码:4 F% K# E0 v2 f5 e/ W" j5 T( h
- pragma solidity ^0.4.24;# D& G: ^; \9 f
- library SafeMath { /**9 b/ n3 `3 Q- X" Z/ N; \: u7 [
- * @dev Multiplies two numbers, throws on overflow.
- */0 s1 \0 `' U# u2 p2 v; v
- function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c). F, S2 v! K" ?
- { if (_a == 0) { return 0;" n+ u. P9 v& a& t
- }
- c = _a * _b;7 J( g7 n- p Q2 y7 p. f
- assert(c / _a == _b); return c;
- } /**
- * Integer division of two numbers, truncating the quotient.; t9 Z" V2 ^$ ^- N. U$ a9 O5 _. W
- */" V' L! p" G5 _" `" V, S6 N
- 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).
- */
- function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
- assert(_b = _a); return c;( Z5 E! V2 n7 ^ J Z) h( s$ C
- }
- }
- contract ERC20 {
- uint256 public totalSupply; function balanceOf(address _who)
- public view returns (uint256);
- function allowance(address _owner, address _spender) public view returns (uint256);
- function transfer(address _to, uint256 _value) public returns (bool);
- function approve(address _spender, uint256 _value) public returns (bool);
- function transferFrom(address _from, address _to, uint256 _value) public returns (bool);
- event Transfer( address indexed from, address indexed to, uint256 value); ! {- h# o$ ?9 F
- event Approval(address indexed owner, address indexed spender, uint256 value);
- event Burn(address indexed from, uint256 value);- A2 B9 E% J6 Z
- }contract StandardToken is ERC20 {* t0 o1 \! |1 {4 m/ a7 V
- using SafeMath for uint256;; r+ d3 ^6 s8 D* }! R
- mapping(address => uint256) balances;
- mapping (address => mapping (address => uint256)) internal allowed; /**
- * Gets the balance of the specified address.2 T+ J2 S, j" k) s* b+ Z7 [
- * @param _owner The address to query the the balance of.
- * @return An uint256 representing the amount owned by the passed address." j3 X) k9 o) ~" a9 ?, r6 s* z
- */" g, I6 E; E6 \0 U- N S
- function balanceOf(address _owner) public view returns (uint256)
- { return balances[_owner];
- } /**
- * 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.
- */
- function allowance(address _owner, address _spender) public view returns
- (uint256){ return allowed[_owner][_spender];
- } /**
- * Transfer token for a specified address! v+ G6 B; v0 B4 p% v9 X. S
- * @param _to The address to transfer to.
- * @param _value The amount to be transferred.
- */
- function transfer(address _to, uint256 _value) public returns ( E2 Y7 o- p/ O* T( @9 W
- (bool) { require(_value = oldValue) {$ E! x9 J: ^0 Y9 t( ]
- allowed[msg.sender][_spender] = 0;1 a- d. r/ l4 H( Q% ]8 Y( z6 k
- } else {: [! Z. k9 l3 d/ K5 o% ?
- allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);7 q- P: I8 \; r$ T- H! g1 | T
- }4 K) P: C3 l! j8 ^; `+ V
- emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); return true;
- }
- /**8 Q# x5 [8 L: o9 R% f$ g0 c. Z
- * Destroy tokens' R, i8 N/ L; t6 s3 V8 D& E- y
- *
- * Remove `_value` tokens from the system irreversibly$ o4 e3 y7 X0 E, v$ C c
- *
- * @param _value the amount of money to burn
- */
- function burn(uint256 _value) public returns (bool success)" z, {$ M$ E2 D: T( {
- { require(balances[msg.sender] >= _value);
- balances[msg.sender] = balances[msg.sender].sub(_value); 8 C( [0 h7 n0 R* H- K" O- F
- totalSupply = totalSupply.sub(_value); 8 L" r/ \% V; h4 p. X w/ D
- emit Burn(msg.sender, _value); return true;
- } /**: a& y4 y9 m- w; s2 W
- * Destroy tokens from other account
- *2 j/ e+ E7 `! [! I2 b) X
- * Remove `_value` tokens from the system irreversibly on behalf of `_from`." }; F4 x( n5 u7 o1 z. r7 u- l
- *5 X5 ?) w( ]: }
- * @param _from the address of the sender) F! \8 u- v1 G0 B& x8 K' }/ O$ Z7 V
- * @param _value the amount of money to burn" S" Z& m) M# ^5 \8 Z+ G4 \
- */ z* N1 C/ ^% h3 w
- function burnFrom(address _from, uint256 _value) public returns (bool success)
- { require(balances[_from] >= _value);
- require(_value
这些代码是什么意思呢?
一、ERC20(EIP-20)说明
ERC20是一个标准的token接口规范:A standard interface for tokens4 A. j* U0 g2 n2 Y& t
该接口的官网说明在下面的链接:https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md S5 @' ?: {' o/ p( ]7 v9 i
我们来简单介绍下这个接口规范,规范里面定义了9个方法和2个事件3 m7 V6 H$ S' H" K9 j
9个方法包括:
返回token的名字! a1 B: p* s I6 U3 e
function name() view returns (string name)
返回token的代码
function symbol() view returns (string symbol)
返回token的小数点位数
function decimals() view returns (uint8 decimals)
返回供应总量 t2 ?0 O% U; E
unction totalSupply() view returns (uint256 totalSupply)" J9 v% U; [: f E* V2 Y9 z
查询某个地址的余额
function balanceOf(address _owner) view returns (uint256 balance)4 m3 {5 m, p( j v! ?8 _( e
给某个地址转账,如果余额不足,该方法必须抛出异常而不是返回false
function transfer(address _to, uint256 _value) returns (bool success)
从from地址转账到to地址,该方法用于委托给其他合约代理你转账1 K$ O2 @6 R: o0 h* f$ R% q- S
function transferFrom(address _from, address _to, uint256 _value) returns (bool success)
允许多次从您的帐户委托到_spender地址,最高_value金额。如果再次调用此函数,则会覆盖当前允许值_value; y0 r; b1 a3 M# z% d- ~! G
function approve(address _spender, uint256 _value) returns (bool success)
返回_spender仍允许退出的金额_owner
function allowance(address _owner, address _spender) view returns (uint256 remaining), U/ e4 B" M( z! Q! o) B# A( |. X
2个事件包括:
转移令牌时必须触发,包括零值转移,注意:创建新token的合约应该触发Transfer事件,即从_from地址设置为0x0创建token的时候 k2 x" B/ I9 H$ y, c$ ^9 R
event Transfer(address indexed _from, address indexed _to, uint256 _value)# r1 e+ A Z1 F, v O/ Y
委托转账必须触发! ^, V/ z# P- y, r- d% H
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
) ~$ a2 @+ b$ p7 u
二、代码解释
代码包括几部分:" Q9 U0 x- q! Y |" |' Q* W r
# ?4 F. D+ S: W/ C) W5 h1 S
安全的数学计算库
contract类 ERC20,主要针对ERC20规范定义方法和事件
contract类StandardToken继承ERC20,定义具体的方法实现
/ s+ u) G4 t3 z; t
contract 类MyTokenERC20集成StandardToken,定义名词,代号,总供应量和创建token