-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathCrowdsale.sol
514 lines (439 loc) · 18.5 KB
/
Crowdsale.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
pragma solidity ^0.4.21;
import './fund/ICrowdsaleFund.sol';
import './fund/ICrowdsaleReservationFund.sol';
import './token/IERC20Token.sol';
import './token/TransferLimitedToken.sol';
import './token/LockedTokens.sol';
import './ownership/Ownable.sol';
import './Pausable.sol';
import './ISimpleCrowdsale.sol';
contract TheAbyssDAICO is Ownable, SafeMath, Pausable, ISimpleCrowdsale {
enum AdditionalBonusState {
Unavailable,
Active,
Applied
}
uint256 public constant ADDITIONAL_BONUS_NUM = 3;
uint256 public constant ADDITIONAL_BONUS_DENOM = 100;
uint256 public constant ETHER_MIN_CONTRIB = 0.2 ether;
uint256 public constant ETHER_MAX_CONTRIB = 20 ether;
uint256 public constant ETHER_MIN_CONTRIB_PRIVATE = 100 ether;
uint256 public constant ETHER_MAX_CONTRIB_PRIVATE = 3000 ether;
uint256 public constant ETHER_MIN_CONTRIB_USA = 0.2 ether;
uint256 public constant ETHER_MAX_CONTRIB_USA = 20 ether;
uint256 public constant SALE_START_TIME = 1524060000; // 18.04.2018 14:00:00 UTC
uint256 public constant SALE_END_TIME = 1526479200; // 16.05.2018 14:00:00 UTC
uint256 public constant BONUS_WINDOW_1_END_TIME = SALE_START_TIME + 2 days;
uint256 public constant BONUS_WINDOW_2_END_TIME = SALE_START_TIME + 7 days;
uint256 public constant BONUS_WINDOW_3_END_TIME = SALE_START_TIME + 14 days;
uint256 public constant BONUS_WINDOW_4_END_TIME = SALE_START_TIME + 21 days;
uint256 public constant MAX_CONTRIB_CHECK_END_TIME = SALE_START_TIME + 1 days;
uint256 public constant BNB_TOKEN_PRICE_NUM = 169;
uint256 public constant BNB_TOKEN_PRICE_DENOM = 1;
uint256 public tokenPriceNum = 0;
uint256 public tokenPriceDenom = 0;
TransferLimitedToken public token;
ICrowdsaleFund public fund;
ICrowdsaleReservationFund public reservationFund;
LockedTokens public lockedTokens;
mapping(address => bool) public whiteList;
mapping(address => bool) public privilegedList;
mapping(address => AdditionalBonusState) public additionalBonusOwnerState;
mapping(address => uint256) public userTotalContributed;
address public bnbTokenWallet;
address public referralTokenWallet;
address public foundationTokenWallet;
address public advisorsTokenWallet;
address public companyTokenWallet;
address public reserveTokenWallet;
address public bountyTokenWallet;
uint256 public totalEtherContributed = 0;
uint256 public rawTokenSupply = 0;
// BNB
IERC20Token public bnbToken;
uint256 public BNB_HARD_CAP = 300000 ether; // 300K BNB
uint256 public BNB_MIN_CONTRIB = 1000 ether; // 1K BNB
mapping(address => uint256) public bnbContributions;
uint256 public totalBNBContributed = 0;
bool public bnbWithdrawEnabled = false;
uint256 public hardCap = 0; // World hard cap will be set right before Token Sale
uint256 public softCap = 0; // World soft cap will be set right before Token Sale
bool public bnbRefundEnabled = false;
event LogContribution(address contributor, uint256 amountWei, uint256 tokenAmount, uint256 tokenBonus, bool additionalBonusApplied, uint256 timestamp);
event ReservationFundContribution(address contributor, uint256 amountWei, uint256 tokensToIssue, uint256 bonusTokensToIssue, uint256 timestamp);
event LogBNBContribution(address contributor, uint256 amountBNB, uint256 tokenAmount, uint256 tokenBonus, bool additionalBonusApplied, uint256 timestamp);
modifier checkContribution() {
require(isValidContribution());
_;
}
modifier checkBNBContribution() {
require(isValidBNBContribution());
_;
}
modifier checkCap() {
require(validateCap());
_;
}
modifier checkTime() {
require(now >= SALE_START_TIME && now <= SALE_END_TIME);
_;
}
function TheAbyssDAICO(
address bnbTokenAddress,
address tokenAddress,
address fundAddress,
address reservationFundAddress,
address _bnbTokenWallet,
address _referralTokenWallet,
address _foundationTokenWallet,
address _advisorsTokenWallet,
address _companyTokenWallet,
address _reserveTokenWallet,
address _bountyTokenWallet,
address _owner
) public
Ownable(_owner)
{
require(tokenAddress != address(0));
bnbToken = IERC20Token(bnbTokenAddress);
token = TransferLimitedToken(tokenAddress);
fund = ICrowdsaleFund(fundAddress);
reservationFund = ICrowdsaleReservationFund(reservationFundAddress);
bnbTokenWallet = _bnbTokenWallet;
referralTokenWallet = _referralTokenWallet;
foundationTokenWallet = _foundationTokenWallet;
advisorsTokenWallet = _advisorsTokenWallet;
companyTokenWallet = _companyTokenWallet;
reserveTokenWallet = _reserveTokenWallet;
bountyTokenWallet = _bountyTokenWallet;
}
/**
* @dev check if address can contribute
*/
function isContributorInLists(address contributor) external view returns(bool) {
return whiteList[contributor] || privilegedList[contributor] || token.limitedWallets(contributor);
}
/**
* @dev check contribution amount and time
*/
function isValidContribution() internal view returns(bool) {
uint256 currentUserContribution = safeAdd(msg.value, userTotalContributed[msg.sender]);
if(whiteList[msg.sender] && msg.value >= ETHER_MIN_CONTRIB) {
if(now <= MAX_CONTRIB_CHECK_END_TIME && currentUserContribution > ETHER_MAX_CONTRIB ) {
return false;
}
return true;
}
if(privilegedList[msg.sender] && msg.value >= ETHER_MIN_CONTRIB_PRIVATE) {
if(now <= MAX_CONTRIB_CHECK_END_TIME && currentUserContribution > ETHER_MAX_CONTRIB_PRIVATE ) {
return false;
}
return true;
}
if(token.limitedWallets(msg.sender) && msg.value >= ETHER_MIN_CONTRIB_USA) {
if(now <= MAX_CONTRIB_CHECK_END_TIME && currentUserContribution > ETHER_MAX_CONTRIB_USA) {
return false;
}
return true;
}
return false;
}
/**
* @dev Check hard cap overflow
*/
function validateCap() internal view returns(bool){
if(msg.value <= safeSub(hardCap, totalEtherContributed)) {
return true;
}
return false;
}
/**
* @dev Set token price once before start of crowdsale
*/
function setTokenPrice(uint256 _tokenPriceNum, uint256 _tokenPriceDenom) public onlyOwner {
require(tokenPriceNum == 0 && tokenPriceDenom == 0);
require(_tokenPriceNum > 0 && _tokenPriceDenom > 0);
tokenPriceNum = _tokenPriceNum;
tokenPriceDenom = _tokenPriceDenom;
}
/**
* @dev Set hard cap.
* @param _hardCap - Hard cap value
*/
function setHardCap(uint256 _hardCap) public onlyOwner {
require(hardCap == 0);
hardCap = _hardCap;
}
/**
* @dev Set soft cap.
* @param _softCap - Soft cap value
*/
function setSoftCap(uint256 _softCap) public onlyOwner {
require(softCap == 0);
softCap = _softCap;
}
/**
* @dev Get soft cap amount
**/
function getSoftCap() external view returns(uint256) {
return softCap;
}
/**
* @dev Check bnb contribution time, amount and hard cap overflow
*/
function isValidBNBContribution() internal view returns(bool) {
if(token.limitedWallets(msg.sender)) {
return false;
}
if(!whiteList[msg.sender] && !privilegedList[msg.sender]) {
return false;
}
uint256 amount = bnbToken.allowance(msg.sender, address(this));
if(amount < BNB_MIN_CONTRIB || safeAdd(totalBNBContributed, amount) > BNB_HARD_CAP) {
return false;
}
return true;
}
/**
* @dev Calc bonus amount by contribution time
*/
function getBonus() internal constant returns (uint256, uint256) {
uint256 numerator = 0;
uint256 denominator = 100;
if(now < BONUS_WINDOW_1_END_TIME) {
numerator = 25;
} else if(now < BONUS_WINDOW_2_END_TIME) {
numerator = 15;
} else if(now < BONUS_WINDOW_3_END_TIME) {
numerator = 10;
} else if(now < BONUS_WINDOW_4_END_TIME) {
numerator = 5;
} else {
numerator = 0;
}
return (numerator, denominator);
}
function addToLists(
address _wallet,
bool isInWhiteList,
bool isInPrivilegedList,
bool isInLimitedList,
bool hasAdditionalBonus
) public onlyOwner {
if(isInWhiteList) {
whiteList[_wallet] = true;
}
if(isInPrivilegedList) {
privilegedList[_wallet] = true;
}
if(isInLimitedList) {
token.addLimitedWalletAddress(_wallet);
}
if(hasAdditionalBonus) {
additionalBonusOwnerState[_wallet] = AdditionalBonusState.Active;
}
if(reservationFund.canCompleteContribution(_wallet)) {
reservationFund.completeContribution(_wallet);
}
}
/**
* @dev Add wallet to whitelist. For contract owner only.
*/
function addToWhiteList(address _wallet) public onlyOwner {
whiteList[_wallet] = true;
}
/**
* @dev Add wallet to additional bonus members. For contract owner only.
*/
function addAdditionalBonusMember(address _wallet) public onlyOwner {
additionalBonusOwnerState[_wallet] = AdditionalBonusState.Active;
}
/**
* @dev Add wallet to privileged list. For contract owner only.
*/
function addToPrivilegedList(address _wallet) public onlyOwner {
privilegedList[_wallet] = true;
}
/**
* @dev Set LockedTokens contract address
*/
function setLockedTokens(address lockedTokensAddress) public onlyOwner {
lockedTokens = LockedTokens(lockedTokensAddress);
}
/**
* @dev Fallback function to receive ether contributions
*/
function () payable public whenNotPaused {
if(whiteList[msg.sender] || privilegedList[msg.sender] || token.limitedWallets(msg.sender)) {
processContribution(msg.sender, msg.value);
} else {
processReservationContribution(msg.sender, msg.value);
}
}
function processReservationContribution(address contributor, uint256 amount) private checkTime checkCap {
require(amount >= ETHER_MIN_CONTRIB);
if(now <= MAX_CONTRIB_CHECK_END_TIME) {
uint256 currentUserContribution = safeAdd(amount, reservationFund.contributionsOf(contributor));
require(currentUserContribution <= ETHER_MAX_CONTRIB);
}
uint256 bonusNum = 0;
uint256 bonusDenom = 100;
(bonusNum, bonusDenom) = getBonus();
uint256 tokenBonusAmount = 0;
uint256 tokenAmount = safeDiv(safeMul(amount, tokenPriceNum), tokenPriceDenom);
if(bonusNum > 0) {
tokenBonusAmount = safeDiv(safeMul(tokenAmount, bonusNum), bonusDenom);
}
reservationFund.processContribution.value(amount)(
contributor,
tokenAmount,
tokenBonusAmount
);
ReservationFundContribution(contributor, amount, tokenAmount, tokenBonusAmount, now);
}
/**
* @dev Process BNB token contribution
* Transfer all amount of tokens approved by sender. Calc bonuses and issue tokens to contributor.
*/
function processBNBContribution() public whenNotPaused checkTime checkBNBContribution {
bool additionalBonusApplied = false;
uint256 bonusNum = 0;
uint256 bonusDenom = 100;
(bonusNum, bonusDenom) = getBonus();
uint256 amountBNB = bnbToken.allowance(msg.sender, address(this));
bnbToken.transferFrom(msg.sender, address(this), amountBNB);
bnbContributions[msg.sender] = safeAdd(bnbContributions[msg.sender], amountBNB);
uint256 tokenBonusAmount = 0;
uint256 tokenAmount = safeDiv(safeMul(amountBNB, BNB_TOKEN_PRICE_NUM), BNB_TOKEN_PRICE_DENOM);
rawTokenSupply = safeAdd(rawTokenSupply, tokenAmount);
if(bonusNum > 0) {
tokenBonusAmount = safeDiv(safeMul(tokenAmount, bonusNum), bonusDenom);
}
if(additionalBonusOwnerState[msg.sender] == AdditionalBonusState.Active) {
additionalBonusOwnerState[msg.sender] = AdditionalBonusState.Applied;
uint256 additionalBonus = safeDiv(safeMul(tokenAmount, ADDITIONAL_BONUS_NUM), ADDITIONAL_BONUS_DENOM);
tokenBonusAmount = safeAdd(tokenBonusAmount, additionalBonus);
additionalBonusApplied = true;
}
uint256 tokenTotalAmount = safeAdd(tokenAmount, tokenBonusAmount);
token.issue(msg.sender, tokenTotalAmount);
totalBNBContributed = safeAdd(totalBNBContributed, amountBNB);
LogBNBContribution(msg.sender, amountBNB, tokenAmount, tokenBonusAmount, additionalBonusApplied, now);
}
/**
* @dev Process ether contribution. Calc bonuses and issue tokens to contributor.
*/
function processContribution(address contributor, uint256 amount) private checkTime checkContribution checkCap {
bool additionalBonusApplied = false;
uint256 bonusNum = 0;
uint256 bonusDenom = 100;
(bonusNum, bonusDenom) = getBonus();
uint256 tokenBonusAmount = 0;
uint256 tokenAmount = safeDiv(safeMul(amount, tokenPriceNum), tokenPriceDenom);
rawTokenSupply = safeAdd(rawTokenSupply, tokenAmount);
if(bonusNum > 0) {
tokenBonusAmount = safeDiv(safeMul(tokenAmount, bonusNum), bonusDenom);
}
if(additionalBonusOwnerState[contributor] == AdditionalBonusState.Active) {
additionalBonusOwnerState[contributor] = AdditionalBonusState.Applied;
uint256 additionalBonus = safeDiv(safeMul(tokenAmount, ADDITIONAL_BONUS_NUM), ADDITIONAL_BONUS_DENOM);
tokenBonusAmount = safeAdd(tokenBonusAmount, additionalBonus);
additionalBonusApplied = true;
}
processPayment(contributor, amount, tokenAmount, tokenBonusAmount, additionalBonusApplied);
}
/**
* @dev Process ether contribution before KYC. Calc bonuses and tokens to issue after KYC.
*/
function processReservationFundContribution(
address contributor,
uint256 tokenAmount,
uint256 tokenBonusAmount
) external payable checkCap {
require(msg.sender == address(reservationFund));
require(msg.value > 0);
rawTokenSupply = safeAdd(rawTokenSupply, tokenAmount);
processPayment(contributor, msg.value, tokenAmount, tokenBonusAmount, false);
}
function processPayment(address contributor, uint256 etherAmount, uint256 tokenAmount, uint256 tokenBonusAmount, bool additionalBonusApplied) internal {
uint256 tokenTotalAmount = safeAdd(tokenAmount, tokenBonusAmount);
token.issue(contributor, tokenTotalAmount);
fund.processContribution.value(etherAmount)(contributor);
totalEtherContributed = safeAdd(totalEtherContributed, etherAmount);
userTotalContributed[contributor] = safeAdd(userTotalContributed[contributor], etherAmount);
LogContribution(contributor, etherAmount, tokenAmount, tokenBonusAmount, additionalBonusApplied, now);
}
/**
* @dev Force crowdsale refund
*/
function forceCrowdsaleRefund() public onlyOwner {
pause();
fund.enableCrowdsaleRefund();
reservationFund.onCrowdsaleEnd();
token.finishIssuance();
bnbRefundEnabled = true;
}
/**
* @dev Finalize crowdsale if we reached hard cap or current time > SALE_END_TIME
*/
function finalizeCrowdsale() public onlyOwner {
if(
totalEtherContributed >= safeSub(hardCap, 1000 ether) ||
(now >= SALE_END_TIME && totalEtherContributed >= softCap)
) {
fund.onCrowdsaleEnd();
reservationFund.onCrowdsaleEnd();
bnbWithdrawEnabled = true;
// Referral
uint256 referralTokenAmount = safeDiv(rawTokenSupply, 10);
token.issue(referralTokenWallet, referralTokenAmount);
// Foundation
uint256 foundationTokenAmount = safeDiv(token.totalSupply(), 2); // 20%
token.issue(address(lockedTokens), foundationTokenAmount);
lockedTokens.addTokens(foundationTokenWallet, foundationTokenAmount, now + 365 days);
uint256 suppliedTokenAmount = token.totalSupply();
// Reserve
uint256 reservedTokenAmount = safeDiv(safeMul(suppliedTokenAmount, 3), 10); // 18%
token.issue(address(lockedTokens), reservedTokenAmount);
lockedTokens.addTokens(reserveTokenWallet, reservedTokenAmount, now + 183 days);
// Advisors
uint256 advisorsTokenAmount = safeDiv(suppliedTokenAmount, 10); // 6%
token.issue(advisorsTokenWallet, advisorsTokenAmount);
// Company
uint256 companyTokenAmount = safeDiv(suppliedTokenAmount, 4); // 15%
token.issue(address(lockedTokens), companyTokenAmount);
lockedTokens.addTokens(companyTokenWallet, companyTokenAmount, now + 730 days);
// Bounty
uint256 bountyTokenAmount = safeDiv(suppliedTokenAmount, 60); // 1%
token.issue(bountyTokenWallet, bountyTokenAmount);
token.finishIssuance();
} else if(now >= SALE_END_TIME) {
// Enable fund`s crowdsale refund if soft cap is not reached
fund.enableCrowdsaleRefund();
reservationFund.onCrowdsaleEnd();
token.finishIssuance();
bnbRefundEnabled = true;
}
}
/**
* @dev Withdraw bnb after crowdsale if crowdsale is not in refund mode
*/
function withdrawBNB() public onlyOwner {
require(bnbWithdrawEnabled);
// BNB transfer
if(bnbToken.balanceOf(address(this)) > 0) {
bnbToken.transfer(bnbTokenWallet, bnbToken.balanceOf(address(this)));
}
}
/**
* @dev Function is called by contributor to refund BNB token payments if crowdsale failed to reach soft cap
*/
function refundBNBContributor() public {
require(bnbRefundEnabled);
require(bnbContributions[msg.sender] > 0);
uint256 amount = bnbContributions[msg.sender];
bnbContributions[msg.sender] = 0;
bnbToken.transfer(msg.sender, amount);
token.destroy(msg.sender, token.balanceOf(msg.sender));
}
}