知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
https://theethereum.wiki/w/index.php/ERC20_Token_Standard
ERC20 “描述了實現代幣合約的標準功能”,ERC20 是各個代幣的標準介面。ERC20 代幣僅僅是以太坊代幣的子集。為了充分兼容 ERC20,開發者需要將一組特定的函數整合到他們的智能合約中,以便在高層面能夠執行以下操作:
ERC20 提供的方法
獲得代幣總供應量
獲得賬戶餘額
轉讓代幣
批准花費代幣
原合約
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); } }
所有的ERC20代幣都是按照下面這些方法來定義的。下面我們講解一下每個方法的作用。
function name() view public returns (string name)
返回string類型的ERC20代幣的名字,例如:Netkiller Reader Coin
function symbol() view public returns (string symbol)
返回string類型的ERC20代幣的符號,也就是代幣的簡稱,例如:NRC。
function decimals() view public returns (uint decimals)
支持幾位小數點後幾位。如果設置為3。也就是支持0.001表示。
function totalSupply() view public returns (uint256 totalSupply)
發行代幣的總量,可以通過這個函數來獲取。所有智能合約發行的代幣總量是一定的,totalSupply必須設置初始值。
function balanceOf(address _owner) public returns (uint256 balance)
輸入地址,可以獲取該地址代幣的餘額。
function transfer(address _to, uint256 _value) public returns (bool success)
調用transfer函數將自己的token轉賬給_to地址,_value為轉賬金額
function approve(address _spender, uint256 _value) public returns (bool success)
批准_spender賬戶從自己的賬戶轉移_value個token。可以分多次轉移。
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)
與approve搭配使用,approve批准之後,調用transferFrom函數來轉移token。
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。