-
Notifications
You must be signed in to change notification settings - Fork 0
/
v. 0.9
621 lines (508 loc) · 22 KB
/
v. 0.9
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
pragma solidity 0.4.18;
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {
address public owner;
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
function Ownable() public {
owner = msg.sender;
}
/**
* @dev revert()s if called by any account other than the owner.
*/
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) onlyOwner public {
if (newOwner != address(0)) {
owner = newOwner;
}
}
}
/**
* Math operations with safety checks
*/
library SafeMath {
function mul256(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function div256(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0); // Solidity automatically revert()s 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 sub256(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a);
return a - b;
}
function add256(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
function max64(uint64 a, uint64 b) internal pure returns (uint64) {
return a >= b ? a : b;
}
function min64(uint64 a, uint64 b) internal pure returns (uint64) {
return a < b ? a : b;
}
function max256(uint256 a, uint256 b) internal pure returns (uint256) {
return a >= b ? a : b;
}
function min256(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
}
/**
* @title ERC20Basic
* @dev Simpler version of ERC20 interface
*/
contract ERC20Basic {
uint256 public totalSupply;
function balanceOf(address who) constant public returns (uint256);
function transfer(address to, uint256 value) public;
event Transfer(address indexed from, address indexed to, uint256 value);
}
/**
* @title ERC20 interface
* @dev ERC20 interface with allowances.
*/
contract ERC20 is ERC20Basic {
function allowance(address owner, address spender) constant public returns (uint256);
function transferFrom(address from, address to, uint256 value) public;
function approve(address spender, uint256 value) public;
event Approval(address indexed owner, address indexed spender, uint256 value);
}
/**
* @title Basic token
* @dev Basic version of StandardToken, with no allowances.
*/
contract BasicToken is ERC20Basic {
using SafeMath for uint256;
mapping(address => uint256) balances;
/**
* @dev Fix for the ERC20 short address attack.
*/
modifier onlyPayloadSize(uint size) {
require(msg.data.length >= size + 4);
_;
}
/**
* @dev transfer token for a specified address
* @param _to The address to transfer to.
* @param _value The amount to be transferred.
*/
function transfer(address _to, uint256 _value) onlyPayloadSize(2 * 32) public {
balances[msg.sender] = balances[msg.sender].sub256(_value);
balances[_to] = balances[_to].add256(_value);
Transfer(msg.sender, _to, _value);
}
/**
* @dev Gets the balance of the specified address.
* @param _owner The address to query the the balance of.
* @return An uint representing the amount owned by the passed address.
*/
function balanceOf(address _owner) constant public returns (uint256 balance) {
return balances[_owner];
}
}
/**
* @title Standard ERC20 token
* @dev Implemantation of the basic standart token.
*/
contract StandardToken is BasicToken, ERC20 {
mapping (address => mapping (address => uint256)) allowed;
/**
* @dev Transfer tokens from one address to another
* @param _from address The address which you want to send tokens from
* @param _to address The address which you want to transfer to
* @param _value uint the amout of tokens to be transfered
*/
function transferFrom(address _from, address _to, uint256 _value) onlyPayloadSize(3 * 32) public {
var _allowance = allowed[_from][msg.sender];
// Check is not needed because sub(_allowance, _value) will already revert() if this condition is not met
// if (_value > _allowance) revert();
balances[_to] = balances[_to].add256(_value);
balances[_from] = balances[_from].sub256(_value);
allowed[_from][msg.sender] = _allowance.sub256(_value);
Transfer(_from, _to, _value);
}
/**
* @dev Aprove the passed address to spend the specified amount of tokens on behalf of msg.sender.
* @param _spender The address which will spend the funds.
* @param _value The amount of tokens to be spent.
*/
function approve(address _spender, uint256 _value) public {
// To change the approve amount you first have to reduce the addresses
// allowance to zero by calling `approve(_spender, 0)` if it is not
// already 0 to mitigate the race condition described here:
// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
if ((_value != 0) && (allowed[msg.sender][_spender] != 0)) revert();
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
}
/**
* @dev Function to check the amount of tokens than an owner allowed to a spender.
* @param _owner address The address which owns the funds.
* @param _spender address The address which will spend the funds.
* @return A uint specifing the amount of tokens still avaible for the spender.
*/
function allowance(address _owner, address _spender) constant public returns (uint256 remaining) {
return allowed[_owner][_spender];
}
}
/**
* @title EcoToken
* @dev The main EcoToken contract
*
*/
contract EcoToken is StandardToken, Ownable{
string public name = "BlockchainEcoToken";
string public symbol = "ETS";
uint public decimals = 8;
event TokenBurned(uint256 value);
function EcoToken() public {
totalSupply = (10 ** 8) * (10 ** decimals);
balances[msg.sender] = totalSupply;
}
/**
* @dev Allows the owner to burn the token
* @param _value number of tokens to be burned.
*/
function burn(uint _value) onlyOwner public {
require(balances[msg.sender] >= _value);
balances[msg.sender] = balances[msg.sender].sub256(_value);
totalSupply = totalSupply.sub256(_value);
TokenBurned(_value);
}
}
/**
* @title EcoInitialTokenSale
* @dev The EcoToken ICO contract
*
*/
contract EcoInitialTokenSale is Ownable {
using SafeMath for uint256;
event LogContribution(address indexed _contributor, uint256 _etherAmount, uint256 _basicTokenAmount, uint256 _timeBonusTokenAmount, uint256 _volumeBonusTokenAmount);
event LogContributionBitcoin(address indexed _contributor, uint256 _bitcoinAmount, uint256 _etherAmount, uint256 _basicTokenAmount, uint256 _timeBonusTokenAmount, uint256 _volumeBonusTokenAmount, uint _contributionDatetime);
event LogOffChainContribution(address indexed _contributor, uint256 _etherAmount, uint256 _tokenAmount);
event LogReferralAward(address indexed _refereeWallet, address indexed _referrerWallet, uint256 _referralBonusAmount);
event LogTokenCollected(address indexed _contributer, uint256 _collectedTokenAmount);
EcoToken constant private token = EcoToken(0x2630331FF97cef5b3913fF2302862318ef980495); // hard coded due to token already deployed
address constant private etherHolderWallet = 0xcfb155AD6BD0239a13fA74969c3C73d6a1aF1495; // hard coded due to deployment for once only
uint256 constant private minContribution = 100 finney;
uint constant private saleStart = 1423804400;
uint constant private saleEnd = 1626498400;
uint constant private etherToTokenConversionRate = 1000;
uint constant private referralAwardPercent = 20;
uint256 constant private maxCollectableToken = 20 * 10 ** 9 * 10 ** 18;
mapping (address => uint256) private referralContribution; // record the referral contribution amount in ether for claiming of referral bonus
mapping (address => uint) private lastContribitionDate; // record the last contribution date/time for valid the referral bonus claiming period
mapping (address => uint256) private collectableToken; // record the token amount to be collected of each contributor
mapping (address => uint8) private clientIdentRejectList; // record a list of contributors who do not pass the client identification process
bool private isCollectTokenStart = false; // flag to indicate if token collection is started
uint256 public totalCollectableToken; // the total amount of token will be colleceted after considering all the contribution and bonus
// ***** private helper functions ***************
/**
* @dev get the current datetime
*/
function getCurrentDatetime() private constant returns (uint) {
return now;
}
/**
* @dev get the current sale day
*/
function getCurrentSaleDay() private saleIsOn view returns (uint) {
return getCurrentDatetime().sub256(saleStart).div256(86400).add256(1);
}
/**
* @dev to get the time bonus Percentage based on the no. of sale day(s)
* @param _days no of sale day to calculate the time bonus
*/
function getTimeBonusPercent(uint _days) private pure returns (uint) {
if (_days <= 45)
return 40;
if (_days <= 30)
return 30;
if (_days <= 15)
return 20;
if (_days <= 1)
return 10;
return 0;
/**
* @dev to get the volumne bonus percentage based on the ether amount contributed
* @param _etherAmount ether amount contributed.
*/
function getVolumeBonusPercent(uint256 _etherAmount) private pure returns (uint) {
if (_etherAmount < 1 ether)
return 0;
if (_etherAmount < 2 ether)
return 35;
if (_etherAmount < 3 ether)
return 40;
if (_etherAmount < 4 ether)
return 45;
if (_etherAmount < 5 ether)
return 50;
if (_etherAmount < 10 ether)
return 55;
if (_etherAmount < 20 ether)
return 60;
if (_etherAmount < 30 ether)
return 65;
if (_etherAmount < 40 ether)
return 70;
if (_etherAmount < 50 ether)
return 75;
if (_etherAmount < 100 ether)
return 80;
if (_etherAmount < 200 ether)
return 90;
if (_etherAmount >= 200 ether)
return 100;
return 0;
}
/**
* @dev to get the time bonus amount given the token amount to be collected from contribution
* @param _tokenAmount token amount to be collected from contribution
*/
function getTimeBonusAmount(uint256 _tokenAmount) private view returns (uint256) {
return _tokenAmount.mul256(getTimeBonusPercent(getCurrentSaleDay())).div256(100);
}
/**
* @dev to get the volume bonus amount given the token amount to be collected from contribution and the ether amount contributed
* @param _tokenAmount token amount to be collected from contribution
* @param _etherAmount ether amount contributed
*/
function getVolumeBonusAmount(uint256 _tokenAmount, uint256 _etherAmount) private pure returns (uint256) {
return _tokenAmount.mul256(getVolumeBonusPercent(_etherAmount)).div256(100);
}
/**
* @dev to get the referral bonus amount given the ether amount contributed
* @param _etherAmount ether amount contributed
*/
function getReferralBonusAmount(uint256 _etherAmount) private pure returns (uint256) {
return _etherAmount.mul256(etherToTokenConversionRate).mul256(referralAwardPercent).div256(100);
}
/**
* @dev to get the basic amount of token to be collected given the ether amount contributed
* @param _etherAmount ether amount contributed
*/
function getBasicTokenAmount(uint256 _etherAmount) private pure returns (uint256) {
return _etherAmount.mul256(etherToTokenConversionRate);
}
// ****** modifiers ************
/**
* @dev modifier to allow contribution only when the sale is ON
*/
modifier saleIsOn() {
require(getCurrentDatetime() >= saleStart && getCurrentDatetime() < saleEnd);
_;
}
/**
* @dev modifier to check if the sale is ended
*/
modifier saleIsEnd() {
require(getCurrentDatetime() >= saleEnd);
_;
}
/**
* @dev modifier to check if token is collectable
*/
modifier tokenIsCollectable() {
require(isCollectTokenStart);
_;
}
/**
* @dev modifier to check if contribution is over the min. contribution amount
*/
modifier overMinContribution(uint256 _etherAmount) {
require(_etherAmount >= minContribution);
_;
}
/**
* @dev modifier to check if max. token pool is not reached
*/
modifier underMaxTokenPool() {
require(maxCollectableToken > totalCollectableToken);
_;
}
// ***** public transactional functions ***************
/**
* @dev called by contributors to record a contribution
*/
function contribute() public payable saleIsOn overMinContribution(msg.value) underMaxTokenPool {
uint256 _basicToken = getBasicTokenAmount(msg.value);
uint256 _timeBonus = getTimeBonusAmount(_basicToken);
uint256 _volumeBonus = getVolumeBonusAmount(_basicToken, msg.value);
uint256 _totalToken = _basicToken.add256(_timeBonus).add256(_volumeBonus);
lastContribitionDate[msg.sender] = getCurrentDatetime();
referralContribution[msg.sender] = referralContribution[msg.sender].add256(msg.value);
collectableToken[msg.sender] = collectableToken[msg.sender].add256(_totalToken);
totalCollectableToken = totalCollectableToken.add256(_totalToken);
assert(etherHolderWallet.send(msg.value));
LogContribution(msg.sender, msg.value, _basicToken, _timeBonus, _volumeBonus);
}
/**
* @dev called by contract owner to record a off chain contribution by Bitcoin. The token collection process is the same as those ether contributors
* @param _bitcoinAmount bitcoin amount contributed
* @param _etherAmount ether equivalent amount contributed
* @param _contributorWallet wallet address of contributor which will be used for token collection
* @param _contributionDatetime date/time of contribution. For calculating time bonus and claiming referral bonus.
*/
function contributeByBitcoin(uint256 _bitcoinAmount, uint256 _etherAmount, address _contributorWallet, uint _contributionDatetime) public overMinContribution(_etherAmount) onlyOwner {
uint256 _basicToken = getBasicTokenAmount(_etherAmount);
uint256 _timeBonus = getTimeBonusAmount(_basicToken);
uint256 _volumeBonus = getVolumeBonusAmount(_basicToken, _etherAmount);
uint256 _totalToken = _basicToken.add256(_timeBonus).add256(_volumeBonus);
if (_contributionDatetime > lastContribitionDate[_contributorWallet])
lastContribitionDate[_contributorWallet] = _contributionDatetime;
referralContribution[_contributorWallet] = referralContribution[_contributorWallet].add256(_etherAmount);
collectableToken[_contributorWallet] = collectableToken[_contributorWallet].add256(_totalToken);
totalCollectableToken = totalCollectableToken.add256(_totalToken);
LogContributionBitcoin(_contributorWallet, _bitcoinAmount, _etherAmount, _basicToken, _timeBonus, _volumeBonus, _contributionDatetime);
}
/**
* @dev called by contract owner to record a off chain contribution by Ether. The token are distributed off chain already. The contributor can only entitle referral bonus through this smart contract
* @param _etherAmount ether equivalent amount contributed
* @param _contributorWallet wallet address of contributor which will be used for referral bonus collection
* @param _tokenAmount amunt of token distributed to the contributor. For reference only in the event log
*/
function recordOffChainContribute(uint256 _etherAmount, address _contributorWallet, uint256 _tokenAmount) public overMinContribution(_etherAmount) onlyOwner {
lastContribitionDate[_contributorWallet] = now;
LogOffChainContribution(_contributorWallet, _etherAmount, _tokenAmount);
}
/**
* @dev called by contributor to claim the referral bonus
* @param _referrerWallet wallet address of referrer. Referrer must also be a contributor
*/
function referral(address _referrerWallet) public {
require (msg.sender != _referrerWallet);
require (referralContribution[msg.sender] > 0);
require (lastContribitionDate[_referrerWallet] > 0);
require (getCurrentDatetime() - lastContribitionDate[msg.sender] <= (4 * 24 * 60 * 60));
uint256 _referralBonus = getReferralBonusAmount(referralContribution[msg.sender]);
referralContribution[msg.sender] = 0;
collectableToken[msg.sender] = collectableToken[msg.sender].add256(_referralBonus);
collectableToken[_referrerWallet] = collectableToken[_referrerWallet].add256(_referralBonus);
totalCollectableToken = totalCollectableToken.add256(_referralBonus).add256(_referralBonus);
LogReferralAward(msg.sender, _referrerWallet, _referralBonus);
}
/**
* @dev called by contract owener to register a list of rejected clients who cannot pass the client identification process. At most 10 clients can be set at a time
* @param client1 wallet address of client #1
* @param client2 wallet address of client #2
* @param client3 wallet address of client #3
* @param client4 wallet address of client #4
* @param client5 wallet address of client #5
* @param client6 wallet address of client #6
* @param client7 wallet address of client #7
* @param client8 wallet address of client #8
* @param client9 wallet address of client #9
* @param client10 wallet address of client #10
* @param _valueToSet 1 - add to reject list, 0 - remove from reject list
*/
function setClientIdentRejectList(
address client1,
address client2,
address client3,
address client4,
address client5,
address client6,
address client7,
address client8,
address client9,
address client10,
uint8 _valueToSet
) public onlyOwner {
if (client1 != address(0))
clientIdentRejectList[client1] = _valueToSet;
if (client2 != address(0))
clientIdentRejectList[client2] = _valueToSet;
if (client3 != address(0))
clientIdentRejectList[client3] = _valueToSet;
if (client4 != address(0))
clientIdentRejectList[client4] = _valueToSet;
if (client5 != address(0))
clientIdentRejectList[client5] = _valueToSet;
if (client6 != address(0))
clientIdentRejectList[client6] = _valueToSet;
if (client7 != address(0))
clientIdentRejectList[client7] = _valueToSet;
if (client8 != address(0))
clientIdentRejectList[client8] = _valueToSet;
if (client9 != address(0))
clientIdentRejectList[client9] = _valueToSet;
if (client10 != address(0))
clientIdentRejectList[client10] = _valueToSet;
}
/**
* @dev called by contract owner to flag to start the token collection process
*/
function setTokenCollectable() public onlyOwner saleIsEnd {
isCollectTokenStart = true;
}
/**
* @dev called by contributor to collect tokens. If they are rejected by the client identification process, error will be thrown
*/
function collectToken() public tokenIsCollectable {
uint256 _collToken = collectableToken[msg.sender];
require(clientIdentRejectList[msg.sender] <= 0);
require(_collToken > 0);
collectableToken[msg.sender] = 0;
token.transfer(msg.sender, _collToken);
LogTokenCollected(msg.sender, _collToken);
}
/**
* @dev Allow owner to transfer out the token left in the contract
* @param _to address to transfer to
* @param _amount amount to transfer
*/
function transferTokenOut(address _to, uint256 _amount) public onlyOwner {
token.transfer(_to, _amount);
}
/**
* @dev Allow owner to transfer out the ether left in the contract
* @param _to address to transfer to
* @param _amount amount to transfer
*/
function transferEtherOut(address _to, uint256 _amount) public onlyOwner {
assert(_to.send(_amount));
}
// ***** public constant functions ***************
/**
* @dev to get the amount of token collectable by any contributor
* @param _contributor contributor to get amont
*/
function collectableTokenOf(address _contributor) public constant returns (uint256) {
return collectableToken[_contributor] ;
}
/**
* @dev to get the amount of token collectable by any contributor
* @param _contributor contributor to get amont
*/
function isClientIdentRejectedOf(address _contributor) public constant returns (uint8) {
return clientIdentRejectList[_contributor];
}
/**
* @dev Fallback function which receives ether and create the appropriate number of tokens for the
* msg.sender.
*/
function() external payable {
contribute();
}
}