-
Notifications
You must be signed in to change notification settings - Fork 5
/
AntsReview.sol
444 lines (362 loc) · 14.8 KB
/
AntsReview.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
/// @title Ants-Review
/// @author Nazzareno Massari @naszam
/// @notice AntsReview to allows issuer to issue an antReview which peer-reviewers can fulfill
/// @dev All function calls are currently implemented without side effects through TDD approach
/// @dev OpenZeppelin library is used for secure contract development
/**
█████ ███ ██ ████████ ███████ ██████ ███████ ██ ██ ██ ███████ ██ ██
██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██ ██ ██ ███████ █████ ██████ █████ ██ ██ ██ █████ ██ █ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██
██ ██ ██ ████ ██ ███████ ██ ██ ███████ ████ ██ ███████ ███ ███
**/
import "./AntsReviewRoles.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/EnumerableSet.sol";
interface AntsToken {
function transfer(address recipient, uint amount) external returns (bool);
function transferFrom(address sender, address recipient, uint amount) external returns (bool);
function balanceOf(address account) external view returns (uint);
}
contract AntsReview is AntsReviewRoles {
using SafeMath for uint256;
using Address for address payable;
using Counters for Counters.Counter;
using EnumerableSet for EnumerableSet.AddressSet;
/// @dev Enums
enum AntReviewStatus { CREATED, PAID}
/// @dev Token
AntsToken internal immutable ants;
/// @dev Counter
Counters.Counter public antReviewIdTracker;
/// @dev Storage
mapping (uint256 => AntReview) public antreviews;
mapping (uint256 => Peer_Review[]) public peer_reviews;
mapping (uint256 => Contribution[]) public contributions;
mapping (uint256 => EnumerableSet.AddressSet) private approvers;
/// @dev Structs
struct AntReview {
address payable[] issuers;
string paperHash;
string requirementsHash;
uint256 deadline;
AntReviewStatus status;
uint256 balance;
}
struct Peer_Review {
bool accepted;
address payable peer_reviewer;
string reviewHash;
}
struct Contribution {
address payable contributor;
uint amount;
bool refunded;
}
/// @dev Events
event AntReviewIssued(uint antId, address payable[] issuers, string paperHash, string requirementsHash, uint64 deadline);
event ContributionAdded(uint antId, uint contributionId, address contributor, uint amount);
event ContributionRefunded(uint antId, uint contributionId, address contributor);
event AntReviewFulfilled(uint antId, uint reviewId, address peer_reviewer, string reviewHash);
event ReviewUpdated(uint antId, uint reviewId, string reviewHash);
event AntReviewAccepted(uint antId, uint reviewId, address approver, uint amount);
event AntReviewChanged(uint antId, address issuer, address payable[] issuers, string paperHash, string requirementsHash, uint64 deadline);
event ApproverAdded(uint antId, uint issuerId, address approver);
event ApproverRemoved(uint antId, uint issuerId, address approver);
event AntReviewWithdrawn(uint antId, address issuer, uint amount, uint balance);
constructor(address ants_) public {
ants = AntsToken(ants_);
}
/// @dev Fallback
fallback() external {
revert();
}
/// @dev Modifiers
modifier antReviewExists(uint256 _antId){
require(_antId <= antReviewIdTracker.current());
_;
}
modifier reviewExists(uint256 _antId, uint256 _reviewId){
require(_reviewId < peer_reviews[_antId].length);
_;
}
modifier hasStatus(uint256 _antId, AntReviewStatus _desiredStatus) {
require(antreviews[_antId].status == _desiredStatus);
_;
}
modifier peerReviewNotYetAccepted(uint256 _antId, uint256 _reviewId) {
require(peer_reviews[_antId][_reviewId].accepted == false);
_;
}
modifier validateDeadline(uint256 _deadline) {
require(_deadline > now);
_;
}
modifier isBeforeDeadline(uint256 _antId) {
require(now < antreviews[_antId].deadline);
_;
}
modifier validAmount(uint256 _amount) {
require(_amount > 0, "Insufficient amount");
_;
}
modifier onlyContributor(uint _antId, uint _contributionId) {
require(msg.sender == contributions[_antId][_contributionId].contributor, "Caller is not a Contributor");
_;
}
modifier hasNotPaid(uint _antId)
{
require(antreviews[_antId].status != AntReviewStatus.PAID);
_;
}
modifier hasNotRefunded(uint _antId, uint _contributionId)
{
require(!contributions[_antId][_contributionId].refunded);
_;
}
modifier onlySubmitter(uint _antId, uint _reviewId)
{
require(msg.sender == peer_reviews[_antId][_reviewId].peer_reviewer, "Caller is not the submitter");
_;
}
modifier onlyApprover(uint _antId)
{
require(approvers[_antId].contains(msg.sender), "Caller is not the approver");
_;
}
modifier hasIssuer(uint _antId, uint _issuerId)
{
require(antreviews[_antId].issuers[_issuerId] == msg.sender, "Caller is not the issuer");
_;
}
/// @dev Getters
function getApprover(uint _antId, uint _approverId) external view whenNotPaused returns (address approver) {
return approvers[_antId].at(_approverId);
}
/// @notice Create a new AntReview
/// @dev Access restricted to Issuer
/// @param _issuers The issuers of the AntReview
/// @param _approver The approver of the AntReview
/// @param _paperHash The IPFS Hash of the Scientific Paper
/// @param _requirementsHash The IPFS Hash of the Peer-Review Requirements
/// @param _deadline The unix timestamp after which fulfillments will no longer be accepted
/// @return True If the AntReview is successfully issued
function issueAntReview(
address payable[] calldata _issuers,
address _approver,
string calldata _paperHash,
string calldata _requirementsHash,
uint64 _deadline)
external
onlyIssuer()
validateDeadline(_deadline)
whenNotPaused()
returns (bool)
{
uint antId = antReviewIdTracker.current();
AntReview storage newAntReview = antreviews[antId];
newAntReview.issuers = _issuers;
newAntReview.paperHash = _paperHash;
newAntReview.requirementsHash = _requirementsHash;
newAntReview.deadline = _deadline;
newAntReview.status = AntReviewStatus.CREATED;
require(_addApprover(antId, _approver));
antReviewIdTracker.increment();
emit AntReviewIssued(antId, _issuers, _paperHash, _requirementsHash, _deadline);
return true;
}
/// @notice Change an AntReview
/// @dev Access restricted to Issuer
/// @param _antId The AntReview Id
/// @param _issuerId The Issuer Id
/// @param _issuers The issuers of the AntReview
/// @param _paperHash The IPFS Hash of the Scientific Paper
/// @param _requirementsHash The IPFS Hash of the Peer-Review Requirements
/// @param _deadline The unix timestamp after which fulfillments will no longer be accepted
/// @return True If the AntReview is successfully changed
function changeAntReview(
uint _antId,
uint _issuerId,
address payable[] calldata _issuers,
string calldata _paperHash,
string calldata _requirementsHash,
uint64 _deadline)
external
hasIssuer(_antId, _issuerId)
whenNotPaused()
returns (bool)
{
antreviews[_antId].issuers = _issuers;
antreviews[_antId].paperHash = _paperHash;
antreviews[_antId].requirementsHash = _requirementsHash;
antreviews[_antId].deadline = _deadline;
emit AntReviewChanged(_antId, msg.sender, _issuers, _paperHash, _requirementsHash, _deadline);
return true;
}
/// @notice Add Approver
/// @dev Internal function used in issueAntReview()
/// @param _antId The AntReview Id
/// @param _account The account to be added as Approver
/// @return True if the account is successfully added as Approver
function _addApprover(uint _antId, address _account) private whenNotPaused returns (bool) {
return approvers[_antId].add(_account);
}
/// @notice Add Approver
/// @dev Access restricted to Issuers
/// @param _antId The AntReview Id
/// @param _issuerId The Issuer Id
/// @param _account The account to be added as Approver
/// @return True if the account is successfully added as Approver
function addApprover(uint _antId, uint _issuerId, address _account)
external
antReviewExists(_antId)
hasIssuer(_antId, _issuerId)
whenNotPaused()
returns (bool)
{
require(!approvers[_antId].contains(_account), "Account is already an approver");
require(approvers[_antId].add(_account));
emit ApproverAdded(_antId, _issuerId, _account);
return true;
}
/// @notice Remove Approver
/// @dev Access restricted to Issuers
/// @param _antId The AntReview Id
/// @param _issuerId The Issuer Id
/// @param _account The account to be removed as Approver
/// @return True if the account is successfully removed as Approver
function removeApprover(uint _antId, uint _issuerId, address _account)
external
antReviewExists(_antId)
hasIssuer(_antId, _issuerId)
whenNotPaused()
returns (bool)
{
require(approvers[_antId].contains(_account), "Account is not an approver");
require(approvers[_antId].remove(_account));
emit ApproverRemoved(_antId, _issuerId, _account);
return true;
}
/// @notice Contribute to an AntReview
/// @dev Linked to ANTS token
/// @param _antId The AntReview Id
/// @param _amount The Contribution amount
/// @return True if the account is the contribution is successfully added
function contribute(uint _antId, uint _amount)
external
antReviewExists(_antId)
validAmount(_amount)
whenNotPaused()
returns (bool)
{
contributions[_antId].push(Contribution(msg.sender, _amount, false));
antreviews[_antId].balance = antreviews[_antId].balance.add(_amount);
require(ants.transferFrom(msg.sender, address(this), _amount));
emit ContributionAdded(_antId, contributions[_antId].length.sub(1), msg.sender, _amount);
return true;
}
/// @notice Refund Contributors
/// @dev Access restricted to Contributor
/// @param _antId The AntReview Id
/// @param _contributionId The Contribution Id
/// @return True if the contribution is successfully refunded
function refund(uint _antId, uint _contributionId)
external
antReviewExists(_antId)
onlyContributor(_antId, _contributionId)
hasNotPaid(_antId)
hasNotRefunded(_antId, _contributionId)
whenNotPaused()
returns (bool)
{
require(now > antreviews[_antId].deadline, "Deadline has not elapsed");
Contribution storage contribution = contributions[_antId][_contributionId];
contribution.refunded = true;
antreviews[_antId].balance = antreviews[_antId].balance.sub(contribution.amount);
require(ants.transfer(contribution.contributor, contribution.amount));
emit ContributionRefunded(_antId, _contributionId, msg.sender);
return true;
}
/// @notice Submit a fulfillment for the given antReview
/// @dev Access restricted to Peer-Reviewer
/// @param _antId The AntReview Id
/// @param _reviewHash The IPFS Hash of the peer-review
/// @return True If the AntReview is successfully fulfilled
function fulfillAntReview(uint256 _antId, string calldata _reviewHash)
external
onlyPeerReviewer()
antReviewExists(_antId)
hasStatus(_antId, AntReviewStatus.CREATED)
isBeforeDeadline(_antId)
whenNotPaused()
returns (bool)
{
peer_reviews[_antId].push(Peer_Review(false, msg.sender, _reviewHash));
emit AntReviewFulfilled(_antId, peer_reviews[_antId].length.sub(1), msg.sender, _reviewHash);
return true;
}
/// @notice Update AntReview
/// @dev Access restricted to Peer_Reviewers
/// @param _antId The AntReview Id
/// @param _reviewId The Peer_Review Id
/// @param _reviewHash The IPFS Hash of the updated Peer_Review
/// @return True If the Peer_Review is successfully updated
function updateReview(uint _antId, uint _reviewId, string calldata _reviewHash)
external
onlySubmitter(_antId, _reviewId)
reviewExists(_antId, _reviewId)
hasStatus(_antId, AntReviewStatus.CREATED)
isBeforeDeadline(_antId)
whenNotPaused()
returns (bool)
{
peer_reviews[_antId][_reviewId].reviewHash = _reviewHash;
emit ReviewUpdated(_antId, _reviewId, _reviewHash);
return true;
}
/// @notice Accept a given Peer-Review
/// @dev Access restricted to Issuer
/// @param _antId The AntReview Id
/// @param _reviewId The Peer_Review Id
/// @return True If the AntReview is successfully being accepted
function acceptAntReview(uint _antId, uint _reviewId, uint _amount)
external
onlyApprover(_antId)
reviewExists(_antId, _reviewId)
hasStatus(_antId, AntReviewStatus.CREATED)
peerReviewNotYetAccepted(_antId, _reviewId)
whenNotPaused()
returns (bool)
{
antreviews[_antId].status = AntReviewStatus.PAID;
antreviews[_antId].balance = antreviews[_antId].balance.sub(_amount);
require(ants.transfer(peer_reviews[_antId][_reviewId].peer_reviewer, _amount));
emit AntReviewAccepted(_antId, _reviewId, msg.sender, _amount);
return true;
}
/// @notice Withdraw AntReview
/// @dev Access restricted to Issuer
/// @param _antId The AntReview Id
/// @param _issuerId The Issuer Id
/// @param _amount The amount to withdraw
/// @return True If the AntReview is successfully withdrawn
function withdrawAntReview(uint _antId, uint _issuerId, uint _amount)
external
antReviewExists(_antId)
hasIssuer(_antId, _issuerId)
whenNotPaused()
returns (bool)
{
require(now > antreviews[_antId].deadline, "Deadline has not elapsed");
require(antreviews[_antId].balance >= _amount, "Amount exceed AntReview balance");
antreviews[_antId].balance = antreviews[_antId].balance.sub(_amount);
require(ants.transfer(msg.sender, _amount));
emit AntReviewWithdrawn(_antId, msg.sender, _amount, antreviews[_antId].balance);
return true;
}
}