废话不少,先上完整代码:
- pragma solidity ^0.4.24;
- library SafeMath { /**( A1 r) Y3 `7 w$ ?
- * @dev Multiplies two numbers, throws on overflow.3 H* U0 s4 g' T2 D/ \) e
- */' o* p+ |& N7 r$ U! k$ |
- function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c)
- { if (_a == 0) { return 0;
- }- H; q, l6 B* t# w7 Z
- c = _a * _b;
- assert(c / _a == _b); return c; i# F- H/ ~& g, f
- } /**
- * Integer division of two numbers, truncating the quotient.4 e0 p7 H1 _, U6 f: \
- */; R( A J& g6 J O# D% G
- function div(uint256 _a, uint256 _b) internal pure returns (uint256) { return _a / _b;
- } /**2 i4 k- `; T5 j7 {) E# L6 w
- * Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).- b. B x* E8 y+ H
- */
- function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
- assert(_b = _a); return c;
- }
- }
- contract ERC20 {
- uint256 public totalSupply; function balanceOf(address _who)
- public view returns (uint256); + h# t8 m4 E& v# w
- function allowance(address _owner, address _spender) public view returns (uint256); : O% z* g+ v+ E3 I1 s O4 ^, W4 z2 G
- 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); I5 r7 ]2 |. J7 t5 P( U
- event Approval(address indexed owner, address indexed spender, uint256 value); ) n0 ~8 J6 R% G2 n; l+ S+ Y2 I ?
- event Burn(address indexed from, uint256 value);7 D; Q @9 `3 N9 `4 g3 m, ^
- }contract StandardToken is ERC20 {+ c4 `& a/ ]7 q/ D) N# ^
- using SafeMath for uint256;$ |" {0 a0 [" J2 m, j* ` X% [
- mapping(address => uint256) balances;8 T; K( Y+ R. m
- 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) 1 t: N: I+ C* d
- { return balances[_owner];2 e1 d8 U& Y- \
- } /**
- * Function to check the amount of tokens that an owner allowed to a spender.8 \9 g, F5 v, v1 a
- * @param _owner address The address which owns the funds. s8 A6 H5 U1 q: R; }. _
- * @param _spender address The address which will spend the funds.4 y1 S* d0 V/ @
- * @return A uint256 specifying the amount of tokens still available for the spender.% m1 L! ^2 M: j
- */! X* X/ ^4 d8 e1 X: F1 ^
- function allowance(address _owner, address _spender) public view returns( u6 Z, I* y# I" \
- (uint256){ return allowed[_owner][_spender];) M' o& t; _5 [* i6 A" ?5 m1 h
- } /**9 \0 E' s7 v( r5 [! g
- * Transfer token for a specified address( L0 W$ B$ [" y! M' E7 Q! f+ b
- * @param _to The address to transfer to.8 P% ^1 X! u; h* }. N; \! U' A
- * @param _value The amount to be transferred.
- */
- function transfer(address _to, uint256 _value) public returns
- (bool) { require(_value = oldValue) {
- allowed[msg.sender][_spender] = 0;" I2 K$ u/ E ]0 m5 S9 u! Q
- } else {
- allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);4 w* h0 @6 ~) V) W8 L) U
- }1 o0 m8 r- O9 P, Z9 s3 {3 H
- emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); return true;7 l9 w: z+ A* m) |) O
- }
- /**
- * Destroy tokens. }6 V% Q: n+ Y- R* b _. M
- *
- * Remove `_value` tokens from the system irreversibly
- *, Y6 p9 X1 t4 Z% N3 f# O0 g
- * @param _value the amount of money to burn
- */
- function burn(uint256 _value) public returns (bool success)
- { require(balances[msg.sender] >= _value); 7 T- @; w% B2 S" Y
- balances[msg.sender] = balances[msg.sender].sub(_value);
- totalSupply = totalSupply.sub(_value);
- emit Burn(msg.sender, _value); return true;
- } /**
- * Destroy tokens from other account
- *
- * Remove `_value` tokens from the system irreversibly on behalf of `_from`.
- *. U5 g4 g" h0 {; i; s, b' t9 h* d
- * @param _from the address of the sender% Y# H! S. T- }: I" x( s( q: f6 l
- * @param _value the amount of money to burn
- */2 l% t& P; a' V/ l8 E% ^
- function burnFrom(address _from, uint256 _value) public returns (bool success) % b2 [$ d' \$ }
- { require(balances[_from] >= _value); 6 U% a* B: d- W! Y
- require(_value
这些代码是什么意思呢?# Y4 g) m8 ?" X
一、ERC20(EIP-20)说明
ERC20是一个标准的token接口规范:A standard interface for tokens
该接口的官网说明在下面的链接:https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md2 L7 J, F& E2 W: X. q+ E
我们来简单介绍下这个接口规范,规范里面定义了9个方法和2个事件
9个方法包括:
返回token的名字; Y, I" @0 P- c6 _9 G
function name() view returns (string name)5 P9 m( ~( i% F$ u; [$ \
返回token的代码
function symbol() view returns (string symbol)6 l4 X1 J2 {- p+ j
返回token的小数点位数5 U. P$ S2 L9 m, h \ g
function decimals() view returns (uint8 decimals)$ }; k- A2 i/ P* B `
返回供应总量
unction totalSupply() view returns (uint256 totalSupply)
查询某个地址的余额4 q7 ]6 {. U+ b
function balanceOf(address _owner) view returns (uint256 balance)3 p9 {4 [9 M2 s1 ~" U: O
给某个地址转账,如果余额不足,该方法必须抛出异常而不是返回false
function transfer(address _to, uint256 _value) returns (bool success)
从from地址转账到to地址,该方法用于委托给其他合约代理你转账
function transferFrom(address _from, address _to, uint256 _value) returns (bool success)
允许多次从您的帐户委托到_spender地址,最高_value金额。如果再次调用此函数,则会覆盖当前允许值_value
function approve(address _spender, uint256 _value) returns (bool success)* t- t' k0 X( d1 b2 t
返回_spender仍允许退出的金额_owner
function allowance(address _owner, address _spender) view returns (uint256 remaining)6 I3 q" u2 {1 N+ Y5 B
2个事件包括:
转移令牌时必须触发,包括零值转移,注意:创建新token的合约应该触发Transfer事件,即从_from地址设置为0x0创建token的时候
event Transfer(address indexed _from, address indexed _to, uint256 _value)
委托转账必须触发
event Approval(address indexed _owner, address indexed _spender, uint256 _value), N; B2 \2 ^) f9 x0 q
: h* O6 u# ~5 y" C- k3 s
7 T- D9 f" {- W/ m3 `7 ]
二、代码解释
代码包括几部分:7 N0 p& F* r% }, O" @7 e+ b
3 L+ B7 v S# g& A, r+ }* e
安全的数学计算库' l5 Z! b M6 b8 n. X
contract类 ERC20,主要针对ERC20规范定义方法和事件/ c+ | L( _! x) J2 I+ a
; N6 x6 M5 R6 \( r& w, d$ n/ @
contract类StandardToken继承ERC20,定义具体的方法实现, ]; T# n( d" n
contract 类MyTokenERC20集成StandardToken,定义名词,代号,总供应量和创建token