废话不少,先上完整代码:2 L; h1 ~5 o) U4 ]. ~6 A
- pragma solidity ^0.4.24;: j, a; R: [: s$ ~3 j2 A/ ]; Y- k
- library SafeMath { /**$ q. `( A8 u9 o! ~$ ?( D' [
- * @dev Multiplies two numbers, throws on overflow.' L- J( l G) Z$ W, U
- */
- function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c)" A. R5 s& [7 B: t- A
- { if (_a == 0) { return 0;
- }, p. I7 L. R% i% q7 d( P
- c = _a * _b;, }& B# ~3 B4 W+ e
- assert(c / _a == _b); return c;
- } /**6 D" ]7 L7 O5 e/ |* i& P) K. y0 k
- * Integer division of two numbers, truncating the quotient.
- */
- 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).
- */- U7 X; k+ G! ~3 X0 U9 e \% p
- function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
- assert(_b = _a); return c;
- }
- }4 I9 N2 p) {9 \/ j4 o( {* \: M
- contract ERC20 {
- uint256 public totalSupply; function balanceOf(address _who)
- public view returns (uint256); # k) _: [2 k! x, r' T* j
- 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); # V0 X' x G% l" `' j( j
- event Transfer( address indexed from, address indexed to, uint256 value);
- event Approval(address indexed owner, address indexed spender, uint256 value); 5 N' V2 [# F. H
- event Burn(address indexed from, uint256 value);- F4 s9 l7 h7 [& q# R
- }contract StandardToken is ERC20 {# C3 {( q" j f$ q* Y+ V
- using SafeMath for uint256;; v6 ~" Y. D0 p$ g' v
- mapping(address => uint256) balances;
- mapping (address => mapping (address => uint256)) internal allowed; /**
- * Gets the balance of the specified address.- {+ s) C" T# V
- * @param _owner The address to query the the balance of.# P# A' p- [1 |/ i. z% K- o; p- V
- * @return An uint256 representing the amount owned by the passed address.3 ]/ h( b* s0 {, k. ~) F
- */
- function balanceOf(address _owner) public view returns (uint256)
- { return balances[_owner];. D. ^7 q3 Y( A& h( G+ W$ z) m
- } /**
- * Function to check the amount of tokens that an owner allowed to a spender.+ U7 S& X: }7 C
- * @param _owner address The address which owns the funds.3 r3 ^+ d5 C& A, O
- * @param _spender address The address which will spend the funds.
- * @return A uint256 specifying the amount of tokens still available for the spender.2 I% X$ d$ Q: B0 Q. c
- */
- function allowance(address _owner, address _spender) public view returns
- (uint256){ return allowed[_owner][_spender];
- } /**
- * Transfer token for a specified address% W% s% X0 @; D6 }: M9 Q& L6 b- B; E
- * @param _to The address to transfer to.% \. M2 G( c! p( z4 i/ P
- * @param _value The amount to be transferred.
- */: o6 `( `4 N. P0 O7 @/ I8 Z
- function transfer(address _to, uint256 _value) public returns / f5 G: @( j0 {( O+ C E% H {
- (bool) { require(_value = oldValue) {. O- S, Z' }) o+ ?
- allowed[msg.sender][_spender] = 0;
- } else {
- allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
- }
- emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); return true;
- } 9 C9 r% Y2 J9 A+ J* m
- /**
- * Destroy tokens+ A# w+ i6 w4 f
- *
- * Remove `_value` tokens from the system irreversibly }7 A* h1 r/ x- n
- *# i5 h c4 V; ]9 i+ K$ S4 }
- * @param _value the amount of money to burn9 Y( S, A1 L: d6 R8 ~2 O0 O
- */! @' z, V& w. M8 b3 Y
- function burn(uint256 _value) public returns (bool success)6 W# E Y8 C( y
- { require(balances[msg.sender] >= _value);
- balances[msg.sender] = balances[msg.sender].sub(_value); 3 y Z$ T8 \' F/ X1 ]0 G: x
- totalSupply = totalSupply.sub(_value); 2 B0 R0 A" p8 j2 t$ B" l$ `
- emit Burn(msg.sender, _value); return true;
- } /**
- * Destroy tokens from other account
- *
- * Remove `_value` tokens from the system irreversibly on behalf of `_from`.3 T! {0 R& ?% l1 K
- *5 v2 `* g" N4 b. B: z
- * @param _from the address of the sender% m& s8 \" n4 q/ r2 }- ?
- * @param _value the amount of money to burn3 ]3 r+ o& t& _, C+ b
- */
- function burnFrom(address _from, uint256 _value) public returns (bool success) ; a1 P' w1 M4 Y5 F0 ]; [
- { require(balances[_from] >= _value); # \% N5 i0 M* s; l
- require(_value
这些代码是什么意思呢?8 C$ o7 e* p: M5 x$ P) @
一、ERC20(EIP-20)说明
ERC20是一个标准的token接口规范:A standard interface for tokens
该接口的官网说明在下面的链接:https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md2 ~7 ]2 G. e! K3 f$ k, w
我们来简单介绍下这个接口规范,规范里面定义了9个方法和2个事件2 P# Q4 ^. t+ ~3 `$ H2 I5 c
9个方法包括:
返回token的名字
function name() view returns (string name)1 c7 d' I% e9 `0 B" o# A
返回token的代码
function symbol() view returns (string symbol)- P7 b# [5 Q' ]) R0 T" t
返回token的小数点位数
function decimals() view returns (uint8 decimals)
返回供应总量( i; L t! c! y; G3 c! _ [: L
unction totalSupply() view returns (uint256 totalSupply)1 m3 r. O* ]! t& K K+ g
查询某个地址的余额0 f2 b! J/ d" Z4 d2 @5 H/ }/ `. q
function balanceOf(address _owner) view returns (uint256 balance)0 R3 i) a, G( ^
给某个地址转账,如果余额不足,该方法必须抛出异常而不是返回false) C* D' G6 o9 N! j
function transfer(address _to, uint256 _value) returns (bool success)
从from地址转账到to地址,该方法用于委托给其他合约代理你转账 O2 v2 U: @. z! ]' e
function transferFrom(address _from, address _to, uint256 _value) returns (bool success)3 a: A! l' c" X9 D; k
允许多次从您的帐户委托到_spender地址,最高_value金额。如果再次调用此函数,则会覆盖当前允许值_value# K, r- [; z, m, L
function approve(address _spender, uint256 _value) returns (bool success)
返回_spender仍允许退出的金额_owner" K! \$ l$ p% c3 v5 \" t6 Z! h
function allowance(address _owner, address _spender) view returns (uint256 remaining)
2个事件包括:5 {* N, a& t- z5 ]$ P1 F5 Y I
转移令牌时必须触发,包括零值转移,注意:创建新token的合约应该触发Transfer事件,即从_from地址设置为0x0创建token的时候
event Transfer(address indexed _from, address indexed _to, uint256 _value)
委托转账必须触发3 X$ }; |8 w8 t/ k
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
二、代码解释6 t2 t. t* s! V8 f' o# ^# G8 }
代码包括几部分:4 I( b) L% w' ~# a' B9 Z$ D
: v: M: O6 \6 c3 Y2 F: _
安全的数学计算库
! d. I! b9 Y. A
contract类 ERC20,主要针对ERC20规范定义方法和事件3 u' _9 `! {2 Y' w3 E
8 ]2 N6 Z* H* v4 z# w8 B4 u8 r5 ~
contract类StandardToken继承ERC20,定义具体的方法实现
contract 类MyTokenERC20集成StandardToken,定义名词,代号,总供应量和创建token