/
TokenERC20.sol
292 lines (253 loc) · 10.3 KB
/
TokenERC20.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
pragma solidity ^0.4.21;
import "./SafeMath.sol";
contract Ownable {
address internal owner;
/* you have to use this contract to be inherited because it is internal.*/
constructor() internal {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
}
contract Pausable is Ownable {
event Pause();
event Unpause();
bool internal paused;
modifier whenNotPaused() {
require(!paused);
_;
}
modifier whenPaused() {
require(paused);
_;
}
modifier noReentrancy() {
require(!paused);
paused = true;
_;
paused = false;
}
/* When you discover your smart contract is under attack, you can buy time to upgrade the contract by
immediately pausing the contract.
*/
function pause() public onlyOwner whenNotPaused {
paused = true;
emit Pause();
}
function unpause() public onlyOwner whenPaused {
paused = false;
emit Unpause();
}
}
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }
/**
* NemoLab ERC20 Token
* Written by Shin HyunJae
* version 12
*/
contract TokenERC20 is Pausable {
using SafeMath for uint256;
// Public variables of the token
string public name;
string public symbol;
uint8 public decimals = 18; // 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;
/* This creates an array with all balances */
mapping (address => uint256) public balances;
mapping (address => mapping (address => uint256)) public allowed;
mapping (address => bool) public frozenAccount;
// This generates a public event on the blockchain that will notify clients
event Transfer(address indexed from, address indexed to, uint256 value);
event LastBalance(address indexed account, uint256 value);
// This notifies clients about the allowance of balance
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
// This notifies clients about the amount burnt
event Burn(address indexed from, uint256 value);
// This notifies clients about the amount resupplied
event Resupply(address indexed from, uint256 value);
// This notifies clients about the freezing address
event FrozenFunds(address target, bool frozen);
/**
* Constructor function
*
* Initializes contract with initial supply tokens to the creator of the contract
*/
constructor(string tokenName, string tokenSymbol, uint256 initialSupply) public {
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes
totalSupply = convertToDecimalUnits(initialSupply); // Update total supply with the decimal amount
balances[msg.sender] = totalSupply; // Give the creator all initial tokens
emit Transfer(address(this), msg.sender, totalSupply);
emit LastBalance(address(this), 0);
emit LastBalance(msg.sender, totalSupply);
}
/**
* Convert tokens units to token decimal units
*
* @param _value Tokens units without decimal units
*/
function convertToDecimalUnits(uint256 _value) internal view returns (uint256 value) {
value = _value.mul(10 ** uint256(decimals));
return value;
}
/**
* Get tokens balance
*
* @notice Query tokens balance of the _account
*
* @param _account Account address to query tokens balance
*/
function balanceOf(address _account) external view returns (uint256 balance) {
balance = balances[_account];
return balance;
}
/**
* Get allowed tokens balance
*
* @notice Query tokens balance allowed to _spender
*
* @param _owner Owner address to query tokens balance
* @param _spender The address allowed tokens balance
*/
function allowance(address _owner, address _spender) external view returns (uint256 remaining) {
remaining = allowed[_owner][_spender];
return remaining;
}
/**
* Internal transfer, only can be called by this contract
*/
function _transfer(address _from, address _to, uint256 _value) internal {
require(_to != 0x0); // Prevent transfer to 0x0 address. Use burn() instead
require(balances[_from] >= _value); // Check if the sender has enough
require(!frozenAccount[_from]); // Check if sender is frozen
require(!frozenAccount[_to]); // Check if recipient is frozen
uint256 previousBalances = balances[_from].add(balances[_to]); // Save this for an assertion in the future
balances[_from] = balances[_from].sub(_value); // Subtract from the sender
balances[_to] = balances[_to].add(_value); // Add the same to the recipient
emit Transfer(_from, _to, _value);
emit LastBalance(_from, balances[_from]);
emit LastBalance(_to, balances[_to]);
// Asserts are used to use static analysis to find bugs in your code. They should never fail
assert(balances[_from] + balances[_to] == previousBalances);
}
/**
* Transfer tokens
*
* @notice 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 noReentrancy returns (bool success) {
_transfer(msg.sender, _to, _value);
success = true;
return success;
}
/**
* Transfer tokens from other address
*
* Send `_value` tokens to `_to` on 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 noReentrancy returns (bool success) {
require(_value <= allowed[_from][msg.sender]); // Check allowance
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
_transfer(_from, _to, _value);
success = true;
return success;
}
/**
* Internal approve, only can be called by this contract
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
*/
function _approve(address _spender, uint256 _value) internal returns (bool success) {
allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
success = true;
return success;
}
/**
* Set allowance for other address
*
* Allows `_spender` to spend no more than `_value` tokens on your behalf
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
*/
function approve(address _spender, uint256 _value) public noReentrancy returns (bool success) {
success = _approve(_spender, _value);
return success;
}
/**
* Set allowance for other address and notify
*
* @notice Allows `_spender` to spend no more than `_value` tokens on 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 noReentrancy returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (_approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value, this, _extraData);
success = true;
return success;
}
}
/**
* Destroy tokens
*
* Remove `_value` tokens from the system irreversibly
*
* @param _value the amount of money to burn
*/
function burn(uint256 _value) onlyOwner public returns (bool success) {
require(balances[msg.sender] >= _value); // Check if the sender has enough
balances[msg.sender] = balances[msg.sender].sub(_value); // Subtract from the sender
totalSupply = totalSupply.sub(_value); // Updates totalSupply
emit Burn(msg.sender, _value);
success = true;
return success;
}
/**
* Destroy tokens from other account
*
* @notice 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) onlyOwner public returns (bool success) {
require(balances[_from] >= _value); // Check if the targeted balance is enough
require(allowed[_from][msg.sender] >= _value); // Check allowance
balances[_from] = balances[_from].sub(_value); // Subtract from the targeted balance
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value); // Subtract from the sender's allowance
totalSupply = totalSupply.sub(_value); // Update totalSupply
emit Burn(_from, _value);
success = true;
return success;
}
/// @notice `freeze? Prevent` `target` from sending & receiving tokens
/// @param target Address to be frozen
function freezeAccount(address target) onlyOwner public {
frozenAccount[target] = true;
emit FrozenFunds(target, true);
}
/// @notice `freeze? Allow` `target` from sending & receiving tokens
/// @param target Address to be unfrozen
function unfreezeAccount(address target) onlyOwner public {
frozenAccount[target] = false;
emit FrozenFunds(target, false);
}
}