-
Notifications
You must be signed in to change notification settings - Fork 3
/
TheSpace.t.sol
922 lines (731 loc) · 30.3 KB
/
TheSpace.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
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
//SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.11;
import "./BaseTheSpace.t.sol";
contract TheSpaceTest is BaseTheSpaceTest {
/**
* Upgradablitity
*/
function testUpgradeTo() public {
// deploy new logic contract
TheSpace thespace2 = new TheSpace(
address(currency),
address(registry),
TOKEN_IMAGE_URI,
ACL_MANAGER,
MARKET_ADMIN,
TREASURY_ADMIN
);
assertEq(address(thespace2.registry()), address(registry));
assertEq(registry.owner(), address(thespace));
// upgrade from old logic contract
vm.prank(ACL_MANAGER);
thespace.upgradeTo(address(thespace2));
assertEq(registry.owner(), address(thespace2));
}
function testCannotUpgradeByAttacker() public {
vm.expectRevert(abi.encodeWithSignature("RoleRequired(uint8)", ROLE_ACL_MANAGER));
thespace.upgradeTo(address(1234));
}
/**
* Total Supply
*/
function testTotalSupply() public {
assertGt(registry.totalSupply(), 0);
}
function testSetTotalSupply(uint256 newTotalSupply) public {
vm.assume(newTotalSupply > 0);
// bid a token
_bid(PIXEL_PRICE, PIXEL_PRICE);
// collect tax
_rollBlock();
assertGt(thespace.getTax(PIXEL_ID), 0);
thespace.settleTax(PIXEL_ID);
// check prev UBI
uint256 prevTotalSupply = registry.totalSupply();
uint256 prevUBI = thespace.ubiAvailable(PIXEL_ID);
assertGt(prevUBI, 0);
// change total supply
vm.prank(MARKET_ADMIN);
thespace.setTotalSupply(newTotalSupply);
// check new UBI
uint256 newUBI = thespace.ubiAvailable(PIXEL_ID);
if (newTotalSupply >= prevTotalSupply) {
assertLe(newUBI, prevUBI);
} else {
assertGt(newUBI, prevUBI);
}
}
function testCannotSetTotalSupplyByAttacker() public {
vm.expectRevert(abi.encodeWithSignature("RoleRequired(uint8)", ROLE_MARKET_ADMIN));
vm.prank(ATTACKER);
thespace.setTotalSupply(1000);
}
/**
* @dev Config
*/
function testGetConfig() public {
assertGe(registry.taxConfig(CONFIG_TAX_RATE), 0);
assertGe(registry.taxConfig(CONFIG_TREASURY_SHARE), 0);
assertGe(registry.taxConfig(CONFIG_MINT_TAX), 0);
}
function testCannotSetConfigByAttacker() public {
vm.expectRevert(abi.encodeWithSignature("RoleRequired(uint8)", ROLE_MARKET_ADMIN));
vm.prank(ATTACKER);
thespace.setTaxConfig(CONFIG_TAX_RATE, 1000);
}
function testSetTaxRate() public {
// set tax rate
uint256 taxRate = 1000;
vm.prank(MARKET_ADMIN);
vm.expectEmit(true, true, false, false);
emit Config(CONFIG_TAX_RATE, taxRate);
thespace.setTaxConfig(CONFIG_TAX_RATE, taxRate);
// bid a token
_bid(PIXEL_PRICE, PIXEL_PRICE);
// roll block.number and check tax
uint256 blockRollsTo = block.number + TAX_WINDOW;
(, uint256 lastTaxCollection, ) = registry.tokenRecord(PIXEL_ID);
uint256 tax = (PIXEL_PRICE * taxRate * (blockRollsTo - lastTaxCollection)) / (1000 * 10000);
vm.roll(blockRollsTo);
assertEq(thespace.getTax(PIXEL_ID), tax);
assertGt(thespace.getTax(PIXEL_ID), 0);
// change tax rate and recheck
uint256 newTaxRate = 10;
vm.prank(MARKET_ADMIN);
vm.expectEmit(true, true, false, false);
emit Config(CONFIG_TAX_RATE, newTaxRate);
thespace.setTaxConfig(CONFIG_TAX_RATE, newTaxRate);
uint256 newTax = (PIXEL_PRICE * newTaxRate * (blockRollsTo - lastTaxCollection)) / (1000 * 10000);
vm.roll(blockRollsTo);
assertEq(thespace.getTax(PIXEL_ID), newTax);
assertGt(thespace.getTax(PIXEL_ID), 0);
// zero tax
vm.prank(MARKET_ADMIN);
vm.expectEmit(true, true, false, false);
emit Config(CONFIG_TAX_RATE, 0);
thespace.setTaxConfig(CONFIG_TAX_RATE, 0);
vm.roll(blockRollsTo);
assertEq(thespace.getTax(PIXEL_ID), 0);
}
function testSetTreasuryShare() public {
// set treasury share
uint256 share = 1000;
vm.prank(MARKET_ADMIN);
vm.expectEmit(true, true, false, false);
emit Config(CONFIG_TREASURY_SHARE, share);
thespace.setTaxConfig(CONFIG_TREASURY_SHARE, share);
// bid a token
_bid(PIXEL_PRICE, PIXEL_PRICE);
_rollBlock();
// check treasury share
uint256 tax = thespace.getTax(PIXEL_ID);
(, uint256 prevTreasury, ) = registry.treasuryRecord();
uint256 expectTreasury = prevTreasury + ((tax * share) / 10000);
thespace.settleTax(PIXEL_ID);
(, uint256 newTreasury, ) = registry.treasuryRecord();
assertEq(newTreasury, expectTreasury);
assertGt(newTreasury, 0);
}
function testSetMintTax() public {
// set mint tax
uint256 mintTax = 50 * (10**uint256(currency.decimals()));
vm.expectEmit(true, true, false, false);
emit Config(CONFIG_MINT_TAX, mintTax);
_setMintTax(mintTax);
// bid a token with mint tax as amount
uint256 prevBalance = currency.balanceOf(PIXEL_OWNER);
_bid(mintTax);
assertEq(currency.balanceOf(PIXEL_OWNER), prevBalance - mintTax);
}
function testWithdrawTreasury() public {
// bid a token
_bid(PIXEL_PRICE, PIXEL_PRICE);
// collect tax
_rollBlock();
thespace.settleTax(PIXEL_ID);
uint256 prevTreasuryBalance = currency.balanceOf(TREASURY);
uint256 prevContractBalance = currency.balanceOf(address(registry));
(, uint256 accumulatedTreasury, uint256 treasuryWithdrawn) = registry.treasuryRecord();
uint256 amount = accumulatedTreasury - treasuryWithdrawn;
// withdraw treasury
vm.prank(TREASURY_ADMIN);
vm.expectEmit(true, true, false, false);
emit Treasury(TREASURY, amount);
thespace.withdrawTreasury(TREASURY);
// check treasury balance
assertEq(currency.balanceOf(TREASURY), prevTreasuryBalance + amount);
(, , uint256 newTreasuryWithdrawn) = registry.treasuryRecord();
assertEq(newTreasuryWithdrawn, accumulatedTreasury);
// check contract balance
assertEq(currency.balanceOf(address(registry)), prevContractBalance - amount);
}
/**
* @dev Pixel
*/
function testGetNonExistingPixel() public {
uint256 mintTax = 10 * (10**uint256(currency.decimals()));
_setMintTax(mintTax);
ITheSpaceRegistry.Pixel memory pixel = thespace.getPixel(PIXEL_ID);
assertEq(pixel.price, mintTax);
assertEq(pixel.color, 0);
assertEq(pixel.owner, address(0));
}
function testGetExistingPixel() public {
_bid(PIXEL_PRICE, PIXEL_PRICE);
ITheSpaceRegistry.Pixel memory pixel = thespace.getPixel(PIXEL_ID);
assertEq(pixel.price, PIXEL_PRICE);
assertEq(pixel.color, 0);
assertEq(pixel.owner, PIXEL_OWNER);
}
function testSetPixel(uint256 bidPrice) public {
vm.assume(bidPrice > PIXEL_PRICE && bidPrice < registry.currency().totalSupply());
uint256 newPrice = bidPrice + 1;
// bid with PIXEL_OWNER
_bid(PIXEL_PRICE, PIXEL_PRICE);
// bid with PIXEL_OWNER_1
vm.expectEmit(true, true, true, true);
emit Deal(PIXEL_ID, PIXEL_OWNER, PIXEL_OWNER_1, thespace.getPrice(PIXEL_ID));
// set to new price from `setPrice()`
vm.expectEmit(true, true, true, false);
emit Price(PIXEL_ID, newPrice, PIXEL_OWNER_1);
vm.expectEmit(true, true, true, false);
emit Color(PIXEL_ID, PIXEL_COLOR, PIXEL_OWNER_1);
vm.prank(PIXEL_OWNER_1);
thespace.setPixel(PIXEL_ID, bidPrice, newPrice, PIXEL_COLOR);
assertEq(thespace.getPrice(PIXEL_ID), newPrice);
assertEq(thespace.getColor(PIXEL_ID), PIXEL_COLOR);
}
function testCannotSetPixel(uint256 bidPrice) public {
vm.assume(bidPrice < PIXEL_PRICE);
// bid with PIXEL_OWNER
_bid(PIXEL_PRICE, PIXEL_PRICE);
// PIXEL_OWNER_1 bids with lower price
vm.prank(PIXEL_OWNER_1);
vm.expectRevert(abi.encodePacked(bytes4(keccak256("PriceTooLow()"))));
thespace.setPixel(PIXEL_ID, bidPrice, bidPrice, PIXEL_COLOR);
}
function testBatchSetPixels(uint16 price, uint8 color) public {
uint256 finalPrice = uint256(price) + 1;
uint256 finalColor = 5;
bytes[] memory data = new bytes[](3);
// bid pixel
data[0] = abi.encodeWithSignature(
"setPixel(uint256,uint256,uint256,uint256)",
PIXEL_ID,
PIXEL_PRICE,
uint256(price),
uint256(color)
);
// set price
data[1] = abi.encodeWithSignature(
"setPixel(uint256,uint256,uint256,uint256)",
PIXEL_ID,
PIXEL_PRICE,
finalPrice,
uint256(color)
);
// set color
data[2] = abi.encodeWithSignature(
"setPixel(uint256,uint256,uint256,uint256)",
PIXEL_ID,
PIXEL_PRICE,
finalPrice,
finalColor
);
vm.prank(PIXEL_OWNER);
thespace.multicall(data);
assertEq(thespace.getPrice(PIXEL_ID), finalPrice);
assertEq(thespace.getColor(PIXEL_ID), finalColor);
}
/**
* @dev Color
*/
function testSetColor() public {
_bid(PIXEL_PRICE, PIXEL_PRICE);
uint256 color = 5;
vm.prank(PIXEL_OWNER);
vm.expectEmit(true, true, true, false);
emit Color(PIXEL_ID, color, PIXEL_OWNER);
thespace.setColor(PIXEL_ID, color);
assertEq(thespace.getColor(PIXEL_ID), color);
}
function testCannotSetColorByAttacker() public {
_bid(PIXEL_PRICE, PIXEL_PRICE);
uint256 color = 6;
vm.prank(ATTACKER);
vm.expectRevert(abi.encodePacked(bytes4(keccak256("Unauthorized()"))));
thespace.setColor(PIXEL_ID, color);
}
/**
* @dev Owner Tokens
*/
function _assertEqArray(ITheSpaceRegistry.Pixel[] memory a, ITheSpaceRegistry.Pixel[] memory b) private {
assert(a.length == b.length);
for (uint256 i = 0; i < a.length; i++) {
assertEq(a[i].tokenId, b[i].tokenId);
assertEq(a[i].price, b[i].price);
assertEq(a[i].lastTaxCollection, b[i].lastTaxCollection);
assertEq(a[i].ubi, b[i].ubi);
assertEq(a[i].owner, b[i].owner);
assertEq(a[i].color, b[i].color);
}
}
function testGetPixelsByOwnerWithNoPixels() public {
ITheSpaceRegistry.Pixel[] memory empty = new ITheSpaceRegistry.Pixel[](0);
assertEq(registry.balanceOf(PIXEL_OWNER), 0);
(uint256 total0, uint256 limit0, uint256 offset0, ITheSpaceRegistry.Pixel[] memory pixels0) = thespace
.getPixelsByOwner(PIXEL_OWNER, 1, 0);
assertEq(total0, 0);
assertEq(limit0, 1);
assertEq(offset0, 0);
_assertEqArray(pixels0, empty);
(, , , ITheSpaceRegistry.Pixel[] memory pixels1) = thespace.getPixelsByOwner(PIXEL_OWNER, 1, 1);
_assertEqArray(pixels1, empty);
(, , , ITheSpaceRegistry.Pixel[] memory pixels2) = thespace.getPixelsByOwner(PIXEL_OWNER, 0, 0);
_assertEqArray(pixels2, empty);
}
function testGetPixelsByOwnerWithOnePixel() public {
uint256 tokenId1 = 100;
ITheSpaceRegistry.Pixel[] memory empty = new ITheSpaceRegistry.Pixel[](0);
ITheSpaceRegistry.Pixel[] memory one = new ITheSpaceRegistry.Pixel[](1);
_bidThis(tokenId1, PIXEL_PRICE);
one[0] = thespace.getPixel(tokenId1);
assertEq(registry.balanceOf(PIXEL_OWNER), 1);
// get this pixel
(uint256 total0, uint256 limit0, uint256 offset0, ITheSpaceRegistry.Pixel[] memory pixels0) = thespace
.getPixelsByOwner(PIXEL_OWNER, 1, 0);
assertEq(total0, 1);
assertEq(limit0, 1);
assertEq(offset0, 0);
_assertEqArray(pixels0, one);
(, , , ITheSpaceRegistry.Pixel[] memory pixels1) = thespace.getPixelsByOwner(PIXEL_OWNER, 10, 0);
_assertEqArray(pixels1, one);
// query with limit=0
(uint256 total2, uint256 limit2, , ITheSpaceRegistry.Pixel[] memory pixels2) = thespace.getPixelsByOwner(
PIXEL_OWNER,
0,
0
);
assertEq(total2, 1);
assertEq(limit2, 0);
_assertEqArray(pixels2, empty);
(, , , ITheSpaceRegistry.Pixel[] memory pixels3) = thespace.getPixelsByOwner(PIXEL_OWNER, 0, 1);
_assertEqArray(pixels3, empty);
// query with offset>=total
(, , , ITheSpaceRegistry.Pixel[] memory pixels4) = thespace.getPixelsByOwner(PIXEL_OWNER, 1, 1);
_assertEqArray(pixels4, empty);
(, , , ITheSpaceRegistry.Pixel[] memory pixels5) = thespace.getPixelsByOwner(PIXEL_OWNER, 1, 2);
_assertEqArray(pixels5, empty);
}
function testGetPixelsPageByOwnerWithPixels() public {
uint256 tokenId1 = 100;
uint256 tokenId2 = 101;
ITheSpaceRegistry.Pixel[] memory empty = new ITheSpaceRegistry.Pixel[](0);
ITheSpaceRegistry.Pixel[] memory two = new ITheSpaceRegistry.Pixel[](2);
_bidThis(tokenId1, PIXEL_PRICE);
_bidThis(tokenId2, PIXEL_PRICE);
two[0] = thespace.getPixel(tokenId1);
two[1] = thespace.getPixel(tokenId2);
// query with limit>=total
assertEq(registry.balanceOf(PIXEL_OWNER), 2);
(uint256 total0, uint256 limit0, uint256 offset0, ITheSpaceRegistry.Pixel[] memory pixels0) = thespace
.getPixelsByOwner(PIXEL_OWNER, 2, 0);
assertEq(total0, 2);
assertEq(limit0, 2);
assertEq(offset0, 0);
_assertEqArray(pixels0, two);
(, , , ITheSpaceRegistry.Pixel[] memory pixels1) = thespace.getPixelsByOwner(PIXEL_OWNER, 10, 0);
_assertEqArray(pixels1, two);
// query with 0<limit<total
ITheSpaceRegistry.Pixel[] memory pixelsPage1 = new ITheSpaceRegistry.Pixel[](1);
ITheSpaceRegistry.Pixel[] memory pixelsPage2 = new ITheSpaceRegistry.Pixel[](1);
pixelsPage1[0] = thespace.getPixel(tokenId1);
pixelsPage2[0] = thespace.getPixel(tokenId2);
(uint256 total2, , , ITheSpaceRegistry.Pixel[] memory pixels2) = thespace.getPixelsByOwner(PIXEL_OWNER, 1, 0);
assertEq(total2, 2);
_assertEqArray(pixels2, pixelsPage1);
(, , , ITheSpaceRegistry.Pixel[] memory pixels3) = thespace.getPixelsByOwner(PIXEL_OWNER, 1, 1);
_assertEqArray(pixels3, pixelsPage2);
// query with offset>=total
(, , , ITheSpaceRegistry.Pixel[] memory pixels4) = thespace.getPixelsByOwner(PIXEL_OWNER, 1, 2);
_assertEqArray(pixels4, empty);
(, , , ITheSpaceRegistry.Pixel[] memory pixels5) = thespace.getPixelsByOwner(PIXEL_OWNER, 1, 10);
_assertEqArray(pixels5, empty);
}
/**
* @dev Price
*/
function testGetPrice() public {
_bid(PIXEL_PRICE, PIXEL_PRICE);
assertEq(thespace.getPrice(PIXEL_ID), PIXEL_PRICE);
}
function testGetNonExistingPrice() public {
uint256 mintTax = registry.taxConfig(CONFIG_MINT_TAX);
assertEq(thespace.getPrice(PIXEL_ID + 1), mintTax);
}
function testSetPrice(uint256 price) public {
vm.assume(price <= registry.currency().totalSupply());
vm.assume(price > 0);
// bid a token and set price
_bid(PIXEL_PRICE, price);
assertEq(thespace.getPrice(PIXEL_ID), price);
}
function testSetPriceByOperator(uint256 price) public {
vm.assume(price <= registry.currency().totalSupply());
vm.assume(price > 0);
// bid a token and set price
_bid(PIXEL_PRICE, PIXEL_PRICE);
// approve pixel to operator
vm.prank(PIXEL_OWNER);
registry.approve(OPERATOR, PIXEL_ID);
// set price
vm.expectEmit(true, true, true, false);
emit Price(PIXEL_ID, price, PIXEL_OWNER);
vm.prank(OPERATOR);
thespace.setPrice(PIXEL_ID, price);
assertEq(thespace.getPrice(PIXEL_ID), price);
}
function testCannotSetPriceByNonOwner() public {
// bid a token
_bid(PIXEL_PRICE, PIXEL_PRICE);
// someone tries to set price
vm.expectRevert(abi.encodeWithSignature("Unauthorized()"));
vm.prank(ATTACKER);
thespace.setPrice(PIXEL_ID, PIXEL_PRICE);
}
function testSetPriceTooHigh() public {
_bid(PIXEL_PRICE, PIXEL_PRICE);
uint256 maxPrice = registry.currency().totalSupply();
uint256 newPrice = maxPrice + 1;
vm.expectRevert(abi.encodeWithSignature("PriceTooHigh(uint256)", maxPrice));
vm.prank(PIXEL_OWNER);
thespace.setPrice(PIXEL_ID, newPrice);
}
/**
* @dev Owner
*/
function testGetOwner() public {
_bid(PIXEL_PRICE, PIXEL_PRICE);
assertEq(thespace.getOwner(PIXEL_ID), PIXEL_OWNER);
_bidAs(PIXEL_OWNER_1, thespace.getPrice(PIXEL_ID));
assertEq(thespace.getOwner(PIXEL_ID), PIXEL_OWNER_1);
}
function testGetOwnerOfNonExistingToken() public {
assertEq(thespace.getOwner(PIXEL_ID), address(0));
}
/**
* @dev Bid
*/
function testBidNewToken() public {
_bid(PIXEL_PRICE, PIXEL_PRICE);
assertEq(registry.balanceOf(PIXEL_OWNER), 1);
// check price: bid a new pixel will set the price
assertEq(thespace.getPrice(PIXEL_ID), PIXEL_PRICE);
}
function testBidExistingToken() public {
// PIXEL_OWNER bids a pixel
_bid(PIXEL_PRICE, PIXEL_PRICE);
uint256 bidderCurrencyOldBlanace = currency.balanceOf(PIXEL_OWNER_1);
uint256 sellerCurrencyOldBlanace = currency.balanceOf(PIXEL_OWNER);
// PIXEL_OWNER_1 bids a pixel from PIXEL_OWNER
uint256 newBidPrice = PIXEL_PRICE + 1000;
_bidAs(PIXEL_OWNER_1, newBidPrice);
// check ERC-721 balance
assertEq(registry.balanceOf(PIXEL_OWNER), 0);
assertEq(registry.balanceOf(PIXEL_OWNER_1), 1);
// check currency balance
assertEq(currency.balanceOf(PIXEL_OWNER_1), bidderCurrencyOldBlanace - PIXEL_PRICE);
assertEq(currency.balanceOf(PIXEL_OWNER), sellerCurrencyOldBlanace + PIXEL_PRICE);
// check ownership
assertEq(thespace.getOwner(PIXEL_ID), PIXEL_OWNER_1);
// check price
assertEq(thespace.getPrice(PIXEL_ID), PIXEL_PRICE);
}
function testBatchBid() public {
bytes[] memory data = new bytes[](3);
// bid PIXEL_ID as PIXEL_OWNER
_bid(PIXEL_PRICE, PIXEL_PRICE);
data[0] = abi.encodeWithSignature("bid(uint256,uint256)", PIXEL_ID, PIXEL_PRICE);
data[1] = abi.encodeWithSignature("bid(uint256,uint256)", PIXEL_ID + 1, PIXEL_PRICE);
data[2] = abi.encodeWithSignature("bid(uint256,uint256)", PIXEL_ID + 2, PIXEL_PRICE);
vm.prank(PIXEL_OWNER_1);
thespace.multicall(data);
assertEq(thespace.getOwner(PIXEL_ID), PIXEL_OWNER_1);
assertEq(thespace.getOwner(PIXEL_ID + 1), PIXEL_OWNER_1);
assertEq(thespace.getOwner(PIXEL_ID + 2), PIXEL_OWNER_1);
}
function testBidDefaultedToken() public {
// bid a token and set a high price
uint256 price = registry.currency().totalSupply();
_bid(PIXEL_PRICE, price);
// check tax is greater than balance
_rollBlock();
uint256 tax = thespace.getTax(PIXEL_ID);
assertLt(currency.balanceOf(PIXEL_OWNER_1), tax);
// set mint tax
uint256 mintTax = 50 * (10**uint256(currency.decimals()));
_setMintTax(mintTax);
// bid and token will be defaulted
uint256 prevBalance = currency.balanceOf(PIXEL_OWNER_1);
_bidAs(PIXEL_OWNER_1, PIXEL_PRICE);
// bidder only pays the mint tax
assertEq(currency.balanceOf(PIXEL_OWNER_1), prevBalance - mintTax);
// tax paid by the old owner
assertEq(currency.balanceOf(PIXEL_OWNER), 0);
// tax was clear
assertEq(thespace.getTax(PIXEL_ID), 0);
// ownership was transferred
assertEq(thespace.getOwner(PIXEL_ID), PIXEL_OWNER_1);
// price didn't change since bid price is lower than the old price
assertEq(thespace.getPrice(PIXEL_ID), price);
}
function testCannotBidOutBoundTokens() public {
vm.startPrank(PIXEL_OWNER_1);
uint256 totalSupply = registry.totalSupply();
// oversupply id
vm.expectRevert(abi.encodeWithSignature("InvalidTokenId(uint256,uint256)", 1, totalSupply));
thespace.bid(totalSupply + 1, PIXEL_PRICE);
// zero id
vm.expectRevert(abi.encodeWithSignature("InvalidTokenId(uint256,uint256)", 1, totalSupply));
thespace.bid(0, PIXEL_PRICE);
vm.stopPrank();
}
function testCannotBidPriceTooLow() public {
// bid and set price
_bid(PIXEL_PRICE, PIXEL_PRICE);
// price too low to bid a existing token
vm.expectRevert(abi.encodeWithSignature("PriceTooLow()"));
_bidAs(PIXEL_OWNER_1, PIXEL_PRICE - 1);
// price too low to bid a non-existing token
uint256 mintTax = 50 * (10**uint256(currency.decimals()));
_setMintTax(mintTax);
vm.expectRevert(abi.encodeWithSignature("PriceTooLow()"));
_bidAs(PIXEL_OWNER_1, mintTax - 1);
}
function testCannotBidExceedAllowance() public {
// set mint tax
uint256 mintTax = 50 * (10**uint256(currency.decimals()));
_setMintTax(mintTax);
// revoke currency approval
vm.prank(PIXEL_OWNER);
currency.approve(address(registry), 0);
// bid a pixel
vm.expectRevert("ERC20: insufficient allowance");
vm.prank(PIXEL_OWNER);
thespace.bid(PIXEL_ID, PIXEL_PRICE);
}
/**
* @dev Ownership & Tax
*/
function testTokenShouldBeDefaulted() public {
// bid and set price
_bid(PIXEL_PRICE, PIXEL_PRICE);
vm.startPrank(PIXEL_OWNER);
_rollBlock();
uint256 tax = thespace.getTax(PIXEL_ID);
// check if token should be defaulted
currency.approve(address(registry), tax - 1);
(, bool shouldDefault) = thespace.evaluateOwnership(PIXEL_ID);
assertTrue(shouldDefault);
vm.stopPrank();
}
function testCollectableTax() public {
// bid and set price
_bid(PIXEL_PRICE, PIXEL_PRICE);
vm.startPrank(PIXEL_OWNER);
_rollBlock();
uint256 tax = thespace.getTax(PIXEL_ID);
// tax can be fully collected
(uint256 collectableTax, bool shouldDefault) = thespace.evaluateOwnership(PIXEL_ID);
assertEq(collectableTax, tax);
assertFalse(shouldDefault);
// tax can't be fully collected
currency.approve(address(registry), tax - 1);
(uint256 collectableTax2, bool shouldDefault2) = thespace.evaluateOwnership(PIXEL_ID);
assertLt(collectableTax2, tax);
assertTrue(shouldDefault2);
vm.stopPrank();
}
function testDefault() public {
// bid and set price
_bid(PIXEL_PRICE, PIXEL_PRICE);
_rollBlock();
vm.prank(PIXEL_OWNER);
currency.approve(address(registry), 0);
thespace.settleTax(PIXEL_ID);
// token was burned
assertEq(registry.balanceOf(PIXEL_OWNER), 0);
// price was reset
uint256 mintTax = 50 * (10**uint256(currency.decimals()));
_setMintTax(mintTax);
assertEq(thespace.getPrice(PIXEL_ID), mintTax);
// bid with mint tax
vm.expectEmit(true, true, true, false);
emit Tax(PIXEL_ID, PIXEL_OWNER_1, mintTax);
uint256 prevBalance = currency.balanceOf(PIXEL_OWNER_1);
_bidAs(PIXEL_OWNER_1, mintTax);
assertEq(currency.balanceOf(PIXEL_OWNER_1), prevBalance - mintTax);
}
function testTaxCalculation() public {
uint256 initialBlockNo = block.number;
uint256 blockRollsToFirstWindow = initialBlockNo + TAX_WINDOW;
uint256 blockRollsToSecondWindow = blockRollsToFirstWindow + TAX_WINDOW;
uint256 blockRollsToThirdWindow = blockRollsToSecondWindow + TAX_WINDOW;
/**
* bid and set a zero price
*/
_bid(PIXEL_PRICE, 0);
// rolls block to first window
vm.roll(blockRollsToFirstWindow);
// tax is zero
assertEq(thespace.getTax(PIXEL_ID), 0);
// lastTaxCollection is still initialBlockNo
(, uint256 lastTaxCollection, ) = registry.tokenRecord(PIXEL_ID);
assertEq(lastTaxCollection, initialBlockNo);
/**
* bid with new owner and still zero price
*/
_bidAs(PIXEL_OWNER_1, 0, 0);
// tax is still zero
assertEq(thespace.getTax(PIXEL_ID), 0);
// but lastTaxCollection is unchanged
(, uint256 lastTaxCollection1, ) = registry.tokenRecord(PIXEL_ID);
assertEq(lastTaxCollection1, initialBlockNo);
/**
* bid with original owner but with non-zero price
*/
// rolls block to first window
vm.roll(blockRollsToSecondWindow);
_bid(0, 100);
// lastTaxCollection is changed to blockRollsToSecondWindow
(, uint256 lastTaxCollection2, ) = registry.tokenRecord(PIXEL_ID);
assertEq(lastTaxCollection2, blockRollsToSecondWindow);
// and tax is still zero
assertEq(thespace.getTax(PIXEL_ID), 0);
// keep rolling to third window and tax is now non-zero
vm.roll(blockRollsToThirdWindow);
assertGt(thespace.getTax(PIXEL_ID), 0);
}
function testGetTax() public {
uint256 blockRollsTo = block.number + TAX_WINDOW;
uint256 taxRate = registry.taxConfig(CONFIG_TAX_RATE);
// bid and set price
_bid(PIXEL_PRICE, PIXEL_PRICE);
vm.roll(blockRollsTo);
(, uint256 lastTaxCollection, ) = registry.tokenRecord(PIXEL_ID);
uint256 tax = (PIXEL_PRICE * taxRate * (blockRollsTo - lastTaxCollection)) / (1000 * 10000);
assertEq(thespace.getTax(PIXEL_ID), tax);
// zero price
_bidAs(PIXEL_OWNER_1, PIXEL_PRICE, 0);
vm.roll(block.number + TAX_WINDOW);
assertEq(thespace.getTax(PIXEL_ID), 0);
}
function testCannotGetTaxWithNonExistingToken() public {
vm.expectRevert(abi.encodeWithSignature("TokenNotExists()"));
thespace.getTax(0);
}
function testSettleTax() public {
// bid and set price
_bid(PIXEL_PRICE, PIXEL_PRICE);
uint256 blockRollsTo = block.number + TAX_WINDOW;
vm.roll(blockRollsTo);
(uint256 prevUBI, uint256 prevTreasury, ) = registry.treasuryRecord();
uint256 tax = thespace.getTax(PIXEL_ID);
vm.expectEmit(true, true, true, false);
emit Tax(PIXEL_ID, PIXEL_OWNER, tax);
thespace.settleTax(PIXEL_ID);
// check tax
(uint256 newUBI, uint256 newTreasury, ) = registry.treasuryRecord();
assertEq(newUBI + newTreasury, tax + prevUBI + prevTreasury);
// check lastTaxCollection
(, uint256 lastTaxCollection, ) = registry.tokenRecord(PIXEL_ID);
assertEq(lastTaxCollection, blockRollsTo);
}
/**
* @dev UBI
*/
function testWithdrawUBI() public {
uint256 newBidPrice = PIXEL_PRICE + 1000;
_bidAs(PIXEL_OWNER_1, PIXEL_PRICE, newBidPrice);
//collect tax
_rollBlock();
thespace.settleTax(PIXEL_ID);
// check UBI
uint256 ubi = thespace.ubiAvailable(PIXEL_ID);
assertGt(ubi, 0);
// withdraw UBI
uint256 prevBalance = currency.balanceOf(PIXEL_OWNER_1);
vm.prank(PIXEL_OWNER_1);
vm.expectEmit(true, true, true, false);
emit UBI(PIXEL_ID, PIXEL_OWNER_1, ubi);
thespace.withdrawUbi(PIXEL_ID);
assertEq(currency.balanceOf(PIXEL_OWNER_1), prevBalance + ubi);
// check UBI
assertEq(thespace.ubiAvailable(PIXEL_ID), 0);
}
/**
* @dev Transfer
*/
function testCannotTransferFromIfDefault() public {
// bid and set price
_bid(PIXEL_PRICE, PIXEL_PRICE);
_rollBlock();
vm.startPrank(PIXEL_OWNER);
currency.approve(address(registry), 0);
registry.transferFrom(PIXEL_OWNER, PIXEL_OWNER_1, PIXEL_ID);
vm.stopPrank();
// ownership was transferred to zero address
assertEq(thespace.getOwner(PIXEL_ID), address(0));
// price was reset to mint tax since it was burned
uint256 mintTax = 50 * (10**uint256(currency.decimals()));
_setMintTax(mintTax);
assertEq(thespace.getPrice(PIXEL_ID), mintTax);
// tax was clear
_bidAs(PIXEL_OWNER_1, mintTax);
assertEq(thespace.getTax(PIXEL_ID), 0);
}
function testCanTransferFromIfSettleTax() public {
// bid and set price
_bid(PIXEL_PRICE, PIXEL_PRICE);
_rollBlock();
vm.startPrank(PIXEL_OWNER);
registry.transferFrom(PIXEL_OWNER, PIXEL_OWNER_1, PIXEL_ID);
vm.stopPrank();
assertEq(thespace.getOwner(PIXEL_ID), PIXEL_OWNER_1);
// tax was clear
assertEq(thespace.getTax(PIXEL_ID), 0);
// price was reset to zero
uint256 mintTax = 50 * (10**uint256(currency.decimals()));
_setMintTax(mintTax);
assertEq(thespace.getPrice(PIXEL_ID), 0);
}
/**
* @dev Token URI
*/
function testGetTokenImageURI() public {
assertEq(thespace.tokenImageURI(), TOKEN_IMAGE_URI);
}
function testSetTokenImageURI() public {
_bid(PIXEL_PRICE, PIXEL_PRICE);
string memory prevTokenURI = registry.tokenURI(PIXEL_ID);
vm.prank(ACL_MANAGER);
thespace.setTokenImageURI("ipfs://1235413");
string memory newTokenURI = registry.tokenURI(PIXEL_ID);
bool isSameURI = (keccak256(abi.encodePacked((prevTokenURI))) == keccak256(abi.encodePacked((newTokenURI))));
assertFalse(isSameURI);
}
function testCannotSetTokenImageURIByNonACLManager() public {
vm.expectRevert(abi.encodeWithSignature("RoleRequired(uint8)", ROLE_ACL_MANAGER));
thespace.setTokenImageURI("123");
}
function testGetTokenURI() public {
_bid(PIXEL_PRICE, PIXEL_PRICE);
string memory tokenURI = registry.tokenURI(PIXEL_ID);
console2.log(tokenURI);
}
function testCannotGetTokenURIInLogicContract() public {
_bid(PIXEL_PRICE, PIXEL_PRICE);
vm.expectRevert(abi.encodeWithSignature("Unauthorized()"));
thespace._tokenURI(PIXEL_ID);
}
}