-
Notifications
You must be signed in to change notification settings - Fork 1
/
ERC721Universal.ts
839 lines (708 loc) · 32.8 KB
/
ERC721Universal.ts
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
// Import necessary Hardhat and ethers.js components
import { expect } from "chai";
import { ethers } from "hardhat";
import { RevertType } from "../utils/enums.ts";
import { ERC721Universal } from "../typechain-types/contracts/ERC721Universal.js";
import { ERC721ReceiverMock } from "../typechain-types/contracts/tests/ERC721ReceiverMock.js";
import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers";
function buildTokenId(slot: string, addr: string) {
return ethers.toBeHex(
"0x" + slot + addr.substring(2),
32,
);
}
describe("ERC721Universal", function () {
const maxBalance = 2n ** 96n;
const defaultURI = "evochain1/collectionId/";
const nullAddress = ethers.toBeHex(0, 20);
let addr1: HardhatEthersSigner;
let addr2: HardhatEthersSigner;
let addr3: HardhatEthersSigner;
let erc721: ERC721Universal;
let erc721Receiver: ERC721ReceiverMock;
const RECEIVER_MAGIC_VALUE = "0x150b7a02";
// Deploy the contract and prepare accounts
beforeEach(async function () {
[addr1, addr2, addr3] = await ethers.getSigners();
const ERC721UniversalFactory = await ethers.getContractFactory(
"ERC721Universal",
);
erc721 = await ERC721UniversalFactory.deploy(
addr1.address,
"laos-kitties",
"LAK",
defaultURI,
);
await erc721.waitForDeployment();
});
it("Should report correct version of the uERC721 interface", async function () {
expect(await erc721.ERC721UniversalVersion()).to.equal(2);
});
it("Should support the standard ERC721 interface", async function () {
const InterfaceIdFactory = await ethers.getContractFactory(
"InterfaceId",
);
const interfaceId = await InterfaceIdFactory.deploy();
await interfaceId.waitForDeployment();
// Tests both via direct contract calls, as well the on-chain OpenZeppelin lib checker:
const specifiedId = "0x80ac58cd";
expect(await interfaceId.getERC721Id()).to.equal(specifiedId);
expect(await erc721.supportsInterface(specifiedId)).to.equal(true);
expect(await interfaceId.supportsInterface(await erc721.getAddress(), specifiedId)).to.equal(true);
});
it("Should support standard ERC165 interface", async function () {
const InterfaceIdFactory = await ethers.getContractFactory(
"InterfaceId",
);
const interfaceId = await InterfaceIdFactory.deploy();
await interfaceId.waitForDeployment();
// Tests both via direct contract calls, as well the on-chain OpenZeppelin lib checker:
const mustReplyTrue = "0x01ffc9a7";
const mustReplyFalse = "0xffffffff";
expect(await erc721.supportsInterface(mustReplyTrue)).to.equal(true);
expect(await erc721.supportsInterface(mustReplyFalse)).to.equal(false);
expect(await interfaceId.supportsERC165(await erc721.getAddress())).to.equal(true);
});
it("Should support ERC721Universal interface", async function () {
const InterfaceIdFactory = await ethers.getContractFactory(
"InterfaceId",
);
const interfaceId = await InterfaceIdFactory.deploy();
await interfaceId.waitForDeployment();
// Tests both via direct contract calls, as well the on-chain OpenZeppelin lib checker:
const specified721UniversalId = "0x9832f941";
expect(await interfaceId.getERC721UniversalId()).to.equal(specified721UniversalId);
expect(await erc721.supportsInterface(specified721UniversalId)).to.equal(true);
expect(await interfaceId.supportsInterface(await erc721.getAddress(), specified721UniversalId)).to.equal(true);
});
it("Can query BaseURI", async function () {
expect(await erc721.baseURI()).to.equal(defaultURI);
});
it("Should emit OwnershipTransferred event on deploy", async function () {
const deployedTx = erc721.deploymentTransaction();
await expect(deployedTx)
.to.emit(erc721, "OwnershipTransferred")
.withArgs(nullAddress, addr1.address);
// assert that the signature of the event (topic0) matches the expected value
// first by computing it from the hash of the event type:
const expectedTopic0 = "0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0";
const computedTopic0 = ethers.id("OwnershipTransferred(address,address)");
expect(computedTopic0).to.equal(expectedTopic0);
// second by retrieving it from the TX directly
const receipt = await deployedTx?.wait();
const eventIdx = 0;
expect(receipt?.logs[eventIdx].topics[0]).to.equal(expectedTopic0);
});
it("Should emit NewERC721Universal event on deploy", async function () {
const deployedTx = erc721.deploymentTransaction();
const deployedAddress = await erc721.getAddress();
await expect(deployedTx)
.to.emit(erc721, "NewERC721Universal")
.withArgs(deployedAddress, defaultURI);
// assert that the signature of the event (topic0) matches the expected value
// first by computing it from the hash of the event type:
const expectedTopic0 = "0x74b81bc88402765a52dad72d3d893684f472a679558f3641500e0ee14924a10a";
const computedTopic0 = ethers.id("NewERC721Universal(address,string)");
expect(computedTopic0).to.equal(expectedTopic0);
// second by retrieving it from the TX directly
const receipt = await deployedTx?.wait();
const eventIdx = 1;
expect(receipt?.logs[eventIdx].topics[0]).to.equal(expectedTopic0);
});
it("Should have the correct name and symbol", async function () {
const name = await erc721.name();
const symbol = await erc721.symbol();
expect(name).to.equal("laos-kitties");
expect(symbol).to.equal("LAK");
});
it("Should return correct tokenURI", async function () {
const tokenId = 1;
expect(await erc721.tokenURI(tokenId)).to.equal(defaultURI + 'GeneralKey(' + tokenId + ')');
const largeTokenId = buildTokenId("34", addr1.address);
const largeTokenIdAsUint256 = ethers.toBigInt(largeTokenId);
expect(await erc721.tokenURI(largeTokenId)).to.equal(
defaultURI + 'GeneralKey(' + largeTokenIdAsUint256.toString() + ')',
);
});
it("Should return the initial owner of the token if it is not transferred yet", async function () {
const tokenId = 2;
const ownerOfToken = await erc721.ownerOf(tokenId);
expect(ownerOfToken).to.equal(ethers.toBeHex(tokenId, 20));
});
it("The null address is the only one that cannot own tokens", async function () {
const tokenId = buildTokenId("34", nullAddress);
await expect(erc721.ownerOf(tokenId))
.to.be.revertedWithCustomError(erc721, "ERC721NonexistentToken")
.withArgs(tokenId);
});
it("initOwner decodes as expected", async function () {
let tokenId = buildTokenId("111", addr1.address);
expect(await erc721.initOwner(tokenId)).to.equal(addr1.address);
tokenId = buildTokenId("", addr1.address);
expect(await erc721.initOwner(tokenId)).to.equal(addr1.address);
tokenId = buildTokenId("", addr2.address);
expect(await erc721.initOwner(tokenId)).to.equal(addr2.address);
tokenId = buildTokenId("", nullAddress);
expect(await erc721.initOwner(tokenId)).to.equal(nullAddress);
const largestSlot = ethers.toBeHex(2n ** 96n - 1n, 12);
tokenId = buildTokenId(largestSlot.substring(2), addr2.address);
expect(await erc721.initOwner(tokenId)).to.equal(addr2.address);
});
it("Owner of the asset should be able to transfer asset", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.ownerOf(tokenId)).to.equal(addr1.address);
expect(await erc721.balanceOf(addr1.address)).to.equal(maxBalance);
expect(await erc721.balanceOf(addr2.address)).to.equal(maxBalance);
await expect(
erc721.connect(addr1).transferFrom(addr1.address, addr2.address, tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, addr2.address, tokenId);
expect(await erc721.ownerOf(tokenId)).to.equal(addr2.address);
expect(await erc721.balanceOf(addr1.address)).to.equal(maxBalance);
expect(await erc721.balanceOf(addr2.address)).to.equal(maxBalance);
await expect(
erc721.connect(addr2).transferFrom(addr2.address, addr3.address, tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr2.address, addr3.address, tokenId);
expect(await erc721.ownerOf(tokenId)).to.equal(addr3.address);
expect(await erc721.balanceOf(addr2.address)).to.equal(maxBalance);
expect(await erc721.balanceOf(addr3.address)).to.equal(maxBalance);
});
it("Owner of the asset should be able to burn the asset", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.ownerOf(tokenId)).to.equal(addr1.address);
expect(await erc721.balanceOf(addr1.address)).to.equal(maxBalance);
expect(await erc721.isBurned(tokenId)).to.equal(false);
await expect(erc721.connect(addr1).burn(tokenId))
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, nullAddress, tokenId);
expect(await erc721.isBurned(tokenId)).to.equal(true);
await expect(erc721.ownerOf(tokenId))
.to.be.revertedWithCustomError(erc721, "ERC721NonexistentToken")
.withArgs(tokenId);
expect(await erc721.balanceOf(addr1.address)).to.equal(maxBalance);
});
it("burn can be executed by approved operator", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.ownerOf(tokenId)).to.equal(addr1.address);
expect(await erc721.balanceOf(addr1.address)).to.equal(maxBalance);
await expect(erc721.connect(addr1).setApprovalForAll(addr2.address, true))
.to.emit(erc721, "ApprovalForAll")
.withArgs(addr1.address, addr2.address, true);
await expect(erc721.connect(addr2).burn(tokenId))
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, nullAddress, tokenId);
expect(await erc721.balanceOf(addr1.address)).to.equal(maxBalance);
});
it("Asset cannot be burned by address that is not its owner", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.ownerOf(tokenId)).to.equal(addr1.address);
await expect(erc721.connect(addr2).burn(tokenId))
.to.be.revertedWithCustomError(erc721, "ERC721InsufficientApproval")
.withArgs(addr2.address, tokenId);
});
it("Asset cannot be burned twice", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.ownerOf(tokenId)).to.equal(addr1.address);
await expect(erc721.connect(addr1).burn(tokenId))
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, nullAddress, tokenId);
await expect(erc721.connect(addr1).burn(tokenId))
.to.be.revertedWithCustomError(erc721, "ERC721NonexistentToken")
.withArgs(tokenId);
});
it("Burned asset cannot be transferred", async function () {
const tokenId = buildTokenId("111", addr1.address);
await expect(erc721.connect(addr1).burn(tokenId))
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, nullAddress, tokenId);
await expect(
erc721.connect(addr1).transferFrom(addr1.address, addr2.address, tokenId),
)
.to.be.revertedWithCustomError(erc721, "ERC721NonexistentToken")
.withArgs(tokenId);
await expect(
erc721.connect(addr1).transferFrom(nullAddress, addr2.address, tokenId),
)
.to.be.revertedWithCustomError(erc721, "ERC721NonexistentToken")
.withArgs(tokenId);
});
it("Burned asset has no owner - query must fail", async function () {
const tokenId = buildTokenId("111", addr1.address);
await expect(erc721.connect(addr1).burn(tokenId))
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, nullAddress, tokenId);
await expect(
erc721.connect(addr1).transferFrom(addr1.address, addr2.address, tokenId),
)
.to.be.revertedWithCustomError(erc721, "ERC721NonexistentToken")
.withArgs(tokenId);
await expect(erc721.ownerOf(tokenId))
.to.be.revertedWithCustomError(erc721, "ERC721NonexistentToken")
.withArgs(tokenId);
});
it("Burned asset has no tokenURI - query must fail", async function () {
const tokenId = buildTokenId("111", addr1.address);
await expect(erc721.connect(addr1).burn(tokenId))
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, nullAddress, tokenId);
await expect(
erc721.connect(addr1).transferFrom(addr1.address, addr2.address, tokenId),
)
.to.be.revertedWithCustomError(erc721, "ERC721NonexistentToken")
.withArgs(tokenId);
await expect(erc721.tokenURI(tokenId))
.to.be.revertedWithCustomError(erc721, "ERC721NonexistentToken")
.withArgs(tokenId);
});
it("Owner of the asset cannot transfer to null address via transfer method", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.ownerOf(tokenId)).to.equal(addr1.address);
await expect(
erc721.connect(addr2).transferFrom(addr2.address, nullAddress, tokenId),
)
.to.be.revertedWithCustomError(erc721, "ERC721InvalidReceiver")
.withArgs(nullAddress);
});
it("User should not be able to transfer an asset that he does not own", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.ownerOf(tokenId)).to.equal(addr1.address);
await expect(
erc721.connect(addr2).transferFrom(addr2.address, addr1.address, tokenId),
)
.to.be.revertedWithCustomError(erc721, "ERC721InsufficientApproval")
.withArgs(addr2.address, tokenId);
});
it("Owner of the asset should be able to do safe transfer of asset", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.ownerOf(tokenId)).to.equal(addr1.address);
const ERC721ReceiverMock =
await ethers.getContractFactory("ERC721ReceiverMock");
erc721Receiver = await ERC721ReceiverMock.deploy(
RECEIVER_MAGIC_VALUE,
RevertType.None,
);
await erc721Receiver.waitForDeployment();
const receiverContractAddress = await erc721Receiver.getAddress();
await expect(
erc721
.connect(addr1)
.safeTransferFrom(addr1.address, receiverContractAddress, tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, receiverContractAddress, tokenId);
expect(await erc721.ownerOf(tokenId)).to.equal(receiverContractAddress);
});
it("Owner of the asset should be able to do safe transfer of asset with data", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.ownerOf(tokenId)).to.equal(addr1.address);
const ERC721ReceiverMock =
await ethers.getContractFactory("ERC721ReceiverMock");
erc721Receiver = await ERC721ReceiverMock.deploy(
RECEIVER_MAGIC_VALUE,
RevertType.None,
);
await erc721Receiver.waitForDeployment();
const receiverContractAddress = await erc721Receiver.getAddress();
await expect(
erc721
.connect(addr1)
.safeTransferFrom(
addr1.address,
receiverContractAddress,
tokenId,
"0x43",
{ gasLimit: 300000 },
),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, receiverContractAddress, tokenId);
expect(await erc721.ownerOf(tokenId)).to.equal(receiverContractAddress);
});
it("When Owner of the asset does safe transfer the receiver contract reverts on call", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.ownerOf(tokenId)).to.equal(addr1.address);
const ERC721ReceiverMockFactory =
await ethers.getContractFactory("ERC721ReceiverMock");
erc721Receiver = await ERC721ReceiverMockFactory.deploy(
RECEIVER_MAGIC_VALUE,
RevertType.RevertWithMessage,
);
await erc721Receiver.waitForDeployment();
const receiverContractAddress = await erc721Receiver.getAddress();
await expect(
erc721
.connect(addr1)
.safeTransferFrom(addr1.address, receiverContractAddress, tokenId),
).to.be.revertedWith("ERC721ReceiverMock: reverting");
});
it("When Owner of the asset does safe transfer with data the receiver contract reverts on call", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.ownerOf(tokenId)).to.equal(addr1.address);
const ERC721ReceiverMockFactory =
await ethers.getContractFactory("ERC721ReceiverMock");
erc721Receiver = await ERC721ReceiverMockFactory.deploy(
RECEIVER_MAGIC_VALUE,
RevertType.RevertWithMessage,
);
await erc721Receiver.waitForDeployment();
const receiverContractAddress = await erc721Receiver.getAddress();
await expect(
erc721
.connect(addr1)
.safeTransferFrom(
addr1.address,
receiverContractAddress,
tokenId,
"0x43",
{ gasLimit: 300000 },
),
).to.be.revertedWith("ERC721ReceiverMock: reverting");
});
});
describe("ERC721UpdatableBaseURI", function () {
const defaultURI = "evochain1/collectionId/";
let addr1: HardhatEthersSigner;
let addr2: HardhatEthersSigner;
let erc721: ERC721Universal;
// Deploy the contract and prepare accounts
beforeEach(async function () {
[addr1, addr2] = await ethers.getSigners();
const ERC721UniversalFactory = await ethers.getContractFactory(
"ERC721Universal",
);
erc721 = await ERC721UniversalFactory.deploy(
addr1.address,
"laos-kitties",
"LAK",
defaultURI,
);
await erc721.waitForDeployment();
});
it("Should support the standard ERC721UpdatableBaseURI interface", async function () {
const InterfaceIdFactory = await ethers.getContractFactory(
"InterfaceId",
);
const interfaceId = await InterfaceIdFactory.deploy();
await interfaceId.waitForDeployment();
// Tests both via direct contract calls, as well the on-chain OpenZeppelin lib checker:
const specifiedId = "0x418fb255";
expect(await interfaceId.getERC721UpdatableBaseURIId()).to.equal(specifiedId);
expect(await erc721.supportsInterface(specifiedId)).to.equal(true);
expect(await interfaceId.supportsInterface(await erc721.getAddress(), specifiedId)).to.equal(true);
});
it("baseURI cannot be updated by address that is not owner", async function () {
await expect(erc721.connect(addr2).updateBaseURI("new/mate"))
.to.be.revertedWithCustomError(erc721, "OwnableUnauthorizedAccount")
.withArgs(addr2.address);
});
it("updates to baseURI work", async function () {
await erc721.connect(addr1).updateBaseURI("new/mate/");
expect(await erc721.baseURI()).to.equal("new/mate/");
await erc721.connect(addr1).updateBaseURI("old/mate/");
expect(await erc721.baseURI()).to.equal("old/mate/");
expect(await erc721.tokenURI(1)).to.equal("old/mate/GeneralKey(1)");
});
it("change in baseURI emits expected event", async function () {
await expect(await erc721.connect(addr1).updateBaseURI("new/mate"))
.to.emit(erc721, "UpdatedBaseURI")
.withArgs("new/mate");
});
it("is not locked on deploy", async function () {
expect(await erc721.isBaseURILocked()).to.equal(false);
});
it("onlyOwner can lock baseURI", async function () {
await expect(erc721.connect(addr2).lockBaseURI())
.to.be.revertedWithCustomError(erc721, "OwnableUnauthorizedAccount")
.withArgs(addr2.address);
});
it("locking baseURI prevents further changes of baseURI", async function () {
await expect(erc721.connect(addr1).lockBaseURI())
.to.emit(erc721, "LockedBaseURI")
.withArgs(defaultURI);
expect(await erc721.isBaseURILocked()).to.equal(true);
await expect(erc721.connect(addr1).updateBaseURI("new/mate"))
.to.be.revertedWithCustomError(erc721, "BaseURIAlreadyLocked")
.withArgs();
});
it("locking baseURI prevents further changes of affixes", async function () {
await expect(erc721.connect(addr1).lockBaseURI())
.to.emit(erc721, "LockedBaseURI")
.withArgs(defaultURI);
await expect(erc721.connect(addr1).updateTokenIdAffixes("newprefix", "newsuffix"))
.to.be.revertedWithCustomError(erc721, "BaseURIAlreadyLocked")
.withArgs();
});
it("locking baseURI cannot be done twice", async function () {
await expect(erc721.connect(addr1).lockBaseURI())
.to.emit(erc721, "LockedBaseURI")
.withArgs(defaultURI);
await expect(erc721.connect(addr1).lockBaseURI())
.to.be.revertedWithCustomError(erc721, "BaseURIAlreadyLocked")
.withArgs();
});
it("onlyOwner can update affixes", async function () {
await expect(erc721.connect(addr2).updateTokenIdAffixes("newprefix", "newsuffix"))
.to.be.revertedWithCustomError(erc721, "OwnableUnauthorizedAccount")
.withArgs(addr2.address);
});
it("updates to affixes work", async function () {
expect(await erc721.baseURI()).to.equal(defaultURI);
expect(await erc721.tokenURI(1)).to.equal(defaultURI + "GeneralKey(1)");
await erc721.connect(addr1).updateTokenIdAffixes("newprefix", "newsuffix");
expect(await erc721.baseURI()).to.equal(defaultURI);
expect(await erc721.tokenURI(1)).to.equal(defaultURI + "newprefix1newsuffix");
});
it("updates to affixes emits expected event", async function () {
await expect(await erc721.connect(addr1).updateTokenIdAffixes("newprefix", "newsuffix"))
.to.emit(erc721, "UpdatedTokenIdAffixes")
.withArgs("newprefix", "newsuffix");
});
});
describe("ERC721Broadcast", function () {
const defaultURI = "evochain1/collectionId/";
const nullAddress = ethers.toBeHex(0, 20);
let addr1: HardhatEthersSigner;
let addr2: HardhatEthersSigner;
let erc721: ERC721Universal;
// Deploy the contract and prepare accounts
beforeEach(async function () {
[addr1, addr2] = await ethers.getSigners();
const ERC721UniversalFactory = await ethers.getContractFactory(
"ERC721Universal",
);
erc721 = await ERC721UniversalFactory.deploy(
addr1.address,
"laos-kitties",
"LAK",
defaultURI,
);
await erc721.waitForDeployment();
});
it("Should support the standard ERC721Broadcast interface", async function () {
const InterfaceIdFactory = await ethers.getContractFactory(
"InterfaceId",
);
const interfaceId = await InterfaceIdFactory.deploy();
await interfaceId.waitForDeployment();
// Tests both via direct contract calls, as well the on-chain OpenZeppelin lib checker:
const specifiedId = "0x8f8376c4";
expect(await interfaceId.getERC721BroadcastId()).to.equal(specifiedId);
expect(await erc721.supportsInterface(specifiedId)).to.equal(true);
expect(await interfaceId.supportsInterface(await erc721.getAddress(), specifiedId)).to.equal(true);
});
it("wasEverTransferred returns false on non-transferred assets", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.wasEverTransferred(tokenId)).to.equal(false);
});
it("wasEverTransferred returns true on transferred assets", async function () {
const tokenId = buildTokenId("111", addr1.address);
await expect(
erc721.connect(addr1).transferFrom(addr1.address, addr2.address, tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, addr2.address, tokenId);
expect(await erc721.wasEverTransferred(tokenId)).to.equal(true);
});
it("wasEverTransferred returns true on burned assets", async function () {
const tokenId = buildTokenId("111", addr1.address);
await expect(erc721.connect(addr1).burn(tokenId))
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, nullAddress, tokenId);
expect(await erc721.wasEverTransferred(tokenId)).to.equal(true);
});
it("wasEverTransferred returns true on burned assets after having been transferred", async function () {
const tokenId = buildTokenId("111", addr1.address);
expect(await erc721.wasEverTransferred(tokenId)).to.equal(false);
await expect(
erc721.connect(addr1).transferFrom(addr1.address, addr2.address, tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, addr2.address, tokenId);
expect(await erc721.wasEverTransferred(tokenId)).to.equal(true);
await expect(erc721.connect(addr2).burn(tokenId))
.to.emit(erc721, "Transfer")
.withArgs(addr2.address, nullAddress, tokenId);
expect(await erc721.wasEverTransferred(tokenId)).to.equal(true);
});
it("broadcastMint works on non-transferred asset, and emits expected event", async function () {
const tokenId = buildTokenId("111", addr1.address);
// note that the broadcasts are sent by any address; in this example, the address is not the owner of the asset
await expect(erc721.connect(addr2).broadcastMint(tokenId))
.to.emit(erc721, "Transfer")
.withArgs(nullAddress, addr1.address, tokenId);
});
it("broadcastMintBatch works on non-transferred assets, and emits expected events", async function () {
const tokenId = buildTokenId("111", addr1.address);
const tokenId2 = buildTokenId("222", addr2.address);
// note that the broadcasts are sent by any address; in this example, the address is not the owner of the asset
await expect(erc721.connect(addr2).broadcastMintBatch([tokenId, tokenId2]))
.to.emit(erc721, "Transfer")
.withArgs(nullAddress, addr1.address, tokenId)
.and.to.emit(erc721, "Transfer")
.withArgs(nullAddress, addr2.address, tokenId2);
});
it("broadcastSelfTransfer works on non-transferred asset, and emits expected event", async function () {
const tokenId = buildTokenId("111", addr1.address);
// note that the broadcasts are sent by any address; in this example, the address is not the owner of the asset
await expect(erc721.connect(addr2).broadcastSelfTransfer(tokenId))
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, addr1.address, tokenId);
});
it("broadcastSelfTransferBatch works on non-transferred assets, and emits expected events", async function () {
const tokenId = buildTokenId("111", addr1.address);
const tokenId2 = buildTokenId("222", addr2.address);
// note that the broadcasts are sent by any address; in this example, the address is not the owner of the asset
await expect(erc721.connect(addr2).broadcastSelfTransferBatch([tokenId, tokenId2]))
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, addr1.address, tokenId)
.and.to.emit(erc721, "Transfer")
.withArgs(addr2.address, addr2.address, tokenId2);
});
it("Standard mint on ERC721Enumerable costs gas as expected", async function () {
const ERC721EnumerableFactory = await ethers.getContractFactory(
"ERC721EnumerableMock",
);
const erc721Enum = await ERC721EnumerableFactory.deploy(
"laos-kitties",
"LAK"
);
await erc721Enum.waitForDeployment();
const tokenId = buildTokenId("111", addr1.address);
const tx = await erc721Enum.connect(addr2).mint(addr2.address, tokenId);
const receipt = await tx.wait();
expect(receipt?.gasUsed).to.equal(140683);
});
it("broadcastMint cost of gas is as expected", async function () {
const tokenId = buildTokenId("111", addr1.address);
// note that the broadcasts are sent by any address; in this example, the address is not the owner of the asset
const tx = await erc721.connect(addr2).broadcastMint(tokenId);
const receipt = await tx.wait();
expect(receipt?.gasUsed).to.equal(28207);
});
it("broadcastMintBatch cost of gas is as expected", async function () {
const tokenIds: string[] = [];
const nTokens = 100;
for (let slot = 111; slot < 111 + nTokens; slot++) {
const tokenId = buildTokenId(slot.toString(), addr1.address);
tokenIds.push(tokenId);
}
// note that the broadcasts are sent by any address; in this example, the address is not the owner of the asset
const tx = await erc721.connect(addr2).broadcastMintBatch(tokenIds);
const receipt = await tx.wait();
expect(receipt?.gasUsed).to.equal(718563);
});
it("broadcastSelfTransfer cost of gas is as expected", async function () {
const tokenId = buildTokenId("111", addr1.address);
// note that the broadcasts are sent by any address; in this example, the address is not the owner of the asset
const tx = await erc721.connect(addr2).broadcastSelfTransfer(tokenId);
const receipt = await tx.wait();
expect(receipt?.gasUsed).to.equal(28164);
});
it("broadcastSelfTransferBatch cost of gas is as expected", async function () {
const tokenIds: string[] = [];
const nTokens = 100;
for (let slot = 111; slot < 111 + nTokens; slot++) {
const tokenId = buildTokenId(slot.toString(), addr1.address);
tokenIds.push(tokenId);
}
// note that the broadcasts are sent by any address; in this example, the address is not the owner of the asset
const tx = await erc721.connect(addr2).broadcastSelfTransferBatch(tokenIds);
const receipt = await tx.wait();
expect(receipt?.gasUsed).to.equal(721319);
});
it("broadcastMint reverts on transferred assets", async function () {
const tokenId = buildTokenId("111", addr1.address);
await expect(
erc721.connect(addr1).transferFrom(addr1.address, addr2.address, tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, addr2.address, tokenId);
await expect(erc721.connect(addr2).broadcastMint(tokenId))
.to.be.revertedWithCustomError(erc721, "ERC721UniversalAlreadyTransferred")
.withArgs(tokenId);
});
it("broadcastMintBatch reverts on at least one transferred asset", async function () {
const tokenId = buildTokenId("111", addr1.address);
const tokenId2 = buildTokenId("222", addr1.address);
await expect(
erc721.connect(addr1).transferFrom(addr1.address, addr2.address, tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, addr2.address, tokenId);
await expect(erc721.connect(addr2).broadcastMintBatch([tokenId, tokenId2]))
.to.be.revertedWithCustomError(erc721, "ERC721UniversalAlreadyTransferred")
.withArgs(tokenId);
});
it("broadcastSelfTransfer reverts on transferred assets", async function () {
const tokenId = buildTokenId("111", addr1.address);
await expect(
erc721.connect(addr1).transferFrom(addr1.address, addr2.address, tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, addr2.address, tokenId);
await expect(erc721.connect(addr2).broadcastSelfTransfer(tokenId))
.to.be.revertedWithCustomError(erc721, "ERC721UniversalAlreadyTransferred")
.withArgs(tokenId);
});
it("broadcastSelfTransferBatch reverts on at least one transferred asset", async function () {
const tokenId = buildTokenId("111", addr1.address);
const tokenId2 = buildTokenId("222", addr1.address);
await expect(
erc721.connect(addr1).transferFrom(addr1.address, addr2.address, tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, addr2.address, tokenId);
await expect(erc721.connect(addr2).broadcastSelfTransferBatch([tokenId, tokenId2]))
.to.be.revertedWithCustomError(erc721, "ERC721UniversalAlreadyTransferred")
.withArgs(tokenId);
});
it("broadcastMint reverts on burned assets", async function () {
const tokenId = buildTokenId("111", addr1.address);
await expect(
erc721.connect(addr1).burn(tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, nullAddress, tokenId);
await expect(erc721.connect(addr2).broadcastMint(tokenId))
.to.be.revertedWithCustomError(erc721, "ERC721UniversalAlreadyTransferred")
.withArgs(tokenId);
});
it("broadcastMintBatch reverts on at least one burned asset", async function () {
const tokenId = buildTokenId("111", addr1.address);
const tokenId2 = buildTokenId("222", addr1.address);
await expect(
erc721.connect(addr1).burn(tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, nullAddress, tokenId);
await expect(erc721.connect(addr2).broadcastMintBatch([tokenId, tokenId2]))
.to.be.revertedWithCustomError(erc721, "ERC721UniversalAlreadyTransferred")
.withArgs(tokenId);
});
it("broadcastSelfTransfer reverts on burned assets", async function () {
const tokenId = buildTokenId("111", addr1.address);
await expect(
erc721.connect(addr1).burn(tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, nullAddress, tokenId);
await expect(erc721.connect(addr2).broadcastSelfTransfer(tokenId))
.to.be.revertedWithCustomError(erc721, "ERC721UniversalAlreadyTransferred")
.withArgs(tokenId);
});
it("broadcastSelfTransferBatch reverts on at least one burned asset", async function () {
const tokenId = buildTokenId("111", addr1.address);
const tokenId2 = buildTokenId("222", addr1.address);
await expect(
erc721.connect(addr1).burn(tokenId),
)
.to.emit(erc721, "Transfer")
.withArgs(addr1.address, nullAddress, tokenId);
await expect(erc721.connect(addr2).broadcastSelfTransferBatch([tokenId, tokenId2]))
.to.be.revertedWithCustomError(erc721, "ERC721UniversalAlreadyTransferred")
.withArgs(tokenId);
});
});