1. 程式人生 > >solidity智慧合約[55]-token

solidity智慧合約[55]-token

數字貨幣

貨幣是用來交換、償還債務的媒介。古代貨幣為金、銀、貝殼等實物。現代中央銀行發行的紙幣等。相對於古代的一般等價物而言
現在的貨幣本質上是由政府信用的背書。其本身並沒有價值。 同理、比特幣與以太幣本身並沒有價值,但是依託於區塊鏈網路的特性,使得其擁有貨幣的完美屬性。包括:
·便攜
·耐用
·可分割
·可識別
·可替代
·稀缺且難以仿冒

數字貨幣引入

如下是一段簡單的代幣程式碼。balanceOf對映表儲存金額。建構函式,定義了初始化發行與管理者。 transfer函式定義了轉賬操作。完成了貨幣儲存與轉移的功能。 本質上,此貨幣就是儲存在balanceOf對映表中的數字。數字本身並沒有價值,只有當你認為他有價值的時候,它才會有價值。所以,數字貨幣的背後,常常是對應與一定的商業價值。例如公司的股權等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
pragma solidity ^0.4.23;


contract tokenDemo{
   mapping(address=>uint) public balanceOf;
   address owner;

   constructor(uint initSupply) public {
       balanceOf[msg.sender] = initSupply;
       owner = msg.sender;

   }

   function transfer(address _to,uint _value) public {
       balanceOf[owner] -= _value;
        balanceOf[_to] += _value;
   }
}

一般代幣合約的缺陷

如上面寫好的簡單代幣demo,有三種缺陷。
1、沒有許可權的控制,任何人都可以呼叫transfer進行轉賬
2、沒有防止溢位***
3、功能有限
4、沒有統一的規範。

想象一下,如果每一個人或企業都寫一個屬於自己的代幣合約。每一個合約都有自己的查詢資金的函式名。都有自己的轉賬的函式名。那麼每一次當我們需要用到其他人的代幣,都需要查詢,此代幣合約中,每一個函式的功能。大大降低了效率。
這就為我們引出了ERC20代幣。

ERC20協議

ERC是以太坊徵求意見( Ethereum Request for Comment-20)的縮寫. 20代表它的序號。其規範了代幣合約,也就意味著,一旦合約滿足了ERC20代幣的規範,那麼其必然有規範的函式標準。如下,就是ERC20代幣協議規定的不同的函式及其功能。ERC20協議只是定義了函式的定義與功能,需要代幣設計者自定義的實現函式功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
interface ERC20Interface {
    //總髮行數量
   function totalSupply()  external returns (uint);
   //查詢數量
   function balanceOf(address tokenOwner)  external returns (uint balance);
   //查詢授權數量
   function allowance(address tokenOwner, address spender)  external returns (uint remaining);
    //轉賬
   function transfer(address to, uint tokens) external  returns (bool success);
   //授權
   function approve(address spender, uint tokens) external returns (bool success);
   //授權轉賬
   function transferFrom(address from, address to, uint tokens) external returns (bool success);

   event Transfer(address indexed from, address indexed to, uint tokens);
   event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}

ERC20代幣實現

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
pragma solidity ^0.4.18;

// ----------------------------------------------------------------------------
// '' token contract
//
// 部署地址 :
// 標誌      : LOVE
// 名字    :  LOVE TOKEN
// 總供應量 100000000000000000000000000
// 精度    : 18

// ---------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// SafeMath安全庫
// ----------------------------------------------------------------------------
contract SafeMath {
   function safeAdd(uint a, uint b) public pure returns (uint c) {
       c = a + b;
       require(c >= a);
   }
   function safeSub(uint a, uint b) public pure returns (uint c) {
       require(b <= a);
       c = a - b;
   }
   function safeMul(uint a, uint b) public pure returns (uint c) {
       c = a * b;
       require(a == 0 || c / a == b);
   }
   function safeDiv(uint a, uint b) public pure returns (uint c) {
       require(b > 0);
       c = a / b;
   }
}


// ----------------------------------------------------------------------------
// ERC20 代幣標準
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
// ----------------------------------------------------------------------------
contract ERC20Interface {
   //總髮行數量
   function totalSupply() public constant returns (uint);
   //查詢數量
   function balanceOf(address tokenOwner) public constant returns (uint balance);
   //查詢授權數量
   function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
    //轉賬
   function transfer(address to, uint tokens) public returns (bool success);
   //授權
   function approve(address spender, uint tokens) public returns (bool success);
   //授權轉賬
   function transferFrom(address from, address to, uint tokens) public returns (bool success);

   event Transfer(address indexed from, address indexed to, uint tokens);
   event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}


// ----------------------------------------------------------------------------
// 所有者合約
// ----------------------------------------------------------------------------
contract Owned {
   address public owner;
   address public newOwner;

   event OwnershipTransferred(address indexed _from, address indexed _to);

   function Owned() public {
       owner = msg.sender;
   }

   modifier onlyOwner {
       require(msg.sender == owner);
       _;
   }

   function transferOwnership(address _newOwner) public onlyOwner {
       newOwner = _newOwner;
   }
   function acceptOwnership() public {
       require(msg.sender == newOwner);
       OwnershipTransferred(owner, newOwner);
       owner = newOwner;
       newOwner = address(0);
   }
}


// ----------------------------------------------------------------------------
// ERC20代幣,增加標誌、名字、精度
// 代幣轉移
// ----------------------------------------------------------------------------
contract LOVEToken is ERC20Interface, Owned, SafeMath {
   string public symbol;
   string public  name;
   uint8 public decimals;
   uint public _totalSupply;

   mapping(address => uint) balances;
   mapping(address => mapping(address => uint)) allowed;


   // ------------------------------------------------------------------------
   // 建構函式
   // ------------------------------------------------------------------------
   function LOVEToken() public {
       symbol = "LOVER";
       name = "LOVER Token";
       decimals = 18;
       _totalSupply = 100000000000000000000000000;

       balances[0x6AFe57C1F589C4744ab9FF4ac8899080695a6f5e] = _totalSupply;

       Transfer(address(0), 0x6AFe57C1F589C4744ab9FF4ac8899080695a6f5e, _totalSupply);
   }


   // ------------------------------------------------------------------------
   // 總供應量
   // ------------------------------------------------------------------------
   function totalSupply() public constant returns (uint) {
       return _totalSupply  - balances[address(0)];
   }


   // ------------------------------------------------------------------------
   // 得到資金的數量
   // ------------------------------------------------------------------------
   function balanceOf(address tokenOwner) public constant returns (uint balance) {
       return balances[tokenOwner];
   }


   // ------------------------------------------------------------------------
   // 轉賬從代幣擁有者的賬戶到其他賬戶
   // - 所有者的賬戶必須有充足的資金去轉賬
   // - 0值的轉賬也是被允許的
   // ------------------------------------------------------------------------
   function transfer(address to, uint tokens) public returns (bool success) {
       balances[msg.sender] = safeSub(balances[msg.sender], tokens);
       balances[to] = safeAdd(balances[to], tokens);
       Transfer(msg.sender, to, tokens);
       return true;
   }


   // ------------------------------------------------------------------------
   // 授權
   // ------------------------------------------------------------------------
   function approve(address spender, uint tokens) public returns (bool success) {
       allowed[msg.sender][spender] = tokens;
       Approval(msg.sender, spender, tokens);
       return true;
   }


   // ------------------------------------------------------------------------
   // 和approve連線在一起
   //
   // The calling account must already have sufficient tokens approve(...)-d
   // for spending from the from account and
   // - From account must have sufficient balance to transfer
   // - Spender must have sufficient allowance to transfer
   // - 0 value transfers are allowed
   // ------------------------------------------------------------------------
   function transferFrom(address from, address to, uint tokens) public returns (bool success) {
       balances[from] = safeSub(balances[from], tokens);
       allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);
       balances[to] = safeAdd(balances[to], tokens);
       Transfer(from, to, tokens);
       return true;
   }


   // ------------------------------------------------------------------------
   // 返回授權數量
   // ------------------------------------------------------------------------
   function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
       return allowed[tokenOwner][spender];
   }


   // ------------------------------------------------------------------------
   // 合約不接受以太幣
   // ------------------------------------------------------------------------
   function () public payable {
       revert();
   }


      // ------------------------------------------------------------------------
     // Owner can transfer out any accidentally sent ERC20 tokens
     //所有者能夠轉移任何ERC20代幣的介面
    // ------------------------------------------------------------------------
   function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {
       return ERC20Interface(tokenAddress).transfer(owner, tokens);
   }
}

image.png