天天看點

【Ethereum】以太坊ERC20 Token标準完整說明什麼是ERC20 token參考資料

市面上出現了大量的用ETH做的代币,他們都遵守REC20協定,那麼我們需要知道什麼是REC20協定。

token代表數字資産,具有價值,但是并不是都符合特定的規範。

基于ERC20的貨币更容易互換,并且能夠在Dapps上相同的工作。

新的标準可以讓token更相容,允許其他功能,包括投票标記化。操作更像一個投票操作

Token的持有人可以完全控制資産,遵守ERC20的token可以跟蹤任何人在任何時間擁有多少token.基于eth合約的子貨币,是以容易實施。隻能自己去轉讓。

标準化非常有利,也就意味着這些資産可以用于不同的平台和項目,否則隻能用在特定的場合。

EIP: 20 Title: ERC-20 Token Standard Type: Standard Category: ERC Status: Accepted Created: 2015-11-19

token的接口标準

以下标準允許在智能合約中實施标記的标記API。 該标準提供了轉移token的基本功能,并允許token被準許,以便他們可以由另一個線上第三方使用。

标準接口可以讓Ethereum上的任何令牌被其他應用程式重新使用:從錢包到分散式交換。

注意:調用者必須處理傳回<code>false</code>的<code>returns (bool success)</code>.調用者絕對不能假設傳回<code>false</code>的情況不存在。

傳回這個令牌的名字,比如<code>"MyToken"</code>.

可選 - 這種方法可以用來提高可用性,但接口和其他契約不能指望這些值存在。

傳回令牌的符号,比如<code>HIX</code>.

傳回token使用的小數點後幾位, 比如 <code>8</code>,表示配置設定token數量為<code>100000000</code>

傳回token的總供應量。

傳回位址是<code>_owner</code>的賬戶的賬戶餘額。

轉移<code>_value</code>的token數量到的位址<code>_to</code>,并且必須觸發<code>Transfer</code>事件。 如果<code>_from</code>帳戶餘額沒有足夠的令牌來支出,該函數應該被<code>throw</code>。

建立新令牌的令牌合同應該在建立令牌時将<code>_from</code>位址設定為<code>0x0</code>觸發傳輸事件。

注意 0值的傳輸必須被視為正常傳輸并觸發傳輸事件。

從位址<code>_from</code>發送數量為<code>_value</code>的token到位址<code>_to</code>,必須觸發<code>Transfer</code>事件。

transferFrom方法用于提取工作流,允許合同代您轉移token。這可以用于例如允許合約代您轉讓代币和/或以子貨币收取費用。除了_from帳戶已經通過某種機制故意地授權消息的發送者之外,該函數**應該**throw。

允許<code>_spender</code>多次取回您的帳戶,最高達<code>_value</code>金額。 如果再次調用此函數,它将以<code>_value</code>覆寫目前的餘量。

傳回<code>_spender</code>仍然被允許從<code>_owner</code>提取的金額。

當token被轉移(包括0值),必須被觸發。

當任何成功調用<code>approve(address _spender, uint256 _value)</code>後,必須被觸發。

在Ethereum網絡上部署了大量符合ERC20标準的令牌。 具有不同權衡的各種團隊已經編寫了不同的實施方案:從節省gas到提高安全性。

<a href="https://github.com/ConsenSys/Tokens/blob/master/contracts/StandardToken.sol" target="_blank">https://github.com/ConsenSys/Tokens/blob/master/contracts/StandardToken.sol</a>

<a href="https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/StandardToken.sol" target="_blank">https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/StandardToken.sol</a>

<a href="https://github.com/Giveth/minime/blob/master/contracts/MiniMeToken.sol" target="_blank">https://github.com/Giveth/minime/blob/master/contracts/MiniMeToken.sol</a>

以下是一個接口合同,聲明所需的功能和事件以符合ERC20标準:

大部分Ethereum主要标記符合ERC20标準。

一些令牌包括描述令牌合同的進一步資訊:

以下是令牌合約的一個片段,用于示範令牌合約如何維護Ethereum帳戶的令牌餘額

假設token合約内有兩個持有者

<code>0x1111111111111111111111111111111111111111</code>有100個機關

<code>0x2222222222222222222222222222222222222222</code>有200個機關

那麼這個合約的<code>balances</code>結構就會存儲下面的内容

那麼,<code>balanceOf(...)</code>就會傳回下面的結果

如果<code>0x1111111111111111111111111111111111111111</code>想要轉移10個機關給<code>0x2222222222222222222222222222222222222222</code>,那麼<code>0x1111111111111111111111111111111111111111</code>會執行下面的函數

token合約的<code>transfer(...)</code>方法将會改變<code>balances</code>結構中的資訊

<code>balanceOf(...)</code>調用就會傳回下面的資訊

如果<code>0x1111111111111111111111111111111111111111</code>想要準許<code>0x2222222222222222222222222222222222222222</code>傳輸一些token到<code>0x2222222222222222222222222222222222222222</code>,那麼<code>0x1111111111111111111111111111111111111111</code>會執行下面的函數

然後<code>allowed</code>(這裡官方文檔寫的是approve,很明顯是錯的)結構就會存儲下面的内容

如果<code>0x2222222222222222222222222222222222222222</code>想要晚點轉移token從<code>0x1111111111111111111111111111111111111111</code>到他自己,<code>0x2222222222222222222222222222222222222222</code>将要執行<code>transferFrom(...)</code>函數

<code>balances</code>的資訊就會變成下面的

然後<code>allowed</code>就會變成下面的内容

<code>0x2222222222222222222222222222222222222222</code>仍然可以從<code>0x1111111111111111111111111111111111111111</code>轉移10個機關。

以下是一個樣本令牌合同,固定供應量為1000000機關,最初配置設定給合同所有者:

注意的是,最後的例子中allowed限定的第二維的參數是調用者的轉移數量,而開始的例子是接收者的數量。

<a href="http://themerkle.com/what-is-the-erc20-ethereum-token-standard/" target="_blank">http://themerkle.com/what-is-the-erc20-ethereum-token-standard/</a>

<a href="https://github.com/ethereum/EIPs/pull/610" target="_blank">https://github.com/ethereum/EIPs/pull/610</a>

<a href="https://github.com/ethereum/EIPs/issues/20" target="_blank">https://github.com/ethereum/EIPs/issues/20</a>

<a href="https://theethereum.wiki/w/index.php/ERC20_Token_Standard" target="_blank">https://theethereum.wiki/w/index.php/ERC20_Token_Standard</a>

<a href="https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md" target="_blank">https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md</a>

<a href="https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729" target="_blank">https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729</a>

<a href="https://docs.google.com/document/d/1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh4DYKjA_jp-RLM/edit#heading=h.m9fhqynw2xvt" target="_blank">https://docs.google.com/document/d/1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh4DYKjA_jp-RLM/edit#heading=h.m9fhqynw2xvt</a>

繼續閱讀