@zoralabs/protocol-sdk 0.3.2 → 0.3.3

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.
@@ -2,14 +2,16 @@ import { foundry } from "viem/chains";
2
2
  import { describe, expect, vi } from "vitest";
3
3
  import { createPremintClient } from "./premint-client";
4
4
  import { anvilTest } from "src/anvil";
5
- import { BackendChainNamesLookup } from "src/apis/chain-constants";
6
5
 
7
6
  describe("ZoraCreator1155Premint", () => {
8
7
  anvilTest(
9
8
  "can sign on the forked premint contract",
10
9
  async ({ viemClients: { walletClient, publicClient } }) => {
11
10
  const [deployerAccount] = await walletClient.getAddresses();
12
- const premintClient = createPremintClient({ chain: foundry });
11
+ const premintClient = createPremintClient({
12
+ chain: foundry,
13
+ publicClient,
14
+ });
13
15
 
14
16
  premintClient.apiClient.getNextUID = vi
15
17
  .fn()
@@ -20,7 +22,6 @@ describe("ZoraCreator1155Premint", () => {
20
22
 
21
23
  await premintClient.createPremint({
22
24
  walletClient,
23
- publicClient,
24
25
  account: deployerAccount!,
25
26
  checkSignature: true,
26
27
  collection: {
@@ -36,7 +37,6 @@ describe("ZoraCreator1155Premint", () => {
36
37
  });
37
38
 
38
39
  expect(premintClient.apiClient.postSignature).toHaveBeenCalledWith({
39
- chain_name: BackendChainNamesLookup.ZORA_GOERLI,
40
40
  collection: {
41
41
  contractAdmin: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
42
42
  contractName: "Testing Contract",
@@ -71,7 +71,10 @@ describe("ZoraCreator1155Premint", () => {
71
71
  anvilTest(
72
72
  "can validate premint on network",
73
73
  async ({ viemClients: { publicClient } }) => {
74
- const premintClient = createPremintClient({ chain: foundry });
74
+ const premintClient = createPremintClient({
75
+ chain: foundry,
76
+ publicClient,
77
+ });
75
78
 
76
79
  const premintData = {
77
80
  collection: {
@@ -106,7 +109,6 @@ describe("ZoraCreator1155Premint", () => {
106
109
  const signatureValid = await premintClient.isValidSignature({
107
110
  // @ts-ignore: Fix enum type
108
111
  data: premintData,
109
- publicClient,
110
112
  });
111
113
  expect(signatureValid.isValid).toBe(true);
112
114
  },
@@ -149,19 +151,22 @@ describe("ZoraCreator1155Premint", () => {
149
151
  });
150
152
  premintClient.apiClient.postSignature = vi.fn();
151
153
 
152
- const { request } = await premintClient.executePremint({
153
- account: deployerAccount!,
154
- data: await premintClient.getPremintData({
155
- address: "0xf8dA7f53c283d898818af7FB9d98103F559bDac2",
156
- uid: 3,
157
- }),
158
- mintArguments: {
159
- quantityToMint: 1,
160
- mintComment: "",
154
+ const simulateContractParameters = await premintClient.makeMintParameters(
155
+ {
156
+ account: deployerAccount!,
157
+ data: await premintClient.getPremintData({
158
+ address: "0xf8dA7f53c283d898818af7FB9d98103F559bDac2",
159
+ uid: 3,
160
+ }),
161
+ mintArguments: {
162
+ quantityToMint: 1,
163
+ mintComment: "",
164
+ },
161
165
  },
162
- });
163
- const { request: simulateRequest } =
164
- await publicClient.simulateContract(request);
166
+ );
167
+ const { request: simulateRequest } = await publicClient.simulateContract(
168
+ simulateContractParameters,
169
+ );
165
170
  const hash = await walletClient.writeContract(simulateRequest);
166
171
  const receipt = await publicClient.waitForTransactionReceipt({ hash });
167
172
  const { premintedLog, urls } =
@@ -1,4 +1,4 @@
1
- import { decodeEventLog } from "viem";
1
+ import { createPublicClient, decodeEventLog, http } from "viem";
2
2
  import type {
3
3
  Account,
4
4
  Address,
@@ -21,9 +21,10 @@ import type {
21
21
  } from "./premint-api-client";
22
22
  import { PremintAPIClient } from "./premint-api-client";
23
23
  import type { DecodeEventLogReturnType } from "viem";
24
- import { ClientBase } from "../apis/client-base";
25
24
  import { OPEN_EDITION_MINT_SIZE } from "../constants";
26
25
  import { REWARD_PER_TOKEN } from "src/apis/chain-constants";
26
+ import { IHttpClient } from "src/apis/http-api-base";
27
+ import { getApiNetworkConfigForChain } from "src/mint/mint-api-client";
27
28
 
28
29
  type MintArgumentsSettings = {
29
30
  tokenURI: string;
@@ -143,16 +144,20 @@ export const encodePremintForAPI = ({
143
144
  * Preminter API to access ZORA Premint functionality.
144
145
  * Currently only supports V1 premints.
145
146
  */
146
- class PremintClient extends ClientBase {
147
- apiClient: typeof PremintAPIClient;
148
-
149
- constructor(chain: Chain, apiClient?: typeof PremintAPIClient) {
150
- super(chain);
151
-
152
- if (!apiClient) {
153
- apiClient = PremintAPIClient;
154
- }
155
- this.apiClient = apiClient;
147
+ class PremintClient {
148
+ readonly apiClient: PremintAPIClient;
149
+ readonly publicClient: PublicClient;
150
+ readonly chain: Chain;
151
+
152
+ constructor(
153
+ chain: Chain,
154
+ publicClient?: PublicClient,
155
+ httpClient?: IHttpClient,
156
+ ) {
157
+ this.chain = chain;
158
+ this.apiClient = new PremintAPIClient(chain.id, httpClient);
159
+ this.publicClient =
160
+ publicClient || createPublicClient({ chain, transport: http() });
156
161
  }
157
162
 
158
163
  /**
@@ -219,7 +224,6 @@ class PremintClient extends ClientBase {
219
224
  collection: Address;
220
225
  }): Promise<SignedPremintResponse> {
221
226
  const signatureResponse = await this.apiClient.getSignature({
222
- chain_name: this.network.zoraBackendChainName,
223
227
  collection_address: collection.toLowerCase(),
224
228
  uid: uid,
225
229
  });
@@ -241,7 +245,6 @@ class PremintClient extends ClientBase {
241
245
  account,
242
246
  checkSignature: false,
243
247
  verifyingContract: collection,
244
- publicClient: this.getPublicClient(),
245
248
  uid: uid,
246
249
  collection: {
247
250
  ...signerData.collection,
@@ -270,16 +273,13 @@ class PremintClient extends ClientBase {
270
273
  uid,
271
274
  account,
272
275
  collection,
273
- publicClient,
274
276
  }: {
275
277
  walletClient: WalletClient;
276
- publicClient: PublicClient;
277
278
  uid: number;
278
279
  account?: Account | Address;
279
280
  collection: Address;
280
281
  }) {
281
282
  const signatureResponse = await this.apiClient.getSignature({
282
- chain_name: this.network.zoraBackendChainName,
283
283
  collection_address: collection.toLowerCase(),
284
284
  uid: uid,
285
285
  });
@@ -298,7 +298,6 @@ class PremintClient extends ClientBase {
298
298
  account,
299
299
  checkSignature: false,
300
300
  verifyingContract: collection,
301
- publicClient: this.getPublicClient(publicClient),
302
301
  uid: uid,
303
302
  collection: signerData.collection,
304
303
  premintConfig: signerData.premint,
@@ -313,7 +312,6 @@ class PremintClient extends ClientBase {
313
312
  */
314
313
  private async signAndSubmitPremint({
315
314
  walletClient,
316
- publicClient,
317
315
  verifyingContract,
318
316
  premintConfig,
319
317
  uid,
@@ -321,7 +319,6 @@ class PremintClient extends ClientBase {
321
319
  checkSignature,
322
320
  collection,
323
321
  }: {
324
- publicClient: PublicClient;
325
322
  uid: number;
326
323
  walletClient: WalletClient;
327
324
  verifyingContract: Address;
@@ -347,7 +344,7 @@ class PremintClient extends ClientBase {
347
344
  });
348
345
 
349
346
  if (checkSignature) {
350
- const [isValidSignature] = await publicClient.readContract({
347
+ const [isValidSignature] = await this.publicClient.readContract({
351
348
  abi: zoraCreator1155PremintExecutorImplABI,
352
349
  address: this.getExecutorAddress(),
353
350
  functionName: "isValidSignature",
@@ -361,7 +358,6 @@ class PremintClient extends ClientBase {
361
358
  const apiData = {
362
359
  collection,
363
360
  premint: encodePremintForAPI(premintConfig),
364
- chain_name: this.network.zoraBackendChainName,
365
361
  signature: signature,
366
362
  };
367
363
 
@@ -394,7 +390,6 @@ class PremintClient extends ClientBase {
394
390
  account,
395
391
  collection,
396
392
  token,
397
- publicClient,
398
393
  walletClient,
399
394
  executionSettings,
400
395
  checkSignature = false,
@@ -404,15 +399,12 @@ class PremintClient extends ClientBase {
404
399
  walletClient: WalletClient;
405
400
  collection: PremintSignatureGetResponse["collection"];
406
401
  token: MintArgumentsSettings;
407
- publicClient?: PublicClient;
408
402
  executionSettings?: {
409
403
  deleted?: boolean;
410
404
  uid?: number;
411
405
  };
412
406
  }) {
413
- publicClient = this.getPublicClient(publicClient);
414
-
415
- const newContractAddress = await publicClient.readContract({
407
+ const newContractAddress = await this.publicClient.readContract({
416
408
  address: this.getExecutorAddress(),
417
409
  abi: zoraCreator1155PremintExecutorImplABI,
418
410
  functionName: "getContractAddress",
@@ -429,7 +421,6 @@ class PremintClient extends ClientBase {
429
421
  let uid = executionSettings?.uid;
430
422
  if (!uid) {
431
423
  const uidResponse = await this.apiClient.getNextUID({
432
- chain_name: this.network.zoraBackendChainName,
433
424
  collection_address: newContractAddress.toLowerCase(),
434
425
  });
435
426
  uid = uidResponse.next_uid;
@@ -454,7 +445,6 @@ class PremintClient extends ClientBase {
454
445
  premintConfig,
455
446
  checkSignature,
456
447
  account,
457
- publicClient,
458
448
  walletClient,
459
449
  collection,
460
450
  });
@@ -475,7 +465,6 @@ class PremintClient extends ClientBase {
475
465
  uid: number;
476
466
  }): Promise<PremintSignatureGetResponse> {
477
467
  return await this.apiClient.getSignature({
478
- chain_name: this.network.zoraBackendChainName,
479
468
  collection_address: address,
480
469
  uid,
481
470
  });
@@ -489,19 +478,15 @@ class PremintClient extends ClientBase {
489
478
  */
490
479
  async isValidSignature({
491
480
  data,
492
- publicClient,
493
481
  }: {
494
482
  data: PremintSignatureGetResponse;
495
- publicClient?: PublicClient;
496
483
  }): Promise<{
497
484
  isValid: boolean;
498
485
  contractAddress: Address;
499
486
  recoveredSigner: Address;
500
487
  }> {
501
- publicClient = this.getPublicClient(publicClient);
502
-
503
488
  const [isValid, contractAddress, recoveredSigner] =
504
- await publicClient.readContract({
489
+ await this.publicClient.readContract({
505
490
  abi: zoraCreator1155PremintExecutorImplABI,
506
491
  address: this.getExecutorAddress(),
507
492
  functionName: "isValidSignature",
@@ -530,19 +515,21 @@ class PremintClient extends ClientBase {
530
515
 
531
516
  const zoraTokenPath = uid ? `premint-${uid}` : tokenId;
532
517
 
518
+ const network = getApiNetworkConfigForChain(this.chain.id);
519
+
533
520
  return {
534
521
  explorer: tokenId
535
522
  ? `https://${this.chain.blockExplorers?.default.url}/token/${address}/instance/${tokenId}`
536
523
  : null,
537
524
  zoraCollect: `https://${
538
- this.network.isTestnet ? "testnet." : ""
525
+ network.isTestnet ? "testnet." : ""
539
526
  }zora.co/collect/${
540
- this.network.zoraPathChainName
527
+ network.zoraPathChainName
541
528
  }:${address}/${zoraTokenPath}`,
542
529
  zoraManage: `https://${
543
- this.network.isTestnet ? "testnet." : ""
530
+ network.isTestnet ? "testnet." : ""
544
531
  }zora.co/collect/${
545
- this.network.zoraPathChainName
532
+ network.zoraPathChainName
546
533
  }:${address}/${zoraTokenPath}`,
547
534
  };
548
535
  }
@@ -559,7 +546,7 @@ class PremintClient extends ClientBase {
559
546
  * @param settings.publicClient Optional public client for preflight checks.
560
547
  * @returns receipt, log, zoraURL
561
548
  */
562
- async executePremint({
549
+ async makeMintParameters({
563
550
  data,
564
551
  account,
565
552
  mintArguments,
@@ -570,7 +557,7 @@ class PremintClient extends ClientBase {
570
557
  quantityToMint: number;
571
558
  mintComment?: string;
572
559
  };
573
- }): Promise<{ request: SimulateContractParameters }> {
560
+ }): Promise<SimulateContractParameters> {
574
561
  if (mintArguments && mintArguments?.quantityToMint < 1) {
575
562
  throw new Error("Quantity to mint cannot be below 1");
576
563
  }
@@ -591,7 +578,7 @@ class PremintClient extends ClientBase {
591
578
 
592
579
  const value = numberToMint * REWARD_PER_TOKEN;
593
580
 
594
- const request = {
581
+ const request: SimulateContractParameters = {
595
582
  account,
596
583
  abi: zoraCreator1155PremintExecutorImplABI,
597
584
  functionName: "premint",
@@ -600,18 +587,18 @@ class PremintClient extends ClientBase {
600
587
  args,
601
588
  };
602
589
 
603
- return {
604
- request,
605
- };
590
+ return request;
606
591
  }
607
592
  }
608
593
 
609
594
  export function createPremintClient({
610
595
  chain,
611
- premintAPIClient,
596
+ httpClient,
597
+ publicClient,
612
598
  }: {
613
599
  chain: Chain;
614
- premintAPIClient?: typeof PremintAPIClient;
600
+ publicClient?: PublicClient;
601
+ httpClient?: IHttpClient;
615
602
  }) {
616
- return new PremintClient(chain, premintAPIClient);
603
+ return new PremintClient(chain, publicClient, httpClient);
617
604
  }