废话不少,先上完整代码:% Q, c& z; C% {. M' n
- pragma solidity ^0.4.24;
- library SafeMath { /**% }9 q- r, o" S2 v+ J8 j
- * @dev Multiplies two numbers, throws on overflow.
- */, i% z% ~. H L: V$ Q b
- function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c)+ Q1 X; X; O" `
- { if (_a == 0) { return 0;
- }
- c = _a * _b;
- assert(c / _a == _b); return c;
- } /**9 B4 D- }. n. W. B u7 c0 }
- * Integer division of two numbers, truncating the quotient.
- */' V2 Z% W1 X+ O1 E7 H \, g/ U1 l7 p
- 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).4 O" y+ O* J# f L6 l1 m6 e
- */( F9 Q! p: B) e4 C
- function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {, p- o& b2 _1 a& X$ L& N& b! L
- assert(_b = _a); return c;
- }
- }
- contract ERC20 {, i' A$ M _ N* O7 z
- 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); " q5 ]" }( V) V6 Y6 h
- function approve(address _spender, uint256 _value) public returns (bool);
- function transferFrom(address _from, address _to, uint256 _value) public returns (bool); 4 v0 i0 y2 s1 n) {
- event Transfer( address indexed from, address indexed to, uint256 value); {1 q w+ O% I3 u/ O% k8 L& A
- event Approval(address indexed owner, address indexed spender, uint256 value); 7 g' n$ T& C0 {& l
- event Burn(address indexed from, uint256 value);6 M% Z% q8 ?. h7 B" A# p
- }contract StandardToken is ERC20 {
- using SafeMath for uint256;
- mapping(address => uint256) balances;6 a" T% l! u1 R0 T# \
- mapping (address => mapping (address => uint256)) internal allowed; /**
- * Gets the balance of the specified address.' J j0 D% w9 x6 s+ _9 `9 N
- * @param _owner The address to query the the balance of.
- * @return An uint256 representing the amount owned by the passed address.3 r% T8 ]/ [8 n
- */$ X1 d( s/ |0 [, e9 g$ c3 Q
- function balanceOf(address _owner) public view returns (uint256)
- { return balances[_owner]; b: v. v% g1 N
- } /**
- * Function to check the amount of tokens that an owner allowed to a spender.
- * @param _owner address The address which owns the funds." ]6 q! Y5 l5 {$ f7 ` [6 O
- * @param _spender address The address which will spend the funds.
- * @return A uint256 specifying the amount of tokens still available for the spender.
- */- O f4 w! Y* H
- function allowance(address _owner, address _spender) public view returns
- (uint256){ return allowed[_owner][_spender];
- } /**
- * Transfer token for a specified address
- * @param _to The address to transfer to.
- * @param _value The amount to be transferred.
- */& W/ G! E: K6 S L
- function transfer(address _to, uint256 _value) public returns 9 O" A' `' `- m. }3 q+ T, H8 m* |
- (bool) { require(_value = oldValue) {
- allowed[msg.sender][_spender] = 0;
- } else {8 Q1 J. }8 R8 z# K9 w4 N P! t
- allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
- }- l& \1 y: M# Y6 B8 ~- [
- emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); return true;9 g* m2 X. G( D0 @% [% M. H$ w
- } 4 b2 d5 I7 `4 N- t# K+ U/ ~
- /**
- * Destroy tokens
- *
- * Remove `_value` tokens from the system irreversibly
- *
- * @param _value the amount of money to burn
- */
- function burn(uint256 _value) public returns (bool success)
- { require(balances[msg.sender] >= _value); # Z$ k# Z( f% W; l
- balances[msg.sender] = balances[msg.sender].sub(_value); - n0 O. g4 }- n( P+ y* ]
- totalSupply = totalSupply.sub(_value);
- emit Burn(msg.sender, _value); return true;
- } /**9 v, L1 v* E1 z( ^0 E7 U
- * Destroy tokens from other account
- *
- * Remove `_value` tokens from the system irreversibly on behalf of `_from`.
- *
- * @param _from the address of the sender O( y8 W' P) ^# Q% M% L N
- * @param _value the amount of money to burn: }, _! L4 D; b9 G5 ^7 q$ K% Q
- */
- function burnFrom(address _from, uint256 _value) public returns (bool success) ) O" ^/ N& k- ~' a( L; t u
- { require(balances[_from] >= _value); * y5 w" C/ g) P0 J! W' T
- 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个方法包括:9 i$ |+ I/ `9 o+ B7 C. t
返回token的名字; k& O3 X1 k% v0 D) G
function name() view returns (string name)
返回token的代码
function symbol() view returns (string symbol)& o. q8 U# t0 m( \
返回token的小数点位数, @1 _7 S( R0 Q! V/ _
function decimals() view returns (uint8 decimals)# z* |; H$ u% d
返回供应总量
unction totalSupply() view returns (uint256 totalSupply)
查询某个地址的余额
function balanceOf(address _owner) view returns (uint256 balance)
给某个地址转账,如果余额不足,该方法必须抛出异常而不是返回false
function transfer(address _to, uint256 _value) returns (bool success)" j# L7 ^* i' F
从from地址转账到to地址,该方法用于委托给其他合约代理你转账5 u w: o% x2 }5 L
function transferFrom(address _from, address _to, uint256 _value) returns (bool success)
允许多次从您的帐户委托到_spender地址,最高_value金额。如果再次调用此函数,则会覆盖当前允许值_value
function approve(address _spender, uint256 _value) returns (bool success)- C6 ^& U3 g* ?% s9 j; D
返回_spender仍允许退出的金额_owner2 j/ _ M7 X8 E/ c. s
function allowance(address _owner, address _spender) view returns (uint256 remaining)% o1 l+ r8 X* q% S6 ^9 s ]: {
2个事件包括:
转移令牌时必须触发,包括零值转移,注意:创建新token的合约应该触发Transfer事件,即从_from地址设置为0x0创建token的时候
event Transfer(address indexed _from, address indexed _to, uint256 _value)
委托转账必须触发2 r7 q' c% p6 Z, D) ^1 D1 m$ h
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
1 }# t* r! r4 V' u3 g5 H& M X
二、代码解释, g+ N# a. {" D p# j# d5 V
代码包括几部分:
* M' @( C/ e1 B5 C. t5 Z% U
安全的数学计算库6 ^8 L& X: Q- h& m! a8 `
* i1 Z# I: D: S
contract类 ERC20,主要针对ERC20规范定义方法和事件
! O8 f4 T) U) k7 U
contract类StandardToken继承ERC20,定义具体的方法实现
2 t J$ Q9 j! f8 V. [3 z9 Q
contract 类MyTokenERC20集成StandardToken,定义名词,代号,总供应量和创建token