-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenesis.sol
More file actions
269 lines (223 loc) · 7.1 KB
/
Genesis.sol
File metadata and controls
269 lines (223 loc) · 7.1 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract Genesis {
address public owner;
uint public projectTax;
uint public projectCount;
uint public balance;
statsStruct public stats;
projectStruct[] projects;
mapping(address => projectStruct[]) projectsOf;
mapping(uint => backerStruct[]) backersOf;
mapping(uint => bool) public projectExist;
enum statusEnum {
OPEN,
APPROVED,
REVERTED,
DELETED,
PAIDOUT
}
struct statsStruct {
uint totalProjects;
uint totalBacking;
uint totalDonations;
}
struct backerStruct {
address owner;
uint contribution;
uint timestamp;
bool refunded;
}
struct projectStruct {
uint id;
address owner;
string title;
string description;
string imageURL;
uint cost;
uint raised;
uint timestamp;
uint expiresAt;
uint backers;
statusEnum status;
}
modifier ownerOnly(){
require(msg.sender == owner, "Owner reserved only");
_;
}
event Action (
uint256 id,
string actionType,
address indexed executor,
uint256 timestamp
);
constructor(uint _projectTax) {
owner = msg.sender;
projectTax = _projectTax;
}
function createProject(
string memory title,
string memory description,
string memory imageURL,
uint cost,
uint expiresAt
) public returns (bool) {
require(bytes(title).length > 0, "Title cannot be empty");
require(bytes(description).length > 0, "Description cannot be empty");
require(bytes(imageURL).length > 0, "ImageURL cannot be empty");
require(cost > 0 ether, "Cost cannot be zero");
projectStruct memory project;
project.id = projectCount;
project.owner = msg.sender;
project.title = title;
project.description = description;
project.imageURL = imageURL;
project.cost = cost;
project.timestamp = block.timestamp;
project.expiresAt = expiresAt;
projects.push(project);
projectExist[projectCount] = true;
projectsOf[msg.sender].push(project);
stats.totalProjects += 1;
emit Action (
projectCount++,
"PROJECT CREATED",
msg.sender,
block.timestamp
);
return true;
}
function updateProject(
uint id,
string memory title,
string memory description,
string memory imageURL,
uint expiresAt
) public returns (bool) {
require(msg.sender == projects[id].owner, "Unauthorized Entity");
require(bytes(title).length > 0, "Title cannot be empty");
require(bytes(description).length > 0, "Description cannot be empty");
require(bytes(imageURL).length > 0, "ImageURL cannot be empty");
projects[id].title = title;
projects[id].description = description;
projects[id].imageURL = imageURL;
projects[id].expiresAt = expiresAt;
emit Action (
id,
"PROJECT UPDATED",
msg.sender,
block.timestamp
);
return true;
}
function deleteProject(uint id) public returns (bool) {
require(projects[id].status == statusEnum.OPEN, "Project no longer opened");
require(msg.sender == projects[id].owner, "Unauthorized Entity");
projects[id].status = statusEnum.DELETED;
performRefund(id);
emit Action (
id,
"PROJECT DELETED",
msg.sender,
block.timestamp
);
return true;
}
function performRefund(uint id) internal {
for(uint i = 0; i < backersOf[id].length; i++) {
address _owner = backersOf[id][i].owner;
uint _contribution = backersOf[id][i].contribution;
backersOf[id][i].refunded = true;
backersOf[id][i].timestamp = block.timestamp;
payTo(_owner, _contribution);
stats.totalBacking -= 1;
stats.totalDonations -= _contribution;
}
}
function backProject(uint id) public payable returns (bool) {
require(msg.value > 0 ether, "Ether must be greater than zero");
require(projectExist[id], "Project not found");
require(projects[id].status == statusEnum.OPEN, "Project no longer opened");
stats.totalBacking += 1;
stats.totalDonations += msg.value;
projects[id].raised += msg.value;
projects[id].backers += 1;
backersOf[id].push(
backerStruct(
msg.sender,
msg.value,
block.timestamp,
false
)
);
emit Action (
id,
"PROJECT BACKED",
msg.sender,
block.timestamp
);
if(projects[id].raised >= projects[id].cost) {
projects[id].status = statusEnum.APPROVED;
balance += projects[id].raised;
performPayout(id);
return true;
}
if(block.timestamp >= projects[id].expiresAt) {
projects[id].status = statusEnum.REVERTED;
performRefund(id);
return true;
}
return true;
}
function performPayout(uint id) internal {
uint raised = projects[id].raised;
uint tax = (raised * projectTax) / 100;
projects[id].status = statusEnum.PAIDOUT;
payTo(projects[id].owner, (raised - tax));
payTo(owner, tax);
balance -= projects[id].raised;
emit Action (
id,
"PROJECT PAID OUT",
msg.sender,
block.timestamp
);
}
function requestRefund(uint id) public returns (bool) {
require(
projects[id].status != statusEnum.REVERTED ||
projects[id].status != statusEnum.DELETED,
"Project not marked as revert or delete"
);
projects[id].status = statusEnum.REVERTED;
performRefund(id);
return true;
}
function payOutProject(uint id) public returns (bool) {
require(projects[id].status == statusEnum.APPROVED, "Project not APPROVED");
require(
msg.sender == projects[id].owner ||
msg.sender == owner,
"Unauthorized Entity"
);
performPayout(id);
return true;
}
function changeTax(uint _taxPct) public ownerOnly {
projectTax = _taxPct;
}
function getProject(uint id) public view returns (projectStruct memory) {
require(projectExist[id], "Project not found");
return projects[id];
}
function getProjects() public view returns (projectStruct[] memory) {
return projects;
}
function getBackers(uint id) public view returns (backerStruct[] memory) {
return backersOf[id];
}
function payTo(address to, uint256 amount) internal {
(bool success, ) = payable(to).call{value: amount}("");
require(success);
}
}