Skip to content

Commit 9c47be2

Browse files
committed
Added crowdfund contract
1 parent 8cde1f9 commit 9c47be2

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

crowdfund.sol

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
contract CrowdFund {
2+
struct Funder {
3+
address addr;
4+
uint amount;
5+
}
6+
7+
event FundsReceived(address sender);
8+
event GoalCrossed();
9+
10+
mapping (uint => Funder) funders;
11+
uint public numberOfFunders = 0;
12+
address accountToSendTo;
13+
uint public goalAmount;
14+
uint public deadline;
15+
bool public isOpen = true;
16+
uint public amountReceived = 0;
17+
18+
function CrowdFund(address _accountToSendTo, uint _goalAmount, uint _duration) {
19+
accountToSendTo = _accountToSendTo;
20+
goalAmount = _goalAmount * 1 ether;
21+
deadline = now + _duration * 1 minutes;
22+
}
23+
24+
// This function is called when someone sends money to the contract.
25+
function () {
26+
uint amount = msg.value;
27+
funders[numberOfFunders++] = (Funder({addr: msg.sender, amount: amount}));
28+
amountReceived += amount;
29+
FundsReceived(msg.sender);
30+
if (amountReceived > goalAmount) {
31+
GoalCrossed();
32+
}
33+
}
34+
35+
// Return the status of the goal and received amount
36+
function checkStatus() constant returns (uint goal, uint received) {
37+
goal = goalAmount;
38+
received = amountReceived;
39+
}
40+
41+
function transfer() {
42+
if (now >= deadline) {
43+
if (amountReceived >= goalAmount) {
44+
accountToSendTo.send(amountReceived);
45+
} else {
46+
for (uint i = 0; i < numberOfFunders; i++) {
47+
Funder funder = funders[i];
48+
funder.addr.send(funder.amount);
49+
}
50+
}
51+
isOpen = false;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)