Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

13.2. ERC20 Token Solidity 0.4.24

https://theethereum.wiki/w/index.php/ERC20_Token_Standard

ERC20 “描述了實現代幣合約的標準功能”,ERC20 是各個代幣的標準介面。ERC20 代幣僅僅是以太坊代幣的子集。為了充分兼容 ERC20,開發者需要將一組特定的函數整合到他們的智能合約中,以便在高層面能夠執行以下操作:

ERC20 提供的方法

13.2.1. 構造方法

原合約

			
pragma solidity ^0.4.21;

contract NetkillerToken {
  address public owner;
  string public name;
  string public symbol;
  uint public decimals;
  uint256 public totalSupply;

  event Transfer(address indexed from, address indexed to, uint256 value);

  /* This creates an array with all balances */
  mapping (address => uint256) public balanceOf;

  function NetkillerToken(uint256 initialSupply, string tokenName, string tokenSymbol, uint decimalUnits) public {
    owner = msg.sender;
    name = tokenName;
    symbol = tokenSymbol;
    decimals = decimalUnits;
    totalSupply = initialSupply * 10 ** uint256(decimals);
    balanceOf[msg.sender] = totalSupply;
  }

  /* Send coins */
  function transfer(address _to, uint256 _value) public {
    /* Check if the sender has balance and for overflows */
    require(balanceOf[msg.sender] >= _value && balanceOf[_to] + _value >= balanceOf[_to]);

    /* Add and subtract new balances */
    balanceOf[msg.sender] -= _value;
    balanceOf[_to] += _value;

    /* Notify anyone listening that this transfer took place */
    emit Transfer(msg.sender, _to, _value);
  }
}
			
			

新版合約

			
pragma solidity ^0.4.24;

contract NetkillerToken {
  address public owner;
  string public name;
  string public symbol;
  uint public decimals;
  uint256 public totalSupply;

  event Transfer(address indexed from, address indexed to, uint256 value);

  /* This creates an array with all balances */
  mapping (address => uint256) public balanceOf;

  constructor(uint256 initialSupply, string tokenName, string tokenSymbol, uint decimalUnits) public {
    owner = msg.sender;
    name = tokenName;
    symbol = tokenSymbol;
    decimals = decimalUnits;
    totalSupply = initialSupply * 10 ** uint256(decimals);
    balanceOf[msg.sender] = totalSupply;
  }

  /* Send coins */
  function transfer(address _to, uint256 _value) public {
    /* Check if the sender has balance and for overflows */
    require(balanceOf[msg.sender] >= _value && balanceOf[_to] + _value >= balanceOf[_to]);

    /* Add and subtract new balances */
    balanceOf[msg.sender] -= _value;
    balanceOf[_to] += _value;

    /* Notify anyone listening that this transfer took place */
    emit Transfer(msg.sender, _to, _value);
  }
}
			
			

13.2.2. 官方規定 Method 方法

所有的ERC20代幣都是按照下面這些方法來定義的。下面我們講解一下每個方法的作用。

13.2.2.1. name

function name() view public returns (string name)

返回string類型的ERC20代幣的名字,例如:Netkiller Reader Coin

13.2.2.2. symbol

function symbol() view public returns (string symbol)

返回string類型的ERC20代幣的符號,也就是代幣的簡稱,例如:NRC。

13.2.2.3. decimals

function decimals() view public returns (uint decimals)

支持幾位小數點後幾位。如果設置為3。也就是支持0.001表示。

13.2.2.4. totalSupply

function totalSupply() view public returns (uint256 totalSupply)

發行代幣的總量,可以通過這個函數來獲取。所有智能合約發行的代幣總量是一定的,totalSupply必須設置初始值。

13.2.2.5. balanceOf

function balanceOf(address _owner) public returns (uint256 balance)

輸入地址,可以獲取該地址代幣的餘額。

13.2.2.6. transfer

function transfer(address _to, uint256 _value) public returns (bool success)

調用transfer函數將自己的token轉賬給_to地址,_value為轉賬金額

13.2.2.7. approve

function approve(address _spender, uint256 _value) public returns (bool success)

批准_spender賬戶從自己的賬戶轉移_value個token。可以分多次轉移。

13.2.2.8. transferFrom

function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)

與approve搭配使用,approve批准之後,調用transferFrom函數來轉移token。

13.2.2.9. allowance

function allowance(address _owner, address _spender) public returns (uint256 remaining)

返回_spender還能提取token的個數。

approve、transferFrom及allowance解釋:賬戶A有1000個代幣,想允許B賬戶隨意調用100個代幣。A賬戶按照以下形式調用approve函數approve(B,100)。當B賬戶想用這100個代幣中的10個代幣給C賬戶時,則調用transferFrom(A, C, 10)。這時調用allowance(A, B)可以查看B賬戶還能夠調用A賬戶多少個token。

13.2.3. 事件

13.2.3.1. Transfer

event Transfer(address indexed _from, address indexed _to, uint256 _value)

當成功轉移token時,一定要觸發Transfer事件

13.2.3.2. Approval

event Approval(address indexed _owner, address indexed _spender, uint256 _value)

當調用approval函數成功時,一定要觸發Approval事件