废话不少,先上完整代码:
- pragma solidity ^0.4.24;
- library SafeMath { /**2 p" H% ?+ o* Y. ~
- * @dev Multiplies two numbers, throws on overflow.
- */
- function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c)
- { if (_a == 0) { return 0;
- }
- c = _a * _b;9 I/ N- a! U; I1 u8 @
- assert(c / _a == _b); return c;/ U: U, N7 V5 o& ?
- } /**
- * Integer division of two numbers, truncating the quotient.
- */# n |- E$ A! Q8 i2 l/ g0 p" S$ J6 Z
- function div(uint256 _a, uint256 _b) internal pure returns (uint256) { return _a / _b;# E5 z5 m$ P' F4 T/ k& ~
- } /**
- * Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
- */. f) \8 A* ~1 T: Q) d3 C" C
- function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {* x R7 c1 Y. I, T( x. z7 K
- assert(_b = _a); return c;; Z q- p) O9 d$ R# v5 J6 Z
- }2 U! P, P- T# u
- }
- contract ERC20 {0 ]- F9 q' R! x+ m
- 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); * P! Y/ O/ c7 t5 y2 N$ z
- 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);' w# B% W" q5 Y- w H, ^
- }contract StandardToken is ERC20 {
- using SafeMath for uint256;2 `9 Q* J% H+ ]' U2 x8 b4 `, B
- mapping(address => uint256) balances;5 W3 O) i0 H7 r/ P0 T5 {' b8 g
- mapping (address => mapping (address => uint256)) internal allowed; /**( X: u" X7 S K' L* m
- * Gets the balance of the specified address.+ F* B% h/ |1 W- C
- * @param _owner The address to query the the balance of.2 q! t# q" T2 ]% F# }6 _+ D/ u
- * @return An uint256 representing the amount owned by the passed address.
- */
- function balanceOf(address _owner) public view returns (uint256) 3 I8 f7 A' ^5 E
- { return balances[_owner];
- } /**! y& |; f0 L& e @, }
- * Function to check the amount of tokens that an owner allowed to a spender.0 Q3 e0 y4 r2 r8 D6 k4 q4 A+ D, ^
- * @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.' y# c+ W; w' ^+ V, S! |
- */6 _1 s4 W9 z1 x; w* q
- function allowance(address _owner, address _spender) public view returns/ i9 x2 T. B3 w! a
- (uint256){ return allowed[_owner][_spender];* I. X- N( R2 P) p9 I% K
- } /**
- * Transfer token for a specified address0 _1 E; t3 S! m1 {9 A' T. J+ q1 h
- * @param _to The address to transfer to.
- * @param _value The amount to be transferred.
- */% }0 R; a- U2 j0 N+ i! e6 u, r
- function transfer(address _to, uint256 _value) public returns
- (bool) { require(_value = oldValue) {6 f6 y+ r! C0 `7 I
- allowed[msg.sender][_spender] = 0;; f- _+ U+ p+ V
- } else {& q$ I* H, n) C5 N G: j( _
- allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);" W0 Y& H r2 p6 Y( a7 B# n m
- }6 e. u) x1 @1 ?2 B x
- emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); return true;
- }
- /**
- * Destroy tokens, x0 p4 P8 i5 y/ T" H8 d! y
- *5 ^( `, m- f# F9 k" l9 s {
- * Remove `_value` tokens from the system irreversibly
- *9 K$ C! w) {3 {+ q6 w, D6 e
- * @param _value the amount of money to burn; H g* ?- V% P
- */
- function burn(uint256 _value) public returns (bool success)
- { require(balances[msg.sender] >= _value);
- balances[msg.sender] = balances[msg.sender].sub(_value); ' Z6 \; H8 V: [5 F0 e
- totalSupply = totalSupply.sub(_value);
- emit Burn(msg.sender, _value); return true;
- } /**
- * Destroy tokens from other account
- *$ V6 S6 |- k$ b! d s
- * Remove `_value` tokens from the system irreversibly on behalf of `_from`.5 \" Y; H3 W4 U6 e8 u+ Z
- *, d& Y6 P9 H2 `' O9 o+ |5 `: a
- * @param _from the address of the sender
- * @param _value the amount of money to burn/ M2 f8 L: i7 h: ?) \, B$ ?' l2 p% n
- */' ]4 }) h, r- Z6 i; i
- 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 tokens
该接口的官网说明在下面的链接:https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
我们来简单介绍下这个接口规范,规范里面定义了9个方法和2个事件
9个方法包括:
返回token的名字
function name() view returns (string name)
返回token的代码$ q9 S( B# q, x8 ]
function symbol() view returns (string symbol)9 M1 Z, I8 L) |) S% a5 b" w: B- b
返回token的小数点位数
function decimals() view returns (uint8 decimals)% y; l' \6 s6 G* V+ N
返回供应总量: @. S, z- S% B' I# n
unction totalSupply() view returns (uint256 totalSupply)
查询某个地址的余额$ }9 g; S3 L/ f2 k- s8 V3 @& M" D
function balanceOf(address _owner) view returns (uint256 balance)1 R. W3 Y. o6 R# O" g4 W
给某个地址转账,如果余额不足,该方法必须抛出异常而不是返回false8 M/ {: J- x+ d: b+ q9 h. S
function transfer(address _to, uint256 _value) returns (bool success)
从from地址转账到to地址,该方法用于委托给其他合约代理你转账5 Z$ r6 m3 L4 w o N" Z
function transferFrom(address _from, address _to, uint256 _value) returns (bool success)
允许多次从您的帐户委托到_spender地址,最高_value金额。如果再次调用此函数,则会覆盖当前允许值_value2 e4 t1 a7 F3 s+ G* F3 c
function approve(address _spender, uint256 _value) returns (bool success)
返回_spender仍允许退出的金额_owner
function allowance(address _owner, address _spender) view returns (uint256 remaining)
2个事件包括:
转移令牌时必须触发,包括零值转移,注意:创建新token的合约应该触发Transfer事件,即从_from地址设置为0x0创建token的时候# Z) G. }: s a, H( S$ J! z. F
event Transfer(address indexed _from, address indexed _to, uint256 _value)
委托转账必须触发
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
二、代码解释' b4 w N( ^6 ^" G; G
0 I. q6 A' \( n }$ K1 A
代码包括几部分:1 R& @; S4 T3 W1 a% m, r
7 O7 e( q* i) C- Y% M2 b& A
安全的数学计算库0 i2 ~- k* t* h. r) C
" \/ g0 N- `" {+ @2 o' V; z4 J3 m4 ]
contract类 ERC20,主要针对ERC20规范定义方法和事件, G! N0 G |$ g1 Y9 E
contract类StandardToken继承ERC20,定义具体的方法实现
contract 类MyTokenERC20集成StandardToken,定义名词,代号,总供应量和创建token