@zoralabs/protocol-sdk 0.3.3 → 0.3.5

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.
Files changed (37) hide show
  1. package/.turbo/turbo-build.log +7 -7
  2. package/CHANGELOG.md +28 -0
  3. package/README.md +7 -26
  4. package/dist/anvil.d.ts +4 -2
  5. package/dist/anvil.d.ts.map +1 -1
  6. package/dist/constants.d.ts +32 -0
  7. package/dist/constants.d.ts.map +1 -1
  8. package/dist/index.cjs +476 -323
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.js +447 -298
  11. package/dist/index.js.map +1 -1
  12. package/dist/mint/mint-api-client.d.ts +16 -213
  13. package/dist/mint/mint-api-client.d.ts.map +1 -1
  14. package/dist/mint/mint-client.d.ts +7 -226
  15. package/dist/mint/mint-client.d.ts.map +1 -1
  16. package/dist/premint/premint-client.d.ts +63 -17
  17. package/dist/premint/premint-client.d.ts.map +1 -1
  18. package/dist/premint/preminter.d.ts +106 -19
  19. package/dist/premint/preminter.d.ts.map +1 -1
  20. package/dist/types.d.ts +2 -0
  21. package/dist/types.d.ts.map +1 -0
  22. package/package.json +2 -1
  23. package/src/anvil.ts +7 -4
  24. package/src/constants.ts +7 -0
  25. package/src/create/1155-create-helper.ts +1 -1
  26. package/src/mint/mint-api-client.ts +79 -53
  27. package/src/mint/mint-client.test.ts +9 -11
  28. package/src/mint/mint-client.ts +50 -215
  29. package/src/premint/premint-client.test.ts +9 -8
  30. package/src/premint/premint-client.ts +113 -61
  31. package/src/premint/preminter.test.ts +207 -130
  32. package/src/premint/preminter.ts +388 -51
  33. package/src/types.ts +1 -0
  34. package/tsconfig.json +2 -17
  35. package/dist/apis/generated/discover-api-types.d.ts +0 -2131
  36. package/dist/apis/generated/discover-api-types.d.ts.map +0 -1
  37. package/src/apis/generated/discover-api-types.ts +0 -2180
@@ -11,10 +11,20 @@ import type {
11
11
  } from "viem";
12
12
  import {
13
13
  zoraCreator1155PremintExecutorImplABI,
14
- zoraCreator1155PremintExecutorImplAddress,
15
14
  zoraCreatorFixedPriceSaleStrategyAddress,
16
15
  } from "@zoralabs/protocol-deployments";
17
- import { PremintConfig, preminterTypedDataDefinition } from "./preminter";
16
+ import {
17
+ PremintConfigAndVersion,
18
+ PremintConfigV1,
19
+ PremintConfigV2,
20
+ PremintConfigVersion,
21
+ getPremintCollectionAddress,
22
+ premintTypedDataDefinition,
23
+ ContractCreationConfig,
24
+ isValidSignature,
25
+ isAuthorizedToCreatePremint,
26
+ getPremintExecutorAddress,
27
+ } from "./preminter";
18
28
  import type {
19
29
  PremintSignatureGetResponse,
20
30
  PremintSignatureResponse,
@@ -96,7 +106,7 @@ export function getPremintedLogFromReceipt(
96
106
  * @param premint Premint object from the server to convert to one that's compatible with viem
97
107
  * @returns Viem type-compatible premint object
98
108
  */
99
- export const convertPremint = (
109
+ export const convertPremintV1 = (
100
110
  premint: PremintSignatureGetResponse["premint"],
101
111
  ) => ({
102
112
  ...premint,
@@ -125,10 +135,10 @@ export const convertCollection = (
125
135
  * @param premint Premint object from viem to convert to a JSON compatible type.
126
136
  * @returns JSON compatible premint
127
137
  */
128
- export const encodePremintForAPI = ({
138
+ export const encodePremintV1ForAPI = ({
129
139
  tokenConfig,
130
140
  ...premint
131
- }: PremintConfig) => ({
141
+ }: PremintConfigV1) => ({
132
142
  ...premint,
133
143
  tokenConfig: {
134
144
  ...tokenConfig,
@@ -140,6 +150,34 @@ export const encodePremintForAPI = ({
140
150
  },
141
151
  });
142
152
 
153
+ export const encodePremintV2ForAPI = ({
154
+ tokenConfig,
155
+ ...premint
156
+ }: PremintConfigV2) => ({
157
+ ...premint,
158
+ tokenConfig: {
159
+ ...tokenConfig,
160
+ maxSupply: tokenConfig.maxSupply.toString(),
161
+ pricePerToken: tokenConfig.pricePerToken.toString(),
162
+ mintStart: tokenConfig.mintStart.toString(),
163
+ mintDuration: tokenConfig.mintDuration.toString(),
164
+ maxTokensPerAddress: tokenConfig.maxTokensPerAddress.toString(),
165
+ },
166
+ });
167
+
168
+ export const encodePremintForAPI = ({
169
+ premintConfig,
170
+ premintConfigVersion,
171
+ }: PremintConfigAndVersion) => {
172
+ if (premintConfigVersion === PremintConfigVersion.V1) {
173
+ return encodePremintV1ForAPI(premintConfig);
174
+ }
175
+ if (premintConfigVersion === PremintConfigVersion.V2) {
176
+ return encodePremintV2ForAPI(premintConfig);
177
+ }
178
+ throw new Error(`Invalid premint config version ${premintConfigVersion}`);
179
+ };
180
+
143
181
  /**
144
182
  * Preminter API to access ZORA Premint functionality.
145
183
  * Currently only supports V1 premints.
@@ -160,16 +198,6 @@ class PremintClient {
160
198
  publicClient || createPublicClient({ chain, transport: http() });
161
199
  }
162
200
 
163
- /**
164
- * The premint executor address is deployed to the same address across all chains.
165
- * Can be overridden as needed by making a parent class.
166
- *
167
- * @returns Executor address for premints
168
- */
169
- getExecutorAddress() {
170
- return zoraCreator1155PremintExecutorImplAddress[999];
171
- }
172
-
173
201
  /**
174
202
  * The fixed price minter address is the same across all chains for our current
175
203
  * deployer strategy.
@@ -228,7 +256,7 @@ class PremintClient {
228
256
  uid: uid,
229
257
  });
230
258
 
231
- const convertedPremint = convertPremint(signatureResponse.premint);
259
+ const convertedPremint = convertPremintV1(signatureResponse.premint);
232
260
  const signerData = {
233
261
  ...signatureResponse,
234
262
  premint: {
@@ -251,6 +279,7 @@ class PremintClient {
251
279
  contractAdmin: signerData.collection.contractAdmin as Address,
252
280
  },
253
281
  premintConfig: signerData.premint,
282
+ premintConfigVersion: PremintConfigVersion.V1,
254
283
  });
255
284
  }
256
285
 
@@ -288,7 +317,7 @@ class PremintClient {
288
317
  ...signatureResponse,
289
318
  collection: convertCollection(signatureResponse.collection),
290
319
  premint: {
291
- ...convertPremint(signatureResponse.premint),
320
+ ...convertPremintV1(signatureResponse.premint),
292
321
  deleted: true,
293
322
  },
294
323
  };
@@ -301,6 +330,7 @@ class PremintClient {
301
330
  uid: uid,
302
331
  collection: signerData.collection,
303
332
  premintConfig: signerData.premint,
333
+ premintConfigVersion: PremintConfigVersion.V1,
304
334
  });
305
335
  }
306
336
 
@@ -313,20 +343,19 @@ class PremintClient {
313
343
  private async signAndSubmitPremint({
314
344
  walletClient,
315
345
  verifyingContract,
316
- premintConfig,
317
346
  uid,
318
347
  account,
319
348
  checkSignature,
320
349
  collection,
350
+ ...premintConfigAndVersion
321
351
  }: {
322
352
  uid: number;
323
353
  walletClient: WalletClient;
324
354
  verifyingContract: Address;
325
355
  checkSignature: boolean;
326
356
  account?: Address | Account;
327
- premintConfig: PremintConfig;
328
357
  collection: PremintSignatureGetResponse["collection"];
329
- }) {
358
+ } & PremintConfigAndVersion) {
330
359
  if (!account) {
331
360
  account = walletClient.account;
332
361
  }
@@ -336,28 +365,37 @@ class PremintClient {
336
365
 
337
366
  const signature = await walletClient.signTypedData({
338
367
  account,
339
- ...preminterTypedDataDefinition({
368
+ ...premintTypedDataDefinition({
340
369
  verifyingContract,
341
- premintConfig,
370
+ ...premintConfigAndVersion,
342
371
  chainId: this.chain.id,
343
372
  }),
344
373
  });
345
374
 
346
375
  if (checkSignature) {
347
- const [isValidSignature] = await this.publicClient.readContract({
348
- abi: zoraCreator1155PremintExecutorImplABI,
349
- address: this.getExecutorAddress(),
350
- functionName: "isValidSignature",
351
- args: [convertCollection(collection), premintConfig, signature],
376
+ const convertedCollection = convertCollection(collection);
377
+ const isAuthorized = await isAuthorizedToCreatePremint({
378
+ collection: convertCollection(collection),
379
+ signature,
380
+ publicClient: this.publicClient,
381
+ signer: typeof account === "string" ? account : account.address,
382
+ collectionAddress: await this.getCollectionAddress(convertedCollection),
383
+ ...premintConfigAndVersion,
352
384
  });
353
- if (!isValidSignature) {
354
- throw new Error("Invalid signature");
385
+ if (!isAuthorized) {
386
+ throw new Error("Not authorized to create premint");
355
387
  }
356
388
  }
357
389
 
390
+ if (
391
+ premintConfigAndVersion.premintConfigVersion === PremintConfigVersion.V2
392
+ ) {
393
+ throw new Error("premint config v2 not supported yet");
394
+ }
395
+
358
396
  const apiData = {
359
397
  collection,
360
- premint: encodePremintForAPI(premintConfig),
398
+ premint: encodePremintV1ForAPI(premintConfigAndVersion.premintConfig),
361
399
  signature: signature,
362
400
  };
363
401
 
@@ -404,11 +442,9 @@ class PremintClient {
404
442
  uid?: number;
405
443
  };
406
444
  }) {
407
- const newContractAddress = await this.publicClient.readContract({
408
- address: this.getExecutorAddress(),
409
- abi: zoraCreator1155PremintExecutorImplABI,
410
- functionName: "getContractAddress",
411
- args: [convertCollection(collection)],
445
+ const newContractAddress = await getPremintCollectionAddress({
446
+ publicClient: this.publicClient,
447
+ collection: convertCollection(collection),
412
448
  });
413
449
 
414
450
  const tokenConfig = {
@@ -432,7 +468,7 @@ class PremintClient {
432
468
 
433
469
  let deleted = executionSettings?.deleted || false;
434
470
 
435
- const premintConfig = {
471
+ const premintConfig: PremintConfigV1 = {
436
472
  tokenConfig: tokenConfig,
437
473
  uid,
438
474
  version: 1,
@@ -443,6 +479,7 @@ class PremintClient {
443
479
  uid,
444
480
  verifyingContract: newContractAddress,
445
481
  premintConfig,
482
+ premintConfigVersion: PremintConfigVersion.V1,
446
483
  checkSignature,
447
484
  account,
448
485
  walletClient,
@@ -470,34 +507,42 @@ class PremintClient {
470
507
  });
471
508
  }
472
509
 
510
+ /**
511
+ * Gets the deterministic contract address for a premint collection
512
+ * @param collection Collection to get the address for
513
+ * @returns deterministic contract address
514
+ */
515
+ async getCollectionAddress(collection: ContractCreationConfig) {
516
+ return await getPremintCollectionAddress({
517
+ collection,
518
+ publicClient: this.publicClient,
519
+ });
520
+ }
521
+
473
522
  /**
474
523
  * Check user signature for v1
475
524
  *
476
525
  * @param data Signature data from the API
477
- * @returns isValid = signature is valid or not, contractAddress = assumed contract address, recoveredSigner = signer from contract
526
+ * @returns isValid = signature is valid or not, recoveredSigner = signer from contract
478
527
  */
479
528
  async isValidSignature({
480
- data,
481
- }: {
482
- data: PremintSignatureGetResponse;
483
- }): Promise<{
529
+ signature,
530
+ premint,
531
+ collection,
532
+ }: PremintSignatureResponse): Promise<{
484
533
  isValid: boolean;
485
- contractAddress: Address;
486
- recoveredSigner: Address;
534
+ recoveredSigner: Address | undefined;
487
535
  }> {
488
- const [isValid, contractAddress, recoveredSigner] =
489
- await this.publicClient.readContract({
490
- abi: zoraCreator1155PremintExecutorImplABI,
491
- address: this.getExecutorAddress(),
492
- functionName: "isValidSignature",
493
- args: [
494
- convertCollection(data.collection),
495
- convertPremint(data.premint),
496
- data.signature as Hex,
497
- ],
498
- });
536
+ const { isAuthorized, recoveredAddress } = await isValidSignature({
537
+ chainId: this.chain.id,
538
+ signature: signature as Hex,
539
+ premintConfig: convertPremintV1(premint),
540
+ premintConfigVersion: PremintConfigVersion.V1,
541
+ collection: convertCollection(collection),
542
+ publicClient: this.publicClient,
543
+ });
499
544
 
500
- return { isValid, contractAddress, recoveredSigner };
545
+ return { isValid: isAuthorized, recoveredSigner: recoveredAddress };
501
546
  }
502
547
 
503
548
  protected makeUrls({
@@ -557,16 +602,20 @@ class PremintClient {
557
602
  quantityToMint: number;
558
603
  mintComment?: string;
559
604
  };
560
- }): Promise<SimulateContractParameters> {
605
+ }): Promise<
606
+ SimulateContractParameters<
607
+ typeof zoraCreator1155PremintExecutorImplABI,
608
+ "premint"
609
+ >
610
+ > {
561
611
  if (mintArguments && mintArguments?.quantityToMint < 1) {
562
612
  throw new Error("Quantity to mint cannot be below 1");
563
613
  }
564
614
 
565
- const targetAddress = this.getExecutorAddress();
566
615
  const numberToMint = BigInt(mintArguments?.quantityToMint || 1);
567
616
  const args = [
568
617
  convertCollection(data.collection),
569
- convertPremint(data.premint),
618
+ convertPremintV1(data.premint),
570
619
  data.signature as Hex,
571
620
  numberToMint,
572
621
  mintArguments?.mintComment || "",
@@ -578,14 +627,17 @@ class PremintClient {
578
627
 
579
628
  const value = numberToMint * REWARD_PER_TOKEN;
580
629
 
581
- const request: SimulateContractParameters = {
630
+ const request = {
582
631
  account,
583
632
  abi: zoraCreator1155PremintExecutorImplABI,
584
633
  functionName: "premint",
585
634
  value,
586
- address: targetAddress,
635
+ address: getPremintExecutorAddress(),
587
636
  args,
588
- };
637
+ } satisfies SimulateContractParameters<
638
+ typeof zoraCreator1155PremintExecutorImplABI,
639
+ "premint"
640
+ >;
589
641
 
590
642
  return request;
591
643
  }