forked from xilibi2003/jeth1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathico.sol
82 lines (61 loc) · 2.06 KB
/
ico.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
pragma solidity ^0.4.16;
interface token {
function transfer(address receiver, uint amount) external ;
}
contract Ico {
address public beneficiary;
uint public fundingGoal;
uint public amountRaised;
uint public deadline;
uint public price;
token public tokenReward;
mapping(address => uint256) public balanceOf;
bool crowdsaleClosed = false;
event GoalReached(address recipient, uint totalAmountRaised);
event FundTransfer(address backer, uint amount, bool isContribution);
constructor (
uint fundingGoalInEthers,
uint durationInMinutes,
uint etherCostOfEachToken,
address addressOfTokenUsedAsReward
) public {
beneficiary = msg.sender;
fundingGoal = fundingGoalInEthers * 1 ether;
deadline = now + durationInMinutes * 1 minutes;
price = etherCostOfEachToken * 1 ether;
tokenReward = token(addressOfTokenUsedAsReward);
}
function () public payable {
require(!crowdsaleClosed);
uint amount = msg.value; // wei
balanceOf[msg.sender] += amount;
amountRaised += amount;
tokenReward.transfer(msg.sender, amount / price);
emit FundTransfer(msg.sender, amount, true);
}
modifier afterDeadline() {
if (now >= deadline) {
_;
}
}
function checkGoalReached() public afterDeadline {
if (amountRaised >= fundingGoal) {
emit GoalReached(beneficiary, amountRaised);
}
crowdsaleClosed = true;
}
function safeWithdrawal() public afterDeadline {
if (amountRaised < fundingGoal) {
uint amount = balanceOf[msg.sender];
balanceOf[msg.sender] = 0;
if (amount > 0) {
msg.sender.transfer(amount);
emit FundTransfer(msg.sender, amount, false);
}
}
if (fundingGoal <= amountRaised && beneficiary == msg.sender) {
beneficiary.transfer(amountRaised);
emit FundTransfer(beneficiary, amountRaised, false);
}
}
}