-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fundraisers.sol
232 lines (207 loc) · 7.8 KB
/
Fundraisers.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
import "@openzeppelin/contracts/access/Ownable.sol";
/// @title Fundraiser donations
/// @author kwight
/// @notice This contract allows register charities to receive donations for campaigns.
contract Fundraisers is Ownable {
mapping(address => Charity) private charityRegistry;
address[] private charities;
Program[] private programs;
Donation[] private donations;
enum ProgramStatus {
Active,
Cancelled,
Complete
}
struct Charity {
string name;
uint256 index;
}
struct Program {
string title;
ProgramStatus status;
address charity;
}
struct Donation {
address doner;
uint256 programId;
uint256 amount;
}
/// @notice Emit an event when a charity is registered.
/// @param charityAddress Address of the registered charity.
/// @param name Name of the charity.
event CharityRegistered(address charityAddress, string name);
/// @notice Emit an event when a charity is removed by the owner.
/// @param charityAddress Address of the removed charity.
event CharityRemoved(address charityAddress);
/// @notice Emit an event when a charity registers a program.
/// @param programId Index in the programs array.
/// @param charityAddress Address of the charity doing the registering.
event ProgramRegistered(uint256 programId, address charityAddress);
/// @notice Emit an event when a charity cancels a program.
/// @param programId Index in the programs array.
/// @param charityAddress Address of the charity doing the cancelling.
event ProgramCancelled(uint256 programId, address charityAddress);
/// @notice Emit an event when a charity marks a program as completed.
/// @param programId Index in the programs array.
/// @param charityAddress Address of the charity marking the completion.
event ProgramCompleted(uint256 programId, address charityAddress);
/// @notice Emit an event when a donation is received.
/// @param amount Amount of the donation (in wei).
/// @param charityAddress Address of the charity receiving the funds.
/// @param programId Index in the programs array.
/// @param doner Address of the doner.
event DonationReceived(
uint256 amount,
address charityAddress,
uint256 programId,
address doner
);
modifier isOwnerOrCharity(address charityAddress) {
require(
msg.sender == owner() || msg.sender == charityAddress,
"unauthorized"
);
_;
}
modifier onlyCharity() {
require(isRegisteredCharity(msg.sender) == true, "unauthorized");
_;
}
/// @notice Verify a given address is a registered charity.
/// @param charityAddress Address being verified.
/// @return True if registered, false otherwise.
function isRegisteredCharity(address charityAddress)
public
view
returns (bool)
{
if (charities.length == 0) return false;
return (charities[charityRegistry[charityAddress].index] ==
charityAddress);
}
/// @notice Register a charity.
/// @param charityAddress Address of the charity to be registered.
/// @param name Name of the charity.
function registerCharity(address charityAddress, string memory name)
public
onlyOwner
{
require(
isRegisteredCharity(charityAddress) == false,
"charity already exists"
);
charities.push(charityAddress);
charityRegistry[charityAddress].name = name;
charityRegistry[charityAddress].index = charities.length - 1;
emit CharityRegistered(charityAddress, name);
}
/// @notice Remove a charity.
/// @param charityAddress Address of the charity to be removed.
function removeCharity(address charityAddress)
public
isOwnerOrCharity(charityAddress)
{
require(
isRegisteredCharity(charityAddress) == true,
"charity does not exist"
);
uint256 toRemove = charityRegistry[charityAddress].index;
address toMove = charities[charities.length - 1];
charityRegistry[toMove].index = toRemove;
charities[toRemove] = toMove;
charities.pop();
emit CharityRemoved(charityAddress);
}
/// @notice Get stored data for the given charity.
/// @param charityAddress Address of the charity requested.
/// @return Charity struct instance.
function getCharity(address charityAddress)
public
view
returns (Charity memory)
{
require(
isRegisteredCharity(charityAddress) == true,
"charity does not exist"
);
return charityRegistry[charityAddress];
}
/// @notice Get data for all registered charities.
/// @return Array of Charity struct instances.
function getCharities() public view returns (address[] memory) {
return charities;
}
/// @notice Get data for all registered programs.
/// @return Array of Program struct instances.
function getPrograms() public view returns (Program[] memory) {
return programs;
}
/// @notice Register a program.
/// @param title The name of the program.
function registerProgram(string memory title) public onlyCharity {
programs.push(
Program({
title: title,
status: ProgramStatus.Active,
charity: msg.sender
})
);
emit ProgramRegistered(programs.length - 1, msg.sender);
}
/// @notice Cancel an active program.
/// @param programId Index of the program to be cancelled.
function cancelProgram(uint256 programId) public onlyCharity {
require(programs.length > programId, "program does not exist");
require(programs[programId].charity == msg.sender, "unauthorized");
require(
programs[programId].status == ProgramStatus.Active,
"program is not active"
);
programs[programId].status = ProgramStatus.Cancelled;
emit ProgramCancelled(programId, msg.sender);
}
/// @notice Mark an active program as complete.
/// @param programId Index of the program to be marked complete.
function completeProgram(uint256 programId) public onlyCharity {
require(programs.length > programId, "program does not exist");
require(programs[programId].charity == msg.sender, "unauthorized");
require(
programs[programId].status == ProgramStatus.Active,
"program is not active"
);
programs[programId].status = ProgramStatus.Complete;
emit ProgramCompleted(programId, msg.sender);
}
/// @notice Donate ether to a charity's program.
/// @param programId Index of the program "receiving" the donation.
function donate(uint256 programId) public payable {
require(programs.length > programId, "program does not exist");
require(
programs[programId].status == ProgramStatus.Active,
"program is not active"
);
Program memory receivingProgram = programs[programId];
donations.push(
Donation({
doner: msg.sender,
programId: programId,
amount: msg.value
})
);
(bool sent, ) = receivingProgram.charity.call{value: msg.value}("");
require(sent, "ether not sent to charity");
emit DonationReceived(
msg.value,
receivingProgram.charity,
programId,
msg.sender
);
}
/// @notice Get data for all donations.
/// @return Array of Donation struct instances.
function getDonations() public view returns (Donation[] memory) {
return donations;
}
}