-
Notifications
You must be signed in to change notification settings - Fork 14
/
campaign.sol
104 lines (79 loc) · 2.64 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract CampaignFactory {
Campaign[] public campaigns;
function createCampaign(uint minimum) public {
Campaign newCampaign = new Campaign(minimum, msg.sender);
campaigns.push(newCampaign);
}
function getCampaigns() public view returns (Campaign[] memory) {
return campaigns;
}
}
contract Campaign {
struct Request {
address payable recipient;
string description;
uint value;
uint approvalCount;
bool complete;
mapping(address => bool) approvals;
}
address public manager;
uint public minimumContribution;
uint public approversCount;
uint public requestsCount;
uint requestIndex = 0;
mapping(address => bool) public approvers;
mapping(uint => Request) public requests;
modifier restricted {
require(msg.sender == manager, "manager privileges only");
_;
}
constructor(uint minimum, address creator) {
manager = creator;
minimumContribution = minimum;
}
function contribute() public payable {
require(msg.value > minimumContribution, "minimum contribution required");
approvers[msg.sender] = true;
approversCount++;
}
function createRequest(string memory description, uint value, address payable recipient) public restricted {
require(value <= address(this).balance);
Request storage r = requests[requestIndex];
r.description = description;
r.recipient = recipient;
r.value = value;
r.complete = false;
r.approvalCount = 0;
requestIndex++;
requestsCount++;
}
function approveRequest(uint index) public {
Request storage r = requests[index];
require(approvers[msg.sender]);
require(!r.approvals[msg.sender]);
r.approvals[msg.sender] = true;
r.approvalCount++;
}
function finalizeRequest(uint index) public restricted {
Request storage r = requests[index];
require(r.approvalCount > (approversCount / 2));
require(!r.complete);
r.recipient.transfer(r.value);
r.complete = true;
}
function getCampaignDetails() public view returns (uint, uint, uint, uint, address) {
return (
minimumContribution,
address(this).balance,
requestsCount,
approversCount,
manager
);
}
function getRequestsCount() public view returns (uint) {
return requestsCount;
}
}