This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClaimManagerV1.sol
executable file
·491 lines (428 loc) · 16.4 KB
/
ClaimManagerV1.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.17;
import '../Storage/ClaimManagerStorage.sol';
import '../../Bounty/Interfaces/IAtomicBounty.sol';
import '../../Bounty/Interfaces/ITieredBounty.sol';
import '../../Bounty/Interfaces/IOngoingBounty.sol';
/// @title ClaimManagerV1
/// @author FlacoJones
/// @notice Sole contract authorized to attempt claims on all bounty types
/// @dev Emitter of all claim-related events
/// @dev Some claim methods are onlyOracle protected, others have exclusively on-chain claim criteria
contract ClaimManagerV1 is ClaimManagerStorageV1 {
constructor() {}
/// @notice Initializes the ClaimManager implementation with oracle address
/// @param _oracle The address of the oracle authorized to call onlyOracle methods (e.g. claimBounty)
/// @dev Can only be called once thanks to initializer (https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable#initializers)
function initialize(address _oracle) external initializer onlyProxy {
__Ownable_init();
__UUPSUpgradeable_init();
__Oraclize_init(_oracle);
}
/// @notice Calls appropriate claim method based on bounty type
/// @param _bountyAddress The payout address of the bounty
/// @param _closer The payout address of the claimant
/// @param _closerData ABI Encoded data associated with this claim
/// @dev see IAtomicBounty.close(_closerData) for _closerData ABI encoding schema
function claimBounty(
address _bountyAddress,
address _closer,
bytes calldata _closerData
) external onlyOracle onlyProxy {
IBounty bounty = IBounty(payable(_bountyAddress));
uint256 _bountyType = bounty.bountyType();
if (_bountyType == OpenQDefinitions.ATOMIC) {
// Decode to ensure data meets closerData schema before emitting any events
abi.decode(_closerData, (address, string, address, string));
_claimAtomicBounty(bounty, _closer, _closerData);
bounty.close(_closer, _closerData);
emit BountyClosed(
bounty.bountyId(),
_bountyAddress,
bounty.organization(),
_closer,
block.timestamp,
bounty.bountyType(),
_closerData,
VERSION_1
);
} else if (_bountyType == OpenQDefinitions.ONGOING) {
_claimOngoingBounty(bounty, _closer, _closerData);
} else if (_bountyType == OpenQDefinitions.TIERED_PERCENTAGE) {
_claimTieredPercentageBounty(bounty, _closer, _closerData);
} else if (_bountyType == OpenQDefinitions.TIERED_FIXED) {
_claimTieredFixedBounty(bounty, _closer, _closerData);
} else {
revert(Errors.UNKNOWN_BOUNTY_TYPE);
}
emit ClaimSuccess(block.timestamp, _bountyType, _closerData, VERSION_1);
}
/// @notice Used for claimants who have:
/// @notice A) Completed KYC with KYC DAO for their tier
/// @notice B) Uploaded invoicing information for their tier
/// @notice C) Uploaded any necessary financial forms for their tier
/// @param _bountyAddress The payout address of the bounty
/// @param _closerData ABI Encoded data associated with this claim
function permissionedClaimTieredBounty(
address _bountyAddress,
bytes calldata _closerData
) external onlyProxy {
IBounty bounty = IBounty(payable(_bountyAddress));
(, , , , uint256 _tier) = abi.decode(
_closerData,
(address, string, address, string, uint256)
);
string memory closer = IOpenQ(openQ).addressToExternalUserId(
msg.sender
);
require(
keccak256(abi.encodePacked(closer)) !=
keccak256(abi.encodePacked('')),
Errors.NO_ASSOCIATED_ADDRESS
);
require(
keccak256(abi.encode(closer)) ==
keccak256(abi.encode(bounty.tierWinners(_tier))),
Errors.CLAIMANT_NOT_TIER_WINNER
);
if (bounty.bountyType() == OpenQDefinitions.TIERED_FIXED) {
_claimTieredFixedBounty(bounty, msg.sender, _closerData);
} else if (bounty.bountyType() == OpenQDefinitions.TIERED_PERCENTAGE) {
_claimTieredPercentageBounty(bounty, msg.sender, _closerData);
} else {
revert(Errors.NOT_A_COMPETITION_CONTRACT);
}
emit ClaimSuccess(
block.timestamp,
bounty.bountyType(),
_closerData,
VERSION_1
);
}
/// @notice Claim method for AtomicBounty
/// @param _bounty The payout address of the bounty
/// @param _closer The payout address of the claimant
/// @param _closerData ABI Encoded data associated with this claim
/// @dev See IAtomicBounty
function _claimAtomicBounty(
IAtomicBounty _bounty,
address _closer,
bytes calldata _closerData
) internal {
_eligibleToClaimAtomicBounty(_bounty, _closer);
for (uint256 i = 0; i < _bounty.getTokenAddresses().length; i++) {
uint256 volume = _bounty.claimBalance(
_closer,
_bounty.getTokenAddresses()[i]
);
emit TokenBalanceClaimed(
_bounty.bountyId(),
address(_bounty),
_bounty.organization(),
_closer,
block.timestamp,
_bounty.getTokenAddresses()[i],
volume,
_bounty.bountyType(),
_closerData,
VERSION_1
);
}
for (uint256 i = 0; i < _bounty.getNftDeposits().length; i++) {
_bounty.claimNft(_closer, _bounty.nftDeposits(i));
emit NFTClaimed(
_bounty.bountyId(),
address(_bounty),
_bounty.organization(),
_closer,
block.timestamp,
_bounty.tokenAddress(_bounty.nftDeposits(i)),
_bounty.tokenId(_bounty.nftDeposits(i)),
_bounty.bountyType(),
_closerData,
VERSION_1
);
}
}
/// @notice Claim method for OngoingBounty
/// @param _bounty The payout address of the bounty
/// @param _closer The payout address of the claimant
/// @param _closerData ABI Encoded data associated with this claim
/// @dev see IBountyCore.claimOngoingPayout.(_closerData) for _closerData ABI encoding schema
function _claimOngoingBounty(
IOngoingBounty _bounty,
address _closer,
bytes calldata _closerData
) internal {
_eligibleToClaimOngoingBounty(_bounty, _closer, _closerData);
(address tokenAddress, uint256 volume) = _bounty.claimOngoingPayout(
_closer,
_closerData
);
emit TokenBalanceClaimed(
_bounty.bountyId(),
address(_bounty),
_bounty.organization(),
_closer,
block.timestamp,
tokenAddress,
volume,
_bounty.bountyType(),
_closerData,
VERSION_1
);
}
/// @notice Claim method for TieredPercentageBounty
/// @param _bounty The payout address of the bounty
/// @param _closer The payout address of the claimant
/// @param _closerData ABI Encoded data associated with this claim
function _claimTieredPercentageBounty(
IBounty _bounty,
address _closer,
bytes calldata _closerData
) internal {
(, , , , uint256 _tier) = abi.decode(
_closerData,
(address, string, address, string, uint256)
);
_eligibleToClaimTier(_bounty, _tier, _closer);
if (_bounty.status() == 0) {
_bounty.closeCompetition();
emit BountyClosed(
_bounty.bountyId(),
address(_bounty),
_bounty.organization(),
address(0),
block.timestamp,
_bounty.bountyType(),
new bytes(0),
VERSION_1
);
}
for (uint256 i = 0; i < _bounty.getTokenAddresses().length; i++) {
uint256 volume = _bounty.claimTiered(
_closer,
_tier,
_bounty.getTokenAddresses()[i]
);
emit TokenBalanceClaimed(
_bounty.bountyId(),
address(_bounty),
_bounty.organization(),
_closer,
block.timestamp,
_bounty.getTokenAddresses()[i],
volume,
_bounty.bountyType(),
_closerData,
VERSION_1
);
}
for (uint256 i = 0; i < _bounty.getNftDeposits().length; i++) {
bytes32 _depositId = _bounty.nftDeposits(i);
if (_bounty.tier(_depositId) == _tier) {
_bounty.claimNft(_closer, _depositId);
emit NFTClaimed(
_bounty.bountyId(),
address(_bounty),
_bounty.organization(),
_closer,
block.timestamp,
_bounty.tokenAddress(_depositId),
_bounty.tokenId(_depositId),
_bounty.bountyType(),
_closerData,
VERSION_1
);
}
}
_bounty.setTierClaimed(_tier);
}
/// @notice Claim method for TieredFixedBounty
/// @param _bounty The payout address of the bounty
/// @param _closer The payout address of the claimant
/// @param _closerData ABI Encoded data associated with this claim
function _claimTieredFixedBounty(
IBounty _bounty,
address _closer,
bytes calldata _closerData
) internal {
(, , , , uint256 _tier) = abi.decode(
_closerData,
(address, string, address, string, uint256)
);
_eligibleToClaimTier(_bounty, _tier, _closer);
if (_bounty.status() == 0) {
_bounty.closeCompetition();
emit BountyClosed(
_bounty.bountyId(),
address(_bounty),
_bounty.organization(),
address(0),
block.timestamp,
_bounty.bountyType(),
new bytes(0),
VERSION_1
);
}
uint256 volume = _bounty.claimTieredFixed(_closer, _tier);
emit TokenBalanceClaimed(
_bounty.bountyId(),
address(_bounty),
_bounty.organization(),
_closer,
block.timestamp,
_bounty.payoutTokenAddress(),
volume,
_bounty.bountyType(),
_closerData,
VERSION_1
);
for (uint256 i = 0; i < _bounty.getNftDeposits().length; i++) {
bytes32 _depositId = _bounty.nftDeposits(i);
if (_bounty.tier(_depositId) == _tier) {
_bounty.claimNft(_closer, _depositId);
emit NFTClaimed(
_bounty.bountyId(),
address(_bounty),
_bounty.organization(),
_closer,
block.timestamp,
_bounty.tokenAddress(_depositId),
_bounty.tokenId(_depositId),
_bounty.bountyType(),
_closerData,
VERSION_1
);
}
}
_bounty.setTierClaimed(_tier);
}
/// @notice Checks if bounty associated with _bountyId is open
/// @return bool True if _bountyId is associated with an open bounty
function bountyIsClaimable(address _bountyAddress)
public
view
returns (bool)
{
IBounty bounty = IBounty(payable(_bountyAddress));
uint256 status = bounty.status();
uint256 _bountyType = bounty.bountyType();
if (
_bountyType == OpenQDefinitions.ATOMIC ||
_bountyType == OpenQDefinitions.ONGOING ||
_bountyType == OpenQDefinitions.TIERED_PERCENTAGE ||
_bountyType == OpenQDefinitions.TIERED_FIXED
) {
return status == 0;
} else {
return status == 1;
}
}
/// @notice Override for UUPSUpgradeable._authorizeUpgrade(address newImplementation) to enforce onlyOwner upgrades
function _authorizeUpgrade(address) internal override onlyOwner {}
/// @notice Exposes internal method Oraclize._transferOracle(address) restricted to onlyOwner called via proxy
/// @param _newOracle The new oracle address
function transferOracle(address _newOracle) external onlyProxy onlyOwner {
require(_newOracle != address(0), Errors.NO_ZERO_ADDRESS);
_transferOracle(_newOracle);
}
/// @notice Sets the OpenQProxy address used for checking IOpenQ(openQ).addressToExternalUserId
function setOpenQ(address _openQ) external onlyProxy onlyOwner {
openQ = _openQ;
}
/// @notice Sets the KYC DAO contract address
/// @param _kyc The KYC DAO contract address
function setKyc(address _kyc) external onlyProxy onlyOwner {
kyc = IKycValidity(_kyc);
}
/// @notice Checks the current KYC DAO contract address (kyc)to see if user has a valid KYC NFT or not
/// @return True if address is KYC with KYC DAO, false otherwise
function hasKYC(address _address) public view returns (bool) {
return kyc.hasValidToken(_address);
}
/// @notice Runs all require statements to determine if the claimant can claim the specified tier on the tiered bounty
function _eligibleToClaimTier(
ITieredBounty _bounty,
uint256 _tier,
address _closer
) internal view {
require(!_bounty.tierClaimed(_tier), Errors.TIER_ALREADY_CLAIMED);
if (_bounty.invoiceRequired()) {
require(
_bounty.invoiceComplete(_tier),
Errors.INVOICE_NOT_COMPLETE
);
}
if (_bounty.supportingDocumentsRequired()) {
require(
_bounty.supportingDocumentsComplete(_tier),
Errors.SUPPORTING_DOCS_NOT_COMPLETE
);
}
if (_bounty.kycRequired()) {
require(hasKYC(_closer), Errors.ADDRESS_LACKS_KYC);
}
}
/// @notice Runs all require statements to determine if the claimant can claim the atomic bounty
function _eligibleToClaimAtomicBounty(IAtomicBounty bounty, address _closer)
internal
view
{
require(
bounty.status() == OpenQDefinitions.OPEN,
Errors.CONTRACT_IS_NOT_CLAIMABLE
);
if (bounty.invoiceRequired()) {
bool _invoiceComplete = abi.decode(
bounty.getInvoiceComplete(),
(bool)
);
require(_invoiceComplete, Errors.INVOICE_NOT_COMPLETE);
}
if (bounty.supportingDocumentsRequired()) {
bool _supportingDocumentsComplete = abi.decode(
bounty.getSupportingDocumentsComplete(),
(bool)
);
require(
_supportingDocumentsComplete,
Errors.SUPPORTING_DOCS_NOT_COMPLETE
);
}
if (bounty.kycRequired()) {
require(hasKYC(_closer), Errors.ADDRESS_LACKS_KYC);
}
}
/// @notice Runs all require statements to determine if the claimant can claim an ongoing bounty payout
function _eligibleToClaimOngoingBounty(
IOngoingBounty bounty,
address _closer,
bytes memory _closerData
) internal view {
require(
bounty.status() == OpenQDefinitions.OPEN,
Errors.CONTRACT_IS_NOT_CLAIMABLE
);
(, string memory claimant, , string memory claimantAsset) = abi.decode(
_closerData,
(address, string, address, string)
);
bytes32 claimId = bounty.generateClaimId(claimant, claimantAsset);
if (bounty.invoiceRequired()) {
require(
bounty.invoiceComplete(claimId),
Errors.INVOICE_NOT_COMPLETE
);
}
if (bounty.supportingDocumentsRequired()) {
require(
bounty.supportingDocumentsComplete(claimId),
Errors.SUPPORTING_DOCS_NOT_COMPLETE
);
}
if (bounty.kycRequired()) {
require(hasKYC(_closer), Errors.ADDRESS_LACKS_KYC);
}
}
}