-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy path0xc5cea8292e514405967d958c2325106f2f48da77-PRFT-Proof.sol
515 lines (429 loc) · 16.9 KB
/
0xc5cea8292e514405967d958c2325106f2f48da77-PRFT-Proof.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
pragma solidity ^0.4.15;
library SafeMath {
function mul(uint256 a, uint256 b) internal constant returns (uint256) {
uint256 c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal constant returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
function sub(uint256 a, uint256 b) internal constant returns (uint256) {
assert(b <= a);
return a - b;
}
function add(uint256 a, uint256 b) internal constant returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
contract ApproveAndCallReceiver {
function receiveApproval(address from, uint256 _amount, address _token, bytes _data) public;
}
contract TokenFactoryInterface {
function createCloneToken(
address _parentToken,
uint _snapshotBlock,
string _tokenName,
string _tokenSymbol
) public returns (ProofToken newToken);
}
contract Controllable {
address public controller;
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender account.
*/
function Controllable() public {
controller = msg.sender;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyController() {
require(msg.sender == controller);
_;
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newController The address to transfer ownership to.
*/
function transferControl(address newController) public onlyController {
if (newController != address(0)) {
controller = newController;
}
}
}
contract ProofToken is Controllable {
using SafeMath for uint256;
ProofTokenInterface public parentToken;
TokenFactoryInterface public tokenFactory;
string public name;
string public symbol;
string public version;
uint8 public decimals;
uint256 public parentSnapShotBlock;
uint256 public creationBlock;
bool public transfersEnabled;
bool public masterTransfersEnabled;
address public masterWallet = 0xD8271285C255Ce31b9b25E46ac63619322Af5934;
struct Checkpoint {
uint128 fromBlock;
uint128 value;
}
Checkpoint[] totalSupplyHistory;
mapping(address => Checkpoint[]) balances;
mapping (address => mapping (address => uint)) allowed;
bool public mintingFinished = false;
bool public presaleBalancesLocked = false;
uint256 public constant TOTAL_PRESALE_TOKENS = 112386712924725508802400;
event Mint(address indexed to, uint256 amount);
event MintFinished();
event ClaimedTokens(address indexed _token, address indexed _owner, uint _amount);
event NewCloneToken(address indexed cloneToken);
event Approval(address indexed _owner, address indexed _spender, uint256 _amount);
event Transfer(address indexed from, address indexed to, uint256 value);
function ProofToken(
address _tokenFactory,
address _parentToken,
uint256 _parentSnapShotBlock,
string _tokenName,
string _tokenSymbol
) public {
tokenFactory = TokenFactoryInterface(_tokenFactory);
parentToken = ProofTokenInterface(_parentToken);
parentSnapShotBlock = _parentSnapShotBlock;
name = _tokenName;
symbol = _tokenSymbol;
decimals = 18;
transfersEnabled = false;
masterTransfersEnabled = false;
creationBlock = block.number;
version = '0.1';
}
function() public payable {
revert();
}
/**
* Returns the total Proof token supply at the current block
* @return total supply {uint256}
*/
function totalSupply() public constant returns (uint256) {
return totalSupplyAt(block.number);
}
/**
* Returns the total Proof token supply at the given block number
* @param _blockNumber {uint256}
* @return total supply {uint256}
*/
function totalSupplyAt(uint256 _blockNumber) public constant returns(uint256) {
// These next few lines are used when the totalSupply of the token is
// requested before a check point was ever created for this token, it
// requires that the `parentToken.totalSupplyAt` be queried at the
// genesis block for this token as that contains totalSupply of this
// token at this block number.
if ((totalSupplyHistory.length == 0) || (totalSupplyHistory[0].fromBlock > _blockNumber)) {
if (address(parentToken) != 0) {
return parentToken.totalSupplyAt(min(_blockNumber, parentSnapShotBlock));
} else {
return 0;
}
// This will return the expected totalSupply during normal situations
} else {
return getValueAt(totalSupplyHistory, _blockNumber);
}
}
/**
* Returns the token holder balance at the current block
* @param _owner {address}
* @return balance {uint256}
*/
function balanceOf(address _owner) public constant returns (uint256 balance) {
return balanceOfAt(_owner, block.number);
}
/**
* Returns the token holder balance the the given block number
* @param _owner {address}
* @param _blockNumber {uint256}
* @return balance {uint256}
*/
function balanceOfAt(address _owner, uint256 _blockNumber) public constant returns (uint256) {
// These next few lines are used when the balance of the token is
// requested before a check point was ever created for this token, it
// requires that the `parentToken.balanceOfAt` be queried at the
// genesis block for that token as this contains initial balance of
// this token
if ((balances[_owner].length == 0) || (balances[_owner][0].fromBlock > _blockNumber)) {
if (address(parentToken) != 0) {
return parentToken.balanceOfAt(_owner, min(_blockNumber, parentSnapShotBlock));
} else {
// Has no parent
return 0;
}
// This will return the expected balance during normal situations
} else {
return getValueAt(balances[_owner], _blockNumber);
}
}
/**
* Standard ERC20 transfer tokens function
* @param _to {address}
* @param _amount {uint}
* @return success {bool}
*/
function transfer(address _to, uint256 _amount) public returns (bool success) {
return doTransfer(msg.sender, _to, _amount);
}
/**
* Standard ERC20 transferFrom function
* @param _from {address}
* @param _to {address}
* @param _amount {uint256}
* @return success {bool}
*/
function transferFrom(address _from, address _to, uint256 _amount) public returns (bool success) {
require(allowed[_from][msg.sender] >= _amount);
allowed[_from][msg.sender] -= _amount;
return doTransfer(_from, _to, _amount);
}
/**
* Standard ERC20 approve function
* @param _spender {address}
* @param _amount {uint256}
* @return success {bool}
*/
function approve(address _spender, uint256 _amount) public returns (bool success) {
require(transfersEnabled);
//https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
require((_amount == 0) || (allowed[msg.sender][_spender] == 0));
allowed[msg.sender][_spender] = _amount;
Approval(msg.sender, _spender, _amount);
return true;
}
/**
* Standard ERC20 approve function
* @param _spender {address}
* @param _amount {uint256}
* @return success {bool}
*/
function approveAndCall(address _spender, uint256 _amount, bytes _extraData) public returns (bool success) {
approve(_spender, _amount);
ApproveAndCallReceiver(_spender).receiveApproval(
msg.sender,
_amount,
this,
_extraData
);
return true;
}
/**
* Standard ERC20 allowance function
* @param _owner {address}
* @param _spender {address}
* @return remaining {uint256}
*/
function allowance(address _owner, address _spender) public constant returns (uint256 remaining) {
return allowed[_owner][_spender];
}
/**
* Internal Transfer function - Updates the checkpoint ledger
* @param _from {address}
* @param _to {address}
* @param _amount {uint256}
* @return success {bool}
*/
function doTransfer(address _from, address _to, uint256 _amount) internal returns(bool) {
if (msg.sender != masterWallet) {
require(transfersEnabled);
} else {
require(masterTransfersEnabled);
}
require(_amount > 0);
require(parentSnapShotBlock < block.number);
require((_to != 0) && (_to != address(this)));
// If the amount being transfered is more than the balance of the
// account the transfer returns false
uint256 previousBalanceFrom = balanceOfAt(_from, block.number);
require(previousBalanceFrom >= _amount);
// First update the balance array with the new value for the address
// sending the tokens
updateValueAtNow(balances[_from], previousBalanceFrom - _amount);
// Then update the balance array with the new value for the address
// receiving the tokens
uint256 previousBalanceTo = balanceOfAt(_to, block.number);
require(previousBalanceTo + _amount >= previousBalanceTo); // Check for overflow
updateValueAtNow(balances[_to], previousBalanceTo + _amount);
// An event to make the transfer easy to find on the blockchain
Transfer(_from, _to, _amount);
return true;
}
/**
* Token creation functions - can only be called by the tokensale controller during the tokensale period
* @param _owner {address}
* @param _amount {uint256}
* @return success {bool}
*/
function mint(address _owner, uint256 _amount) public onlyController canMint returns (bool) {
uint256 curTotalSupply = totalSupply();
uint256 previousBalanceTo = balanceOf(_owner);
require(curTotalSupply + _amount >= curTotalSupply); // Check for overflow
require(previousBalanceTo + _amount >= previousBalanceTo); // Check for overflow
updateValueAtNow(totalSupplyHistory, curTotalSupply + _amount);
updateValueAtNow(balances[_owner], previousBalanceTo + _amount);
Transfer(0, _owner, _amount);
return true;
}
modifier canMint() {
require(!mintingFinished);
_;
}
/**
* Import presale balances before the start of the token sale. After importing
* balances, lockPresaleBalances() has to be called to prevent further modification
* of presale balances.
* @param _addresses {address[]} Array of presale addresses
* @param _balances {uint256[]} Array of balances corresponding to presale addresses.
* @return success {bool}
*/
function importPresaleBalances(address[] _addresses, uint256[] _balances) public onlyController returns (bool) {
require(presaleBalancesLocked == false);
for (uint256 i = 0; i < _addresses.length; i++) {
updateValueAtNow(balances[_addresses[i]], _balances[i]);
Transfer(0, _addresses[i], _balances[i]);
}
updateValueAtNow(totalSupplyHistory, TOTAL_PRESALE_TOKENS);
return true;
}
/**
* Lock presale balances after successful presale balance import
* @return A boolean that indicates if the operation was successful.
*/
function lockPresaleBalances() public onlyController returns (bool) {
presaleBalancesLocked = true;
return true;
}
/**
* Lock the minting of Proof Tokens - to be called after the presale
* @return {bool} success
*/
function finishMinting() public onlyController returns (bool) {
mintingFinished = true;
MintFinished();
return true;
}
/**
* Enable or block transfers - to be called in case of emergency
* @param _value {bool}
*/
function enableTransfers(bool _value) public onlyController {
transfersEnabled = _value;
}
/**
* Enable or block transfers - to be called in case of emergency
* @param _value {bool}
*/
function enableMasterTransfers(bool _value) public onlyController {
masterTransfersEnabled = _value;
}
/**
* Internal balance method - gets a certain checkpoint value a a certain _block
* @param _checkpoints {Checkpoint[]} List of checkpoints - supply history or balance history
* @return value {uint256} Value of _checkpoints at _block
*/
function getValueAt(Checkpoint[] storage _checkpoints, uint256 _block) constant internal returns (uint256) {
if (_checkpoints.length == 0)
return 0;
// Shortcut for the actual value
if (_block >= _checkpoints[_checkpoints.length-1].fromBlock)
return _checkpoints[_checkpoints.length-1].value;
if (_block < _checkpoints[0].fromBlock)
return 0;
// Binary search of the value in the array
uint256 min = 0;
uint256 max = _checkpoints.length-1;
while (max > min) {
uint256 mid = (max + min + 1) / 2;
if (_checkpoints[mid].fromBlock<=_block) {
min = mid;
} else {
max = mid-1;
}
}
return _checkpoints[min].value;
}
/**
* Internal update method - updates the checkpoint ledger at the current block
* @param _checkpoints {Checkpoint[]} List of checkpoints - supply history or balance history
* @return value {uint256} Value to add to the checkpoints ledger
*/
function updateValueAtNow(Checkpoint[] storage _checkpoints, uint256 _value) internal {
if ((_checkpoints.length == 0) || (_checkpoints[_checkpoints.length-1].fromBlock < block.number)) {
Checkpoint storage newCheckPoint = _checkpoints[_checkpoints.length++];
newCheckPoint.fromBlock = uint128(block.number);
newCheckPoint.value = uint128(_value);
} else {
Checkpoint storage oldCheckPoint = _checkpoints[_checkpoints.length-1];
oldCheckPoint.value = uint128(_value);
}
}
function min(uint256 a, uint256 b) internal constant returns (uint) {
return a < b ? a : b;
}
/**
* Clones Proof Token at the given snapshot block
* @param _snapshotBlock {uint256}
* @param _name {string} - The cloned token name
* @param _symbol {string} - The cloned token symbol
* @return clonedTokenAddress {address}
*/
function createCloneToken(uint256 _snapshotBlock, string _name, string _symbol) public returns(address) {
if (_snapshotBlock == 0) {
_snapshotBlock = block.number;
}
if (_snapshotBlock > block.number) {
_snapshotBlock = block.number;
}
ProofToken cloneToken = tokenFactory.createCloneToken(
this,
_snapshotBlock,
_name,
_symbol
);
cloneToken.transferControl(msg.sender);
// An event to make the token easy to find on the blockchain
NewCloneToken(address(cloneToken));
return address(cloneToken);
}
}
contract ProofTokenInterface is Controllable {
event Mint(address indexed to, uint256 amount);
event MintFinished();
event ClaimedTokens(address indexed _token, address indexed _owner, uint _amount);
event NewCloneToken(address indexed _cloneToken, uint _snapshotBlock);
event Approval(address indexed _owner, address indexed _spender, uint256 _amount);
event Transfer(address indexed from, address indexed to, uint256 value);
function totalSupply() public constant returns (uint);
function totalSupplyAt(uint _blockNumber) public constant returns(uint);
function balanceOf(address _owner) public constant returns (uint256 balance);
function balanceOfAt(address _owner, uint _blockNumber) public constant returns (uint);
function transfer(address _to, uint256 _amount) public returns (bool success);
function transferFrom(address _from, address _to, uint256 _amount) public returns (bool success);
function approve(address _spender, uint256 _amount) public returns (bool success);
function approveAndCall(address _spender, uint256 _amount, bytes _extraData) public returns (bool success);
function allowance(address _owner, address _spender) public constant returns (uint256 remaining);
function mint(address _owner, uint _amount) public returns (bool);
function importPresaleBalances(address[] _addresses, uint256[] _balances, address _presaleAddress) public returns (bool);
function lockPresaleBalances() public returns (bool);
function finishMinting() public returns (bool);
function enableTransfers(bool _value) public;
function enableMasterTransfers(bool _value) public;
function createCloneToken(uint _snapshotBlock, string _cloneTokenName, string _cloneTokenSymbol) public returns (address);
}
contract ControllerInterface {
function proxyPayment(address _owner) public payable returns(bool);
function onTransfer(address _from, address _to, uint _amount) public returns(bool);
function onApprove(address _owner, address _spender, uint _amount) public returns(bool);
}