forked from rabbitholegg/quest-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuestClaimable.t.sol
233 lines (195 loc) · 9.54 KB
/
QuestClaimable.t.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
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.19;
// solhint-disable no-global-import
import "forge-std/Test.sol";
import {SampleERC1155} from "contracts/test/SampleERC1155.sol";
import {SampleERC20} from "contracts/test/SampleERC20.sol";
import {QuestFactory} from "contracts/QuestFactory.sol";
import {IQuestFactory} from "contracts/interfaces/IQuestFactory.sol";
import {Quest} from "contracts/Quest.sol";
import {Quest1155} from "contracts/Quest1155.sol";
import {LibClone} from "solady/utils/LibClone.sol";
import {LibString} from "solady/utils/LibString.sol";
import {ECDSA} from "openzeppelin-contracts/utils/cryptography/ECDSA.sol";
import {JSONParserLib} from "solady/utils/JSONParserLib.sol";
import {Errors} from "./helpers/Errors.sol";
import {Events} from "./helpers/Events.sol";
import {TestUtils} from "./helpers/TestUtils.sol";
contract TestQuestClaimable is Test, Errors, Events, TestUtils {
using LibClone for address;
using LibString for *;
using JSONParserLib for string;
QuestFactory questFactory;
SampleERC1155 sampleERC1155;
SampleERC20 sampleERC20;
uint256 claimSignerPrivateKey;
uint256 TOTAL_PARTICIPANTS = 300;
uint256 END_TIME = 1_000_000_000;
uint256 START_TIME = 1_000_000;
uint16 REFERRAL_FEE = 2000;
uint256 NFT_QUEST_FEE = 10;
uint256 REWARD_AMOUNT = 10;
uint16 QUEST_FEE = 2000;
uint256 MINT_FEE = 100;
address protocolFeeRecipient = makeAddr("protocolFeeRecipient");
address questCreator = makeAddr(("questCreator"));
address participant = makeAddr(("participant"));
address referrer = makeAddr(("referrer"));
address anyone = makeAddr(("anyone"));
address owner = makeAddr(("owner"));
function setUp() public {
address payable questFactoryAddress = payable(address(new QuestFactory()).cloneDeterministic(keccak256(abi.encodePacked(msg.sender, "SALT"))));
questFactory = QuestFactory(questFactoryAddress);
sampleERC1155 = new SampleERC1155();
sampleERC20 = new SampleERC20("name", "symbol", 1000000, questCreator);
claimSignerPrivateKey = uint256(vm.envUint("TEST_CLAIM_SIGNER_PRIVATE_KEY"));
vm.deal(owner, 1000000);
vm.deal(participant, 1000000);
vm.deal(questCreator, 1000000);
vm.deal(anyone, 1000000);
questFactory.initialize(
vm.addr(claimSignerPrivateKey),
protocolFeeRecipient,
address(new Quest()),
payable(address(new Quest1155())),
owner,
NFT_QUEST_FEE,
REFERRAL_FEE,
MINT_FEE
);
}
/*//////////////////////////////////////////////////////////////
CLAIM ERC20
//////////////////////////////////////////////////////////////*/
function test_claim_with_referrer() public {
vm.startPrank(owner);
questFactory.setRewardAllowlistAddress(address(sampleERC20), true);
vm.startPrank(questCreator);
sampleERC20.approve(address(questFactory), calculateTotalRewardsPlusFee(TOTAL_PARTICIPANTS, REWARD_AMOUNT, QUEST_FEE));
address questAddress = questFactory.createERC20Quest(
101,
address(sampleERC20),
END_TIME,
START_TIME,
TOTAL_PARTICIPANTS,
REWARD_AMOUNT,
"550e8400-e29b-41d4-a716-446655440000",
"actionType",
"questName",
"projectName"
);
vm.warp(START_TIME + 1);
bytes32 txHash = hex'001975a6bf513022a8cc382a3cdb1e1dbcd58ebb1cb9abf11e64aadb21262516';
string memory json = '{"actionTxHashes":["0x001975a6bf513022a8cc382a3cdb1e1dbcd58ebb1cb9abf11e64aadb21262516"],"actionNetworkChainIds":[101],"actionType":"actionType"}';
bytes memory signData = abi.encode(participant, referrer, "550e8400-e29b-41d4-a716-446655440000", json);
bytes32 msgHash = keccak256(signData);
bytes32 digest = ECDSA.toEthSignedMessageHash(msgHash);
(, bytes32 r, bytes32 vs) = TestUtils.getSplitSignature(claimSignerPrivateKey, digest);
bytes memory data = abi.encodePacked(txHash, r, vs, referrer);
bytes memory payload = abi.encodePacked(abi.encodeWithSignature("claim()"), data);
vm.startPrank(participant, participant);
vm.recordLogs();
vm.expectRevert(IQuestFactory.Deprecated.selector);
(bool success, ) = questAddress.call{value: MINT_FEE}(payload);
vm.stopPrank();
}
function test_claim_without_referrer() public {
vm.startPrank(owner);
questFactory.setRewardAllowlistAddress(address(sampleERC20), true);
referrer = address(0);
vm.startPrank(questCreator);
sampleERC20.approve(address(questFactory), calculateTotalRewardsPlusFee(TOTAL_PARTICIPANTS, REWARD_AMOUNT, QUEST_FEE));
address questAddress = questFactory.createERC20Quest(
101,
address(sampleERC20),
END_TIME,
START_TIME,
TOTAL_PARTICIPANTS,
REWARD_AMOUNT,
"550e8400-e29b-41d4-a716-446655440000",
"actionType",
"questName",
"projectName"
);
vm.warp(START_TIME + 1);
bytes32 txHash = hex'001975a6bf513022a8cc382a3cdb1e1dbcd58ebb1cb9abf11e64aadb21262516';
string memory json = '{"actionTxHashes":["0x001975a6bf513022a8cc382a3cdb1e1dbcd58ebb1cb9abf11e64aadb21262516"],"actionNetworkChainIds":[101],"actionType":"actionType"}';
bytes memory signData = abi.encode(participant, referrer, "550e8400-e29b-41d4-a716-446655440000", json);
bytes32 msgHash = keccak256(signData);
bytes32 digest = ECDSA.toEthSignedMessageHash(msgHash);
(, bytes32 r, bytes32 vs) = TestUtils.getSplitSignature(claimSignerPrivateKey, digest);
bytes memory data = abi.encodePacked(txHash, r, vs);
bytes memory payload = abi.encodePacked(abi.encodeWithSignature("claim()"), data);
vm.startPrank(participant, participant);
vm.recordLogs();
vm.expectRevert(IQuestFactory.Deprecated.selector);
(bool success, ) = questAddress.call{value: MINT_FEE}(payload);
vm.stopPrank();
}
/*//////////////////////////////////////////////////////////////
CLAIM ERC1155
//////////////////////////////////////////////////////////////*/
function test_claim_1155_with_ref() public{
vm.startPrank(questCreator);
sampleERC1155.mintSingle(questCreator, 1, TOTAL_PARTICIPANTS);
sampleERC1155.setApprovalForAll(address(questFactory), true);
address questAddress = questFactory.createERC1155Quest{value: NFT_QUEST_FEE * TOTAL_PARTICIPANTS}(
7777777,
address(sampleERC1155),
END_TIME,
START_TIME,
TOTAL_PARTICIPANTS,
1,
"550e8400-e29b-41d4-a716-446655440000",
"actionType",
"questName",
"projectName"
);
vm.warp(START_TIME + 1);
bytes32 txHash = hex'7e1975a6bf513022a8cc382a3cdb1e1dbcd58ebb1cb9abf11e64aadb21262516';
string memory json = '{"actionTxHashes":["0x7e1975a6bf513022a8cc382a3cdb1e1dbcd58ebb1cb9abf11e64aadb21262516"],"actionNetworkChainIds":[7777777],"actionType":"actionType"}';
bytes memory signData = abi.encode(participant, referrer, "550e8400-e29b-41d4-a716-446655440000", json);
bytes32 msgHash = keccak256(signData);
bytes32 digest = ECDSA.toEthSignedMessageHash(msgHash);
(, bytes32 r, bytes32 vs) = TestUtils.getSplitSignature(claimSignerPrivateKey, digest);
bytes memory data = abi.encodePacked(txHash, r, vs, referrer);
bytes memory payload = abi.encodePacked(abi.encodeWithSignature("claim()"), data);
vm.startPrank(participant, participant);
vm.recordLogs();
vm.expectRevert(IQuestFactory.Deprecated.selector);
(bool success, ) = questAddress.call{value: MINT_FEE}(payload);
vm.stopPrank();
}
function test_claim_1155_without_ref() public{
vm.startPrank(questCreator);
referrer = address(0);
sampleERC1155.mintSingle(questCreator, 1, TOTAL_PARTICIPANTS);
sampleERC1155.setApprovalForAll(address(questFactory), true);
address questAddress = questFactory.createERC1155Quest{value: NFT_QUEST_FEE * TOTAL_PARTICIPANTS}(
7777777,
address(sampleERC1155),
END_TIME,
START_TIME,
TOTAL_PARTICIPANTS,
1,
"550e8400-e29b-41d4-a716-446655440000",
"actionType",
"questName",
"projectName"
);
vm.warp(START_TIME + 1);
bytes32 txHash = hex'7e1975a6bf513022a8cc382a3cdb1e1dbcd58ebb1cb9abf11e64aadb21262516';
string memory json = '{"actionTxHashes":["0x7e1975a6bf513022a8cc382a3cdb1e1dbcd58ebb1cb9abf11e64aadb21262516"],"actionNetworkChainIds":[7777777],"actionType":"actionType"}';
bytes memory signData = abi.encode(participant, referrer, "550e8400-e29b-41d4-a716-446655440000", json);
bytes32 msgHash = keccak256(signData);
bytes32 digest = ECDSA.toEthSignedMessageHash(msgHash);
(, bytes32 r, bytes32 vs) = TestUtils.getSplitSignature(claimSignerPrivateKey, digest);
bytes memory data = abi.encodePacked(txHash, r, vs);
bytes memory payload = abi.encodePacked(abi.encodeWithSignature("claim()"), data);
vm.startPrank(participant, participant);
vm.recordLogs();
vm.expectRevert(IQuestFactory.Deprecated.selector);
(bool success, ) = questAddress.call{value: MINT_FEE}(payload);
vm.stopPrank();
}
}