区块链100讲:ERC20 中文版

image

1

摘要

ERC20 中文版简单总结即:代币的标准接口。git

下面的标准容许在智能合约中代币的标准API的实现。 该标准提供了转帐代币的基本功能,并容许批准代币,以便其余链上第三方可使用这些代币。github

2

动机

标准接口容许以太坊上的任一代币能够被其余应用程序重用:从钱包转到去中心化的交易所。安全

3

规范

代币微信

方法网络

注意: 调用者必须处理 returns (bool success) 返回的 false 。调用者必定不能假设从不返回 false !app

name区块链

返回代币的名字 - 好比 "MyToken" 。Returns the name of the token - e.g. "MyToken".ui

可选的 - 该方法能够用来改善可用性, 但接口及其余合约必定不能指望这些值存在。(译者注:即不能假设 name 必定能够返回代币名字)token

function name() view returns (string name)接口

symbol

返回代币的标识符。 如 “HIX”。

可选的 - 该方法能够用来改善可用性, 但接口及其余合约必定不能指望这些值存在。(译者注:即不能假设 name 必定能够返回代币名字)

function symbol() view returns (string symbol)

decimals

返回代币使用的小数点位数 - 如 8 ,意思是代币数量除以 100000000 以获得表明用户的最小单位。

可选的 - 该方法能够用来改善可用性, 但接口及其余合约必定不能指望这些值存在。(译者注:即不能假设 name 必定能够返回代币名字)

function decimals() view returns (uint8 decimals)

totalSupply

返回所有的代币供应量。Returns the total token supply.

function totalSupply() view returns (uint256 totalSupply)

balanceOf

返回 _owner 地址的帐户余额。

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

transfer

转帐 _value 数量的代币给地址 _to , 且必定会触发 Transfer 事件。 若是 _from 帐户余额不足,则该方法应该 throw 。

注意 值为0的转帐必须当作正常转帐处理且触发 Transfer 事件。

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

transferFrom

从 _from 地址转帐 _value 给地址 _to ,且必须触发 Transfer 事件。

transferFrom 方法用于取款工做流,容许合约表明你来转帐代币。 好比这能够用于容许合约代币你来转帐代币,或以子货币来收取费用。 若是 _from 帐户没有有意的经过某种机制受权消息的发送者,则该方法应该 throw 。

注意 值为0的转帐必须当作正常转帐处理且触发 Transfer 事件。

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

approve

容许 _spender 从你的帐户屡次取款,最大额度为 _value 。若是该方法再次调用,会用 _value 从新当前的额度。

注意:为了防止攻击向量,如这个这里所述 以及 这里讨论,客户应该确保建立用户接口,为相同花费者设置其余值的最大额度前,首先设置当前额度为 0 。 尽管合约自己不该该强制如此,然而这是为了容许向后兼容以前部署的合同。

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

allowance

返回 _spender 还被容许从 _owner 提款的额度。

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

Events

Transfer

代币转帐时必须触发,包括价值为0的转帐。

建立新代币的合约,在代币建立时应该触发 Transfer 事件,并将 _from 地址设为 0x0 。

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

Approval

任何成功的调用 approve(address _spender, uint256 _value) 都必须触发该事件。

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

4

实现

已经有许多 ERC20 兼容的代币部署在以太坊网络上。 不一样的团队编写了不一样的实现,这些团队有不一样的权衡:从节约 gas 到提升安全性。

可用的示例实现以下:

https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/ERC20/StandardToken.sol

https://github.com/ConsenSys/Tokens/blob/master/contracts/eip20/EIP20.sol

再次调用"approve"前增长强制设为0的实现:

https://github.com/Giveth/minime/blob/master/contracts/MiniMeToken.sol

5

历史

与该标准有关的历史连接:

**Vitalik Buterin的原始提议: **

https://github.com/ethereum/wiki/wiki/Standardized_Contract_APIs/499c882f3ec123537fc2fccd57eaa29e6032fe4a

**Reddit discussion: **

https://www.reddit.com/r/ethereum/comments/3n8fkn/lets_talk_about_the_coin_standard/

**Original Issue #20: **

https://github.com/ethereum/EIPs/issues/20

本文做者:HiBlock区块链技术布道群-Bob

原文发布于GitHub

原文地址:

https://github.com/bobjiang/EIPs/blob/master/EIPS-CN/eip-20.md

加微信baobaotalk_com,加入技术布道群

线下活动推荐

技术工坊|解密区块链DApp的代码逻辑,从请求到数据存储都要经历什么?(上海)

技术沙龙|利用IPFS,去中心化存储如何让钱包更安全?(南京)

image

image

相关文章
相关标签/搜索