@zoralabs/protocol-sdk 0.9.0 → 0.9.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.
Files changed (52) hide show
  1. package/.turbo/turbo-build.log +7 -7
  2. package/CHANGELOG.md +12 -0
  3. package/dist/anvil.d.ts.map +1 -1
  4. package/dist/apis/http-api-base.d.ts +1 -1
  5. package/dist/apis/http-api-base.d.ts.map +1 -1
  6. package/dist/apis/subgraph-querier.d.ts +6 -0
  7. package/dist/apis/subgraph-querier.d.ts.map +1 -1
  8. package/dist/create/1155-create-helper.d.ts +4 -1
  9. package/dist/create/1155-create-helper.d.ts.map +1 -1
  10. package/dist/create/contract-getter.d.ts +30 -0
  11. package/dist/create/contract-getter.d.ts.map +1 -0
  12. package/dist/create/contract-setup.d.ts +4 -8
  13. package/dist/create/contract-setup.d.ts.map +1 -1
  14. package/dist/create/mint-from-create.d.ts +12 -0
  15. package/dist/create/mint-from-create.d.ts.map +1 -0
  16. package/dist/create/subgraph-queries.d.ts +13 -0
  17. package/dist/create/subgraph-queries.d.ts.map +1 -0
  18. package/dist/create/types.d.ts +2 -0
  19. package/dist/create/types.d.ts.map +1 -1
  20. package/dist/index.cjs +267 -74
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.js +261 -68
  23. package/dist/index.js.map +1 -1
  24. package/dist/mint/mint-queries.d.ts +3 -1
  25. package/dist/mint/mint-queries.d.ts.map +1 -1
  26. package/dist/mint/subgraph-mint-getter.d.ts +2 -2
  27. package/dist/mint/subgraph-mint-getter.d.ts.map +1 -1
  28. package/dist/mint/subgraph-queries.d.ts +1 -5
  29. package/dist/mint/subgraph-queries.d.ts.map +1 -1
  30. package/dist/mint/types.d.ts +1 -0
  31. package/dist/mint/types.d.ts.map +1 -1
  32. package/dist/retries.d.ts +7 -0
  33. package/dist/retries.d.ts.map +1 -0
  34. package/dist/sdk.d.ts +2 -0
  35. package/dist/sdk.d.ts.map +1 -1
  36. package/package.json +1 -1
  37. package/src/apis/http-api-base.ts +12 -20
  38. package/src/apis/subgraph-querier.ts +7 -0
  39. package/src/create/1155-create-helper.test.ts +58 -59
  40. package/src/create/1155-create-helper.ts +37 -7
  41. package/src/create/contract-getter.ts +90 -0
  42. package/src/create/contract-setup.ts +17 -46
  43. package/src/create/mint-from-create.ts +111 -0
  44. package/src/create/subgraph-queries.ts +35 -0
  45. package/src/create/types.ts +2 -0
  46. package/src/mint/mint-queries.ts +8 -8
  47. package/src/mint/subgraph-mint-getter.ts +5 -2
  48. package/src/mint/subgraph-queries.ts +1 -7
  49. package/src/mint/types.ts +3 -0
  50. package/src/retries.ts +49 -0
  51. package/src/sdk.ts +8 -0
  52. package/test-integration/create-multiple-tokens-on-contract.ts +104 -0
package/dist/index.js CHANGED
@@ -2503,6 +2503,44 @@ import {
2503
2503
  premintTypedDataDefinition as premintTypedDataDefinition2
2504
2504
  } from "@zoralabs/protocol-deployments";
2505
2505
 
2506
+ // src/retries.ts
2507
+ async function wait(delayMs) {
2508
+ return new Promise((resolve) => {
2509
+ setTimeout(resolve, delayMs);
2510
+ });
2511
+ }
2512
+ var retryInternal = async ({
2513
+ tryFn,
2514
+ maxTries = 3,
2515
+ atTry,
2516
+ linearBackoffMS = 200,
2517
+ shouldRetryOnError = () => true
2518
+ }) => {
2519
+ try {
2520
+ return await tryFn();
2521
+ } catch (err) {
2522
+ if (shouldRetryOnError(err)) {
2523
+ if (atTry <= maxTries) {
2524
+ await wait(atTry * linearBackoffMS);
2525
+ return await retryInternal({
2526
+ tryFn,
2527
+ maxTries,
2528
+ atTry: atTry + 1,
2529
+ linearBackoffMS,
2530
+ shouldRetryOnError
2531
+ });
2532
+ }
2533
+ }
2534
+ throw err;
2535
+ }
2536
+ };
2537
+ var retriesGeneric = async (params) => {
2538
+ return retryInternal({
2539
+ ...params,
2540
+ atTry: 1
2541
+ });
2542
+ };
2543
+
2506
2544
  // src/apis/http-api-base.ts
2507
2545
  var BadResponseError = class extends Error {
2508
2546
  constructor(message, status, json) {
@@ -2512,11 +2550,6 @@ var BadResponseError = class extends Error {
2512
2550
  this.json = json;
2513
2551
  }
2514
2552
  };
2515
- async function wait(delayMs) {
2516
- return new Promise((resolve) => {
2517
- setTimeout(resolve, delayMs);
2518
- });
2519
- }
2520
2553
  var get = async (url) => {
2521
2554
  const response = await fetch(url, { method: "GET" });
2522
2555
  if (response.status !== 200) {
@@ -2562,20 +2595,16 @@ var post = async (url, data) => {
2562
2595
  }
2563
2596
  return await response.json();
2564
2597
  };
2565
- var retries = async (tryFn, maxTries = 3, atTry = 1, linearBackoffMS = 200) => {
2566
- try {
2567
- return await tryFn();
2568
- } catch (err) {
2569
- if (err instanceof BadResponseError) {
2570
- if (err.status >= 500) {
2571
- if (atTry <= maxTries) {
2572
- await wait(atTry * linearBackoffMS);
2573
- return await retries(tryFn, maxTries, atTry + 1);
2574
- }
2575
- }
2576
- }
2577
- throw err;
2578
- }
2598
+ var retries = async (tryFn, maxTries = 3, linearBackoffMS = 200) => {
2599
+ const shouldRetry = (err) => {
2600
+ return err instanceof BadResponseError && err.status >= 500;
2601
+ };
2602
+ return retriesGeneric({
2603
+ tryFn,
2604
+ maxTries,
2605
+ linearBackoffMS,
2606
+ shouldRetryOnError: shouldRetry
2607
+ });
2579
2608
  };
2580
2609
  var httpClient = {
2581
2610
  get,
@@ -4067,7 +4096,7 @@ var makeOnchainPrepareMint = (result) => (params) => {
4067
4096
  token: result,
4068
4097
  mintParams: params
4069
4098
  }),
4070
- erc20Approval: getRequiredErc20Approvals(params, result),
4099
+ erc20Approval: getRequiredErc20Approvals(params, result.salesConfig),
4071
4100
  costs: parseMintCosts({
4072
4101
  salesConfig: result.salesConfig,
4073
4102
  quantityToMint: BigInt(params.quantityToMint),
@@ -4100,13 +4129,13 @@ function toPremintMintReturn({
4100
4129
  prepareMint: makePremintPrepareMint(mintable, mintFee, premint)
4101
4130
  };
4102
4131
  }
4103
- function getRequiredErc20Approvals(params, result) {
4104
- if (result.salesConfig?.saleType !== "erc20")
4132
+ function getRequiredErc20Approvals(params, salesConfig) {
4133
+ if (salesConfig?.saleType !== "erc20")
4105
4134
  return void 0;
4106
4135
  return {
4107
- quantity: result.salesConfig.pricePerToken * BigInt(params.quantityToMint),
4108
- approveTo: result.salesConfig.address,
4109
- erc20: result.salesConfig.currency
4136
+ quantity: salesConfig.pricePerToken * BigInt(params.quantityToMint),
4137
+ approveTo: salesConfig.address,
4138
+ erc20: salesConfig.currency
4110
4139
  };
4111
4140
  }
4112
4141
 
@@ -4230,36 +4259,6 @@ function new1155ContractVersion(chainId) {
4230
4259
  }
4231
4260
  return address.CONTRACT_1155_IMPL_VERSION;
4232
4261
  }
4233
- async function getContractInfoExistingContract({
4234
- publicClient,
4235
- contractAddress
4236
- }) {
4237
- let contractVersion;
4238
- try {
4239
- contractVersion = await publicClient.readContract({
4240
- abi: zoraCreator1155ImplABI3,
4241
- address: contractAddress,
4242
- functionName: "contractVersion"
4243
- });
4244
- } catch (e) {
4245
- throw new Error(`Contract does not exist at ${contractAddress}`);
4246
- }
4247
- const nextTokenId = await publicClient.readContract({
4248
- address: contractAddress,
4249
- abi: zoraCreator1155ImplABI3,
4250
- functionName: "nextTokenId"
4251
- });
4252
- const contractName = await publicClient.readContract({
4253
- address: contractAddress,
4254
- abi: zoraCreator1155ImplABI3,
4255
- functionName: "name"
4256
- });
4257
- return {
4258
- contractVersion,
4259
- contractName,
4260
- nextTokenId
4261
- };
4262
- }
4263
4262
  async function getDeterministicContractAddress({
4264
4263
  publicClient,
4265
4264
  account,
@@ -4281,6 +4280,17 @@ async function getDeterministicContractAddress({
4281
4280
  });
4282
4281
  return contractAddress;
4283
4282
  }
4283
+ async function getNewContractMintFee({
4284
+ publicClient,
4285
+ chainId
4286
+ }) {
4287
+ const implAddress = contracts1155.addresses[chainId].CONTRACT_1155_IMPL;
4288
+ return await publicClient.readContract({
4289
+ abi: zoraCreator1155ImplABI3,
4290
+ address: implAddress,
4291
+ functionName: "mintFee"
4292
+ });
4293
+ }
4284
4294
 
4285
4295
  // src/create/token-setup.ts
4286
4296
  var semver2 = __toESM(require_semver2(), 1);
@@ -5814,6 +5824,95 @@ var contractSupportsMintRewards = (contractVersion, contractStandard) => {
5814
5824
  }
5815
5825
  };
5816
5826
 
5827
+ // src/create/mint-from-create.ts
5828
+ import { parseEther as parseEther3, zeroAddress as zeroAddress6 } from "viem";
5829
+ async function toSalesStrategyFromSubgraph({
5830
+ minter,
5831
+ salesConfig,
5832
+ getContractMintFee
5833
+ }) {
5834
+ if (salesConfig.type === "timed") {
5835
+ return {
5836
+ saleType: "timed",
5837
+ address: minter,
5838
+ // for now we hardcode this
5839
+ mintFeePerQuantity: parseEther3("0.000111"),
5840
+ saleStart: salesConfig.saleStart.toString(),
5841
+ saleEnd: salesConfig.saleEnd.toString(),
5842
+ // the following are not needed for now but we wanna satisfy concrete
5843
+ erc20Z: zeroAddress6,
5844
+ mintFee: 0n,
5845
+ pool: zeroAddress6,
5846
+ secondaryActivated: false
5847
+ };
5848
+ }
5849
+ if (salesConfig.type === "erc20Mint") {
5850
+ return {
5851
+ saleType: "erc20",
5852
+ address: minter,
5853
+ mintFeePerQuantity: 0n,
5854
+ saleStart: salesConfig.saleStart.toString(),
5855
+ saleEnd: salesConfig.saleEnd.toString(),
5856
+ currency: salesConfig.currency,
5857
+ pricePerToken: salesConfig.pricePerToken,
5858
+ maxTokensPerAddress: salesConfig.maxTokensPerAddress
5859
+ };
5860
+ }
5861
+ const contractMintFee = await getContractMintFee();
5862
+ if (salesConfig.type === "fixedPrice") {
5863
+ return {
5864
+ saleType: "fixedPrice",
5865
+ address: minter,
5866
+ maxTokensPerAddress: salesConfig.maxTokensPerAddress,
5867
+ mintFeePerQuantity: contractMintFee,
5868
+ pricePerToken: salesConfig.pricePerToken,
5869
+ saleStart: salesConfig.saleStart.toString(),
5870
+ saleEnd: salesConfig.saleEnd.toString()
5871
+ };
5872
+ }
5873
+ return {
5874
+ saleType: "allowlist",
5875
+ address: minter,
5876
+ saleStart: salesConfig.saleStart.toString(),
5877
+ saleEnd: salesConfig.saleEnd.toString(),
5878
+ merkleRoot: salesConfig.presaleMerkleRoot,
5879
+ mintFeePerQuantity: contractMintFee
5880
+ };
5881
+ }
5882
+ function makeOnchainPrepareMintFromCreate({
5883
+ contractAddress,
5884
+ tokenId,
5885
+ result,
5886
+ minter,
5887
+ getContractMintFee,
5888
+ contractVersion
5889
+ }) {
5890
+ return async (params) => {
5891
+ const subgraphSalesConfig = await toSalesStrategyFromSubgraph({
5892
+ minter,
5893
+ getContractMintFee,
5894
+ salesConfig: result
5895
+ });
5896
+ return {
5897
+ parameters: makePrepareMint1155TokenParams({
5898
+ salesConfigAndTokenInfo: {
5899
+ salesConfig: subgraphSalesConfig,
5900
+ contractVersion
5901
+ },
5902
+ ...params,
5903
+ tokenContract: contractAddress,
5904
+ tokenId
5905
+ }),
5906
+ costs: parseMintCosts({
5907
+ allowListEntry: params.allowListEntry,
5908
+ quantityToMint: BigInt(params.quantityToMint),
5909
+ salesConfig: subgraphSalesConfig
5910
+ }),
5911
+ erc20Approval: getRequiredErc20Approvals(params, subgraphSalesConfig)
5912
+ };
5913
+ };
5914
+ }
5915
+
5817
5916
  // src/create/1155-create-helper.ts
5818
5917
  var ROYALTY_BPS_DEFAULT = 1e3;
5819
5918
  var getTokenIdFromCreateReceipt = (receipt) => {
@@ -5896,10 +5995,12 @@ function makeCreateTokenCall({
5896
5995
  var Create1155Client = class {
5897
5996
  constructor({
5898
5997
  chainId,
5899
- publicClient
5998
+ publicClient,
5999
+ contractGetter
5900
6000
  }) {
5901
6001
  this.chainId = chainId;
5902
6002
  this.publicClient = publicClient;
6003
+ this.contractGetter = contractGetter;
5903
6004
  }
5904
6005
  async createNew1155(props) {
5905
6006
  return createNew1155ContractAndToken({
@@ -5920,7 +6021,8 @@ var Create1155Client = class {
5920
6021
  token,
5921
6022
  getAdditionalSetupActions,
5922
6023
  publicClient: this.publicClient,
5923
- chainId: this.chainId
6024
+ chainId: this.chainId,
6025
+ contractGetter: this.contractGetter
5924
6026
  });
5925
6027
  }
5926
6028
  };
@@ -5961,6 +6063,18 @@ async function createNew1155ContractAndToken({
5961
6063
  chainId,
5962
6064
  contract
5963
6065
  });
6066
+ const prepareMint = makeOnchainPrepareMintFromCreate({
6067
+ contractAddress,
6068
+ contractVersion,
6069
+ minter,
6070
+ result: newToken.salesConfig,
6071
+ tokenId: nextTokenId,
6072
+ // to get the contract wide mint fee, we get what it would be for a new contract
6073
+ getContractMintFee: async () => getNewContractMintFee({
6074
+ publicClient,
6075
+ chainId
6076
+ })
6077
+ });
5964
6078
  return {
5965
6079
  parameters: request,
5966
6080
  tokenSetupActions,
@@ -5968,7 +6082,8 @@ async function createNew1155ContractAndToken({
5968
6082
  newToken,
5969
6083
  contractAddress,
5970
6084
  contractVersion,
5971
- minter
6085
+ minter,
6086
+ prepareMint
5972
6087
  };
5973
6088
  }
5974
6089
  async function createNew1155Token({
@@ -5976,13 +6091,10 @@ async function createNew1155Token({
5976
6091
  account,
5977
6092
  getAdditionalSetupActions,
5978
6093
  token,
5979
- publicClient,
5980
- chainId
6094
+ chainId,
6095
+ contractGetter
5981
6096
  }) {
5982
- const { nextTokenId, contractVersion } = await getContractInfoExistingContract({
5983
- publicClient,
5984
- contractAddress
5985
- });
6097
+ const { nextTokenId, contractVersion, mintFee } = await contractGetter.getContractInfo({ contractAddress, retries: 5 });
5986
6098
  const {
5987
6099
  minter,
5988
6100
  newToken,
@@ -6000,13 +6112,22 @@ async function createNew1155Token({
6000
6112
  account,
6001
6113
  tokenSetupActions
6002
6114
  });
6115
+ const prepareMint = makeOnchainPrepareMintFromCreate({
6116
+ contractAddress,
6117
+ contractVersion,
6118
+ minter,
6119
+ result: newToken.salesConfig,
6120
+ tokenId: nextTokenId,
6121
+ getContractMintFee: async () => mintFee
6122
+ });
6003
6123
  return {
6004
6124
  parameters: request,
6005
6125
  tokenSetupActions,
6006
6126
  newTokenId: nextTokenId,
6007
6127
  newToken,
6008
6128
  contractVersion,
6009
- minter
6129
+ minter,
6130
+ prepareMint
6010
6131
  };
6011
6132
  }
6012
6133
  async function prepareSetupActions({
@@ -6107,7 +6228,7 @@ import {
6107
6228
  import {
6108
6229
  decodeErrorResult,
6109
6230
  encodeFunctionData as encodeFunctionData3,
6110
- zeroAddress as zeroAddress6
6231
+ zeroAddress as zeroAddress7
6111
6232
  } from "viem";
6112
6233
  var addressOrAccountAddress = (address) => typeof address === "string" ? address : address.address;
6113
6234
  var mintWithEthParams = ({
@@ -6264,7 +6385,7 @@ var encodePremintOnManager = ({
6264
6385
  premintConfig,
6265
6386
  premintSignature,
6266
6387
  mintArguments,
6267
- signerContract = zeroAddress6
6388
+ signerContract = zeroAddress7
6268
6389
  }) => encodeFunctionData3({
6269
6390
  abi: zoraMintsManagerImplConfig.abi,
6270
6391
  functionName: "collectPremintV2",
@@ -6338,6 +6459,77 @@ function decodeCallFailedError(error) {
6338
6459
  });
6339
6460
  }
6340
6461
 
6462
+ // src/create/subgraph-queries.ts
6463
+ function buildContractInfoQuery({
6464
+ contractAddress
6465
+ }) {
6466
+ return {
6467
+ query: `
6468
+ query ($contractAddress: Bytes!) {
6469
+ zoraCreateContract(id: $contractAddress) {
6470
+ contractVersion
6471
+ name
6472
+ mintFeePerQuantity
6473
+ tokens(first: 1, orderBy: tokenId, orderDirection: desc) {
6474
+ tokenId
6475
+ }
6476
+ }
6477
+ }
6478
+ `,
6479
+ variables: {
6480
+ contractAddress: contractAddress.toLowerCase()
6481
+ },
6482
+ parseResponseData: (responseData) => responseData.zoraCreateContract
6483
+ };
6484
+ }
6485
+
6486
+ // src/create/contract-getter.ts
6487
+ var SubgraphContractGetter = class {
6488
+ constructor(chainId, subgraphQuerier) {
6489
+ this.subgraphQuerier = subgraphQuerier || new SubgraphQuerier(httpClient);
6490
+ this.networkConfig = getApiNetworkConfigForChain(chainId);
6491
+ }
6492
+ async querySubgraphWithRetries({
6493
+ query,
6494
+ variables,
6495
+ parseResponseData
6496
+ }) {
6497
+ const responseData = await this.subgraphQuerier.query({
6498
+ subgraphUrl: this.networkConfig.subgraphUrl,
6499
+ query,
6500
+ variables
6501
+ });
6502
+ return parseResponseData(responseData);
6503
+ }
6504
+ async getContractInfo({
6505
+ contractAddress,
6506
+ retries: retries2 = 1
6507
+ }) {
6508
+ const tryFn = async () => {
6509
+ const responseData2 = await this.querySubgraphWithRetries(
6510
+ buildContractInfoQuery({ contractAddress })
6511
+ );
6512
+ if (!responseData2) {
6513
+ console.log("could not find contract");
6514
+ throw new Error("Cannot find contract");
6515
+ }
6516
+ return responseData2;
6517
+ };
6518
+ const responseData = await retriesGeneric({
6519
+ tryFn,
6520
+ maxTries: retries2,
6521
+ linearBackoffMS: 1e3
6522
+ });
6523
+ const nextTokenId = responseData.tokens.length === 0 ? 1n : BigInt(responseData.tokens[0].tokenId) + 1n;
6524
+ return {
6525
+ name: responseData.name,
6526
+ contractVersion: responseData.contractVersion,
6527
+ mintFee: BigInt(responseData.mintFeePerQuantity),
6528
+ nextTokenId
6529
+ };
6530
+ }
6531
+ };
6532
+
6341
6533
  // src/sdk.ts
6342
6534
  function createCreatorClient(clientConfig) {
6343
6535
  const premintClient = new PremintClient({
@@ -6347,7 +6539,8 @@ function createCreatorClient(clientConfig) {
6347
6539
  });
6348
6540
  const create1155CreatorClient = new Create1155Client({
6349
6541
  chainId: clientConfig.chainId,
6350
- publicClient: clientConfig.publicClient
6542
+ publicClient: clientConfig.publicClient,
6543
+ contractGetter: clientConfig.contractGetter || new SubgraphContractGetter(clientConfig.chainId)
6351
6544
  });
6352
6545
  return {
6353
6546
  createPremint: (p) => premintClient.createPremint(p),