-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathQuestFactory.sol
1022 lines (918 loc) · 39.7 KB
/
QuestFactory.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;
// Inherits
import {Initializable} from "openzeppelin-contracts-upgradeable/proxy/utils/Initializable.sol";
import {LegacyStorage} from "./libraries/LegacyStorage.sol";
import {OwnableRoles} from "solady/auth/OwnableRoles.sol";
// Implements
import {IQuestFactory} from "./interfaces/IQuestFactory.sol";
// Leverages
import {ECDSA} from "openzeppelin-contracts/utils/cryptography/ECDSA.sol";
import {LibClone} from "solady/utils/LibClone.sol";
import {LibString} from "solady/utils/LibString.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {LibZip} from "solady/utils/LibZip.sol";
// References
import {IERC1155} from "openzeppelin-contracts/token/ERC1155/IERC1155.sol";
import {IQuestOwnable} from "./interfaces/IQuestOwnable.sol";
import {IQuest1155Ownable} from "./interfaces/IQuest1155Ownable.sol";
import {Quest as QuestContract} from "./Quest.sol";
/// @title QuestFactory
/// @author RabbitHole.gg
/// @dev This contract is used to create quests and handle claims
contract QuestFactory is Initializable, LegacyStorage, OwnableRoles, IQuestFactory {
/*//////////////////////////////////////////////////////////////
USING
//////////////////////////////////////////////////////////////*/
using SafeTransferLib for address;
using LibClone for address;
using LibString for string;
using LibString for uint256;
using LibString for address;
/*//////////////////////////////////////////////////////////////
STORAGE
//////////////////////////////////////////////////////////////*/
address public claimSignerAddress;
address public protocolFeeRecipient;
address public erc20QuestAddress;
address public erc1155QuestAddress;
mapping(string => Quest) public quests;
address private __deprecated_rabbitHoleReceiptContract; // not used
address private __deprecated_rabbitHoleTicketsContract; // not used
mapping(address => bool) private __deprecated_rewardAllowlist; // not used
uint16 public questFee;
uint256 public mintFee;
address public defaultMintFeeRecipient;
uint256 private locked;
address private __deprecated_defaultReferralFeeRecipient; // not used
uint256 private __deprecated_nftQuestFee; // not used
address private __deprecated_questNFTAddress; // not used
mapping(address => address[]) private ownerCollections;
mapping(address => NftQuestFees) private __deprecated_nftQuestFeeList; // not used
uint16 public referralFee;
address private __deprecated_sablierV2LockupLinearAddress; // not used
mapping(address => address) private __deprecated_mintFeeRecipientList; // not used
uint256 public referralRewardTimestamp;
uint16 public referralRewardFee; // fee on erc20 token
// insert new vars here at the end to keep the storage layout the same
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
/// @custom:oz-upgrades-unsafe-allow constructor
// solhint-disable-next-line func-visibility
constructor() initializer {}
function initialize(
address claimSignerAddress_,
address protocolFeeRecipient_,
address erc20QuestAddress_,
address payable erc1155QuestAddress_,
address ownerAddress_,
uint256,
uint16 referralFee_,
uint256 mintFee_
) external initializer {
_initializeOwner(ownerAddress_);
questFee = 250; // in BIPS
locked = 1;
claimSignerAddress = claimSignerAddress_;
protocolFeeRecipient = protocolFeeRecipient_;
erc20QuestAddress = erc20QuestAddress_;
erc1155QuestAddress = erc1155QuestAddress_;
referralFee = referralFee_;
mintFee = mintFee_;
referralRewardTimestamp = block.timestamp;
referralRewardFee = 250; // in BIPS
}
/*//////////////////////////////////////////////////////////////
MODIFIERS
//////////////////////////////////////////////////////////////*/
modifier checkQuest(string memory questId_) {
Quest storage currentQuest = quests[questId_];
if (currentQuest.questAddress != address(0)) revert QuestIdUsed();
if (erc20QuestAddress == address(0)) revert Erc20QuestAddressNotSet();
_;
}
modifier claimChecks(ClaimData memory claimData_) {
Quest storage currentQuest = quests[claimData_.questId];
if (currentQuest.numberMinted + 1 > currentQuest.totalParticipants) revert OverMaxAllowedToMint();
if (currentQuest.addressMinted[claimData_.claimer]) revert AddressAlreadyMinted();
if (recoverSigner(claimData_.hashBytes, claimData_.signature) != claimSignerAddress) revert AddressNotSigned();
_;
}
/// @dev ReentrancyGuard modifier from solmate, copied here because it was added after storage layout was finalized on first deploy
/// @dev from https://github.com/transmissions11/solmate/blob/main/src/utils/ReentrancyGuard.sol
modifier nonReentrant() virtual {
if (locked != 1) revert Reentrancy();
locked = 2;
_;
locked = 1;
}
modifier nonZeroAddress(address address_) {
if (address_ == address(0)) revert ZeroAddressNotAllowed();
_;
}
modifier sufficientMintFee() {
if (msg.value < mintFee) revert InvalidMintFee();
_;
}
/*//////////////////////////////////////////////////////////////
EXTERNAL UPDATE
//////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////
CREATE
//////////////////////////////////////////////////////////////*/
/// @dev Create an erc20 quest and start it at the same time. The function will transfer the reward amount to the quest contract
/// @param txHashChainId_ The chain id of the chain the txHash is on
/// @param rewardTokenAddress_ The contract address of the reward token
/// @param endTime_ The end time of the quest
/// @param startTime_ The start time of the quest
/// @param totalParticipants_ The total amount of participants (accounts) the quest will have
/// @param rewardAmount_ The reward amount for an erc20 quest
/// @param questId_ The id of the quest
/// @param actionType_ The action type for the quest
/// @param questName_ The name of the quest
/// @param projectName_ The name of the project/protocol used for the quest
/// @return address the quest contract address
function createERC20Boost(
uint32 txHashChainId_,
address rewardTokenAddress_,
uint256 endTime_,
uint256 startTime_,
uint256 totalParticipants_,
uint256 rewardAmount_,
string memory questId_,
string memory actionType_,
string memory questName_,
string memory projectName_
) external checkQuest(questId_) returns (address) {
return createERC20QuestInternal(
ERC20QuestData(
txHashChainId_,
rewardTokenAddress_,
endTime_,
startTime_,
totalParticipants_,
rewardAmount_,
questId_,
actionType_,
questName_,
"erc20",
projectName_,
referralRewardFee
)
);
}
/// @dev Create an erc20 quest and start it at the same time. The function will transfer the reward amount to the quest contract
/// @param txHashChainId_ The chain id of the chain the txHash is on
/// @param rewardTokenAddress_ The contract address of the reward token
/// @param endTime_ The end time of the quest
/// @param startTime_ The start time of the quest
/// @param totalParticipants_ The total amount of participants (accounts) the quest will have
/// @param rewardAmount_ The reward amount for an erc20 quest
/// @param questId_ The id of the quest
/// @param actionType_ The action type for the quest
/// @param questName_ The name of the quest
/// @param projectName_ The name of the project/protocol used for the quest
/// @param referralRewardFee_ The fee amount for referrals -- this is no longer used since we now have a flat 2.5% fee
/// @return address the quest contract address
function createERC20Quest(
uint32 txHashChainId_,
address rewardTokenAddress_,
uint256 endTime_,
uint256 startTime_,
uint256 totalParticipants_,
uint256 rewardAmount_,
string memory questId_,
string memory actionType_,
string memory questName_,
string memory projectName_,
uint256 referralRewardFee_
) external checkQuest(questId_) returns (address) {
return createERC20QuestInternal(
ERC20QuestData(
txHashChainId_,
rewardTokenAddress_,
endTime_,
startTime_,
totalParticipants_,
rewardAmount_,
questId_,
actionType_,
questName_,
"erc20",
projectName_,
referralRewardFee
)
);
}
/// @dev Create an erc1155 quest and start it at the same time. The function will transfer the reward amount to the quest contract
/// @param txHashChainId_ The chain id of the chain the txHash is on
/// @param rewardTokenAddress_ The contract address of the reward token
/// @param endTime_ The end time of the quest
/// @param startTime_ The start time of the quest
/// @param totalParticipants_ The total amount of participants (accounts) the quest will have
/// @param tokenId_ The reward token id of the erc1155 at rewardTokenAddress_
/// @param questId_ The id of the quest
/// @param actionType_ The action type for the quest
/// @param questName_ The name of the quest
/// @return address the quest contract address
function createERC1155Quest(
uint32 txHashChainId_,
address rewardTokenAddress_,
uint256 endTime_,
uint256 startTime_,
uint256 totalParticipants_,
uint256 tokenId_,
string memory questId_,
string memory actionType_,
string memory questName_,
string memory projectName_
) external payable nonReentrant returns (address) {
return createERC1155QuestInternal(
ERC1155QuestData(
txHashChainId_,
rewardTokenAddress_,
endTime_,
startTime_,
totalParticipants_,
tokenId_,
questId_,
actionType_,
questName_,
projectName_
)
);
}
/// @notice Deprecated
function createERC1155Quest(
address rewardTokenAddress_,
uint256 endTime_,
uint256 startTime_,
uint256 totalParticipants_,
uint256 tokenId_,
string memory questId_,
string memory actionType_,
string memory questName_,
string memory projectName_
) external payable nonReentrant returns (address) {
return createERC1155QuestInternal(
ERC1155QuestData(
0,
rewardTokenAddress_,
endTime_,
startTime_,
totalParticipants_,
tokenId_,
questId_,
actionType_,
questName_,
projectName_
)
);
}
/// @notice Deprecated
function create1155QuestAndQueue(
address rewardTokenAddress_,
uint256 endTime_,
uint256 startTime_,
uint256 totalParticipants_,
uint256 tokenId_,
string memory questId_,
string memory
) external payable nonReentrant returns (address) {
return createERC1155QuestInternal(
ERC1155QuestData(
0,
rewardTokenAddress_,
endTime_,
startTime_,
totalParticipants_,
tokenId_,
questId_,
"",
"",
""
)
);
}
/// @notice Deprecated
function createERC20Quest(
address rewardTokenAddress_,
uint256 endTime_,
uint256 startTime_,
uint256 totalParticipants_,
uint256 rewardAmount_,
string memory questId_,
string memory actionType_,
string memory questName_
) external checkQuest(questId_) returns (address) {
return createERC20QuestInternal(
ERC20QuestData(
0,
rewardTokenAddress_,
endTime_,
startTime_,
totalParticipants_,
rewardAmount_,
questId_,
actionType_,
questName_,
"erc20",
"",
referralRewardFee
)
);
}
/// @notice Deprecated
function createQuestAndQueue(
address rewardTokenAddress_,
uint256 endTime_,
uint256 startTime_,
uint256 totalParticipants_,
uint256 rewardAmount_,
string memory questId_,
string memory,
uint256
) external checkQuest(questId_) returns (address) {
return createERC20QuestInternal(
ERC20QuestData(
0,
rewardTokenAddress_,
endTime_,
startTime_,
totalParticipants_,
rewardAmount_,
questId_,
"",
"",
"erc20",
"",
referralRewardFee
)
);
}
function cancelQuest(string calldata questId_) external {
Quest storage _questData = quests[questId_];
if (_questData.questCreator != msg.sender) revert Unauthorized();
IQuestOwnable quest = IQuestOwnable(_questData.questAddress);
quest.cancel();
emit QuestCancelled(_questData.questAddress, questId_, quest.endTime());
}
/*//////////////////////////////////////////////////////////////
CLAIM
//////////////////////////////////////////////////////////////*/
/// @dev Claim rewards for a quest
/// @param compressedData_ The claim data in abi encoded bytes, compressed with cdCompress from solady LibZip
function claimCompressed(bytes calldata compressedData_) external payable {
_claimCompressed(compressedData_, msg.sender);
}
function claimCompressedRef(bytes calldata compressedData_, address claimer) external payable {
_claimCompressed(compressedData_, claimer);
}
/// @dev Claim rewards for a quest
/// @param compressedData_ The claim data in abi encoded bytes, compressed with cdCompress from solady LibZip
/// @param claimer The address of the claimer - where rewards are sent
function _claimCompressed(bytes calldata compressedData_, address claimer) internal {
bytes memory data_ = LibZip.cdDecompress(compressedData_);
(
bytes32 txHash_,
bytes32 r_,
bytes32 vs_,
address ref_,
bytes16 questid_,
uint32 txHashChainId_
) = abi.decode(
data_,
(bytes32, bytes32, bytes32, address, bytes16, uint32)
);
string memory questIdString_ = bytes16ToUUID(questid_);
Quest storage quest_ = quests[questIdString_];
string memory jsonData_ = _buildJsonString(txHash_, txHashChainId_, quest_.actionType);
bytes memory claimData_ = abi.encode(claimer, ref_, questIdString_, jsonData_);
// Since `vs_` includes `s` and the bit for `v`, we can extract `s` by masking out the `v` bit.
bytes32 s = vs_ & bytes32(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
// Now extract the `v` by shifting `vs_` right by 255 bits and casting to a uint8
uint8 v = uint8((uint256(vs_) >> 255));
// If `v` is less than 27 (which means it's either 0, 1, or invalid), add 27 to push it into range (27, 28)
// Note that if `v` was neither 0 nor 1, this will push it out of range, and the signature will be invalid
if (v < 27) v += 27;
_claimOptimized(abi.encodePacked(r_, s, v), claimData_);
}
/// @notice External use is deprecated
/// @dev Claim rewards for a quest
/// @param -DEPRECATED- signature_ The signature of the claim data
/// @param -DEPRECATED- data_ The claim data in abi encoded bytes
function claimOptimized(bytes calldata, bytes calldata) external payable {
revert Deprecated();
}
/// @dev Claim rewards for a quest
/// @param signature_ The signature of the claim data
/// @param data_ The claim data in abi encoded bytes
function _claimOptimized(
bytes memory signature_,
bytes memory data_
) internal {
(
address claimer_,
address ref_,
string memory questId_,
string memory jsonData_
) = abi.decode(
data_,
(address, address, string, string)
);
Quest storage quest = quests[questId_];
uint256 numberMintedPlusOne_ = quest.numberMinted + 1;
address rewardToken_ = IQuestOwnable(quest.questAddress).rewardToken();
uint256 rewardAmountOrTokenId;
if (recoverSigner(keccak256(data_), signature_) != claimSignerAddress) revert AddressNotSigned();
if (msg.value < mintFee) revert InvalidMintFee();
if (quest.addressMinted[claimer_]) revert AddressAlreadyMinted();
if (numberMintedPlusOne_ > quest.totalParticipants) revert OverMaxAllowedToMint();
quest.addressMinted[claimer_] = true;
quest.numberMinted = numberMintedPlusOne_;
(bool success_, ) = quest.questAddress.call{value: msg.value}(abi.encodeWithSignature("claimFromFactory(address,address)", claimer_, ref_));
if (!success_) revert ClaimFailed();
emit QuestClaimedData(claimer_, quest.questAddress, jsonData_);
if (quest.questType.eq("erc1155")) {
rewardAmountOrTokenId = IQuest1155Ownable(quest.questAddress).tokenId();
emit Quest1155Claimed(claimer_, quest.questAddress, questId_, rewardToken_, rewardAmountOrTokenId);
} else {
rewardAmountOrTokenId = IQuestOwnable(quest.questAddress).rewardAmountInWei();
emit QuestClaimed(claimer_, quest.questAddress, questId_, rewardToken_, rewardAmountOrTokenId);
}
if(ref_ != address(0)){
if (IQuestOwnable(quest.questAddress).startTime() > referralRewardTimestamp) {
emit QuestClaimReferred(
claimer_,
quest.questAddress,
questId_,
rewardToken_,
rewardAmountOrTokenId,
ref_, 3333,
mintFee,
QuestContract(payable(quest.questAddress)).referralRewardFee(),
QuestContract(payable(quest.questAddress)).referralRewardAmount())
;
} else {
emit QuestClaimedReferred(
claimer_,
quest.questAddress,
questId_,
rewardToken_,
rewardAmountOrTokenId,
ref_, 3333,
mintFee
);
}
emit MintFeePaid(questId_, address(0), 0, address(0), 0, ref_, mintFee / 3);
}
}
/*//////////////////////////////////////////////////////////////
SET
//////////////////////////////////////////////////////////////*/
/// @dev set the claim signer address
/// @param claimSignerAddress_ The address of the claim signer
function setClaimSignerAddress(address claimSignerAddress_) external onlyOwner {
claimSignerAddress = claimSignerAddress_;
}
/// @dev set erc1155QuestAddress
/// @param erc1155QuestAddress_ The address of the erc1155 quest
function setErc1155QuestAddress(address erc1155QuestAddress_) external onlyOwner {
erc1155QuestAddress = erc1155QuestAddress_;
}
/// @dev set erc20QuestAddress
/// @param erc20QuestAddress_ The address of the erc20 quest
function setErc20QuestAddress(address erc20QuestAddress_) external onlyOwner {
erc20QuestAddress = erc20QuestAddress_;
}
/// @dev set the mint fee
/// @notice the mint fee in ether
/// @param mintFee_ The mint fee value
function setMintFee(uint256 mintFee_) external onlyOwner {
mintFee = mintFee_;
emit MintFeeSet(mintFee_);
}
/// @dev set the protocol fee recipient
/// @param protocolFeeRecipient_ The address of the protocol fee recipient
function setProtocolFeeRecipient(address protocolFeeRecipient_) external onlyOwner {
if (protocolFeeRecipient_ == address(0)) revert AddressZeroNotAllowed();
protocolFeeRecipient = protocolFeeRecipient_;
}
/// @dev set the quest fee
/// @notice the quest fee should be in Basis Point units
/// @param questFee_ The quest fee value
function setQuestFee(uint16 questFee_) external onlyOwner {
if (questFee_ > 10_000) revert QuestFeeTooHigh();
questFee = questFee_;
}
/// @dev set the referral reward fee (erc20 tokens)
/// @notice the referral reward fee should be in Basis Point units
/// @param referralRewardFee_ The referral reward fee value
function setReferralRewardFee(uint16 referralRewardFee_) external onlyOwner {
if (referralRewardFee_ > 10_000) revert ReferralFeeTooHigh();
referralRewardFee = referralRewardFee_;
}
/// @dev set the referral fee
/// @param referralFee_ The value of the referralFee
function setReferralFee(uint16 referralFee_) external onlyOwner {
if (referralFee_ > 10_000) revert ReferralFeeTooHigh();
referralFee = referralFee_;
emit ReferralFeeSet(referralFee_);
}
/// @dev set the mintFeeRecipient
/// @param mintFeeRecipient_ The address of the mint fee recipient
function setDefaultMintFeeRecipient(address mintFeeRecipient_) external onlyOwner {
if (mintFeeRecipient_ == address(0)) revert AddressZeroNotAllowed();
defaultMintFeeRecipient = mintFeeRecipient_;
}
function setReferralRewardTimestamp(uint256 timestamp_) external onlyOwner {
referralRewardTimestamp = timestamp_;
}
/*//////////////////////////////////////////////////////////////
EXTERNAL VIEW
//////////////////////////////////////////////////////////////*/
/// @notice This function name is a bit of a misnomer - gets whether an address has claimed a quest yet.
/// @dev return status of whether an address has claimed a quest
/// @param questId_ The id of the quest
/// @param address_ The address to check
/// @return claimed status
function getAddressMinted(string memory questId_, address address_) external view returns (bool) {
return quests[questId_].addressMinted[address_];
}
/// @dev return the number of quest claims
/// @param questId_ The id of the quest
/// @return uint Total quests claimed
function getNumberMinted(string memory questId_) external view returns (uint256) {
return quests[questId_].numberMinted;
}
/// @dev return extended quest data for a questId
/// @param questId_ The id of the quest
function questData(string memory questId_) external view returns (QuestData memory) {
Quest storage thisQuest = quests[questId_];
IQuestOwnable questContract = IQuestOwnable(thisQuest.questAddress);
uint256 rewardAmountOrTokenId;
uint16 erc20QuestFee;
if (thisQuest.questType.eq("erc1155")) {
rewardAmountOrTokenId = IQuest1155Ownable(thisQuest.questAddress).tokenId();
} else {
rewardAmountOrTokenId = questContract.rewardAmountInWei();
erc20QuestFee = questContract.questFee();
}
QuestData memory data = QuestData(
thisQuest.questAddress,
questContract.rewardToken(),
questContract.queued(),
erc20QuestFee,
questContract.startTime(),
questContract.endTime(),
questContract.totalParticipants(),
thisQuest.numberMinted,
thisQuest.numberMinted,
rewardAmountOrTokenId,
questContract.hasWithdrawn()
);
return data;
}
/// @param questId_ The id of the quest
function questJsonData(string memory questId_) external view returns (QuestJsonData memory) {
Quest storage thisQuest = quests[questId_];
QuestJsonData memory data = QuestJsonData(
thisQuest.actionType,
thisQuest.questName,
thisQuest.txHashChainId
);
return data;
}
/// @dev return data in the quest struct for a questId
/// @param questId_ The id of the quest
function questInfo(string memory questId_) external view returns (address, uint256, uint256) {
Quest storage currentQuest = quests[questId_];
return (currentQuest.questAddress, currentQuest.totalParticipants, currentQuest.numberMinted);
}
/// @dev recover the signer from a hash and signature
/// @param hash_ The hash of the message
/// @param signature_ The signature of the hash
function recoverSigner(bytes32 hash_, bytes memory signature_) public view returns (address) {
return ECDSA.recover(ECDSA.toEthSignedMessageHash(hash_), signature_);
}
function withdrawCallback(string calldata questId_, address protocolFeeRecipient_, uint protocolPayout_, address mintFeeRecipient_, uint mintPayout) external {
Quest storage quest = quests[questId_];
if(msg.sender != quest.questAddress) revert QuestAddressMismatch();
emit MintFeePaid(questId_, protocolFeeRecipient_, protocolPayout_, mintFeeRecipient_, mintPayout, address(0), 0);
}
function getQuestName(string calldata questId_) external view returns (string memory) {
return quests[questId_].questName;
}
/*//////////////////////////////////////////////////////////////
INTERNAL UPDATE
//////////////////////////////////////////////////////////////*/
/// @dev claim rewards for a quest with a referral address
/// @param claimData_ The claim data struct
function claim1155RewardsRef(ClaimData memory claimData_) private
nonReentrant
sufficientMintFee
claimChecks(claimData_)
{
Quest storage currentQuest = quests[claimData_.questId];
IQuest1155Ownable questContract_ = IQuest1155Ownable(currentQuest.questAddress);
if (!questContract_.queued()) revert QuestNotQueued();
if (block.timestamp < questContract_.startTime()) revert QuestNotStarted();
if (block.timestamp > questContract_.endTime()) revert QuestEnded();
currentQuest.addressMinted[claimData_.claimer] = true;
++currentQuest.numberMinted;
questContract_.singleClaim(claimData_.claimer);
if (mintFee > 0) {
string memory newJson = processMintFee(claimData_.ref, currentQuest.questCreator, claimData_.questId);
if (bytes(claimData_.extraData).length > 0){
claimData_.extraData = claimData_.extraData.slice(0, bytes(claimData_.extraData).length -1).concat(newJson);
}
}
emit QuestClaimedData(
claimData_.claimer,
currentQuest.questAddress,
claimData_.extraData
);
emit Quest1155Claimed(
claimData_.claimer, currentQuest.questAddress, claimData_.questId, questContract_.rewardToken(), questContract_.tokenId()
);
if (claimData_.ref != address(0)) {
if (IQuestOwnable(currentQuest.questAddress).startTime() > referralRewardTimestamp) {
emit QuestClaimReferred(
claimData_.claimer,
currentQuest.questAddress,
claimData_.questId,
questContract_.rewardToken(),
questContract_.tokenId(),
claimData_.ref,
3333, //referralFee,
mintFee,
0,
0
);
} else {
emit QuestClaimedReferred(
claimData_.claimer,
currentQuest.questAddress,
claimData_.questId,
questContract_.rewardToken(),
questContract_.tokenId(),
claimData_.ref,
3333, //referralFee,
mintFee
);
}
}
}
/// @dev claim rewards with a referral address
/// @param claimData_ The claim data struct
function claimRewardsRef(ClaimData memory claimData_) private
nonReentrant
sufficientMintFee
claimChecks(claimData_)
{
Quest storage currentQuest = quests[claimData_.questId];
IQuestOwnable questContract_ = IQuestOwnable(currentQuest.questAddress);
if (!questContract_.queued()) revert QuestNotQueued();
if (block.timestamp < questContract_.startTime()) revert QuestNotStarted();
if (block.timestamp > questContract_.endTime()) revert QuestEnded();
currentQuest.addressMinted[claimData_.claimer] = true;
++currentQuest.numberMinted;
questContract_.singleClaim(claimData_.claimer);
if (mintFee > 0) {
string memory newJson = processMintFee(claimData_.ref, currentQuest.questCreator, claimData_.questId);
if (bytes(claimData_.extraData).length > 0){
claimData_.extraData = claimData_.extraData.slice(0, bytes(claimData_.extraData).length -1).concat(newJson);
}
}
emit QuestClaimedData(
claimData_.claimer,
currentQuest.questAddress,
claimData_.extraData
);
emit QuestClaimed(
claimData_.claimer,
currentQuest.questAddress,
claimData_.questId,
questContract_.rewardToken(),
questContract_.rewardAmountInWei()
);
if (claimData_.ref != address(0)) {
if (IQuestOwnable(currentQuest.questAddress).startTime() > referralRewardTimestamp) {
emit QuestClaimReferred(
claimData_.claimer,
currentQuest.questAddress,
claimData_.questId,
questContract_.rewardToken(),
questContract_.rewardAmountInWei(),
claimData_.ref,
3333, //referralFee,
mintFee,
0,
0
);
} else {
emit QuestClaimedReferred(
claimData_.claimer,
currentQuest.questAddress,
claimData_.questId,
questContract_.rewardToken(),
questContract_.rewardAmountInWei(),
claimData_.ref,
3333, //referralFee,
mintFee
);
}
}
}
/// @dev Internal function to create an erc1155 quest
/// @param data_ The erc20 quest data struct
function createERC1155QuestInternal(ERC1155QuestData memory data_) internal returns (address) {
Quest storage currentQuest = quests[data_.questId];
if (currentQuest.questAddress != address(0)) revert QuestIdUsed();
address payable newQuest =
payable(erc1155QuestAddress.cloneDeterministic(keccak256(abi.encodePacked(msg.sender, block.chainid, block.timestamp))));
currentQuest.questAddress = address(newQuest);
currentQuest.totalParticipants = data_.totalParticipants;
currentQuest.questType = "erc1155";
currentQuest.questCreator = msg.sender;
currentQuest.actionType = data_.actionType;
currentQuest.questName = data_.questName;
currentQuest.txHashChainId = data_.txHashChainId;
IQuest1155Ownable questContract = IQuest1155Ownable(newQuest);
questContract.initialize(
data_.rewardTokenAddress,
data_.endTime,
data_.startTime,
data_.totalParticipants,
data_.tokenId,
protocolFeeRecipient,
data_.questId
);
IERC1155(data_.rewardTokenAddress).safeTransferFrom(msg.sender, newQuest, data_.tokenId, data_.totalParticipants, "0x00");
questContract.queue();
questContract.transferOwnership(msg.sender);
emit QuestCreated(
msg.sender,
address(newQuest),
data_.projectName,
data_.questName,
data_.questId,
currentQuest.questType,
data_.actionType,
data_.txHashChainId,
data_.rewardTokenAddress,
data_.endTime,
data_.startTime,
data_.totalParticipants,
data_.tokenId
);
return newQuest;
}
/// @dev Internal function to create an erc20 quest
/// @param data_ The erc20 quest data struct
function createERC20QuestInternal(ERC20QuestData memory data_) internal returns (address) {
Quest storage currentQuest = quests[data_.questId];
address newQuest = erc20QuestAddress.cloneDeterministic(keccak256(abi.encodePacked(msg.sender, block.chainid, block.timestamp)));
currentQuest.questAddress = address(newQuest);
currentQuest.totalParticipants = data_.totalParticipants;
currentQuest.questCreator = msg.sender;
currentQuest.questType = data_.questType;
currentQuest.actionType = data_.actionType;
currentQuest.questName = data_.questName;
currentQuest.txHashChainId = data_.txHashChainId;
currentQuest.referralRewardFee = data_.referralRewardFee;
emit QuestCreated(
msg.sender,
address(newQuest),
data_.projectName,
data_.questName,
data_.questId,
currentQuest.questType,
data_.actionType,
data_.txHashChainId,
data_.rewardTokenAddress,
data_.endTime,
data_.startTime,
data_.totalParticipants,
data_.rewardAmount
);
IQuestOwnable(newQuest).initialize(
data_.rewardTokenAddress,
data_.endTime,
data_.startTime,
data_.totalParticipants,
data_.rewardAmount,
data_.questId,
questFee,
protocolFeeRecipient,
referralRewardFee
);
transferTokensAndOwnership(newQuest, data_.rewardTokenAddress);
return newQuest;
}
function processMintFee(address ref_, address mintFeeRecipient_, string memory questId_) private returns (string memory) {
returnChange();
uint256 cachedMintFee = mintFee;
uint256 oneThirdMintfee = cachedMintFee / 3;
uint256 protocolPayout;
uint256 mintPayout;
uint256 referrerPayout;
if(ref_ == address(0)){
protocolPayout = oneThirdMintfee * 2;
mintPayout = oneThirdMintfee;
} else {
protocolPayout = oneThirdMintfee;
mintPayout = oneThirdMintfee;
referrerPayout = oneThirdMintfee;
}
protocolFeeRecipient.safeTransferETH(protocolPayout);
mintFeeRecipient_.safeTransferETH(mintPayout);
if(referrerPayout != 0) ref_.safeTransferETH(referrerPayout);
emit MintFeePaid(questId_, protocolFeeRecipient, protocolPayout, mintFeeRecipient_, mintPayout, ref_, referrerPayout);
return string(abi.encodePacked(
', "claimFee": "', cachedMintFee.toString(),
'", "claimFeePayouts": [{"name": "protocolPayout", "address": "', protocolFeeRecipient.toHexString(),
'", "value": "', protocolPayout.toString(),
'"}, {"name": "mintPayout", "address": "', mintFeeRecipient_.toHexString(),
'", "value": "', mintPayout.toString(),
'"}, {"name": "referrerPayout", "address": "', ref_.toHexString(),
'", "value": "', referrerPayout.toString(), '"}]}'
));
}
// Refund any excess payment
function returnChange() private {
uint256 change = msg.value - mintFee;
if (change > 0) {
msg.sender.safeTransferETH(change);
}
}
/// @dev Transfer the total transfer amount to the quest contract
/// @dev Contract must be approved to transfer first
/// @param newQuest_ The address of the new quest
/// @param rewardTokenAddress_ The contract address of the reward token
function transferTokensAndOwnership(address newQuest_, address rewardTokenAddress_) internal {
address sender = msg.sender;
IQuestOwnable questContract = IQuestOwnable(newQuest_);
rewardTokenAddress_.safeTransferFrom(sender, newQuest_, questContract.totalTransferAmount());
questContract.transferOwnership(sender);
}
/// @dev Build the expected json string for a quest
/// @param txHash The transaction hash
/// @param txHashChainId The chain id of the transaction hash
/// @param actionType The action type for the quest
/// @param -deprecated- The name of the quest
/// @return string The json string
function buildJsonString(
bytes32 txHash,
uint32 txHashChainId,
string memory actionType,
string memory // questName - not used
) external pure returns (string memory) {
return _buildJsonString(txHash, txHashChainId, actionType);
}
/// @dev Build the expected json string for a quest
/// @param txHash The transaction hash
/// @param txHashChainId The chain id of the transaction hash
/// @param actionType The action type for the quest
/// @return string The json string
function _buildJsonString(
bytes32 txHash,
uint32 txHashChainId,
string memory actionType
) internal pure returns (string memory) {
// {
// actionTxHashes: ["actionTxHash1"],
// actionNetworkChainIds: ["chainId1"],
// actionType: "mint"
// }
return string(abi.encodePacked(
'{"actionTxHashes":["', uint256(txHash).toHexString(32),
'"],"actionNetworkChainIds":[', uint256(txHashChainId).toString(),
'],"actionType":"', actionType, '"}'
));
}
/// @dev Convert bytes16 to a UUID string e.g. 550e8400-e29b-41d4-a716-446655440000
/// @param data The bytes16 data e.g. 0x550e8400e29b41d4a716446655440000
function bytes16ToUUID(bytes16 data) internal pure returns (string memory) {
bytes memory hexChars = "0123456789abcdef";
bytes memory uuid = new bytes(36); // UUID length with hyphens
uint256 j = 0; // Position in uuid