forked from zupzup/solidity-example-crowdfunding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.sol
81 lines (78 loc) · 2.65 KB
/
contract.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
pragma solidity ^0.4.6;
contract WinnerTakesAll {
uint minimumEntryFee;
uint public deadlineProjects;
uint public deadlineCampaign;
uint public winningFunds;
address public winningAddress;
struct Project {
address addr;
string name;
string url;
uint funds;
bool initialized;
}
mapping (address => Project) projects;
address[] public projectAddresses;
uint public numberOfProjects;
event ProjectSubmitted(address addr, string name, string url, bool initialized);
event ProjectSupported(address addr, uint amount);
event PayedOutTo(address addr, uint winningFunds);
function WinnerTakesAll(uint _minimumEntryFee, uint _durationProjects, uint _durationCampaign) public {
if (_durationCampaign <= _durationProjects) {
throw;
}
minimumEntryFee = _minimumEntryFee;
deadlineProjects = now + _durationProjects* 1 seconds;
deadlineCampaign = now + _durationCampaign * 1 seconds;
winningAddress = msg.sender;
winningFunds = 0;
}
function submitProject(string name, string url) payable public returns (bool success) {
if (msg.value < minimumEntryFee) {
throw;
}
if (now > deadlineProjects) {
throw;
}
if (!projects[msg.sender].initialized) {
projects[msg.sender] = Project(msg.sender, name, url, 0, true);
projectAddresses.push(msg.sender);
numberOfProjects = projectAddresses.length;
ProjectSubmitted(msg.sender, name, url, projects[msg.sender].initialized);
return true;
}
return false;
}
function supportProject(address addr) payable public returns (bool success) {
if (msg.value <= 0) {
throw;
}
if (now > deadlineCampaign || now <= deadlineProjects) {
throw;
}
if (!projects[addr].initialized) {
throw;
}
projects[addr].funds += msg.value;
if (projects[addr].funds > winningFunds) {
winningAddress = addr;
winningFunds = projects[addr].funds;
}
ProjectSupported(addr, msg.value);
return true;
}
function getProjectInfo(address addr) public constant returns (string name, string url, uint funds) {
var project = projects[addr];
if (!project.initialized) {
throw;
}
return (project.name, project.url, project.funds);
}
function finish() {
if (now >= deadlineCampaign) {
PayedOutTo(winningAddress, winningFunds);
selfdestruct(winningAddress);
}
}
}