forked from owanhunte/ethereum-solidity-course-updated-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Campaign.sol
108 lines (93 loc) · 2.93 KB
/
Campaign.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
pragma solidity >=0.5.0 <0.7.0;
contract CampaignFactory {
Campaign[] public deployedCampaigns;
function createCampaign(uint minimum) public {
Campaign newCampaign = new Campaign(minimum, msg.sender);
deployedCampaigns.push(newCampaign);
}
function getDeployedCampaigns() public view returns(Campaign[] memory) {
return deployedCampaigns;
}
}
contract Campaign {
struct Request {
string description;
uint value;
address payable recipient;
bool complete;
uint approvalCount;
mapping(address => bool) approvals;
}
address public manager;
uint public minimumContribution;
mapping(address => bool) public approvers;
uint public approversCount;
Request[] public requests;
constructor(uint minimum, address creator) public {
manager = creator;
minimumContribution = minimum;
}
function contribute() public payable {
require(
msg.value >= minimumContribution,
"A minumum contribution is required."
);
approvers[msg.sender] = true;
approversCount++;
}
function createRequest(string memory description, uint value, address payable recipient) public onlyManager {
Request memory newRequest = Request({
description: description,
value: value,
recipient: recipient,
complete: false,
approvalCount: 0
});
requests.push(newRequest);
}
function approveRequest(uint index) public {
Request storage request = requests[index];
require(
approvers[msg.sender],
"Only contributors can approve a specific payment request"
);
require(
!request.approvals[msg.sender],
"You have already voted to approve this request"
);
request.approvals[msg.sender] = true;
request.approvalCount++;
}
function finalizeRequest(uint index) public onlyManager {
Request storage request = requests[index];
require(
request.approvalCount > (approversCount / 2),
"This request needs more approvals before it can be finalized"
);
require(
!(request.complete),
"This request has already been finalized"
);
request.recipient.transfer(request.value);
request.complete = true;
}
function getSummary() public view returns (uint, uint, uint, uint, address) {
return (
minimumContribution,
address(this).balance,
requests.length,
approversCount,
manager
);
}
function getRequestsCount() public view returns (uint) {
return requests.length;
}
modifier onlyManager() {
require(
msg.sender == manager,
"Only the campaign manager can call this function."
);
_;
}
}