-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFMW.token.sol
316 lines (253 loc) · 11.7 KB
/
FMW.token.sol
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// 定义语言和版本
pragma solidity ^0.4.16;
// 调用人合约
contract owned {
//地址
address public owner;
constructor () public {
owner = msg.sender;
}
//必须是自己
modifier onlyOwner {
require(msg.sender == owner);
_;
}
//转移所有权
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
}
// 定义令牌接收接口
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }
// 合约主要逻辑
contract TokenERC20 {
// Public variables of the token
// 令牌的公共变量
// 令牌的名称
string public name;
// 令牌的标识
string public symbol;
// 18 decimals is the strongly suggested default, avoid changing it
// 强烈建议18位小数
uint8 public decimals = 18;
// 总供应量
uint256 public totalSupply;
// This creates an array with all balances
// 创建一个map保存所有代币持有者的余额
mapping (address => uint256) public balanceOf;
// 地址配额
mapping (address => mapping (address => uint256)) public allowance;
// This generates a public event on the blockchain that will notify clients
// 这将在区块链上生成将通知客户的公共事件
event Transfer(address indexed from, address indexed to, uint256 value);
// This generates a public event on the blockchain that will notify clients
// 这将在区块链上生成将通知客户的公共事件
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
// This notifies clients about the amount burnt
// 通知客户销毁的总量
event Burn(address indexed from, uint256 value);
/**
* Constrctor function
* 构造函数
*
* Initializes contract with initial supply tokens to the creator of the contract
*/
constructor ( uint256 initialSupply, string tokenName, string tokenSymbol ) public {
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens 给令牌创建者所有初始化的数量
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes
}
/**
* Internal transfer, only can be called by this contract
* 内部转账,私有函数,内部调用
*/
function _transfer( address _from, address _to, uint _value ) internal {
// Prevent transfer to 0x0 address. Use burn() instead
// 检查地址格式
require(_to != 0x0);
// Check if the sender has enough
// 检查转账者是否有足够token
require(balanceOf[_from] >= _value);
// Check for overflows
// 检查是否超过最大量
require(balanceOf[_to] + _value > balanceOf[_to]);
// Save this for an assertion in the future
uint previousBalances = balanceOf[_from] + balanceOf[_to];
// Subtract from the sender
// 转出人减少
balanceOf[_from] -= _value;
// Add the same to the recipient
// 转入人增加
balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);
// Asserts are used to use static analysis to find bugs in your code. They should never fail
// 该断言用于使用静态分析来查找代码中的错误,他们永远不应该失败
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}
/**
* Transfer tokens
* 转账
*
* Send `_value` tokens to `_to` from your account
*
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transfer( address _to, uint256 _value ) public returns (bool success) {
//这里注意发送者就是合约调用者
_transfer(msg.sender, _to, _value);
return true;
}
/**
* Transfer tokens from other address
* 从另一个地址转移一定配额的token
*
* Send `_value` tokens to `_to` in behalf of `_from`
*
* @param _from The address of the sender
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transferFrom( address _from, address _to, uint256 _value ) public returns (bool success) {
require(_value <= allowance[_from][msg.sender]); // Check allowance 检查从from地址中转移一定配额的token到to地址
allowance[_from][msg.sender] -= _value; //转入地址的数量减少
_transfer(_from, _to, _value);
return true;
}
/**
* Set allowance for other address
* 设置配额给其他地址
*
* Allows `_spender` to spend no more than `_value` tokens in your behalf
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
*/
function approve( address _spender, uint256 _value) public returns (bool success) {
allowance[msg.sender][_spender] = _value; //调用地址给指定地址一定数量的配额
emit Approval(msg.sender, _spender, _value);
return true;
}
/**
* Set allowance for other address and notify
* 设置配额给其他地址,并且触发
*
* Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
* @param _extraData some extra information to send to the approved contract
*/
function approveAndCall( address _spender, uint256 _value, bytes _extraData ) public returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
}
/**
* Destroy tokens
* 销毁令牌
*
* Remove `_value` tokens from the system irreversibly
*
* @param _value the amount of money to burn
*/
function burn(uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough 检查销毁地址余额
balanceOf[msg.sender] -= _value; // Subtract from the sender 账户里减少
totalSupply -= _value; // Updates totalSupply 总供应量减少
emit Burn(msg.sender, _value); // 销毁
return true;
}
/**
* Destroy tokens from other account
* 从指定账户销毁令牌
*
* Remove `_value` tokens from the system irreversibly on behalf of `_from`.
*
* @param _from the address of the sender 地址
* @param _value the amount of money to burn 数量
*/
function burnFrom(address _from, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value); // Check if the targeted balance is enough 检查余额
require(_value <= allowance[_from][msg.sender]); // Check allowance 检查配额
balanceOf[_from] -= _value; // Subtract from the targeted balance
allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance
totalSupply -= _value; // Update totalSupply 总供应量减少
emit Burn(_from, _value); // 销毁
return true;
}
}
/******************************************/
/* ADVANCED TOKEN STARTS HERE */
/******************************************/
// 高级版本
contract FOMOWINNER is owned, TokenERC20 {
// 销售价格
uint256 public sellPrice;
// 购买价格
uint256 public buyPrice;
// 定义冻结账户
mapping (address => bool) public frozenAccount;
/* This generates a public event on the blockchain that will notify clients */
// 冻结消息通知
event FrozenFunds(address target, bool frozen);
/* Initializes contract with initial supply tokens to the creator of the contract */
// 构造
constructor ( uint256 initialSupply, string tokenName, string tokenSymbol ) TokenERC20(initialSupply, tokenName, tokenSymbol) public {}
/* Internal transfer, only can be called by this contract */
// 转账,内部私有函数
function _transfer( address _from, address _to, uint _value ) internal {
require (_to != 0x0); // Prevent transfer to 0x0 address. Use burn() instead 检查转账地址格式
require (balanceOf[_from] >= _value); // Check if the sender has enough 检查转出地址余额
require (balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows 检查转入金额不能为负
require(!frozenAccount[_from]); // Check if sender is frozen 转出地址不在冻结账户中
require(!frozenAccount[_to]); // Check if recipient is frozen 转入地址不在冻结账户中
balanceOf[_from] -= _value; // Subtract from the spender 转出地址减少
balanceOf[_to] += _value; // Add the same to the recipient 转入地址增加
emit Transfer(_from, _to, _value);
}
/// @notice Create `mintedAmount` tokens and send it to `target`
/// @param target Address to receive the tokens
/// @param mintedAmount the amount of tokens it will receive
/// 蒸发
function mintToken( address target, uint256 mintedAmount ) onlyOwner public {
balanceOf[target] += mintedAmount;
totalSupply += mintedAmount;
emit Transfer(0, this, mintedAmount);
emit Transfer(this, target, mintedAmount);
}
/// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
/// @param target Address to be frozen
/// @param freeze either to freeze it or not
/// 冻结账户
function freezeAccount( address target, bool freeze ) onlyOwner public {
frozenAccount[target] = freeze;
emit FrozenFunds(target, freeze);
}
/// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth
/// @param newSellPrice Price the users can sell to the contract
/// @param newBuyPrice Price users can buy from the contract
/// 设置价格,针对eth
function setPrices( uint256 newSellPrice, uint256 newBuyPrice ) onlyOwner public {
sellPrice = newSellPrice;
buyPrice = newBuyPrice;
}
/// @notice Buy tokens from contract by sending ether
/// 从合约中购买令牌
function buy() payable public {
uint amount = msg.value / buyPrice; // calculates the amount 计算收到的eth能换多少token
_transfer(this, msg.sender, amount); // makes the transfers token转账
}
/// @notice Sell `amount` tokens to contract
/// @param amount amount of tokens to be sold
/// 向合约卖出令牌
function sell(uint256 amount) public {
address myAddress = this;
require(myAddress.balance >= amount * sellPrice); // checks if the contract has enough ether to buy 检查合约地址是否有足够的eth
_transfer(msg.sender, this, amount); // makes the transfers token转账
msg.sender.transfer(amount * sellPrice); // sends ether to the seller. It's important to do this last to avoid recursion attacks 向对方发送eth
}
}