forked from bitkhabar/Solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArtisTurba_Crowdsale_SourceCode
347 lines (296 loc) · 11.8 KB
/
ArtisTurba_Crowdsale_SourceCode
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
pragma solidity ^0.4.18;
library SafeMath {
// ------------------------------------------------------------------------
// Add a number to another number, checking for overflows
// ------------------------------------------------------------------------
function add(uint a, uint b) internal pure returns (uint) {
uint c = a + b;
assert(c >= a && c >= b);
return c;
}
// ------------------------------------------------------------------------
// Subtract a number from another number, checking for underflows
// ------------------------------------------------------------------------
function sub(uint a, uint b) internal pure returns (uint) {
assert(b <= a);
return a - b;
}
}
contract Owned {
address public owner;
function Owned() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function setOwner(address _newOwner) public onlyOwner {
owner = _newOwner;
}
}
interface token {
function transfer(address receiver, uint amount) public returns (bool success) ;
function balanceOf(address _owner) public constant returns (uint256 balance);
}
contract ArtisTurbaCrowdsale is Owned{
using SafeMath for uint256;
using SafeMath for uint;
struct ContributorData{
bool isActive;
bool isTokenDistributed;
uint contributionAmount; // ETH contribution
uint tokensAmount; // Exchanged ALC amount
}
mapping(address => ContributorData) public contributorList;
mapping(uint => address) contributorIndexes;
uint nextContributorIndex;
uint contributorCount;
address public beneficiary;
uint public fundingLimit;
uint public amountRaised;
uint public remainAmount;
uint public deadline;
uint public exchangeTokenRate;
token public tokenReward;
uint256 public tokenBalance;
bool public crowdsaleClosed = false;
bool public isARTDistributed = false;
// ------------------------------------------------------------------------
// Tranche 1 crowdsale start date and end date
// Start - 23h00
// Tier1 - 23h05
// Tier2 - 23h10
// Tier3 - 23h15
// End - 23h20
// ------------------------------------------------------------------------
uint public constant START_TIME = 1511956800; //29 November 2017 12:00 UTC
uint public constant SECOND_TIER_SALE_START_TIME = 1513166400; //13 December 2017 12:00 UTC
uint public constant THIRD_TIER_SALE_START_TIME = 1514376000; //27 December 2017 12:00 UTC
uint public constant FOURTH_TIER_SALE_START_TIME = 1514980800; //03 January 2018 12:00 UTC
uint public constant END_TIME = 1515585600; //10 Janaury 2018 12:00 UTC
// ------------------------------------------------------------------------
// crowdsale exchange rate
// ------------------------------------------------------------------------
uint public START_RATE = 6000; //50% Bonus
uint public SECOND_TIER_RATE = 5200; //30% Bonus
uint public THIRD_TIER_RATE = 4400; //10% Bonus
uint public FOURTH_RATE = 4000; //0% Bonus
// ------------------------------------------------------------------------
// Funding Goal
// - HARD CAP : 50000 ETH
// ------------------------------------------------------------------------
uint public constant FUNDING_ETH_HARD_CAP = 50000000000000000000000;
// ALC token decimals
uint8 public constant ART_DECIMALS = 8;
uint public constant ART_DECIMALSFACTOR = 10**uint(ART_DECIMALS);
address public constant ART_FOUNDATION_ADDRESS = 0x55BeA1A0335A8Ea56572b8E66f17196290Ca6467;
address public constant ART_CONTRACT_ADDRESS = 0x082E13494f12EBB7206FBf67E22A6E1975A1A669;
event GoalReached(address raisingAddress, uint amountRaised);
event LimitReached(address raisingAddress, uint amountRaised);
event FundTransfer(address backer, uint amount, bool isContribution);
event WithdrawFailed(address raisingAddress, uint amount, bool isContribution);
event FundReturn(address backer, uint amount, bool isContribution);
/**
* Constrctor function
*
* Setup the owner
*/
function ArtisTurbaCrowdsale(
) public {
beneficiary = ART_FOUNDATION_ADDRESS;
fundingLimit = FUNDING_ETH_HARD_CAP;
deadline = END_TIME; // 2018-01-10 12:00:00 UTC
exchangeTokenRate = FOURTH_RATE * ART_DECIMALSFACTOR;
tokenReward = token(ART_CONTRACT_ADDRESS);
contributorCount = 0;
}
/**
* Fallback function
*
* The function without name is the default function that is called whenever anyone sends funds to a contract
*/
function () public payable {
require(!crowdsaleClosed);
require(now >= START_TIME && now < END_TIME);
processTransaction(msg.sender, msg.value);
}
/**
* Process transaction
*/
function processTransaction(address _contributor, uint _amount) internal{
uint contributionEthAmount = _amount;
amountRaised += contributionEthAmount; // add newly received ETH
remainAmount += contributionEthAmount;
// calcualte exchanged token based on exchange rate
if (now >= START_TIME && now < SECOND_TIER_SALE_START_TIME){
exchangeTokenRate = START_RATE * ART_DECIMALSFACTOR;
}
if (now >= SECOND_TIER_SALE_START_TIME && now < THIRD_TIER_SALE_START_TIME){
exchangeTokenRate = SECOND_TIER_RATE * ART_DECIMALSFACTOR;
}
if (now >= THIRD_TIER_SALE_START_TIME && now < FOURTH_TIER_SALE_START_TIME){
exchangeTokenRate = THIRD_TIER_RATE * ART_DECIMALSFACTOR;
}
if (now >= FOURTH_TIER_SALE_START_TIME && now < END_TIME){
exchangeTokenRate = FOURTH_RATE * ART_DECIMALSFACTOR;
}
uint amountArtToken = _amount * exchangeTokenRate / 1 ether;
if (contributorList[_contributor].isActive == false){ // Check if contributor has already contributed
contributorList[_contributor].isActive = true; // Set his activity to true
contributorList[_contributor].contributionAmount = contributionEthAmount; // Set his contribution
contributorList[_contributor].tokensAmount = amountArtToken;
contributorList[_contributor].isTokenDistributed = false;
contributorIndexes[nextContributorIndex] = _contributor; // Set contributors index
nextContributorIndex++;
contributorCount++;
}
else{
contributorList[_contributor].contributionAmount += contributionEthAmount; // Add contribution amount to existing contributor
contributorList[_contributor].tokensAmount += amountArtToken; // log token amount`
}
FundTransfer(msg.sender, contributionEthAmount, true);
if (amountRaised >= fundingLimit){
// close crowdsale because the crowdsale limit is reached
crowdsaleClosed = true;
}
}
modifier afterDeadline() { if (now >= deadline) _; }
modifier afterCrowdsaleClosed() { if (crowdsaleClosed == true || now >= deadline) _; }
/**
* close Crowdsale
*
*/
function closeCrowdSale() public {
require(beneficiary == msg.sender);
if ( beneficiary == msg.sender) {
crowdsaleClosed = true;
}
}
/**
* Check token balance
*
*/
function checkTokenBalance() public {
if ( beneficiary == msg.sender) {
//check current token balance
tokenBalance = tokenReward.balanceOf(address(this));
}
}
/**
* Withdraw the all funds
*
* Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached,
* sends the entire amount to the beneficiary.
*/
function safeWithdrawalAll() public {
if ( beneficiary == msg.sender) {
if (beneficiary.send(amountRaised)) {
FundTransfer(beneficiary, amountRaised, false);
remainAmount = remainAmount - amountRaised;
} else {
WithdrawFailed(beneficiary, amountRaised, false);
//If we fail to send the funds to beneficiary
}
}
}
/**
* Withdraw the funds
*
* Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached,
* sends the entire amount to the beneficiary.
*/
function safeWithdrawalAmount(uint256 withdrawAmount) public {
if (beneficiary == msg.sender) {
if (beneficiary.send(withdrawAmount)) {
FundTransfer(beneficiary, withdrawAmount, false);
remainAmount = remainAmount - withdrawAmount;
} else {
WithdrawFailed(beneficiary, withdrawAmount, false);
//If we fail to send the funds to beneficiary
}
}
}
/**
* Withdraw ART
*
* If there are some remaining ART in the contract
* after all token are distributed the contributor,
* the beneficiary can withdraw the ART in the contract
*
*/
function withdrawART(uint256 tokenAmount) public afterCrowdsaleClosed {
require(beneficiary == msg.sender);
if (isARTDistributed && beneficiary == msg.sender) {
tokenReward.transfer(beneficiary, tokenAmount);
// update token balance
tokenBalance = tokenReward.balanceOf(address(this));
}
}
/**
* Distribute token
*
* Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached,
* distribute token to contributor.
*/
function distributeARTToken() public {
if (beneficiary == msg.sender) { // only ART_FOUNDATION_ADDRESS can distribute the ART
address currentParticipantAddress;
for (uint index = 0; index < contributorCount; index++){
currentParticipantAddress = contributorIndexes[index];
uint amountArtToken = contributorList[currentParticipantAddress].tokensAmount;
if (false == contributorList[currentParticipantAddress].isTokenDistributed){
bool isSuccess = tokenReward.transfer(currentParticipantAddress, amountArtToken);
if (isSuccess){
contributorList[currentParticipantAddress].isTokenDistributed = true;
}
}
}
// check if all ART are distributed
checkIfAllARTDistributed();
// get latest token balance
tokenBalance = tokenReward.balanceOf(address(this));
}
}
/**
* Distribute token by batch
*
* Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached,
* distribute token to contributor.
*/
function distributeARTTokenBatch(uint batchUserCount) public {
if (beneficiary == msg.sender) { // only ART_FOUNDATION_ADDRESS can distribute the ART
address currentParticipantAddress;
uint transferedUserCount = 0;
for (uint index = 0; index < contributorCount && transferedUserCount<batchUserCount; index++){
currentParticipantAddress = contributorIndexes[index];
uint amountArtToken = contributorList[currentParticipantAddress].tokensAmount;
if (false == contributorList[currentParticipantAddress].isTokenDistributed){
bool isSuccess = tokenReward.transfer(currentParticipantAddress, amountArtToken);
transferedUserCount = transferedUserCount + 1;
if (isSuccess){
contributorList[currentParticipantAddress].isTokenDistributed = true;
}
}
}
// check if all ART are distributed
checkIfAllARTDistributed();
// get latest token balance
tokenBalance = tokenReward.balanceOf(address(this));
}
}
/**
* Check if all contributor's token are successfully distributed
*/
function checkIfAllARTDistributed() public {
address currentParticipantAddress;
isARTDistributed = true;
for (uint index = 0; index < contributorCount; index++){
currentParticipantAddress = contributorIndexes[index];
if (false == contributorList[currentParticipantAddress].isTokenDistributed){
isARTDistributed = false;
break;
}
}
}
}