@zoralabs/protocol-sdk 0.5.7-MINT.3 → 0.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,615 @@
1
+ import { Address, zeroAddress } from "viem";
2
+ import { foundry } from "viem/chains";
3
+ import { describe, expect } from "vitest";
4
+ import { parseEther } from "viem";
5
+ import {
6
+ zoraCreator1155PremintExecutorImplABI as preminterAbi,
7
+ zoraCreator1155ImplABI,
8
+ zoraCreator1155FactoryImplAddress,
9
+ zoraCreator1155FactoryImplConfig,
10
+ } from "@zoralabs/protocol-deployments";
11
+
12
+ import {
13
+ premintTypedDataDefinition,
14
+ isValidSignature,
15
+ recoverCreatorFromCreatorAttribution,
16
+ getPremintExecutorAddress,
17
+ getPremintMintCosts,
18
+ } from "./preminter";
19
+ import {
20
+ ContractCreationConfig,
21
+ PremintConfigV1,
22
+ TokenCreationConfigV1,
23
+ PremintConfigVersion,
24
+ TokenCreationConfigV2,
25
+ PremintConfigV2,
26
+ MintArguments,
27
+ } from "./contract-types";
28
+ import { AnvilViemClientsTest, forkUrls, makeAnvilTest } from "src/anvil";
29
+
30
+ // create token and contract creation config:
31
+ const defaultContractConfig = ({
32
+ contractAdmin,
33
+ }: {
34
+ contractAdmin: Address;
35
+ }): ContractCreationConfig => ({
36
+ contractAdmin,
37
+ contractURI: "ipfs://asdfasdfasdf",
38
+ contractName: "My fun NFT",
39
+ });
40
+
41
+ const defaultTokenConfigV1 = (
42
+ fixedPriceMinterAddress: Address,
43
+ creatorAccount: Address,
44
+ ): TokenCreationConfigV1 => ({
45
+ tokenURI: "ipfs://tokenIpfsId0",
46
+ maxSupply: 100n,
47
+ maxTokensPerAddress: 10n,
48
+ pricePerToken: 0n,
49
+ mintStart: 0n,
50
+ mintDuration: 100n,
51
+ royaltyMintSchedule: 30,
52
+ royaltyBPS: 200,
53
+ royaltyRecipient: creatorAccount,
54
+ fixedPriceMinter: fixedPriceMinterAddress,
55
+ });
56
+
57
+ const defaultTokenConfigV2 = (
58
+ fixedPriceMinterAddress: Address,
59
+ creatorAccount: Address,
60
+ createReferral: Address,
61
+ ): TokenCreationConfigV2 => ({
62
+ tokenURI: "ipfs://tokenIpfsId0",
63
+ maxSupply: 100n,
64
+ maxTokensPerAddress: 10n,
65
+ pricePerToken: 0n,
66
+ mintStart: 0n,
67
+ mintDuration: 100n,
68
+ royaltyBPS: 200,
69
+ payoutRecipient: creatorAccount,
70
+ fixedPriceMinter: fixedPriceMinterAddress,
71
+ createReferral,
72
+ });
73
+
74
+ const defaultPremintConfigV1 = ({
75
+ fixedPriceMinter,
76
+ creatorAccount,
77
+ }: {
78
+ fixedPriceMinter: Address;
79
+ creatorAccount: Address;
80
+ }): PremintConfigV1 => ({
81
+ tokenConfig: defaultTokenConfigV1(fixedPriceMinter, creatorAccount),
82
+ deleted: false,
83
+ uid: 105,
84
+ version: 0,
85
+ });
86
+
87
+ const defaultPremintConfigV2 = ({
88
+ fixedPriceMinter,
89
+ creatorAccount,
90
+ createReferral = zeroAddress,
91
+ }: {
92
+ fixedPriceMinter: Address;
93
+ creatorAccount: Address;
94
+ createReferral?: Address;
95
+ }): PremintConfigV2 => ({
96
+ tokenConfig: defaultTokenConfigV2(
97
+ fixedPriceMinter,
98
+ creatorAccount,
99
+ createReferral,
100
+ ),
101
+ deleted: false,
102
+ uid: 106,
103
+ version: 0,
104
+ });
105
+
106
+ const PREMINTER_ADDRESS = getPremintExecutorAddress();
107
+
108
+ const anvilTest = makeAnvilTest({
109
+ forkUrl: forkUrls.zoraSepolia,
110
+ forkBlockNumber: 1265490,
111
+ });
112
+
113
+ async function setupContracts({
114
+ viemClients: { walletClient, testClient, publicClient },
115
+ }: AnvilViemClientsTest) {
116
+ // JSON-RPC Account
117
+ const [deployerAccount, creatorAccount, collectorAccount] =
118
+ (await walletClient.getAddresses()) as [Address, Address, Address, Address];
119
+
120
+ // deploy signature minter contract
121
+ await testClient.setBalance({
122
+ address: deployerAccount,
123
+ value: parseEther("10"),
124
+ });
125
+
126
+ const fixedPriceMinterAddress = await publicClient.readContract({
127
+ abi: zoraCreator1155FactoryImplConfig.abi,
128
+ address: zoraCreator1155FactoryImplAddress[999],
129
+ functionName: "fixedPriceMinter",
130
+ });
131
+
132
+ return {
133
+ accounts: {
134
+ deployerAccount,
135
+ creatorAccount,
136
+ collectorAccount,
137
+ },
138
+ fixedPriceMinterAddress,
139
+ };
140
+ }
141
+
142
+ const zoraSepoliaAnvilTest = makeAnvilTest({
143
+ forkUrl: forkUrls.zoraSepolia,
144
+ forkBlockNumber: 3118200,
145
+ });
146
+
147
+ describe("ZoraCreator1155Preminter", () => {
148
+ // skip for now - we need to make this work on zora testnet chain too
149
+ anvilTest(
150
+ "can sign on the forked premint contract",
151
+ async ({ viemClients }) => {
152
+ const {
153
+ fixedPriceMinterAddress,
154
+ accounts: { creatorAccount },
155
+ } = await setupContracts({ viemClients });
156
+ const premintConfig = defaultPremintConfigV1({
157
+ fixedPriceMinter: fixedPriceMinterAddress,
158
+ creatorAccount,
159
+ });
160
+ const contractConfig = defaultContractConfig({
161
+ contractAdmin: creatorAccount,
162
+ });
163
+
164
+ const preminterAddress = getPremintExecutorAddress();
165
+
166
+ const contractAddress = await viemClients.publicClient.readContract({
167
+ abi: preminterAbi,
168
+ address: preminterAddress,
169
+ functionName: "getContractAddress",
170
+ args: [contractConfig],
171
+ });
172
+
173
+ const signedMessage = await viemClients.walletClient.signTypedData({
174
+ ...premintTypedDataDefinition({
175
+ verifyingContract: contractAddress,
176
+ chainId: 999,
177
+ premintConfig,
178
+ premintConfigVersion: PremintConfigVersion.V1,
179
+ }),
180
+ account: creatorAccount,
181
+ });
182
+
183
+ console.log({
184
+ creatorAccount,
185
+ signedMessage,
186
+ contractConfig,
187
+ premintConfig,
188
+ contractAddress,
189
+ });
190
+ },
191
+ 20 * 1000,
192
+ );
193
+ zoraSepoliaAnvilTest(
194
+ "can sign and recover a v1 premint config signature",
195
+ async ({ viemClients }) => {
196
+ const {
197
+ fixedPriceMinterAddress,
198
+ accounts: { creatorAccount },
199
+ } = await setupContracts({ viemClients });
200
+
201
+ const premintConfig = defaultPremintConfigV1({
202
+ fixedPriceMinter: fixedPriceMinterAddress,
203
+ creatorAccount,
204
+ });
205
+ const contractConfig = defaultContractConfig({
206
+ contractAdmin: creatorAccount,
207
+ });
208
+
209
+ const tokenContract = await viemClients.publicClient.readContract({
210
+ abi: preminterAbi,
211
+ address: PREMINTER_ADDRESS,
212
+ functionName: "getContractAddress",
213
+ args: [contractConfig],
214
+ });
215
+
216
+ // sign message containing contract and token creation config and uid
217
+ const signedMessage = await viemClients.walletClient.signTypedData({
218
+ ...premintTypedDataDefinition({
219
+ verifyingContract: tokenContract,
220
+ // we need to sign here for the anvil chain, cause thats where it is run on
221
+ chainId: foundry.id,
222
+ premintConfig,
223
+ premintConfigVersion: PremintConfigVersion.V1,
224
+ }),
225
+ account: creatorAccount,
226
+ });
227
+
228
+ // recover and verify address is correct
229
+ const { recoveredAddress, isAuthorized } = await isValidSignature({
230
+ collection: contractConfig,
231
+ chainId: viemClients.publicClient.chain!.id,
232
+ premintConfig,
233
+ premintConfigVersion: PremintConfigVersion.V1,
234
+ publicClient: viemClients.publicClient,
235
+ signature: signedMessage,
236
+ });
237
+
238
+ expect(recoveredAddress).to.equal(creatorAccount);
239
+ expect(isAuthorized).toBe(true);
240
+
241
+ expect(recoveredAddress).to.equal(creatorAccount);
242
+ },
243
+
244
+ 20 * 1000,
245
+ );
246
+ zoraSepoliaAnvilTest(
247
+ "can sign and recover a v2 premint config signature",
248
+ async ({ viemClients }) => {
249
+ const {
250
+ fixedPriceMinterAddress,
251
+ accounts: { creatorAccount },
252
+ } = await setupContracts({ viemClients });
253
+
254
+ const premintConfig = defaultPremintConfigV2({
255
+ creatorAccount,
256
+ fixedPriceMinter: fixedPriceMinterAddress,
257
+ createReferral: creatorAccount,
258
+ });
259
+ const contractConfig = defaultContractConfig({
260
+ contractAdmin: creatorAccount,
261
+ });
262
+
263
+ const tokenContract = await viemClients.publicClient.readContract({
264
+ abi: preminterAbi,
265
+ address: PREMINTER_ADDRESS,
266
+ functionName: "getContractAddress",
267
+ args: [contractConfig],
268
+ });
269
+
270
+ // sign message containing contract and token creation config and uid
271
+ const signedMessage = await viemClients.walletClient.signTypedData({
272
+ ...premintTypedDataDefinition({
273
+ verifyingContract: tokenContract,
274
+ // we need to sign here for the anvil chain, cause thats where it is run on
275
+ chainId: foundry.id,
276
+ premintConfig,
277
+ premintConfigVersion: PremintConfigVersion.V2,
278
+ }),
279
+ account: creatorAccount,
280
+ });
281
+
282
+ // recover and verify address is correct
283
+ const { recoveredAddress, isAuthorized } = await isValidSignature({
284
+ collection: contractConfig,
285
+ chainId: viemClients.publicClient.chain!.id,
286
+ premintConfig,
287
+ premintConfigVersion: PremintConfigVersion.V2,
288
+ publicClient: viemClients.publicClient,
289
+ signature: signedMessage,
290
+ });
291
+
292
+ expect(recoveredAddress).to.equal(creatorAccount);
293
+ expect(isAuthorized).toBe(true);
294
+
295
+ expect(recoveredAddress).to.equal(creatorAccount);
296
+ },
297
+
298
+ 20 * 1000,
299
+ );
300
+ zoraSepoliaAnvilTest(
301
+ "can sign and mint multiple tokens",
302
+ async ({ viemClients }) => {
303
+ const {
304
+ fixedPriceMinterAddress,
305
+ accounts: { creatorAccount, collectorAccount },
306
+ } = await setupContracts({ viemClients });
307
+ // setup contract and token creation parameters
308
+ const premintConfig1 = defaultPremintConfigV1({
309
+ fixedPriceMinter: fixedPriceMinterAddress,
310
+ creatorAccount,
311
+ });
312
+ const contractConfig = defaultContractConfig({
313
+ contractAdmin: creatorAccount,
314
+ });
315
+
316
+ // lets make it a random number to not break the existing tests that expect fresh data
317
+ premintConfig1.uid = Math.round(Math.random() * 1000000);
318
+
319
+ let contractAddress = await viemClients.publicClient.readContract({
320
+ abi: preminterAbi,
321
+ address: PREMINTER_ADDRESS,
322
+ functionName: "getContractAddress",
323
+ args: [contractConfig],
324
+ });
325
+
326
+ // have creator sign the message to create the contract
327
+ // and the token
328
+ const signedMessage = await viemClients.walletClient.signTypedData({
329
+ ...premintTypedDataDefinition({
330
+ verifyingContract: contractAddress,
331
+ // we need to sign here for the anvil chain, cause thats where it is run on
332
+ chainId: foundry.id,
333
+ premintConfig: premintConfig1,
334
+ premintConfigVersion: PremintConfigVersion.V1,
335
+ }),
336
+ account: creatorAccount,
337
+ });
338
+
339
+ const quantityToMint = 2n;
340
+
341
+ const valueToSend = (
342
+ await getPremintMintCosts({
343
+ publicClient: viemClients.publicClient,
344
+ quantityToMint,
345
+ tokenContract: contractAddress,
346
+ tokenPrice: premintConfig1.tokenConfig.pricePerToken,
347
+ })
348
+ ).totalCost;
349
+
350
+ await viemClients.testClient.setBalance({
351
+ address: collectorAccount,
352
+ value: parseEther("10"),
353
+ });
354
+
355
+ // get the premint status - it should not be minted
356
+ let [contractCreated, tokenId] =
357
+ await viemClients.publicClient.readContract({
358
+ abi: preminterAbi,
359
+ address: PREMINTER_ADDRESS,
360
+ functionName: "premintStatus",
361
+ args: [contractAddress, premintConfig1.uid],
362
+ });
363
+
364
+ expect(contractCreated).toBe(false);
365
+ expect(tokenId).toBe(0n);
366
+
367
+ const mintArguments: MintArguments = {
368
+ mintComment: "",
369
+ mintRecipient: collectorAccount,
370
+ mintRewardsRecipients: [],
371
+ };
372
+
373
+ // now have the collector execute the first signed message;
374
+ // it should create the contract, the token,
375
+ // and min the quantity to mint tokens to the collector
376
+ // the signature along with contract + token creation
377
+ // parameters are required to call this function
378
+ const mintHash = await viemClients.walletClient.writeContract({
379
+ abi: preminterAbi,
380
+ functionName: "premintV1",
381
+ account: collectorAccount,
382
+ chain: foundry,
383
+ address: PREMINTER_ADDRESS,
384
+ args: [
385
+ contractConfig,
386
+ premintConfig1,
387
+ signedMessage,
388
+ quantityToMint,
389
+ mintArguments,
390
+ ],
391
+ value: valueToSend,
392
+ });
393
+
394
+ // ensure it succeeded
395
+ const receipt = await viemClients.publicClient.waitForTransactionReceipt({
396
+ hash: mintHash,
397
+ });
398
+
399
+ expect(receipt.status).toBe("success");
400
+
401
+ // fetch the premint token id
402
+ [contractCreated, tokenId] = await viemClients.publicClient.readContract({
403
+ abi: preminterAbi,
404
+ address: PREMINTER_ADDRESS,
405
+ functionName: "premintStatus",
406
+ args: [contractAddress, premintConfig1.uid],
407
+ });
408
+
409
+ expect(contractCreated).toBe(true);
410
+ expect(tokenId).not.toBe(0n);
411
+
412
+ // now use what was created, to get the balance from the created contract
413
+ const tokenBalance = await viemClients.publicClient.readContract({
414
+ abi: zoraCreator1155ImplABI,
415
+ address: contractAddress,
416
+ functionName: "balanceOf",
417
+ args: [collectorAccount, tokenId],
418
+ });
419
+
420
+ // get token balance - should be amount that was created
421
+ expect(tokenBalance).toBe(quantityToMint);
422
+
423
+ const premintConfig2 = defaultPremintConfigV2({
424
+ creatorAccount,
425
+ fixedPriceMinter: fixedPriceMinterAddress,
426
+ createReferral: creatorAccount,
427
+ });
428
+
429
+ // sign the message to create the second token
430
+ const signedMessage2 = await viemClients.walletClient.signTypedData({
431
+ ...premintTypedDataDefinition({
432
+ verifyingContract: contractAddress,
433
+ chainId: foundry.id,
434
+ premintConfig: premintConfig2,
435
+ premintConfigVersion: PremintConfigVersion.V2,
436
+ }),
437
+ account: creatorAccount,
438
+ });
439
+
440
+ const quantityToMint2 = 4n;
441
+
442
+ const valueToSend2 = (
443
+ await getPremintMintCosts({
444
+ publicClient: viemClients.publicClient,
445
+ quantityToMint: quantityToMint2,
446
+ tokenContract: contractAddress,
447
+ tokenPrice: premintConfig2.tokenConfig.pricePerToken,
448
+ })
449
+ ).totalCost;
450
+
451
+ const simulationResult = await viemClients.publicClient.simulateContract({
452
+ abi: preminterAbi,
453
+ functionName: "premintV2",
454
+ account: collectorAccount,
455
+ chain: foundry,
456
+ address: PREMINTER_ADDRESS,
457
+ args: [
458
+ contractConfig,
459
+ premintConfig2,
460
+ signedMessage2,
461
+ quantityToMint2,
462
+ mintArguments,
463
+ ],
464
+ value: valueToSend2,
465
+ });
466
+
467
+ // now have the collector execute the second signed message.
468
+ // it should create a new token against the existing contract
469
+ const mintHash2 = await viemClients.walletClient.writeContract(
470
+ simulationResult.request,
471
+ );
472
+
473
+ const premintV2Receipt =
474
+ await viemClients.publicClient.waitForTransactionReceipt({
475
+ hash: mintHash2,
476
+ });
477
+
478
+ expect(premintV2Receipt.status).toBe("success");
479
+
480
+ // now premint status for the second mint, it should be minted
481
+ [, tokenId] = await viemClients.publicClient.readContract({
482
+ abi: preminterAbi,
483
+ address: PREMINTER_ADDRESS,
484
+ functionName: "premintStatus",
485
+ args: [contractAddress, premintConfig2.uid],
486
+ });
487
+
488
+ expect(tokenId).not.toBe(0n);
489
+
490
+ // get balance of second token
491
+ const tokenBalance2 = await viemClients.publicClient.readContract({
492
+ abi: zoraCreator1155ImplABI,
493
+ address: contractAddress,
494
+ functionName: "balanceOf",
495
+ args: [collectorAccount, tokenId],
496
+ });
497
+
498
+ expect(tokenBalance2).toBe(quantityToMint2);
499
+ },
500
+ // 10 second timeout
501
+ 40 * 1000,
502
+ );
503
+
504
+ zoraSepoliaAnvilTest(
505
+ "can decode the CreatorAttribution event",
506
+ async ({ viemClients }) => {
507
+ const {
508
+ fixedPriceMinterAddress,
509
+ accounts: { creatorAccount, collectorAccount },
510
+ } = await setupContracts({ viemClients });
511
+ const premintConfig = defaultPremintConfigV2({
512
+ fixedPriceMinter: fixedPriceMinterAddress,
513
+ creatorAccount,
514
+ });
515
+ const contractConfig = defaultContractConfig({
516
+ contractAdmin: creatorAccount,
517
+ });
518
+
519
+ // lets make it a random number to not break the existing tests that expect fresh data
520
+ premintConfig.uid = Math.round(Math.random() * 1000000);
521
+
522
+ let contractAddress = await viemClients.publicClient.readContract({
523
+ abi: preminterAbi,
524
+ address: PREMINTER_ADDRESS,
525
+ functionName: "getContractAddress",
526
+ args: [contractConfig],
527
+ });
528
+
529
+ const signingChainId = foundry.id;
530
+
531
+ // have creator sign the message to create the contract
532
+ // and the token
533
+ const signedMessage = await viemClients.walletClient.signTypedData({
534
+ ...premintTypedDataDefinition({
535
+ verifyingContract: contractAddress,
536
+ // we need to sign here for the anvil chain, cause thats where it is run on
537
+ chainId: signingChainId,
538
+ premintConfig,
539
+ premintConfigVersion: PremintConfigVersion.V2,
540
+ }),
541
+ account: creatorAccount,
542
+ });
543
+
544
+ const quantityToMint = 2n;
545
+
546
+ const valueToSend = (
547
+ await getPremintMintCosts({
548
+ publicClient: viemClients.publicClient,
549
+ quantityToMint,
550
+ tokenContract: contractAddress,
551
+ tokenPrice: premintConfig.tokenConfig.pricePerToken,
552
+ })
553
+ ).totalCost;
554
+
555
+ await viemClients.testClient.setBalance({
556
+ address: collectorAccount,
557
+ value: parseEther("10"),
558
+ });
559
+
560
+ // now have the collector execute the first signed message;
561
+ // it should create the contract, the token,
562
+ // and min the quantity to mint tokens to the collector
563
+ // the signature along with contract + token creation
564
+ // parameters are required to call this function
565
+ const mintHash = await viemClients.walletClient.writeContract({
566
+ abi: preminterAbi,
567
+ functionName: "premintV2",
568
+ account: collectorAccount,
569
+ chain: foundry,
570
+ address: PREMINTER_ADDRESS,
571
+ args: [
572
+ contractConfig,
573
+ premintConfig,
574
+ signedMessage,
575
+ quantityToMint,
576
+ {
577
+ mintComment: "",
578
+ mintRecipient: collectorAccount,
579
+ mintRewardsRecipients: [],
580
+ },
581
+ ],
582
+ value: valueToSend,
583
+ });
584
+
585
+ // ensure it succeeded
586
+ const receipt = await viemClients.publicClient.waitForTransactionReceipt({
587
+ hash: mintHash,
588
+ });
589
+
590
+ expect(receipt.status).toBe("success");
591
+
592
+ // get the CreatorAttribution event from the erc1155 contract:
593
+ const topics = await viemClients.publicClient.getContractEvents({
594
+ abi: zoraCreator1155ImplABI,
595
+ address: contractAddress,
596
+ eventName: "CreatorAttribution",
597
+ });
598
+
599
+ expect(topics.length).toBe(1);
600
+
601
+ const creatorAttributionEvent = topics[0]!;
602
+
603
+ const { creator: creatorFromEvent } = creatorAttributionEvent.args;
604
+
605
+ const recoveredSigner = await recoverCreatorFromCreatorAttribution({
606
+ creatorAttribution: creatorAttributionEvent.args,
607
+ chainId: signingChainId,
608
+ tokenContract: contractAddress,
609
+ });
610
+
611
+ expect(creatorFromEvent).toBe(creatorAccount);
612
+ expect(recoveredSigner).toBe(creatorFromEvent);
613
+ },
614
+ );
615
+ });