-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoutput
More file actions
3584 lines (3027 loc) · 116 KB
/
output
File metadata and controls
3584 lines (3027 loc) · 116 KB
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
./test/LazyMinter.test.ts
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import {
abi as ERC1155SingletonABI,
bytecode as ERC1155Bytecode,
} from "../artifacts/contracts/ERC1155LazyMint.sol/ERC1155LazyMint.json";
import { bytecode as BeaconBytecode } from "../artifacts/contracts/Beacon.sol/Beacon.json";
import {
createSalt,
createBytes32,
MANAGER_ROLE,
DEFAULT_OWNER_ROLE,
INTERFACE_ID_ERC165,
INTERFACE_ID_ERC1155,
INTERFACE_ID_ERC2981,
INTERFACE_ID_ACCESS_CONTROL,
ZERO_BYTES32,
BASE_POINTS,
ZERO_ADDRESS,
CONTRACT_SALT,
ROYALTY,
CONTRACT_URI,
TOKEN_URI,
LICENSE_URI,
CONTRACT_URI_MIMETYPE,
SEAPORT_1_5_ADDRESS,
} from "./utils";
import { LazyMinter } from "../lib/LazyMinter";
import { ERC1155LazyMint, MockNoPayContract } from "../typechain";
const hre = require("hardhat");
const ethers = hre.ethers;
describe("Lazy Mint", function () {
const SALT = createSalt(CONTRACT_SALT);
async function deploy() {
const [owner, redeemer] = await ethers.getSigners();
const SingletonFactory = await ethers.getContractFactory(
"MockSingletonFactory"
);
const factoryInstance = await SingletonFactory.deploy();
const erc1155Address = await factoryInstance.callStatic.computeAddress(
SALT,
ERC1155Bytecode
);
await factoryInstance.deploy(SALT, ERC1155Bytecode, { gasLimit: 30000000 });
const abiCoder = new ethers.utils.AbiCoder();
const encodedParameters = abiCoder.encode(
["address", "address"],
[erc1155Address, owner.address]
);
const beaconInitCode = BeaconBytecode + encodedParameters.slice(2);
const beaconAddress = await factoryInstance.computeAddress(
SALT,
beaconInitCode
);
await factoryInstance.deploy(SALT, beaconInitCode, { gasLimit: 30000000 });
const beacon = await ethers.getContractAt("Beacon", beaconAddress);
const iface = new ethers.utils.Interface(ERC1155SingletonABI);
const callData = iface.encodeFunctionData("init", [
owner.address,
CONTRACT_URI,
TOKEN_URI,
LICENSE_URI,
ROYALTY,
]);
const proxyAddress = await beacon.callStatic.deployProxyContract(callData);
await beacon.deployProxyContract(callData);
const proxy = (await ethers.getContractAt(
"ERC1155LazyMint",
proxyAddress
)) as unknown as ERC1155LazyMint;
return { proxy, owner, redeemer };
}
describe("Deployment", function () {
it("should deploy contract", async () => {
const { proxy, owner } = await loadFixture(deploy);
expect(proxy).to.exist;
expect(owner).to.exist;
});
it("should redeem an NFT from a signed voucher", async function () {
const { proxy, owner, redeemer } = await loadFixture(deploy);
const tokenPrice = ethers.utils.parseEther("1.0");
const lazyMinter = new LazyMinter({
contractAddress: proxy.address,
signer: owner,
});
const tokenId = 1;
const supply = 1;
const { voucher, signature } = await lazyMinter.createVoucher(
tokenId,
"ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
tokenPrice,
10,
owner.address
);
const beginBalance = await ethers.provider.getBalance(owner.address);
expect(await proxy.balanceOf(redeemer.address, tokenId)).to.equal(0);
const redeemTx = await proxy
.connect(redeemer)
.redeem(redeemer.address, 1, voucher, signature, {
value: tokenPrice,
});
const redeemReceipt = await redeemTx.wait();
const totalSupply = await proxy.totalSupply(tokenId);
const endBalance = await ethers.provider.getBalance(owner.address);
const balanceDelta = endBalance.sub(beginBalance);
expect(balanceDelta).to.equal(tokenPrice);
expect(totalSupply).to.equal(supply);
expect(await proxy.balanceOf(redeemer.address, tokenId)).to.equal(1);
});
it("should mint up to the maxSupply of the voucher", async () => {
const { proxy, owner, redeemer } = await loadFixture(deploy);
const tokenPrice = ethers.utils.parseEther("0");
const lazyMinter = new LazyMinter({
contractAddress: proxy.address,
signer: owner,
});
const tokenId = 1;
const supply = 2;
const { voucher, signature } = await lazyMinter.createVoucher(
tokenId,
"ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
tokenPrice,
supply,
owner.address
);
expect(await proxy.balanceOf(redeemer.address, tokenId)).to.equal(0);
const redeemTx = await proxy
.connect(redeemer)
.redeem(redeemer.address, 1, voucher, signature, {
value: tokenPrice,
});
const redeemTx2 = await proxy
.connect(redeemer)
.redeem(redeemer.address, 1, voucher, signature, {
value: tokenPrice,
});
await redeemTx.wait();
await redeemTx2.wait();
const totalSupply = await proxy.totalSupply(tokenId);
expect(totalSupply).to.equal(supply);
expect(await proxy.balanceOf(redeemer.address, tokenId)).to.equal(supply);
});
it("should fail to redeem an NFT when totalSupply will be > maxSupply", async function () {
const { proxy, owner, redeemer } = await loadFixture(deploy);
const tokenPrice = ethers.utils.parseEther("0");
const lazyMinter = new LazyMinter({
contractAddress: proxy.address,
signer: owner,
});
const tokenId = 1;
const supply = 2;
const { voucher, signature } = await lazyMinter.createVoucher(
tokenId,
"ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
tokenPrice,
supply,
owner.address
);
expect(await proxy.balanceOf(redeemer.address, tokenId)).to.equal(0);
const redeemTx = await proxy
.connect(redeemer)
.redeem(redeemer.address, 1, voucher, signature, {
value: tokenPrice,
});
await expect(
proxy.redeem(redeemer.address, supply + 1, voucher, signature, {
value: tokenPrice,
}) // @ts-ignore
).to.be.reverted;
});
it("should fail to redeem an NFT voucher that's signed by an unauthorized account", async function () {
const { proxy, owner, redeemer } = await loadFixture(deploy);
const tokenPrice = ethers.utils.parseEther("0");
const lazyMinter = new LazyMinter({
contractAddress: proxy.address,
signer: redeemer,
});
const tokenId = 1;
const supply = 2;
const { voucher, signature } = await lazyMinter.createVoucher(
tokenId,
"ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
tokenPrice,
supply,
owner.address
);
expect(await proxy.balanceOf(redeemer.address, tokenId)).to.equal(0);
await proxy.connect(redeemer);
await expect(
proxy.redeem(redeemer.address, 1, voucher, signature, {
value: tokenPrice,
}) // @ts-ignore
).to.be.reverted;
});
it("should fail to redeem an NFT voucher that's been modified", async function () {
const { proxy, owner, redeemer } = await loadFixture(deploy);
const tokenPrice = ethers.utils.parseEther("0");
const lazyMinter = new LazyMinter({
contractAddress: proxy.address,
signer: owner,
});
const tokenId = 1;
const supply = 2;
const { voucher, signature } = await lazyMinter.createVoucher(
tokenId,
"ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
tokenPrice,
supply,
owner.address
);
voucher.recipient = redeemer.address;
expect(await proxy.balanceOf(redeemer.address, tokenId)).to.equal(0);
await proxy.connect(redeemer);
await expect(
proxy.redeem(redeemer.address, 1, voucher, signature, {
value: tokenPrice,
}) // @ts-ignore
).to.be.reverted;
});
it("Should redeem if payment is > minPrice * quantity", async function () {
const { proxy, owner, redeemer } = await loadFixture(deploy);
const tokenPrice = ethers.utils.parseEther("1.0");
const tokenPriceX2 = ethers.utils.parseEther("2.0");
const lazyMinter = new LazyMinter({
contractAddress: proxy.address,
signer: owner,
});
const tokenId = 1;
const supply = 1;
const { voucher, signature } = await lazyMinter.createVoucher(
tokenId,
"ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
tokenPrice,
10,
owner.address
);
const beginBalance = await ethers.provider.getBalance(owner.address);
expect(await proxy.balanceOf(redeemer.address, tokenId)).to.equal(0);
const redeemTx = await proxy
.connect(redeemer)
.redeem(redeemer.address, 1, voucher, signature, {
value: tokenPriceX2,
});
const redeemReceipt = await redeemTx.wait();
const totalSupply = await proxy.totalSupply(tokenId);
const endBalance = await ethers.provider.getBalance(owner.address);
const balanceDelta = endBalance.sub(beginBalance);
expect(balanceDelta).to.equal(tokenPriceX2);
expect(totalSupply).to.equal(supply);
expect(await proxy.balanceOf(redeemer.address, tokenId)).to.equal(1);
});
it("Should fail to redeem if payment is < minPrice", async function () {
const { proxy, owner, redeemer } = await loadFixture(deploy);
const zeroEth = ethers.utils.parseEther("0");
const tokenPrice = ethers.utils.parseEther("1");
const lazyMinter = new LazyMinter({
contractAddress: proxy.address,
signer: owner,
});
const tokenId = 1;
const supply = 2;
const { voucher, signature } = await lazyMinter.createVoucher(
tokenId,
"ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
tokenPrice,
supply,
owner.address
);
expect(await proxy.balanceOf(redeemer.address, tokenId)).to.equal(0);
await proxy.connect(redeemer);
await expect(
proxy.redeem(redeemer.address, 1, voucher, signature, {
value: zeroEth,
}) // @ts-ignore
).to.be.reverted;
});
it.only("Should fail to redeem if the payment receiver cannot receive eth", async function () {
const { proxy, owner, redeemer } = await loadFixture(deploy);
const NoPayContract = await ethers.getContractFactory(
"MockNoPayContract"
);
const noPayContract = (await NoPayContract.deploy({
gasLimit: 30000000,
})) as unknown as MockNoPayContract;
const tokenPrice = ethers.utils.parseEther("1");
const lazyMinter = new LazyMinter({
contractAddress: proxy.address,
signer: owner,
});
const tokenId = 1;
const supply = 2;
const { voucher, signature } = await lazyMinter.createVoucher(
tokenId,
"ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
tokenPrice,
supply,
noPayContract.address
);
await proxy.connect(redeemer);
await expect(
proxy.redeem(redeemer.address, 1, voucher, signature, {
value: tokenPrice,
})
);
});
});
describe("Payment Handling", function () {
it("should correctly transfer funds to the owner on redemption", async function () {});
it("should handle overpayment scenarios correctly", async function () {});
});
});
./test/SingletonFactory.spec.ts
// @ts-nocheck
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import { bytecode } from "../artifacts/contracts/ERC1155Singleton.sol/ERC1155Singleton.json";
import { createSalt } from "./utils";
const hre = require("hardhat");
const ethers = hre.ethers;
describe("SingletonFactory", function () {
let contract;
const SALT = createSalt("Dcentral.me Token Contract");
async function deploy() {
const [owner] = await ethers.getSigners();
const ContractFactory = await ethers.getContractFactory("MockSingletonFactory");
const contract = await ContractFactory.deploy({ gasLimit: 30000000 });
return { contract, owner };
}
beforeEach(async () => {
const { contract: contractFixture } = await loadFixture(deploy);
contract = contractFixture;
});
describe("Deployment", function () {
it("should predict the target address", async () => {
await contract.computeAddress(SALT, bytecode);
});
it("should deploy bytecode", async () => {
await contract.deploy(SALT, bytecode, { gasLimit: 30000000 });
});
it("predicted deployment address should match the deployment address", async () => {
const predictedAddress = await contract.callStatic.computeAddress(
SALT,
bytecode
);
let deployedBytecode = await ethers.provider.getCode(predictedAddress);
expect(deployedBytecode).to.equal("0x");
await contract.deploy(SALT, bytecode, { gasLimit: 30000000 });
deployedBytecode = await ethers.provider.getCode(predictedAddress);
expect(deployedBytecode.length > 2).to.equal(true);
});
it("should revert when trying to deploy with same salt", async () => {
await contract.deploy(SALT, bytecode, { gasLimit: 30000000 });
try {
await contract.deploy(SALT, bytecode, { gasLimit: 30000000 });
} catch (e) {
return;
}
throw new Error("Transaction did not revert duplicate salt.");
});
it("should emit ContractDeployed event when a contract is deployed", async () => {
await expect(await contract.deploy(SALT, bytecode, { gasLimit: 30000000 }))
.to.emit(contract, "ContractDeployed")
.withArgs(await contract.computeAddress(SALT, bytecode));
});
it("should fail to deploy with empty bytecode", async () => {
await expect(contract.deploy(SALT, "0x", { gasLimit: 30000000 })).to.be.reverted;
});
it("should compute different address with different salt", async () => {
const differentSalt = createSalt("Different salt");
const address1 = await contract.computeAddress(SALT, bytecode);
const address2 = await contract.computeAddress(differentSalt, bytecode);
expect(address1).to.not.equal(address2);
});
it("should compute different address with different bytecode", async () => {
const differentBytecode = "0x60606040";
const address1 = await contract.computeAddress(SALT, bytecode);
const address2 = await contract.computeAddress(SALT, differentBytecode);
expect(address1).to.not.equal(address2);
});
});
});
./test/end-to-end/EndToEnd.spec.ts
./test/ERC1155Base.spec.ts
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import {
DEFAULT_OWNER_ROLE,
INTERFACE_ID_ERC165,
INTERFACE_ID_ERC1155,
INTERFACE_ID_ERC2981,
INTERFACE_ID_ACCESS_CONTROL,
ZERO_ADDRESS,
ROYALTY,
ZERO_BYTES32,
CONTRACT_URI,
TOKEN_URI,
LICENSE_URI,
} from "./utils";
import { ERC1155Singleton } from "../typechain";
const hre = require("hardhat");
const ethers = hre.ethers;
describe("ERC1155Singleton Singleton", function () {
async function deploy() {
const [owner] = await ethers.getSigners();
const ContractFactory = await ethers.getContractFactory("ERC1155Singleton");
const contract = (await ContractFactory.deploy({
gasLimit: 30000000,
})) as unknown as ERC1155Singleton;
return { contract, owner };
}
describe("Deployment", function () {
it("sets the DEFAULT_OWNER_ROLE to the 0 address", async function () {
const { contract } = await loadFixture(deploy);
const isSingleton = await contract.hasRole(
DEFAULT_OWNER_ROLE,
ZERO_ADDRESS
);
expect(isSingleton).to.equal(true);
});
});
describe("Initialization", function () {
it("reverts when attempting to initialize again", async function () {
const { contract, owner } = await loadFixture(deploy);
await expect(
contract.init(
owner.address,
CONTRACT_URI,
TOKEN_URI,
LICENSE_URI,
ROYALTY
)
).to.be.revertedWith("Contract has already been initialized");
});
});
describe("Minting", function () {
it("reverts when attempting to mint a new token", async function () {
const { contract, owner } = await loadFixture(deploy);
await expect(contract.mint(owner.address, 100, "0x")).to.be.reverted;
});
it("reverts when attempting to batch mint tokens without the manager role", async function () {
const { contract, owner } = await loadFixture(deploy);
const [_owner, manager, satoshi] = await ethers.getSigners();
await expect(
contract.connect(satoshi).mintBatch(owner.address, [1, 1], "0x")
).to.be.reverted;
});
});
describe("Royalties", function () {
it("reverts when attempting to set default royalty without the manager role", async function () {
const { contract, owner } = await loadFixture(deploy);
await expect(
contract.connect(owner).setDefaultRoyalty(owner.address, ROYALTY)
).to.be.reverted;
});
it("reverts when attempting to delete default royalty without the manager role", async function () {
const { contract, owner } = await loadFixture(deploy);
await expect(contract.connect(owner).deleteDefaultRoyalty()).to.be
.reverted;
});
it("reverts when attempting to set token royalty without the manager role", async function () {
const { contract, owner } = await loadFixture(deploy);
await expect(
contract.connect(owner).setTokenRoyalty(1, owner.address, ROYALTY)
).to.be.reverted;
});
it("reverts when attempting to reset token royalty without the manager role", async function () {
const { contract, owner } = await loadFixture(deploy);
await expect(contract.connect(owner).resetTokenRoyalty(1)).to.be.reverted;
});
});
describe("ERC165 Interface support", function () {
it("should support ERC165 interface", async () => {
const { contract, owner } = await loadFixture(deploy);
expect(await contract.supportsInterface(INTERFACE_ID_ERC165)).to.equal(
true
);
});
it("should support ERC1155 interface", async () => {
const { contract } = await loadFixture(deploy);
expect(await contract.supportsInterface(INTERFACE_ID_ERC1155)).to.equal(
true
);
});
it("should support ERC2981 interface", async () => {
const { contract } = await loadFixture(deploy);
expect(await contract.supportsInterface(INTERFACE_ID_ERC2981)).to.equal(
true
);
});
it("should support Access Control interface", async () => {
const { contract } = await loadFixture(deploy);
expect(
await contract.supportsInterface(INTERFACE_ID_ACCESS_CONTROL)
).to.equal(true);
});
});
describe("Role Management", function () {
it("allows a role to be granted to an account", async function () {
const { contract, owner } = await loadFixture(deploy);
const newRole = ethers.utils.keccak256(
ethers.utils.toUtf8Bytes("NEW_ROLE")
);
await expect(contract.grantRole(newRole, owner.address)).to.be.reverted;
});
it("reverts when attempting to grant a role to the zero address", async function () {
const { contract, owner } = await loadFixture(deploy);
const newRole = ethers.utils.keccak256(
ethers.utils.toUtf8Bytes("NEW_ROLE")
);
await expect(contract.grantRole(newRole, ZERO_ADDRESS)).to.be.reverted;
});
it("reverts when a non-admin tries to grant a role", async function () {
const { contract, owner } = await loadFixture(deploy);
const newRole = ethers.utils.keccak256(
ethers.utils.toUtf8Bytes("NEW_ROLE")
);
await expect(contract.connect(owner).grantRole(newRole, owner.address)).to
.be.reverted;
});
});
});
./test/utils/index.ts
const hre = require("hardhat");
const ethers = hre.ethers;
export function createBytes32(string: string) {
return ethers.utils.formatBytes32String(string);
}
export function createSalt(string: string) {
return ethers.utils.formatBytes32String(string);
}
export function keccak256(data: Uint8Array) {
return ethers.utils.keccak256(data);
}
export function toUtf8Bytes(data: any): Uint8Array {
return ethers.utils.toUtf8Bytes(data);
}
export const INTERFACE_ID_ERC165 = "0x01ffc9a7";
export const INTERFACE_ID_ERC1155 = "0xd9b67a26";
export const INTERFACE_ID_ERC2981 = "0x2a55205a";
export const INTERFACE_ID_ACCESS_CONTROL = "0x01ffc9a7";
export const BASE_POINTS = 10000;
export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
export const ZERO_BYTES32 =
"0x0000000000000000000000000000000000000000000000000000000000000000";
export const DEFAULT_OWNER_ROLE = ZERO_BYTES32;
export const MANAGER_ROLE = keccak256(toUtf8Bytes("MANAGER_ROLE"));
export const CONTRACT_SALT = "Dcentral.me Token Contract";
export const ROYALTY = 5000;
export const CONTRACT_URI_MIMETYPE = "data:application/json;utf8,";
export const CONTRACT_URI = JSON.stringify({
name: "Dcentral Tokens",
description:
"Dcentral Tokens are adorable aquatic beings primarily for demonstrating what can be done using the Dcentral platform. Adopt one today to try out all the Dcentral buying, selling, and bidding feature set.",
image: "external-link-url/image.png",
external_link: "external-link-url",
salt: "User-level Salt",
});
export const TOKEN_URI = "https://dcentral.me/nft/ethereum/";
export const LICENSE_URI = "https://dcentral.me";
export const SEAPORT_1_5_ADDRESS = "0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC";
./test/ERC1155Proxy.spec.ts
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import {
abi as ERC1155SingletonABI,
bytecode as ERC1155Bytecode,
} from "../artifacts/contracts/ERC1155Singleton.sol/ERC1155Singleton.json";
import { bytecode as BeaconBytecode } from "../artifacts/contracts/Beacon.sol/Beacon.json";
import {
createSalt,
createBytes32,
MANAGER_ROLE,
DEFAULT_OWNER_ROLE,
INTERFACE_ID_ERC165,
INTERFACE_ID_ERC1155,
INTERFACE_ID_ERC2981,
INTERFACE_ID_ACCESS_CONTROL,
ZERO_BYTES32,
BASE_POINTS,
ZERO_ADDRESS,
CONTRACT_SALT,
ROYALTY,
CONTRACT_URI,
TOKEN_URI,
LICENSE_URI,
CONTRACT_URI_MIMETYPE,
SEAPORT_1_5_ADDRESS,
} from "./utils";
import { ERC1155Singleton } from "../typechain";
const hre = require("hardhat");
const ethers = hre.ethers;
describe("ERC1155Proxy", function () {
const SALT = createSalt(CONTRACT_SALT);
async function deploy() {
const [owner, manager] = await ethers.getSigners();
const SingletonFactory = await ethers.getContractFactory(
"MockSingletonFactory"
);
const factoryInstance = await SingletonFactory.deploy();
const erc1155Address = await factoryInstance.callStatic.computeAddress(
SALT,
ERC1155Bytecode
);
await factoryInstance.deploy(SALT, ERC1155Bytecode, { gasLimit: 30000000 });
const abiCoder = new ethers.utils.AbiCoder();
const encodedParameters = abiCoder.encode(
["address", "address"],
[erc1155Address, owner.address]
);
const beaconInitCode = BeaconBytecode + encodedParameters.slice(2);
const beaconAddress = await factoryInstance.computeAddress(
SALT,
beaconInitCode
);
await factoryInstance.deploy(SALT, beaconInitCode, { gasLimit: 30000000 });
const beacon = await ethers.getContractAt("Beacon", beaconAddress);
const iface = new ethers.utils.Interface(ERC1155SingletonABI);
const callData = iface.encodeFunctionData("init", [
owner.address,
CONTRACT_URI,
TOKEN_URI,
LICENSE_URI,
ROYALTY,
]);
const proxyAddress = await beacon.callStatic.deployProxyContract(callData);
await beacon.deployProxyContract(callData);
const proxy = (await ethers.getContractAt(
"ERC1155Singleton",
proxyAddress
)) as unknown as ERC1155Singleton;
return { proxy, owner, manager };
}
describe("Deployment", function () {
it("should deploy contract", async () => {
const { proxy, owner } = await loadFixture(deploy);
expect(proxy).to.exist;
expect(owner).to.exist;
});
it("sets the contract uri", async () => {
const { proxy, owner } = await loadFixture(deploy);
const uri = await proxy.contractURI();
console.log(uri);
expect(uri).to.equal(CONTRACT_URI_MIMETYPE.concat(CONTRACT_URI));
});
it("sets the token uri", async () => {
const { proxy, owner } = await loadFixture(deploy);
const tokenId = 0;
const uri = await proxy.tokenURI(tokenId);
expect(uri.toLowerCase()).to.equal(
TOKEN_URI.concat(proxy.address)
.concat("/")
.concat(tokenId.toString())
.toLowerCase()
);
});
it("sets a default approval for Seaport 1.5 contract address for all users", async () => {
const { proxy, owner, manager } = await loadFixture(deploy);
const isApprovedForOwner = await proxy.isApprovedForAll(
owner.address,
SEAPORT_1_5_ADDRESS
);
const isApprovedForRandoUser = await proxy.isApprovedForAll(
manager.address,
SEAPORT_1_5_ADDRESS
);
expect(isApprovedForOwner).to.equal(true);
expect(isApprovedForRandoUser).to.equal(true);
});
});
describe("ERC165 Interface support", function () {
it("should support ERC165 interface", async () => {
const { proxy } = await loadFixture(deploy);
expect(await proxy.supportsInterface(INTERFACE_ID_ERC165)).to.equal(true);
});
it("should support ERC1155 interface", async () => {
const { proxy } = await loadFixture(deploy);
expect(await proxy.supportsInterface(INTERFACE_ID_ERC1155)).to.equal(
true
);
});
it("should support Access Control interface", async () => {
const { proxy } = await loadFixture(deploy);
expect(
await proxy.supportsInterface(INTERFACE_ID_ACCESS_CONTROL)
).to.equal(true);
});
});
describe("Minting", function () {
it("should mint a token", async () => {
const { proxy, owner } = await loadFixture(deploy);
await proxy.mint(owner.address, 1, ZERO_BYTES32);
const balance = await proxy.balanceOf(owner.address, 0);
expect(balance).to.equal(1);
});
it("should mint multiple tokens", async () => {
const { proxy, owner } = await loadFixture(deploy);
await proxy.mint(owner.address, 1, ZERO_BYTES32); // 0
await proxy.mint(owner.address, 1, ZERO_BYTES32); // 1
await proxy.mint(owner.address, 1, ZERO_BYTES32); // 2
await proxy.mintBatch(owner.address, [1, 1], ZERO_BYTES32);
const balance1 = await proxy.balanceOf(owner.address, 3);
const balance2 = await proxy.balanceOf(owner.address, 4);
expect(balance1).to.equal(1);
expect(balance2).to.equal(1);
});
it("should reject a mint if not an owner", async () => {
const { proxy, owner } = await loadFixture(deploy);
const [_, _manager, satoshi] = await ethers.getSigners();
await expect(
proxy.connect(satoshi).mint(satoshi.address, 1, ZERO_BYTES32)
).to.be.reverted;
});
});
describe("Supply", function () {
it("tracks the supply of tokens", async () => {
const { proxy, owner, manager } = await loadFixture(deploy);
await proxy.mint(owner.address, 2, ZERO_BYTES32);
const balance1 = await proxy.balanceOf(owner.address, 0);
expect(balance1).to.equal(2);
const totalSupply = await proxy.totalSupply(0);
expect(totalSupply).to.equal(2);
});
});
describe("License", function () {
it("should create a license for the collection", async () => {
const { proxy } = await loadFixture(deploy);
expect(await proxy.licenseURI()).to.equal(LICENSE_URI);
});
});
describe("ERC2981 Compliance", function () {
it("feeDenominator returns BASE_POINTS", async function () {
const { proxy } = await loadFixture(deploy);
const feeDenominator = await proxy.feeDenominator();
expect(feeDenominator).to.equal(BASE_POINTS);
});
it("sets the default royalty at init", async function () {
const { proxy, owner } = await loadFixture(deploy);
const [receiver, royaltyFraction] = await proxy.royaltyInfo(
0,
BASE_POINTS
);
expect(receiver).to.equal(owner.address);
expect(royaltyFraction).to.equal(ROYALTY);
});
it("setDefaultRoyalty correctly sets a new default royalty", async function () {
const { proxy, owner } = await loadFixture(deploy);
const TEN_PERCENT = 1000;
await proxy.setDefaultRoyalty(owner.address, TEN_PERCENT, {
from: owner.address,
});
const [receiver, royaltyFraction] = await proxy.royaltyInfo(
0,
BASE_POINTS
);
expect(receiver).to.equal(owner.address);
expect(royaltyFraction).to.equal(TEN_PERCENT);
});
it("setDefaultRoyalty correctly overrides a previous royalty", async function () {
const { proxy, owner } = await loadFixture(deploy);
const TEN_PERCENT = 1000;
const [receiver, royaltyFraction] = await proxy.royaltyInfo(
0,
BASE_POINTS
);
await proxy.setDefaultRoyalty(owner.address, ROYALTY, {
from: owner.address,
});
expect(royaltyFraction).to.equal(ROYALTY);
await proxy.setDefaultRoyalty(owner.address, TEN_PERCENT, {
from: owner.address,
});
const [_receiver, _royaltyFraction] = await proxy.royaltyInfo(
0,
BASE_POINTS
);
expect(receiver).to.equal(owner.address);
expect(_royaltyFraction).to.equal(TEN_PERCENT);
});
});
describe("Transferring", function () {
it("should safely transfer a token from one account to another", async () => {
const { proxy, owner } = await loadFixture(deploy);
const [_owner, _manager, satoshi] = await ethers.getSigners();
await proxy.mint(owner.address, 1, ZERO_BYTES32);
await proxy.safeTransferFrom(
owner.address,
satoshi.address,
0,
1,
ZERO_BYTES32
);
const balance = await proxy.balanceOf(satoshi.address, 0);
expect(balance).to.equal(1);
});
it("should safely transfer multiple tokens from one account to another", async () => {
const { proxy, owner } = await loadFixture(deploy);
const [_owner, _manager, satoshi] = await ethers.getSigners();
await proxy.mint(owner.address, 1, ZERO_BYTES32); // 0
await proxy.mint(owner.address, 1, ZERO_BYTES32); // 1
await proxy.mint(owner.address, 1, ZERO_BYTES32); // 2
await proxy.safeBatchTransferFrom(
owner.address,
satoshi.address,
[1, 2],
[1, 1],
ZERO_BYTES32
);
const balance1 = await proxy.balanceOf(satoshi.address, 1);
const balance2 = await proxy.balanceOf(satoshi.address, 2);
expect(balance1).to.equal(1);
expect(balance2).to.equal(1);
});
it("should reject a transfer if not an owner", async () => {
const { proxy, owner } = await loadFixture(deploy);
const [_owner, _manager, satoshi] = await ethers.getSigners();
await proxy.mint(owner.address, 1, ZERO_BYTES32);
await expect(
proxy
.connect(satoshi)
.safeTransferFrom(
satoshi.address,
satoshi.address,
1,
1,
ZERO_BYTES32
)
).to.be.reverted;
});
});
describe("Approvals", function () {
it("should set approval for all", async () => {
const { proxy, owner } = await loadFixture(deploy);
const [_owner, _manager, satoshi] = await ethers.getSigners();
await proxy.setApprovalForAll(satoshi.address, true);
const isApproved = await proxy.isApprovedForAll(
owner.address,
satoshi.address
);
expect(isApproved).to.equal(true);
});