-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guilds.t.sol
529 lines (422 loc) · 17.2 KB
/
Guilds.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
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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {Test, console2, stdError} from "forge-std/Test.sol";
import {MarketplaceNFT} from "./MarketplaceNFT.sol";
import {Guilds} from "../src/Guilds.sol";
contract GuildsTest is Test {
////////
MarketplaceNFT public cube;
Guilds public guilds;
uint8 private constant EDGE_LENGTH = 8;
uint8 private constant NUM_COMMONS = 64;
uint8 private constant MOSAIC_ID = 81;
uint8 private constant CUBE_ID = 0;
uint8 private constant RANDOM_MOMENT = 8;
uint8 private constant RANDOM_OFFSET = 3;
uint8 private constant GUILD_ID_BLACKSMITHS = 2;
uint8 private constant STYLE_ID_SISTINE_CHAPEL = 2;
address private constant GUILDS_DEPLOYER = address(0xA1);
address private constant GUILDS_SALES = address(0xA2);
address private constant NEO_ETH = address(0xA3);
address private constant FAN_ETH = address(0xA4);
////////
function setUp() public {
vm.label(GUILDS_DEPLOYER, "Guilds Deployer");
vm.label(GUILDS_SALES, "Guilds Sales");
vm.label(NEO_ETH, "Neodaoist.eth");
vm.label(FAN_ETH, "ClassicalMusicFan.eth");
// Deploy contracts
vm.prank(NEO_ETH);
cube = new MarketplaceNFT();
vm.prank(GUILDS_DEPLOYER);
guilds = new Guilds(GUILDS_SALES, address(cube), 1);
// Mint initial 8x8x8 ultrarare CUBE
vm.prank(GUILDS_SALES);
cube.mint();
assertEq(cube.balanceOf(GUILDS_SALES), 1);
assertEq(cube.ownerOf(1), GUILDS_SALES);
// Send ultrarare CUBE to meltable contract, first time and only time
vm.prank(GUILDS_SALES);
cube.safeTransferFrom(GUILDS_SALES, address(guilds), 1);
}
//////// Initial Marketplace
function test_initialSendCube() public {
// Then
assertEq(cube.balanceOf(address(guilds)), 1);
assertEq(cube.ownerOf(1), address(guilds));
for (uint256 i = 1; i <= NUM_COMMONS; i++) {
assertEq(guilds.balanceOf(GUILDS_SALES, i), EDGE_LENGTH);
}
assertEq(guilds.balanceOf(GUILDS_SALES, CUBE_ID), 0);
}
//////// View Functions
// TODO all tests are passing; commenting out for now just to avoid
// having to create a test harness for internal function testing
// function test_parseMoment() public {
// uint8 guildId;
// uint8 styleId;
// (guildId, styleId) = guilds._parseMoment(1);
// assertEq(guildId, 1);
// assertEq(styleId, 1);
// (guildId, styleId) = guilds._parseMoment(64);
// assertEq(guildId, 8);
// assertEq(styleId, 8);
// }
// function test_parseMoment_many() public {
// uint8 guildId;
// uint8 styleId;
// for (uint256 i = 0; i < EDGE_LENGTH; ++i) {
// for (uint256 j = 1; j <= EDGE_LENGTH; ++j) {
// uint8 moment = uint8((i * EDGE_LENGTH) + j);
// (guildId, styleId) = guilds._parseMoment(moment);
// assertEq(guildId, j);
// assertEq(styleId, i + 1);
// }
// }
// }
// function testRevert_parseMoment() public {
// vm.expectRevert();
// guilds._parseMoment(0);
// vm.expectRevert();
// guilds._parseMoment(65);
// }
// function test_parseGuildStrip_many() public {
// assertEq(guilds._parseGuildStrip(65), 1);
// assertEq(guilds._parseGuildStrip(66), 2);
// assertEq(guilds._parseGuildStrip(67), 3);
// assertEq(guilds._parseGuildStrip(68), 4);
// assertEq(guilds._parseGuildStrip(69), 5);
// assertEq(guilds._parseGuildStrip(70), 6);
// assertEq(guilds._parseGuildStrip(71), 7);
// assertEq(guilds._parseGuildStrip(72), 8);
// }
// function testRevert_parseGuildStrip() public {
// vm.expectRevert();
// guilds._parseGuildStrip(1);
// vm.expectRevert();
// guilds._parseGuildStrip(8);
// vm.expectRevert();
// guilds._parseGuildStrip(73);
// }
// function test_parseStyleStrip_many() public {
// assertEq(guilds._parseStyleStrip(73), 1);
// assertEq(guilds._parseStyleStrip(74), 2);
// assertEq(guilds._parseStyleStrip(75), 3);
// assertEq(guilds._parseStyleStrip(76), 4);
// assertEq(guilds._parseStyleStrip(77), 5);
// assertEq(guilds._parseStyleStrip(78), 6);
// assertEq(guilds._parseStyleStrip(79), 7);
// assertEq(guilds._parseStyleStrip(80), 8);
// }
// function testRevert_parseStyleStrip() public {
// vm.expectRevert();
// guilds._parseStyleStrip(1);
// vm.expectRevert();
// guilds._parseStyleStrip(8);
// vm.expectRevert();
// guilds._parseStyleStrip(65);
// vm.expectRevert();
// guilds._parseStyleStrip(81);
// }
//////// Meltable Functions
// Melt all 8 common styles of a single guild into 1 uncommon GUILD moment strip
function test_meltGuildStrip() public {
// Do this for BLACKSMITHS, Guild #2
// Given
vm.startPrank(GUILDS_SALES);
for (uint256 i = 0; i < EDGE_LENGTH; i++) {
guilds.safeTransferFrom(GUILDS_SALES, FAN_ETH, (i * EDGE_LENGTH) + GUILD_ID_BLACKSMITHS, 1, "");
}
vm.stopPrank();
for (uint256 i = 0; i < EDGE_LENGTH; i++) {
assertEq(guilds.balanceOf(GUILDS_SALES, (i * EDGE_LENGTH) + GUILD_ID_BLACKSMITHS), EDGE_LENGTH - 1);
assertEq(guilds.balanceOf(FAN_ETH, (i * EDGE_LENGTH) + GUILD_ID_BLACKSMITHS), 1);
}
assertEq(guilds.balanceOf(FAN_ETH, NUM_COMMONS + GUILD_ID_BLACKSMITHS), 0);
// When
vm.prank(FAN_ETH);
guilds.meltGuildStrip(GUILD_ID_BLACKSMITHS);
// Then
for (uint256 i = 0; i < EDGE_LENGTH; i++) {
assertEq(guilds.balanceOf(FAN_ETH, (i * EDGE_LENGTH) + GUILD_ID_BLACKSMITHS), 0);
}
assertEq(guilds.balanceOf(FAN_ETH, NUM_COMMONS + GUILD_ID_BLACKSMITHS), 1);
}
function test_meltGuildStrip_many() public {
// Do this for all 8 guilds
for (uint8 guildId = 1; guildId <= EDGE_LENGTH; guildId++) {
// Given
vm.startPrank(GUILDS_SALES);
for (uint256 i = 0; i < EDGE_LENGTH; i++) {
guilds.safeTransferFrom(GUILDS_SALES, FAN_ETH, (i * EDGE_LENGTH) + guildId, 1, "");
}
vm.stopPrank();
for (uint256 i = 0; i < EDGE_LENGTH; i++) {
assertEq(guilds.balanceOf(GUILDS_SALES, (i * EDGE_LENGTH) + guildId), EDGE_LENGTH - 1);
assertEq(guilds.balanceOf(FAN_ETH, (i * EDGE_LENGTH) + guildId), 1);
}
assertEq(guilds.balanceOf(FAN_ETH, NUM_COMMONS + guildId), 0);
// When
vm.prank(FAN_ETH);
guilds.meltGuildStrip(guildId);
// Then
for (uint256 i = 0; i < EDGE_LENGTH; i++) {
assertEq(guilds.balanceOf(FAN_ETH, (i * EDGE_LENGTH) + guildId), 0);
}
assertEq(guilds.balanceOf(FAN_ETH, NUM_COMMONS + guildId), 1);
}
}
function testRevert_meltGuildStrip_missingStyle() public {
// Given
vm.startPrank(GUILDS_SALES);
for (uint256 i = 0; i < EDGE_LENGTH; i++) {
guilds.safeTransferFrom(GUILDS_SALES, FAN_ETH, (i * EDGE_LENGTH) + GUILD_ID_BLACKSMITHS, 1, "");
}
vm.stopPrank();
vm.prank(FAN_ETH);
guilds.safeTransferFrom(FAN_ETH, GUILDS_SALES, (RANDOM_OFFSET * EDGE_LENGTH) + GUILD_ID_BLACKSMITHS, 1, "");
// Then
vm.expectRevert(stdError.arithmeticError);
// When
vm.prank(FAN_ETH);
guilds.meltGuildStrip(GUILD_ID_BLACKSMITHS);
}
// Unmelt 1 uncommon GUILD moment strip into 8 common styles of a single guild
function test_unmeltGuildStrip() public {
// Given
vm.startPrank(GUILDS_SALES);
for (uint256 i = 0; i < EDGE_LENGTH; i++) {
guilds.safeTransferFrom(GUILDS_SALES, FAN_ETH, (i * EDGE_LENGTH) + GUILD_ID_BLACKSMITHS, 1, "");
}
vm.stopPrank();
vm.prank(FAN_ETH);
guilds.meltGuildStrip(GUILD_ID_BLACKSMITHS);
for (uint256 i = 0; i < EDGE_LENGTH; i++) {
assertEq(guilds.balanceOf(FAN_ETH, (i * EDGE_LENGTH) + GUILD_ID_BLACKSMITHS), 0);
}
assertEq(guilds.balanceOf(FAN_ETH, NUM_COMMONS + GUILD_ID_BLACKSMITHS), 1);
// When
vm.prank(FAN_ETH);
guilds.unmeltGuildStrip(GUILD_ID_BLACKSMITHS);
// Then
for (uint256 i = 0; i < EDGE_LENGTH; i++) {
assertEq(guilds.balanceOf(FAN_ETH, (i * EDGE_LENGTH) + GUILD_ID_BLACKSMITHS), 1);
}
assertEq(guilds.balanceOf(FAN_ETH, NUM_COMMONS + GUILD_ID_BLACKSMITHS), 0);
}
function testRevert_unmeltGuildStrip_missingGuildStrip() public {
// Then
vm.expectRevert(stdError.arithmeticError);
// When
vm.prank(FAN_ETH);
guilds.unmeltGuildStrip(GUILD_ID_BLACKSMITHS);
}
// Melt all 8 common guilds of a single style into 1 uncommon STYLE moment strip
function test_meltStyleStrip() public {
// Do this for SISTINE CHAPEL, Style #2
// Given
vm.startPrank(GUILDS_SALES);
for (uint256 i = 1; i <= EDGE_LENGTH; i++) {
guilds.safeTransferFrom(GUILDS_SALES, FAN_ETH, ((STYLE_ID_SISTINE_CHAPEL - 1) * EDGE_LENGTH) + i, 1, "");
}
vm.stopPrank();
for (uint256 i = 1; i <= EDGE_LENGTH; i++) {
assertEq(guilds.balanceOf(GUILDS_SALES, ((STYLE_ID_SISTINE_CHAPEL - 1) * EDGE_LENGTH) + i), EDGE_LENGTH - 1);
assertEq(guilds.balanceOf(FAN_ETH, ((STYLE_ID_SISTINE_CHAPEL - 1) * EDGE_LENGTH) + i), 1);
}
assertEq(guilds.balanceOf(FAN_ETH, NUM_COMMONS + EDGE_LENGTH + STYLE_ID_SISTINE_CHAPEL), 0);
// When
vm.prank(FAN_ETH);
guilds.meltStyleStrip(STYLE_ID_SISTINE_CHAPEL);
// Then
for (uint256 i = 1; i <= EDGE_LENGTH; i++) {
assertEq(guilds.balanceOf(FAN_ETH, ((STYLE_ID_SISTINE_CHAPEL - 1) * EDGE_LENGTH) + i), 0);
}
assertEq(guilds.balanceOf(FAN_ETH, NUM_COMMONS + EDGE_LENGTH + STYLE_ID_SISTINE_CHAPEL), 1);
}
function test_meltStyleStrip_many() public {
// Do this for all 8 styles
for (uint8 styleId = 1; styleId <= EDGE_LENGTH; styleId++) {
// Given
vm.startPrank(GUILDS_SALES);
for (uint256 i = 1; i <= EDGE_LENGTH; i++) {
guilds.safeTransferFrom(GUILDS_SALES, FAN_ETH, ((styleId - 1) * EDGE_LENGTH) + i, 1, "");
}
vm.stopPrank();
for (uint256 i = 1; i <= EDGE_LENGTH; i++) {
assertEq(guilds.balanceOf(GUILDS_SALES, ((styleId - 1) * EDGE_LENGTH) + i), EDGE_LENGTH - 1);
assertEq(guilds.balanceOf(FAN_ETH, ((styleId - 1) * EDGE_LENGTH) + i), 1);
}
assertEq(guilds.balanceOf(FAN_ETH, NUM_COMMONS + EDGE_LENGTH + styleId), 0);
// When
vm.prank(FAN_ETH);
guilds.meltStyleStrip(styleId);
// Then
for (uint256 i = 1; i <= EDGE_LENGTH; i++) {
assertEq(guilds.balanceOf(FAN_ETH, ((styleId - 1) * EDGE_LENGTH) + i), 0);
}
assertEq(guilds.balanceOf(FAN_ETH, NUM_COMMONS + EDGE_LENGTH + styleId), 1);
}
}
function testRevert_meltStyleStrip_missingGuild() public {
// Given
vm.startPrank(GUILDS_SALES);
for (uint256 i = 1; i <= EDGE_LENGTH; i++) {
guilds.safeTransferFrom(GUILDS_SALES, FAN_ETH, ((STYLE_ID_SISTINE_CHAPEL - 1) * EDGE_LENGTH) + i, 1, "");
}
vm.stopPrank();
vm.prank(FAN_ETH);
guilds.safeTransferFrom(
FAN_ETH, GUILDS_SALES, ((STYLE_ID_SISTINE_CHAPEL - 1) * EDGE_LENGTH) + RANDOM_OFFSET, 1, ""
);
// Then
vm.expectRevert(stdError.arithmeticError);
// When
vm.prank(FAN_ETH);
guilds.meltStyleStrip(STYLE_ID_SISTINE_CHAPEL);
}
// Unmelt 1 uncommon STYLE moment strip into 8 common guilds of a single style
function test_unmeltStyleStrip() public {
// Given
vm.startPrank(GUILDS_SALES);
for (uint256 i = 1; i <= EDGE_LENGTH; i++) {
guilds.safeTransferFrom(GUILDS_SALES, FAN_ETH, ((STYLE_ID_SISTINE_CHAPEL - 1) * EDGE_LENGTH) + i, 1, "");
}
vm.stopPrank();
vm.prank(FAN_ETH);
guilds.meltStyleStrip(STYLE_ID_SISTINE_CHAPEL);
// When
vm.prank(FAN_ETH);
guilds.unmeltStyleStrip(STYLE_ID_SISTINE_CHAPEL);
// Then
for (uint256 i = 1; i <= EDGE_LENGTH; i++) {
assertEq(guilds.balanceOf(FAN_ETH, ((STYLE_ID_SISTINE_CHAPEL - 1) * EDGE_LENGTH) + i), 1);
}
assertEq(guilds.balanceOf(FAN_ETH, NUM_COMMONS + EDGE_LENGTH + STYLE_ID_SISTINE_CHAPEL), 0);
}
function testRevert_unmeltStyleStrip_missingStyleStrip() public {
// Then
vm.expectRevert(stdError.arithmeticError);
// When
vm.prank(FAN_ETH);
guilds.unmeltStyleStrip(STYLE_ID_SISTINE_CHAPEL);
}
// Melt all 64 common moments into 1 rare MOSAIC moment sheet
function test_meltMosaicSheet() public {
// Given
vm.startPrank(GUILDS_SALES);
for (uint256 i = 1; i <= NUM_COMMONS; i++) {
guilds.safeTransferFrom(GUILDS_SALES, FAN_ETH, i, 1, "");
}
vm.stopPrank();
for (uint256 i = 1; i <= NUM_COMMONS; i++) {
assertEq(guilds.balanceOf(GUILDS_SALES, i), EDGE_LENGTH - 1);
assertEq(guilds.balanceOf(FAN_ETH, i), 1);
}
assertEq(guilds.balanceOf(GUILDS_SALES, MOSAIC_ID), 0);
// When
vm.prank(FAN_ETH);
guilds.meltMosaicSheet();
// Then
for (uint256 i = 1; i <= NUM_COMMONS; i++) {
assertEq(guilds.balanceOf(FAN_ETH, i), 0);
}
assertEq(guilds.balanceOf(FAN_ETH, MOSAIC_ID), 1);
}
function testRevert_meltMosaicSheet_missingToken() public {
// Given
vm.startPrank(GUILDS_SALES);
for (uint256 i = 1; i <= NUM_COMMONS; i++) {
guilds.safeTransferFrom(GUILDS_SALES, FAN_ETH, i, 1, "");
}
vm.stopPrank();
vm.prank(FAN_ETH);
guilds.safeTransferFrom(FAN_ETH, GUILDS_SALES, RANDOM_MOMENT, 1, "");
// Then
vm.expectRevert(stdError.arithmeticError);
// When
vm.prank(FAN_ETH);
guilds.meltMosaicSheet();
}
// Unmelt 1 rare MOSAIC moment sheet into 64 common moments
function test_unmeltMosaicSheet() public {
// Given
vm.startPrank(GUILDS_SALES);
for (uint256 i = 1; i <= NUM_COMMONS; i++) {
guilds.safeTransferFrom(GUILDS_SALES, FAN_ETH, i, 1, "");
}
vm.stopPrank();
vm.prank(FAN_ETH);
guilds.meltMosaicSheet();
for (uint256 i = 1; i <= NUM_COMMONS; i++) {
assertEq(guilds.balanceOf(FAN_ETH, i), 0);
}
assertEq(guilds.balanceOf(FAN_ETH, MOSAIC_ID), 1);
// When
vm.prank(FAN_ETH);
guilds.unmeltMosaicSheet();
// Then
for (uint256 i = 1; i <= NUM_COMMONS; i++) {
assertEq(guilds.balanceOf(FAN_ETH, i), 1);
}
assertEq(guilds.balanceOf(FAN_ETH, MOSAIC_ID), 0);
}
function testRevert_unmeltMosaicSheet_missingMosaic() public {
// Then
vm.expectRevert(stdError.arithmeticError);
// When
vm.prank(FAN_ETH);
guilds.unmeltMosaicSheet();
}
// Melt all 64 common moments into 1 ultrarare CUBE
function test_meltCube() public {
// Given
for (uint256 i = 1; i <= NUM_COMMONS; i++) {
assertEq(guilds.balanceOf(GUILDS_SALES, i), EDGE_LENGTH);
}
assertEq(guilds.balanceOf(GUILDS_SALES, CUBE_ID), 0);
// When
vm.prank(GUILDS_SALES);
guilds.meltCube();
// Then
for (uint256 i = 1; i <= NUM_COMMONS; i++) {
assertEq(guilds.balanceOf(GUILDS_SALES, i), 0);
}
assertEq(guilds.balanceOf(GUILDS_SALES, CUBE_ID), 1);
}
function testRevert_meltCube_missingToken() public {
// Given
vm.prank(GUILDS_SALES);
guilds.safeTransferFrom(GUILDS_SALES, FAN_ETH, RANDOM_MOMENT, 1, "");
// Then
vm.expectRevert(stdError.arithmeticError);
// When
vm.prank(GUILDS_SALES);
guilds.meltCube();
}
// Unmelt 1 ultrarare CUBE into 64 common moments
function test_unmeltCube() public {
// Given
vm.prank(GUILDS_SALES);
guilds.meltCube();
for (uint256 i = 1; i <= NUM_COMMONS; i++) {
assertEq(guilds.balanceOf(GUILDS_SALES, i), 0);
}
assertEq(guilds.balanceOf(GUILDS_SALES, CUBE_ID), 1);
// When
vm.prank(GUILDS_SALES);
guilds.unmeltCube();
// Then
for (uint256 i = 1; i <= NUM_COMMONS; i++) {
assertEq(guilds.balanceOf(GUILDS_SALES, i), EDGE_LENGTH);
}
assertEq(guilds.balanceOf(GUILDS_SALES, CUBE_ID), 0);
}
function testRevert_unmeltCube_missingCube() public {
// Then
vm.expectRevert(stdError.arithmeticError);
// When
vm.prank(GUILDS_SALES);
guilds.unmeltCube();
}
}