-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrowdFund.sol
111 lines (89 loc) · 3.48 KB
/
CrowdFund.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
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
pragma solidity ^0.4.17;
contract CrowdfundingFactory {
address[] private liveCampaigns;
function createRegistration(uint minimum) public {
address registration = new Crowdfunding(minimum);
liveCampaigns.push(registration);
}
function getRegistrations() public view returns (address[]) {
return liveCampaigns;
}
}
contract Crowdfunding {
modifier owner() {
require(msg.sender == manager);
_;
}
struct PatientDetails {
string PatientName;
uint RequiredMoney;
address recipient;
bool complete;
uint approvalCount;
mapping(address => bool) approvals;
}
address public manager;
uint public approversCount;
uint public minimumContribution;
mapping(address => bool) public approvers;
PatientDetails[] public requests;
uint currentbalance = 0;
// contstructor function setting minimum amount to contribute
constructor(uint minimum) public {
manager = msg.sender;
minimumContribution = minimum;
}
// if you're looking to donate, this is your call
function contribute() public payable {
require(msg.value > minimumContribution);
approvers[msg.sender] = true;
approversCount++;
}
uint256 public numPatients;
mapping (uint256 => PatientDetails) public patient;
// request has some basic fields describing what is owner trying to purchase:
// "ex new laptops for developers", how much is he asking for, and who is the recipient
function requestMoney(string memory PatientName, uint256 value, address recipient) public {
PatientDetails storage newPatient = patient[++numPatients];
newPatient.PatientName = PatientName;
newPatient.RequiredMoney = value;
newPatient.recipient = recipient;
newPatient.complete = false;
newPatient.approvalCount = 0;
requests.push(newPatient);
}
// this is a way for a contributor to inspect the request and say
// "Yes, you can spend my money towards that!"
function approveRequest(uint requestId) public {
PatientDetails storage request = patient[requestId];
// check if approver has already voted
require(!request.approvals[msg.sender]);
request.approvals[msg.sender] = true;
request.approvalCount++;
if(request.approvalCount > (approversCount / 2)){
finalizeRequest(requestId);
}
}
// When there are enough votes, the owner can trigger the
// finalization of the request, and proceed with his work
function finalizeRequest(uint requestId) public payable {
PatientDetails storage request = patient[requestId];
require(!request.complete);
require(request.approvalCount > (approversCount / 2));
request.recipient.call(request.RequiredMoney);
request.complete = true;
}
// Returns all info about the campaign, useful for displaying
// the data for frontend application
function getAllDetails() public view returns (
uint, uint, uint, uint, address
) {
return (
minimumContribution,
address(this).balance,
requests.length,
approversCount,
manager
);
}
}