-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrowdfund.sol
More file actions
69 lines (60 loc) · 1.7 KB
/
crowdfund.sol
File metadata and controls
69 lines (60 loc) · 1.7 KB
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
pragma solidity >=0.3.0;
contract CrowdFund {
struct Funder {
address addr;
uint amount;
}
event Log(string message);
mapping (uint => Funder) funders;
uint public numberOfFunders = 0;
address public accountToSendTo;
uint public goalAmount;
uint public deadline;
bool public isOpen = true;
uint public amountReceived = 0;
function CrowdFund(address _accountToSendTo, uint _goalAmount, uint _duration) {
accountToSendTo = _accountToSendTo;
goalAmount = _goalAmount * 1 ether;
deadline = now + _duration * 1 minutes;
}
// This function is called when someone sends money to the contract.
function () {
uint amount = msg.value;
funders[numberOfFunders++] = (Funder({addr: msg.sender, amount: amount}));
amountReceived += amount;
Log("Funds Received");
if (amountReceived >= goalAmount) {
Log("Goal Crossed");
}
}
// Return the status of the goal and received amount
function checkStatus() constant returns (uint goal, uint received) {
goal = goalAmount;
received = amountReceived;
}
modifier deadlineCrossed() {
if (now >= deadline)
_;
else
Log("Deadline not reached");
}
function kill() deadlineCrossed() {
transfer();
suicide(accountToSendTo);
}
function transfer() deadlineCrossed() {
if (amountReceived >= goalAmount) {
Log("Goal Crossed Transferring to Beneficiary");
if (!accountToSendTo.send(amountReceived))
throw;
} else {
Log("Goal Not Reached. Refunding Amount");
for (uint i = 0; i < numberOfFunders; i++) {
Funder funder = funders[i];
if (!funder.addr.send(funder.amount))
throw;
}
}
isOpen = false;
}
}