-
Notifications
You must be signed in to change notification settings - Fork 0
/
manuscript.sol
427 lines (398 loc) · 15.3 KB
/
manuscript.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Manuscript {
address admin;
address author;
string title;
string link;
string summary; // using sumnmary instead of abstract because abstract is a reserved keyword in solidity
address[2] public assignedReviewers;
address public assignedEditor;
uint256 numReviewed;
uint256 status; /*
0: not reviewed
1: Reviewers Assigned
2: Editors Assigned
3: Comments Submitted
4: Manuscript Accepted
5: Manuscript Rejected
6: Peer review approved (admin)
7: Peer review cancelled (admin)
*/
uint256 public balance; // money donated to the peer review
struct Reviewer {
address addr;
uint256 reputation;
bool hasReviewed;
}
struct Editor {
address addr;
uint256 reputation;
}
mapping(address => Reviewer) public reviewers;
address[] listOfReviewers; // for looping purposes
mapping(address => Editor) public editors;
address[] listOfEditors; // for looping purposes
mapping(address => bool) public registered;
event ReviewerAssigned(
address indexed reviewerAddr1,
address indexed reviewerAddr2
);
event EditorAssigned(address indexed editorAddr);
event CommentsSubmitted(address indexed reviewerAddr);
event ManuscriptAccepted();
event ManuscriptRejected(string reason);
event ReviewApproved();
event ReputationAwarded();
event ReviewCancelled(string reason);
event ReputationStripped();
constructor(
address _admin,
address _author,
string memory _title,
string memory _manuscriptUrl,
string memory _summary
) {
admin = _admin;
author = _author;
status = 0;
title = _title;
link = _manuscriptUrl;
summary = _summary;
numReviewed = 0;
assignedReviewers = [address(0), address(0)];
assignedEditor = address(0);
}
function getManuscriptDetails()
public
view
returns (address, string memory, string memory, string memory, uint256)
{
require(msg.sender == admin, "Unauthorized");
return (author, title, link, summary, status);
}
function isPayable(address testAddress) private returns (bool) {
return payable(testAddress).send(0);
}
function receiveFunds() public payable {
require(
status < 6,
"Thank you for wanting to donate. However, this peer review has already ended."
);
balance += msg.value;
}
function stakeReputationReviewer(uint256 amount) public payable {
require(amount > 0, "Amount must be greater than 0.");
require(status == 0, "Manuscript is already under review.");
if (reviewers[msg.sender].addr == msg.sender) {
reviewers[msg.sender].reputation += amount;
} else {
require(
!registered[msg.sender],
"Sender already registered as an editor."
);
require(isPayable(msg.sender), "Sender is not a payable address.");
require(
msg.sender != admin && msg.sender != author,
"Admin or Author cannot be a reviewer."
);
reviewers[msg.sender] = Reviewer(msg.sender, amount, false);
listOfReviewers.push(msg.sender);
registered[msg.sender] = true;
}
balance += amount;
}
function withdrawReputationReviewer(uint256 amount) public payable {
require(
reviewers[msg.sender].addr == msg.sender,
"Sender is not a registered reviewer."
);
require(
reviewers[msg.sender].reputation >= amount,
"Not enough reputation to withdraw."
);
require(amount > 0, "Amount to be withdrawn must be greater than 0.");
require(status == 0, "Manuscript is already under review.");
reviewers[msg.sender].reputation -= amount;
balance -= amount;
}
function assignReviewers() public payable {
require(msg.sender == admin, "Only admin can assign reviewers.");
require(
listOfReviewers.length >= 2,
"At least two staked reviewers are required for review assignment."
);
uint256 totalReputationStaked = 0;
uint256 numReviewers = listOfReviewers.length;
address[] memory eligibleReviewers = new address[](numReviewers);
uint256[] memory reputationStaked = new uint256[](numReviewers);
for (uint256 i = 0; i < listOfReviewers.length; i++) {
eligibleReviewers[i] = listOfReviewers[i];
reputationStaked[i] = reviewers[listOfReviewers[i]].reputation;
totalReputationStaked += reviewers[listOfReviewers[i]].reputation;
}
require(totalReputationStaked > 2, "Not enough reputation staked");
uint256 numAssignedReviewers = 0;
while (numAssignedReviewers < 2) {
uint256 randomValue = uint256(
keccak256(abi.encodePacked(block.timestamp, block.difficulty))
) % totalReputationStaked;
for (uint256 i = 0; i < numReviewers; i++) {
if (eligibleReviewers[i] != address(0)) {
if (randomValue < reputationStaked[i]) {
assignedReviewers[
numAssignedReviewers
] = eligibleReviewers[i];
numAssignedReviewers++;
totalReputationStaked -= reputationStaked[i];
eligibleReviewers[i] = address(0);
break;
} else {
randomValue -= reputationStaked[i];
}
}
}
}
status = 1;
returnReputationToUnassignedReviewers();
emit ReviewerAssigned(assignedReviewers[0], assignedReviewers[1]);
}
function returnReputationToUnassignedReviewers() public payable {
require(msg.sender == admin, "Only admin can assign reviewers.");
require(status == 1, "Reviewers have not been assigned yet.");
for (uint256 i = 0; i < listOfReviewers.length; i++) {
if (
listOfReviewers[i] != assignedReviewers[0] &&
listOfReviewers[i] != assignedReviewers[1]
) {
require(
balance >= reviewers[listOfReviewers[i]].reputation,
"Insufficient balance in the contract to assign reviewers"
);
payable(listOfReviewers[i]).transfer(
reviewers[listOfReviewers[i]].reputation
);
reviewers[listOfReviewers[i]].reputation = 0;
balance -= reviewers[listOfReviewers[i]].reputation;
}
}
}
function stakeReputationEditor(uint256 amount) public payable {
require(amount > 0, "Amount must be greater than 0.");
require(
status == 0 || status == 1,
"Manuscript is already under review."
);
if (editors[msg.sender].addr == msg.sender) {
editors[msg.sender].reputation += amount;
} else {
require(
!registered[msg.sender],
"Sender already registered as a reviewer."
);
require(isPayable(msg.sender), "Sender is not a payable address.");
require(
msg.sender != admin && msg.sender != author,
"Admin or Author cannot be an editor."
);
editors[msg.sender] = Editor(msg.sender, amount);
listOfEditors.push(msg.sender);
registered[msg.sender] = true;
}
balance += amount;
}
function withdrawReputationEditor(uint256 amount) public payable {
require(
editors[msg.sender].addr == msg.sender,
"Sender is not a registered editor."
);
require(
editors[msg.sender].reputation >= amount,
"Not enough reputation to withdraw."
);
require(amount > 0, "Amount to be withdrawn must be greater than 0.");
require(
status == 0 || status == 1,
"Manuscript is already under review."
);
editors[msg.sender].reputation -= amount;
balance -= amount;
}
function assignEditor() public payable {
require(msg.sender == admin, "Only admin can assign editor.");
require(
listOfEditors.length >= 1,
"At least one staked editor is required for review assignment."
);
require(
assignedReviewers[0] != address(0) &&
assignedReviewers[1] != address(0),
"Reviewers must be assigned first."
);
uint256 totalReputationStaked = 0;
uint256 numEditors = listOfEditors.length;
address[] memory eligibleEditors = new address[](numEditors);
uint256[] memory reputationStaked = new uint256[](numEditors);
for (uint256 i = 0; i < listOfEditors.length; i++) {
eligibleEditors[i] = listOfEditors[i];
reputationStaked[i] = editors[listOfEditors[i]].reputation;
totalReputationStaked += editors[listOfEditors[i]].reputation;
}
require(totalReputationStaked > 10, "Not enough reputation staked");
uint256 numAssignedEditor = 0;
while (numAssignedEditor < 1) {
uint256 randomValue = uint256(
keccak256(abi.encodePacked(block.timestamp, block.difficulty))
) % totalReputationStaked;
for (uint256 i = 0; i < numEditors; i++) {
if (eligibleEditors[i] != address(0)) {
if (randomValue < reputationStaked[i]) {
assignedEditor = eligibleEditors[i];
numAssignedEditor++;
break;
} else {
randomValue -= reputationStaked[i];
}
}
}
}
returnReputationToUnassignedEditors();
status = 2;
emit EditorAssigned(assignedEditor);
}
function returnReputationToUnassignedEditors() public payable {
require(msg.sender == admin, "Only admin can assign editors.");
for (uint256 i = 0; i < listOfEditors.length; i++) {
if (listOfEditors[i] != assignedEditor) {
require(
balance >= editors[listOfEditors[i]].reputation,
"Insufficient balance in the contract to assign editors"
);
payable(listOfEditors[i]).transfer(
editors[listOfEditors[i]].reputation
);
editors[listOfEditors[i]].reputation = 0;
balance -= editors[listOfEditors[i]].reputation;
}
}
}
function submitComments() public {
require(
status == 2,
"Editors and Reviewers need to be assigned first."
);
require(
msg.sender == assignedReviewers[0] ||
msg.sender == assignedReviewers[1],
"Only assigned reviewers can submit comments"
);
require(
reviewers[msg.sender].hasReviewed == false,
"Reviewer has already submitted comments"
);
reviewers[msg.sender].hasReviewed = true;
numReviewed += 1;
if (numReviewed == 2) {
status = 3;
}
emit CommentsSubmitted(msg.sender);
}
function acceptReview() public {
require(
msg.sender == assignedEditor,
"Only assigned editor can accept the review"
);
require(
reviewers[assignedReviewers[0]].hasReviewed == true &&
reviewers[assignedReviewers[1]].hasReviewed == true,
"Comments not finalized"
);
status = 4;
emit ManuscriptAccepted();
}
function rejectReview(string memory _reason) public {
require(
msg.sender == assignedEditor,
"Only assigned editor can reject the review"
);
require(
reviewers[assignedReviewers[0]].hasReviewed == true &&
reviewers[assignedReviewers[1]].hasReviewed == true,
"Comments not finalized"
);
require(
bytes(_reason).length >= 50,
"Reason must be at least 50 characters"
);
status = 5;
emit ManuscriptRejected(_reason);
}
function confirmReview() public {
require(msg.sender == admin, "Only admin can confirm the review");
require(
status == 4 || status == 5,
"Manuscript not accepted or rejected"
);
status = 6;
awardReputation();
emit ReviewApproved();
}
function awardReputation() private {
// transfer reputation to the assigned reviewers
payable(assignedReviewers[0]).transfer(
reviewers[assignedReviewers[0]].reputation
);
payable(assignedReviewers[1]).transfer(
reviewers[assignedReviewers[1]].reputation
);
// transfer reputation to the assigned editor
payable(assignedEditor).transfer(reviewers[assignedEditor].reputation);
// pay 80% of the balance to the assigned reviewers based on their reputation staked
uint256 totalReputationStaked = reviewers[assignedReviewers[0]]
.reputation + reviewers[assignedReviewers[1]].reputation;
uint256 reputationToBeAwarded = uint256((balance * 80) / 100);
payable(assignedReviewers[0]).transfer(
(reviewers[assignedReviewers[0]].reputation /
totalReputationStaked) * reputationToBeAwarded
);
payable(assignedReviewers[1]).transfer(
(reviewers[assignedReviewers[1]].reputation /
totalReputationStaked) * reputationToBeAwarded
);
// pay 20% of the balance to the assigned editor
payable(assignedEditor).transfer(uint256((balance * 20) / 100));
balance = 0;
reviewers[assignedReviewers[0]].reputation = 0;
reviewers[assignedReviewers[1]].reputation = 0;
editors[assignedEditor].reputation = 0;
emit ReputationAwarded();
}
function stripReputation() private {
// transfer reputation staked to the admin
payable(admin).transfer(
reviewers[assignedReviewers[0]].reputation +
reviewers[assignedReviewers[1]].reputation +
editors[assignedEditor].reputation
);
reviewers[assignedReviewers[0]].reputation = 0;
reviewers[assignedReviewers[1]].reputation = 0;
editors[assignedEditor].reputation = 0;
// send balance back to author
payable(author).transfer(balance);
emit ReputationStripped();
}
function cancelReview(string memory _reason) public {
require(msg.sender == admin, "Only admin can cancel the review");
require(
status == 4 || status == 5,
"Manuscript not accepted or rejected"
);
require(
bytes(_reason).length >= 50,
"Reason must be at least 50 characters"
);
status = 7;
stripReputation();
emit ReviewCancelled(_reason);
}
}