@zoralabs/protocol-sdk 0.4.0 → 0.4.2

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.
package/dist/index.js CHANGED
@@ -17,7 +17,8 @@ import {
17
17
  hashDomain,
18
18
  keccak256,
19
19
  concat,
20
- recoverAddress
20
+ recoverAddress,
21
+ parseEther
21
22
  } from "viem";
22
23
 
23
24
  // src/premint/contract-types.ts
@@ -243,17 +244,12 @@ var supportedPremintVersions = async ({
243
244
  tokenContract,
244
245
  publicClient
245
246
  }) => {
246
- try {
247
- return await publicClient.readContract({
248
- abi: preminterAbi,
249
- address: getPremintExecutorAddress(),
250
- functionName: "supportedPremintSignatureVersions",
251
- args: [tokenContract]
252
- });
253
- } catch (e) {
254
- console.error(e);
255
- return ["1"];
256
- }
247
+ return await publicClient.readContract({
248
+ abi: preminterAbi,
249
+ address: getPremintExecutorAddress(),
250
+ functionName: "supportedPremintSignatureVersions",
251
+ args: [tokenContract]
252
+ });
257
253
  };
258
254
  var supportsPremintVersion = async ({
259
255
  version,
@@ -309,6 +305,41 @@ function makeNewPremint({
309
305
  tokenConfig
310
306
  };
311
307
  }
308
+ async function getPremintMintFee({
309
+ tokenContract,
310
+ publicClient
311
+ }) {
312
+ try {
313
+ return await publicClient.readContract({
314
+ address: getPremintExecutorAddress(),
315
+ abi: zoraCreator1155PremintExecutorImplABI,
316
+ functionName: "mintFee",
317
+ args: [tokenContract]
318
+ });
319
+ } catch (e) {
320
+ console.error(e);
321
+ return parseEther("0.000777");
322
+ }
323
+ }
324
+ async function getPremintMintCosts({
325
+ publicClient,
326
+ tokenContract,
327
+ tokenPrice,
328
+ quantityToMint
329
+ }) {
330
+ const mintFee = await getPremintMintFee({ tokenContract, publicClient });
331
+ return {
332
+ mintFee: mintFee * quantityToMint,
333
+ tokenPurchaseCost: tokenPrice * quantityToMint,
334
+ totalCost: (mintFee + tokenPrice) * quantityToMint
335
+ };
336
+ }
337
+ function makeMintRewardsRecipient({
338
+ mintReferral = zeroAddress,
339
+ platformReferral = zeroAddress
340
+ }) {
341
+ return [mintReferral, platformReferral];
342
+ }
312
343
 
313
344
  // src/apis/http-api-base.ts
314
345
  var BadResponseError = class extends Error {
@@ -409,8 +440,8 @@ import {
409
440
  zora,
410
441
  zoraTestnet
411
442
  } from "viem/chains";
412
- import { parseEther } from "viem";
413
- var REWARD_PER_TOKEN = parseEther("0.000777");
443
+ import { parseEther as parseEther2 } from "viem";
444
+ var REWARD_PER_TOKEN = parseEther2("0.000777");
414
445
  var BackendChainNamesLookup = {
415
446
  ZORA_MAINNET: "ZORA-MAINNET",
416
447
  ZORA_GOERLI: "ZORA-GOERLI",
@@ -559,55 +590,55 @@ var MintAPIClient = class {
559
590
  }
560
591
  };
561
592
 
562
- // src/premint/premint-api-client.ts
563
- var postSignature = async ({
564
- httpClient: { post: post2, retries: retries2 } = httpClient,
565
- ...data
566
- }) => retries2(
567
- () => post2(`${ZORA_API_BASE}premint/signature`, data)
568
- );
569
- var getNextUID = async ({
570
- chain_name,
571
- collection_address,
572
- httpClient: { retries: retries2, get: get2 } = httpClient
573
- }) => retries2(
574
- () => get2(
575
- `${ZORA_API_BASE}premint/signature/${chain_name}/${collection_address}/next_uid`
576
- )
577
- );
578
- var getSignature = async ({
579
- collection_address,
580
- uid,
581
- chain_name,
582
- httpClient: { retries: retries2, get: get2 } = httpClient
583
- }) => {
584
- const result = await retries2(
585
- () => get2(
586
- `${ZORA_API_BASE}premint/signature/${chain_name}/${collection_address}/${uid}`
587
- )
588
- );
589
- return {
590
- ...result,
591
- // for now - we stub the backend api to simulate returning v1
592
- premint_config_version: "1" /* V1 */
593
- };
594
- };
595
- var convertCollection = (collection) => ({
593
+ // src/premint/conversions.ts
594
+ var convertCollectionFromApi = (collection) => ({
596
595
  ...collection,
597
596
  contractAdmin: collection.contractAdmin
598
597
  });
599
- var convertPremintV1 = (premint) => ({
600
- ...premint,
601
- tokenConfig: {
602
- ...premint.tokenConfig,
603
- fixedPriceMinter: premint.tokenConfig.fixedPriceMinter,
604
- royaltyRecipient: premint.tokenConfig.royaltyRecipient,
605
- maxSupply: BigInt(premint.tokenConfig.maxSupply),
606
- pricePerToken: BigInt(premint.tokenConfig.pricePerToken),
607
- mintStart: BigInt(premint.tokenConfig.mintStart),
608
- mintDuration: BigInt(premint.tokenConfig.mintDuration),
609
- maxTokensPerAddress: BigInt(premint.tokenConfig.maxTokensPerAddress)
598
+ var convertPremintFromApi = (premint) => {
599
+ if (premint.config_version === "1" /* V1 */ || !premint.config_version) {
600
+ const tokenConfig = premint.tokenConfig;
601
+ return {
602
+ premintConfigVersion: "1" /* V1 */,
603
+ premintConfig: {
604
+ ...premint,
605
+ tokenConfig: {
606
+ ...tokenConfig,
607
+ fixedPriceMinter: tokenConfig.fixedPriceMinter,
608
+ royaltyRecipient: tokenConfig.royaltyRecipient,
609
+ maxSupply: BigInt(tokenConfig.maxSupply),
610
+ pricePerToken: BigInt(tokenConfig.pricePerToken),
611
+ mintStart: BigInt(tokenConfig.mintStart),
612
+ mintDuration: BigInt(tokenConfig.mintDuration),
613
+ maxTokensPerAddress: BigInt(tokenConfig.maxTokensPerAddress)
614
+ }
615
+ }
616
+ };
617
+ } else {
618
+ const tokenConfig = premint.tokenConfig;
619
+ return {
620
+ premintConfigVersion: "2" /* V2 */,
621
+ premintConfig: {
622
+ ...premint,
623
+ tokenConfig: {
624
+ ...tokenConfig,
625
+ fixedPriceMinter: tokenConfig.fixedPriceMinter,
626
+ payoutRecipient: tokenConfig.payoutRecipient,
627
+ createReferral: tokenConfig.createReferral,
628
+ maxSupply: BigInt(tokenConfig.maxSupply),
629
+ pricePerToken: BigInt(tokenConfig.pricePerToken),
630
+ mintStart: BigInt(tokenConfig.mintStart),
631
+ mintDuration: BigInt(tokenConfig.mintDuration),
632
+ maxTokensPerAddress: BigInt(tokenConfig.maxTokensPerAddress)
633
+ }
634
+ }
635
+ };
610
636
  }
637
+ };
638
+ var convertGetPremintApiResponse = (response) => ({
639
+ ...convertPremintFromApi(response.premint),
640
+ collection: convertCollectionFromApi(response.collection),
641
+ signature: response.signature
611
642
  });
612
643
  var encodePremintV1ForAPI = ({
613
644
  tokenConfig,
@@ -649,31 +680,68 @@ var encodePremintForAPI = ({
649
680
  }
650
681
  throw new Error(`Invalid premint config version ${premintConfigVersion}`);
651
682
  };
683
+ var encodePostSignatureInput = ({
684
+ collection,
685
+ premintConfigVersion,
686
+ premintConfig,
687
+ signature,
688
+ chainId
689
+ }) => ({
690
+ premint: encodePremintForAPI({
691
+ premintConfig,
692
+ premintConfigVersion
693
+ }),
694
+ signature,
695
+ collection,
696
+ chain_name: networkConfigByChain[chainId].zoraBackendChainName
697
+ });
698
+
699
+ // src/premint/premint-api-client.ts
700
+ var postSignature = async ({
701
+ httpClient: { post: post2, retries: retries2 } = httpClient,
702
+ ...data
703
+ }) => retries2(
704
+ () => post2(`${ZORA_API_BASE}premint/signature`, data)
705
+ );
706
+ var getNextUID = async ({
707
+ chain_name,
708
+ collection_address,
709
+ httpClient: { retries: retries2, get: get2 } = httpClient
710
+ }) => retries2(
711
+ () => get2(
712
+ `${ZORA_API_BASE}premint/signature/${chain_name}/${collection_address}/next_uid`
713
+ )
714
+ );
715
+ var getSignature = async ({
716
+ collection_address,
717
+ uid,
718
+ chain_name,
719
+ httpClient: { retries: retries2, get: get2 } = httpClient
720
+ }) => {
721
+ const result = await retries2(
722
+ () => get2(
723
+ `${ZORA_API_BASE}premint/signature/${chain_name}/${collection_address}/${uid}`
724
+ )
725
+ );
726
+ return result;
727
+ };
652
728
  var PremintAPIClient = class {
653
729
  constructor(chainId, httpClient2) {
654
730
  this.postSignature = async ({
655
731
  collection,
656
- premintConfigVersion,
657
- premintConfig,
658
- signature
732
+ signature,
733
+ ...premintConfigAndVersion
659
734
  }) => {
660
- if (premintConfigVersion === "1" /* V1 */) {
661
- const data = {
662
- premint: encodePremintForAPI({
663
- premintConfig,
664
- premintConfigVersion
665
- }),
666
- signature,
667
- collection
668
- };
669
- return postSignature({
670
- ...data,
671
- chain_name: this.networkConfig.zoraBackendChainName,
672
- httpClient: this.httpClient
673
- });
674
- } else {
675
- throw new Error("Unsupported premint config version");
676
- }
735
+ const data = encodePostSignatureInput({
736
+ collection,
737
+ ...premintConfigAndVersion,
738
+ chainId: this.networkConfig.chainId,
739
+ signature
740
+ });
741
+ return postSignature({
742
+ ...data,
743
+ httpClient: this.httpClient
744
+ });
677
745
  };
678
746
  this.getNextUID = async (collectionAddress) => (await getNextUID({
679
747
  collection_address: collectionAddress.toLowerCase(),
@@ -690,21 +758,7 @@ var PremintAPIClient = class {
690
758
  chain_name: this.networkConfig.zoraBackendChainName,
691
759
  httpClient: this.httpClient
692
760
  });
693
- const premintConfigVersion = response.premint_config_version || "1" /* V1 */;
694
- let premintConfig;
695
- if (premintConfigVersion === "1" /* V1 */) {
696
- premintConfig = convertPremintV1(response.premint);
697
- } else {
698
- throw new Error(
699
- `Unsupported premint config version: ${premintConfigVersion}`
700
- );
701
- }
702
- return {
703
- signature: response.signature,
704
- collection: convertCollection(response.collection),
705
- premintConfig,
706
- premintConfigVersion
707
- };
761
+ return convertGetPremintApiResponse(response);
708
762
  };
709
763
  this.httpClient = httpClient2 || httpClient;
710
764
  this.networkConfig = getApiNetworkConfigForChain(chainId);
@@ -763,10 +817,10 @@ function getPremintedLogFromReceipt(receipt) {
763
817
  try {
764
818
  const decodedLog = decodeEventLog({
765
819
  abi: zoraCreator1155PremintExecutorImplABI2,
766
- eventName: "Preminted",
820
+ eventName: "PremintedV2",
767
821
  ...data
768
822
  });
769
- if (decodedLog.eventName === "Preminted") {
823
+ if (decodedLog.eventName === "PremintedV2") {
770
824
  return decodedLog.args;
771
825
  }
772
826
  } catch (err) {
@@ -884,7 +938,7 @@ var PremintClient = class {
884
938
  * @returns
885
939
  */
886
940
  async signAndSubmitPremint(params) {
887
- const { premint, verifyingContract } = await signAndSubmitPremint({
941
+ const { verifyingContract } = await signAndSubmitPremint({
888
942
  ...params,
889
943
  chainId: this.chain.id,
890
944
  apiClient: this.apiClient,
@@ -894,8 +948,7 @@ var PremintClient = class {
894
948
  return {
895
949
  urls: this.makeUrls({ address: verifyingContract, uid }),
896
950
  uid,
897
- verifyingContract,
898
- premint
951
+ verifyingContract
899
952
  };
900
953
  }
901
954
  /**
@@ -1016,6 +1069,18 @@ var PremintClient = class {
1016
1069
  chain: this.chain
1017
1070
  });
1018
1071
  }
1072
+ async getMintCosts({
1073
+ tokenContract,
1074
+ quantityToMint,
1075
+ tokenCreationConfig
1076
+ }) {
1077
+ return await getPremintMintCosts({
1078
+ publicClient: this.publicClient,
1079
+ quantityToMint,
1080
+ tokenContract,
1081
+ tokenPrice: tokenCreationConfig.pricePerToken
1082
+ });
1083
+ }
1019
1084
  /**
1020
1085
  * Execute premint on-chain
1021
1086
  *
@@ -1045,12 +1110,25 @@ var PremintClient = class {
1045
1110
  uid
1046
1111
  });
1047
1112
  const numberToMint = BigInt(mintArguments?.quantityToMint || 1);
1048
- const value = numberToMint * REWARD_PER_TOKEN;
1113
+ const value = (await getPremintMintCosts({
1114
+ tokenContract,
1115
+ quantityToMint: numberToMint,
1116
+ publicClient: this.publicClient,
1117
+ tokenPrice: premintConfig.tokenConfig.pricePerToken
1118
+ })).totalCost;
1119
+ const mintArgumentsContract = {
1120
+ mintComment: mintArguments?.mintComment || "",
1121
+ mintRecipient: mintArguments?.mintRecipient || (typeof account === "string" ? account : account.address),
1122
+ mintRewardsRecipients: makeMintRewardsRecipient({
1123
+ mintReferral: mintArguments?.mintReferral,
1124
+ platformReferral: mintArguments?.platformReferral
1125
+ })
1126
+ };
1049
1127
  if (premintConfigVersion === "1" /* V1 */) {
1050
1128
  return {
1051
1129
  account,
1052
1130
  abi: zoraCreator1155PremintExecutorImplABI2,
1053
- functionName: "premint",
1131
+ functionName: "premintV1",
1054
1132
  value,
1055
1133
  address: getPremintExecutorAddress(),
1056
1134
  args: [
@@ -1058,12 +1136,11 @@ var PremintClient = class {
1058
1136
  premintConfig,
1059
1137
  signature,
1060
1138
  numberToMint,
1061
- mintArguments?.mintComment || ""
1139
+ mintArgumentsContract
1062
1140
  ]
1063
1141
  };
1064
1142
  } else if (premintConfigVersion === "2" /* V2 */) {
1065
1143
  const toPost = premintConfig;
1066
- const accountAddress = typeof account === "string" ? account : account.address;
1067
1144
  return {
1068
1145
  account,
1069
1146
  abi: zoraCreator1155PremintExecutorImplABI2,
@@ -1076,11 +1153,7 @@ var PremintClient = class {
1076
1153
  toPost,
1077
1154
  signature,
1078
1155
  numberToMint,
1079
- {
1080
- mintComment: mintArguments?.mintComment || "",
1081
- mintRecipient: mintArguments?.mintRecipient || accountAddress,
1082
- mintReferral: mintArguments?.mintReferral || zeroAddress2
1083
- }
1156
+ mintArgumentsContract
1084
1157
  ]
1085
1158
  };
1086
1159
  }
@@ -1577,10 +1650,13 @@ export {
1577
1650
  getMintCosts,
1578
1651
  getPremintCollectionAddress,
1579
1652
  getPremintExecutorAddress,
1653
+ getPremintMintCosts,
1654
+ getPremintMintFee,
1580
1655
  getPremintedLogFromReceipt,
1581
1656
  getTokenIdFromCreateReceipt,
1582
1657
  isAuthorizedToCreatePremint,
1583
1658
  isValidSignature,
1659
+ makeMintRewardsRecipient,
1584
1660
  makeNewPremint,
1585
1661
  markPremintDeleted,
1586
1662
  migratePremintConfigToV2,