废话不少,先上完整代码:3 t" S- [0 f u4 {( M' U( X
- pragma solidity ^0.4.24;
- library SafeMath { /**. U+ _- |7 d9 }/ w( g i3 R) |9 v
- * @dev Multiplies two numbers, throws on overflow.
- */
- function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c)" Z! j0 W7 K z0 g' h5 Z
- { if (_a == 0) { return 0;
- }
- c = _a * _b;
- assert(c / _a == _b); return c;
- } /**
- * Integer division of two numbers, truncating the quotient.
- */
- function div(uint256 _a, uint256 _b) internal pure returns (uint256) { return _a / _b;7 Y$ k( @ e( v! J" C
- } /**
- * Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).& q1 O& D( E* ]9 [# g! J5 W2 P
- */! g; e5 B1 {% l+ \6 R
- function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
- assert(_b = _a); return c;
- }6 f2 ~$ l- [3 `" }6 Y( {
- }
- 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); 0 x2 F+ ]: F# b, j& C
- 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);
- event Approval(address indexed owner, address indexed spender, uint256 value);
- event Burn(address indexed from, uint256 value);6 {$ h4 r/ _$ U$ S { t: ^
- }contract StandardToken is ERC20 {! C# T" W& c( D" G/ c1 Z3 i
- 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.% O4 f/ f" H8 p- x7 k9 o8 n
- * @return An uint256 representing the amount owned by the passed address.
- */; s% Z# k: d0 T( s. ^6 S; d5 D
- function balanceOf(address _owner) public view returns (uint256)
- { return balances[_owner];$ p) \( j( {3 H6 N# m
- } /**
- * Function to check the amount of tokens that an owner allowed to a spender." T$ H8 r, R% J5 s
- * @param _owner address The address which owns the funds.
- * @param _spender address The address which will spend the funds.) l4 o' d6 C) i7 j6 O r
- * @return A uint256 specifying the amount of tokens still available for the spender.
- */$ \% u0 E: G& Z
- function allowance(address _owner, address _spender) public view returns
- (uint256){ return allowed[_owner][_spender];
- } /**
- * Transfer token for a specified address& Q, ^4 T f$ C8 {
- * @param _to The address to transfer to.
- * @param _value The amount to be transferred.! q* }2 H- Z% f8 H% ?7 R: i2 d4 `
- */
- function transfer(address _to, uint256 _value) public returns $ ?3 y8 |) e0 I% ?3 F
- (bool) { require(_value = oldValue) {
- allowed[msg.sender][_spender] = 0;
- } else {8 l7 d& ?2 U/ ^- m# R
- allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
- }
- emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); return true;
- }
- /**6 `% ]4 P- |; E7 ?' R ~
- * Destroy tokens& I$ U- T. o* P3 `) U
- *
- * Remove `_value` tokens from the system irreversibly& N5 f( ^, n4 z, u" q6 r
- *6 h* K1 Q8 ]' p, H; k9 v) D' w
- * @param _value the amount of money to burn
- */
- function burn(uint256 _value) public returns (bool success)/ @8 w" Q0 |( |3 \; Q S/ J
- { require(balances[msg.sender] >= _value);
- balances[msg.sender] = balances[msg.sender].sub(_value); ) c* y8 J3 Z% |2 s
- totalSupply = totalSupply.sub(_value); 2 r3 J: ]) I. J. o9 j$ s
- emit Burn(msg.sender, _value); return true;; g5 A) [9 c" k* R
- } /**. J1 p' l9 B' B% f0 i+ f# Q L
- * Destroy tokens from other account* M' B% w5 G6 ]) U
- *1 m8 R" h v" Q% R, G" i, m
- * Remove `_value` tokens from the system irreversibly on behalf of `_from`.* Y& N% }9 z- g @; Q
- *( M" Q5 a" H* I0 w
- * @param _from the address of the sender; o/ z2 E0 L( ~
- * @param _value the amount of money to burn
- */
- function burnFrom(address _from, uint256 _value) public returns (bool success)
- { require(balances[_from] >= _value);
- require(_value
这些代码是什么意思呢?
一、ERC20(EIP-20)说明8 I( e* w7 d& F( K, e$ w
ERC20是一个标准的token接口规范:A standard interface for tokens
该接口的官网说明在下面的链接:https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md) f( g& U+ s, Y: ]* k1 V
我们来简单介绍下这个接口规范,规范里面定义了9个方法和2个事件
9个方法包括: p; a5 w$ L6 q# {( s
返回token的名字1 l! s, W( V" l
function name() view returns (string name)
返回token的代码
function symbol() view returns (string symbol)
返回token的小数点位数 v* n6 g5 E/ G$ K- B! [
function decimals() view returns (uint8 decimals)' H* n" ^6 e/ u/ Q2 V
返回供应总量
unction totalSupply() view returns (uint256 totalSupply). |: u2 ~6 r8 \+ f: @
查询某个地址的余额
function balanceOf(address _owner) view returns (uint256 balance)5 J3 C' }3 w* P8 E
给某个地址转账,如果余额不足,该方法必须抛出异常而不是返回false
function transfer(address _to, uint256 _value) returns (bool success)
从from地址转账到to地址,该方法用于委托给其他合约代理你转账 k `: E) z3 @" r
function transferFrom(address _from, address _to, uint256 _value) returns (bool success)
允许多次从您的帐户委托到_spender地址,最高_value金额。如果再次调用此函数,则会覆盖当前允许值_value1 J: Q" X: b6 Q
function approve(address _spender, uint256 _value) returns (bool success)
返回_spender仍允许退出的金额_owner
function allowance(address _owner, address _spender) view returns (uint256 remaining)
2个事件包括:8 Z, `0 d. U% i2 E \# b" a
转移令牌时必须触发,包括零值转移,注意:创建新token的合约应该触发Transfer事件,即从_from地址设置为0x0创建token的时候) i8 L; J8 q; \3 ~/ P9 e
event Transfer(address indexed _from, address indexed _to, uint256 _value)
委托转账必须触发- W1 R* F+ Q: K. G0 j. P4 Z; L
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
3 g; _; K8 `# |8 M" h8 y9 M
0 b4 {. V& N* j1 w) f
二、代码解释: J4 h( C& `# }* j% r4 `8 m
* u% ^# K( S1 M( c7 k
代码包括几部分:
1 h; J3 O, g. u* C4 i3 G' T$ ~* a
安全的数学计算库
contract类 ERC20,主要针对ERC20规范定义方法和事件
1 C- p. y* g3 U$ Y& Q; {$ G/ N
contract类StandardToken继承ERC20,定义具体的方法实现
- ~3 p0 {! a% j2 b1 w9 h! _) f. O
contract 类MyTokenERC20集成StandardToken,定义名词,代号,总供应量和创建token