废话不少,先上完整代码:4 H8 j9 m# N/ U$ l P
- pragma solidity ^0.4.24;' n. b4 Q/ f* f6 E, {7 t& ^* a. R
- library SafeMath { /**1 V# y! f! j: |. V
- * @dev Multiplies two numbers, throws on overflow.
- */2 z8 [5 o; W9 W( m0 J# q3 A1 P
- function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c)
- { if (_a == 0) { return 0;
- }
- c = _a * _b;
- assert(c / _a == _b); return c;& C5 c) Z4 ^: Z7 j* [& q# e
- } /**6 J' R, c$ C a+ ~4 o- \$ ?. t
- * Integer division of two numbers, truncating the quotient.8 Q/ ?, Q% Z# W5 A
- */
- function div(uint256 _a, uint256 _b) internal pure returns (uint256) { return _a / _b;8 { T0 ~' i' n; 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) { h. l6 B; _# W, b
- assert(_b = _a); return c;6 r. u! U) t9 [5 C- \6 n
- }( v0 {3 m( x* e
- }: L9 T4 L9 z: G3 u: \
- contract ERC20 {7 \5 i3 \# h5 A4 V/ x! \6 E
- uint256 public totalSupply; function balanceOf(address _who)
- public view returns (uint256);
- function allowance(address _owner, address _spender) public view returns (uint256); & |% h- D6 R6 i4 {
- function transfer(address _to, uint256 _value) public returns (bool); ! f9 _6 d- V" K [( |) w! _, }
- 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);$ A2 v' V' a; B6 A& d
- }contract StandardToken is ERC20 {7 C8 w7 h" I9 @) |
- using SafeMath for uint256;
- mapping(address => uint256) balances;
- mapping (address => mapping (address => uint256)) internal allowed; /**" Z+ J2 d* S# ]$ z9 J* ^! ^
- * Gets the balance of the specified address.
- * @param _owner The address to query the the balance of.; n( q/ K% P$ P+ v9 t% Q, p
- * @return An uint256 representing the amount owned by the passed address. D. ^) V1 g& F
- */- {" g3 _ c9 w+ j' [
- function balanceOf(address _owner) public view returns (uint256)
- { return balances[_owner];9 z' [3 k1 G$ |# G6 C
- } /**
- * 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];# L6 @9 J1 v# S4 m! b8 q
- } /**( T. @. S7 M+ i8 O
- * Transfer token for a specified address8 s# F' F, V5 }: Z3 L' \
- * @param _to The address to transfer to.
- * @param _value The amount to be transferred.
- */: y0 x% o4 |4 C) f ^5 l
- function transfer(address _to, uint256 _value) public returns
- (bool) { require(_value = oldValue) {! v" F$ q+ D0 O' Z' Y
- allowed[msg.sender][_spender] = 0;
- } else {
- allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);# ?- Z/ Z5 O" N W+ ~! S; e6 S
- }
- emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); return true;& r% C& @. x4 ]; G) Z
- } 6 z* c7 l! ]4 U" z
- /**$ ]% y( o7 l4 n
- * Destroy tokens
- *
- * Remove `_value` tokens from the system irreversibly, p" M: d5 N0 W! w# a
- *
- * @param _value the amount of money to burn. ? E8 J2 U. k$ O* t2 L+ b: z7 B
- */- u) v6 ]8 y0 D, x
- function burn(uint256 _value) public returns (bool success)
- { require(balances[msg.sender] >= _value); - q4 j1 O/ W- M- m
- balances[msg.sender] = balances[msg.sender].sub(_value); 9 @' G1 E% J% ]( E9 |
- totalSupply = totalSupply.sub(_value);
- emit Burn(msg.sender, _value); return true;
- } /**7 b1 ?0 C6 }$ k" x' u) _
- * Destroy tokens from other account
- *
- * Remove `_value` tokens from the system irreversibly on behalf of `_from`.1 E" ]0 I |' v4 ]3 `9 c/ u* k
- *- n) p+ \% r0 A: V' |7 ` G
- * @param _from the address of the sender6 M: }7 u5 d, V6 P; a4 c
- * @param _value the amount of money to burn a J9 d# s( O/ ~5 ~" u
- */, U$ ]* ] _& g2 G( h) O5 H
- 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 tokens6 V$ D0 p# ^+ \) Z" H' `$ [1 w
该接口的官网说明在下面的链接:https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md" n! _7 I6 |' u9 }6 O9 O
我们来简单介绍下这个接口规范,规范里面定义了9个方法和2个事件
9个方法包括:
返回token的名字8 S& q& M, h+ [2 v$ u
function name() view returns (string name)6 |- h0 W$ H7 E( ~3 g, V* J+ w1 h1 d
返回token的代码
function symbol() view returns (string symbol)
返回token的小数点位数) l; z8 H5 [6 ]! n' B. j" Y
function decimals() view returns (uint8 decimals)( G+ r, a6 G! {9 w& r% r k) v8 D
返回供应总量
unction totalSupply() view returns (uint256 totalSupply) N$ M2 O! A5 {! @/ [: L
查询某个地址的余额
function balanceOf(address _owner) view returns (uint256 balance)# p& ~! N; Y" W' e7 H$ g* ~: W
给某个地址转账,如果余额不足,该方法必须抛出异常而不是返回false. u' A5 i* K3 G$ { ~' C! N U
function transfer(address _to, uint256 _value) returns (bool success)6 `3 ?% K8 N Z+ g4 i7 N
从from地址转账到to地址,该方法用于委托给其他合约代理你转账
function transferFrom(address _from, address _to, uint256 _value) returns (bool success)0 @. j- N/ b E" m* ]6 Y$ z- Z
允许多次从您的帐户委托到_spender地址,最高_value金额。如果再次调用此函数,则会覆盖当前允许值_value0 S+ k& M" [: ^* V& k) @8 S
function approve(address _spender, uint256 _value) returns (bool success)
返回_spender仍允许退出的金额_owner( j3 @/ \! D( ^, f
function allowance(address _owner, address _spender) view returns (uint256 remaining)
2个事件包括:! ]+ [5 k$ N( _
转移令牌时必须触发,包括零值转移,注意:创建新token的合约应该触发Transfer事件,即从_from地址设置为0x0创建token的时候
event Transfer(address indexed _from, address indexed _to, uint256 _value) n3 ?$ r4 c+ L9 W6 m
委托转账必须触发+ m+ r" M0 J8 a2 t+ j! w7 _/ g
event Approval(address indexed _owner, address indexed _spender, uint256 _value)- R0 y4 d U- P
; o+ v# l! ~! b+ h# e3 m4 s8 C6 h h
二、代码解释
代码包括几部分:$ n) O" K7 v/ m/ {/ R& {& W* T5 `
安全的数学计算库3 E& `( n% F( h& F! G
contract类 ERC20,主要针对ERC20规范定义方法和事件
' x3 _, q0 N/ O0 q7 r
contract类StandardToken继承ERC20,定义具体的方法实现! R# [+ P1 N# C% s3 [% c# s8 y0 g
contract 类MyTokenERC20集成StandardToken,定义名词,代号,总供应量和创建token