solidity智慧合約[55]-token
數字貨幣
貨幣是用來交換、償還債務的媒介。古代貨幣為金、銀、貝殼等實物。現代中央銀行發行的紙幣等。相對於古代的一般等價物而言
現在的貨幣本質上是由政府信用的背書。其本身並沒有價值。 同理、比特幣與以太幣本身並沒有價值,但是依託於區塊鏈網路的特性,使得其擁有貨幣的完美屬性。包括:
·便攜
·耐用
·可分割
·可識別
·可替代
·稀缺且難以仿冒
數字貨幣引入
如下是一段簡單的代幣程式碼。balanceOf對映表儲存金額。建構函式,定義了初始化發行與管理者。 transfer函式定義了轉賬操作。完成了貨幣儲存與轉移的功能。 本質上,此貨幣就是儲存在balanceOf對映表中的數字。數字本身並沒有價值,只有當你認為他有價值的時候,它才會有價值。所以,數字貨幣的背後,常常是對應與一定的商業價值。例如公司的股權等。
1 |
pragma solidity ^0.4.23; |
一般代幣合約的缺陷
如上面寫好的簡單代幣demo,有三種缺陷。
1、沒有許可權的控制,任何人都可以呼叫transfer進行轉賬
2、沒有防止溢位***
3、功能有限
4、沒有統一的規範。
想象一下,如果每一個人或企業都寫一個屬於自己的代幣合約。每一個合約都有自己的查詢資金的函式名。都有自己的轉賬的函式名。那麼每一次當我們需要用到其他人的代幣,都需要查詢,此代幣合約中,每一個函式的功能。大大降低了效率。
這就為我們引出了ERC20代幣。
ERC20協議
ERC是以太坊徵求意見( Ethereum Request for Comment-20)的縮寫. 20代表它的序號。其規範了代幣合約,也就意味著,一旦合約滿足了ERC20代幣的規範,那麼其必然有規範的函式標準。如下,就是ERC20代幣協議規定的不同的函式及其功能。ERC20協議只是定義了函式的定義與功能,需要代幣設計者自定義的實現函式功能。
1 |
interface ERC20Interface { |
ERC20代幣實現
1 |
pragma solidity ^0.4.18; |
本文連結: https://dreamerjonson.com/2018/11/26/solidity-55-token/
版權宣告: 本部落格所有文章除特別宣告外,均採用 CC BY 4.0 CN協議 許可協議。轉載請註明出處!