@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.cjs CHANGED
@@ -2551,6 +2551,44 @@ var toContractCreationConfigOrAddress = ({
2551
2551
  // src/premint/premint-client.ts
2552
2552
  var import_protocol_deployments4 = require("@zoralabs/protocol-deployments");
2553
2553
 
2554
+ // src/retries.ts
2555
+ async function wait(delayMs) {
2556
+ return new Promise((resolve) => {
2557
+ setTimeout(resolve, delayMs);
2558
+ });
2559
+ }
2560
+ var retryInternal = async ({
2561
+ tryFn,
2562
+ maxTries = 3,
2563
+ atTry,
2564
+ linearBackoffMS = 200,
2565
+ shouldRetryOnError = () => true
2566
+ }) => {
2567
+ try {
2568
+ return await tryFn();
2569
+ } catch (err) {
2570
+ if (shouldRetryOnError(err)) {
2571
+ if (atTry <= maxTries) {
2572
+ await wait(atTry * linearBackoffMS);
2573
+ return await retryInternal({
2574
+ tryFn,
2575
+ maxTries,
2576
+ atTry: atTry + 1,
2577
+ linearBackoffMS,
2578
+ shouldRetryOnError
2579
+ });
2580
+ }
2581
+ }
2582
+ throw err;
2583
+ }
2584
+ };
2585
+ var retriesGeneric = async (params) => {
2586
+ return retryInternal({
2587
+ ...params,
2588
+ atTry: 1
2589
+ });
2590
+ };
2591
+
2554
2592
  // src/apis/http-api-base.ts
2555
2593
  var BadResponseError = class extends Error {
2556
2594
  constructor(message, status, json) {
@@ -2560,11 +2598,6 @@ var BadResponseError = class extends Error {
2560
2598
  this.json = json;
2561
2599
  }
2562
2600
  };
2563
- async function wait(delayMs) {
2564
- return new Promise((resolve) => {
2565
- setTimeout(resolve, delayMs);
2566
- });
2567
- }
2568
2601
  var get = async (url) => {
2569
2602
  const response = await fetch(url, { method: "GET" });
2570
2603
  if (response.status !== 200) {
@@ -2610,20 +2643,16 @@ var post = async (url, data) => {
2610
2643
  }
2611
2644
  return await response.json();
2612
2645
  };
2613
- var retries = async (tryFn, maxTries = 3, atTry = 1, linearBackoffMS = 200) => {
2614
- try {
2615
- return await tryFn();
2616
- } catch (err) {
2617
- if (err instanceof BadResponseError) {
2618
- if (err.status >= 500) {
2619
- if (atTry <= maxTries) {
2620
- await wait(atTry * linearBackoffMS);
2621
- return await retries(tryFn, maxTries, atTry + 1);
2622
- }
2623
- }
2624
- }
2625
- throw err;
2626
- }
2646
+ var retries = async (tryFn, maxTries = 3, linearBackoffMS = 200) => {
2647
+ const shouldRetry = (err) => {
2648
+ return err instanceof BadResponseError && err.status >= 500;
2649
+ };
2650
+ return retriesGeneric({
2651
+ tryFn,
2652
+ maxTries,
2653
+ linearBackoffMS,
2654
+ shouldRetryOnError: shouldRetry
2655
+ });
2627
2656
  };
2628
2657
  var httpClient = {
2629
2658
  get,
@@ -4105,7 +4134,7 @@ var makeOnchainPrepareMint = (result) => (params) => {
4105
4134
  token: result,
4106
4135
  mintParams: params
4107
4136
  }),
4108
- erc20Approval: getRequiredErc20Approvals(params, result),
4137
+ erc20Approval: getRequiredErc20Approvals(params, result.salesConfig),
4109
4138
  costs: parseMintCosts({
4110
4139
  salesConfig: result.salesConfig,
4111
4140
  quantityToMint: BigInt(params.quantityToMint),
@@ -4138,13 +4167,13 @@ function toPremintMintReturn({
4138
4167
  prepareMint: makePremintPrepareMint(mintable, mintFee, premint)
4139
4168
  };
4140
4169
  }
4141
- function getRequiredErc20Approvals(params, result) {
4142
- if (result.salesConfig?.saleType !== "erc20")
4170
+ function getRequiredErc20Approvals(params, salesConfig) {
4171
+ if (salesConfig?.saleType !== "erc20")
4143
4172
  return void 0;
4144
4173
  return {
4145
- quantity: result.salesConfig.pricePerToken * BigInt(params.quantityToMint),
4146
- approveTo: result.salesConfig.address,
4147
- erc20: result.salesConfig.currency
4174
+ quantity: salesConfig.pricePerToken * BigInt(params.quantityToMint),
4175
+ approveTo: salesConfig.address,
4176
+ erc20: salesConfig.currency
4148
4177
  };
4149
4178
  }
4150
4179
 
@@ -4248,7 +4277,7 @@ async function mint({
4248
4277
 
4249
4278
  // src/create/1155-create-helper.ts
4250
4279
  var import_protocol_deployments10 = require("@zoralabs/protocol-deployments");
4251
- var import_viem9 = require("viem");
4280
+ var import_viem10 = require("viem");
4252
4281
 
4253
4282
  // src/create/contract-setup.ts
4254
4283
  var import_protocol_deployments7 = require("@zoralabs/protocol-deployments");
@@ -4259,36 +4288,6 @@ function new1155ContractVersion(chainId) {
4259
4288
  }
4260
4289
  return address.CONTRACT_1155_IMPL_VERSION;
4261
4290
  }
4262
- async function getContractInfoExistingContract({
4263
- publicClient,
4264
- contractAddress
4265
- }) {
4266
- let contractVersion;
4267
- try {
4268
- contractVersion = await publicClient.readContract({
4269
- abi: import_protocol_deployments7.zoraCreator1155ImplABI,
4270
- address: contractAddress,
4271
- functionName: "contractVersion"
4272
- });
4273
- } catch (e) {
4274
- throw new Error(`Contract does not exist at ${contractAddress}`);
4275
- }
4276
- const nextTokenId = await publicClient.readContract({
4277
- address: contractAddress,
4278
- abi: import_protocol_deployments7.zoraCreator1155ImplABI,
4279
- functionName: "nextTokenId"
4280
- });
4281
- const contractName = await publicClient.readContract({
4282
- address: contractAddress,
4283
- abi: import_protocol_deployments7.zoraCreator1155ImplABI,
4284
- functionName: "name"
4285
- });
4286
- return {
4287
- contractVersion,
4288
- contractName,
4289
- nextTokenId
4290
- };
4291
- }
4292
4291
  async function getDeterministicContractAddress({
4293
4292
  publicClient,
4294
4293
  account,
@@ -4310,6 +4309,17 @@ async function getDeterministicContractAddress({
4310
4309
  });
4311
4310
  return contractAddress;
4312
4311
  }
4312
+ async function getNewContractMintFee({
4313
+ publicClient,
4314
+ chainId
4315
+ }) {
4316
+ const implAddress = import_protocol_deployments7.contracts1155.addresses[chainId].CONTRACT_1155_IMPL;
4317
+ return await publicClient.readContract({
4318
+ abi: import_protocol_deployments7.zoraCreator1155ImplABI,
4319
+ address: implAddress,
4320
+ functionName: "mintFee"
4321
+ });
4322
+ }
4313
4323
 
4314
4324
  // src/create/token-setup.ts
4315
4325
  var import_protocol_deployments9 = require("@zoralabs/protocol-deployments");
@@ -5833,12 +5843,101 @@ var contractSupportsMintRewards = (contractVersion, contractStandard) => {
5833
5843
  }
5834
5844
  };
5835
5845
 
5846
+ // src/create/mint-from-create.ts
5847
+ var import_viem9 = require("viem");
5848
+ async function toSalesStrategyFromSubgraph({
5849
+ minter,
5850
+ salesConfig,
5851
+ getContractMintFee
5852
+ }) {
5853
+ if (salesConfig.type === "timed") {
5854
+ return {
5855
+ saleType: "timed",
5856
+ address: minter,
5857
+ // for now we hardcode this
5858
+ mintFeePerQuantity: (0, import_viem9.parseEther)("0.000111"),
5859
+ saleStart: salesConfig.saleStart.toString(),
5860
+ saleEnd: salesConfig.saleEnd.toString(),
5861
+ // the following are not needed for now but we wanna satisfy concrete
5862
+ erc20Z: import_viem9.zeroAddress,
5863
+ mintFee: 0n,
5864
+ pool: import_viem9.zeroAddress,
5865
+ secondaryActivated: false
5866
+ };
5867
+ }
5868
+ if (salesConfig.type === "erc20Mint") {
5869
+ return {
5870
+ saleType: "erc20",
5871
+ address: minter,
5872
+ mintFeePerQuantity: 0n,
5873
+ saleStart: salesConfig.saleStart.toString(),
5874
+ saleEnd: salesConfig.saleEnd.toString(),
5875
+ currency: salesConfig.currency,
5876
+ pricePerToken: salesConfig.pricePerToken,
5877
+ maxTokensPerAddress: salesConfig.maxTokensPerAddress
5878
+ };
5879
+ }
5880
+ const contractMintFee = await getContractMintFee();
5881
+ if (salesConfig.type === "fixedPrice") {
5882
+ return {
5883
+ saleType: "fixedPrice",
5884
+ address: minter,
5885
+ maxTokensPerAddress: salesConfig.maxTokensPerAddress,
5886
+ mintFeePerQuantity: contractMintFee,
5887
+ pricePerToken: salesConfig.pricePerToken,
5888
+ saleStart: salesConfig.saleStart.toString(),
5889
+ saleEnd: salesConfig.saleEnd.toString()
5890
+ };
5891
+ }
5892
+ return {
5893
+ saleType: "allowlist",
5894
+ address: minter,
5895
+ saleStart: salesConfig.saleStart.toString(),
5896
+ saleEnd: salesConfig.saleEnd.toString(),
5897
+ merkleRoot: salesConfig.presaleMerkleRoot,
5898
+ mintFeePerQuantity: contractMintFee
5899
+ };
5900
+ }
5901
+ function makeOnchainPrepareMintFromCreate({
5902
+ contractAddress,
5903
+ tokenId,
5904
+ result,
5905
+ minter,
5906
+ getContractMintFee,
5907
+ contractVersion
5908
+ }) {
5909
+ return async (params) => {
5910
+ const subgraphSalesConfig = await toSalesStrategyFromSubgraph({
5911
+ minter,
5912
+ getContractMintFee,
5913
+ salesConfig: result
5914
+ });
5915
+ return {
5916
+ parameters: makePrepareMint1155TokenParams({
5917
+ salesConfigAndTokenInfo: {
5918
+ salesConfig: subgraphSalesConfig,
5919
+ contractVersion
5920
+ },
5921
+ ...params,
5922
+ tokenContract: contractAddress,
5923
+ tokenId
5924
+ }),
5925
+ costs: parseMintCosts({
5926
+ allowListEntry: params.allowListEntry,
5927
+ quantityToMint: BigInt(params.quantityToMint),
5928
+ salesConfig: subgraphSalesConfig
5929
+ }),
5930
+ erc20Approval: getRequiredErc20Approvals(params, subgraphSalesConfig)
5931
+ };
5932
+ };
5933
+ }
5934
+
5836
5935
  // src/create/1155-create-helper.ts
5837
5936
  var ROYALTY_BPS_DEFAULT = 1e3;
5838
5937
  var getTokenIdFromCreateReceipt = (receipt) => {
5839
5938
  for (const data of receipt.logs) {
5840
5939
  try {
5841
- const decodedLog = (0, import_viem9.decodeEventLog)({
5940
+ const decodedLog = (0, import_viem10.decodeEventLog)({
5842
5941
  abi: import_protocol_deployments10.zoraCreator1155ImplABI,
5843
5942
  eventName: "SetupNewToken",
5844
5943
  ...data
@@ -5856,7 +5955,7 @@ var getTokenIdFromCreateReceipt = (receipt) => {
5856
5955
  var getContractAddressFromReceipt = (receipt) => {
5857
5956
  for (const data of receipt.logs) {
5858
5957
  try {
5859
- const decodedLog = (0, import_viem9.decodeEventLog)({
5958
+ const decodedLog = (0, import_viem10.decodeEventLog)({
5860
5959
  abi: import_protocol_deployments10.zoraCreator1155FactoryImplABI,
5861
5960
  eventName: "SetupNewContract",
5862
5961
  ...data
@@ -5915,10 +6014,12 @@ function makeCreateTokenCall({
5915
6014
  var Create1155Client = class {
5916
6015
  constructor({
5917
6016
  chainId,
5918
- publicClient
6017
+ publicClient,
6018
+ contractGetter
5919
6019
  }) {
5920
6020
  this.chainId = chainId;
5921
6021
  this.publicClient = publicClient;
6022
+ this.contractGetter = contractGetter;
5922
6023
  }
5923
6024
  async createNew1155(props) {
5924
6025
  return createNew1155ContractAndToken({
@@ -5939,7 +6040,8 @@ var Create1155Client = class {
5939
6040
  token,
5940
6041
  getAdditionalSetupActions,
5941
6042
  publicClient: this.publicClient,
5942
- chainId: this.chainId
6043
+ chainId: this.chainId,
6044
+ contractGetter: this.contractGetter
5943
6045
  });
5944
6046
  }
5945
6047
  };
@@ -5980,6 +6082,18 @@ async function createNew1155ContractAndToken({
5980
6082
  chainId,
5981
6083
  contract
5982
6084
  });
6085
+ const prepareMint = makeOnchainPrepareMintFromCreate({
6086
+ contractAddress,
6087
+ contractVersion,
6088
+ minter,
6089
+ result: newToken.salesConfig,
6090
+ tokenId: nextTokenId,
6091
+ // to get the contract wide mint fee, we get what it would be for a new contract
6092
+ getContractMintFee: async () => getNewContractMintFee({
6093
+ publicClient,
6094
+ chainId
6095
+ })
6096
+ });
5983
6097
  return {
5984
6098
  parameters: request,
5985
6099
  tokenSetupActions,
@@ -5987,7 +6101,8 @@ async function createNew1155ContractAndToken({
5987
6101
  newToken,
5988
6102
  contractAddress,
5989
6103
  contractVersion,
5990
- minter
6104
+ minter,
6105
+ prepareMint
5991
6106
  };
5992
6107
  }
5993
6108
  async function createNew1155Token({
@@ -5995,13 +6110,10 @@ async function createNew1155Token({
5995
6110
  account,
5996
6111
  getAdditionalSetupActions,
5997
6112
  token,
5998
- publicClient,
5999
- chainId
6113
+ chainId,
6114
+ contractGetter
6000
6115
  }) {
6001
- const { nextTokenId, contractVersion } = await getContractInfoExistingContract({
6002
- publicClient,
6003
- contractAddress
6004
- });
6116
+ const { nextTokenId, contractVersion, mintFee } = await contractGetter.getContractInfo({ contractAddress, retries: 5 });
6005
6117
  const {
6006
6118
  minter,
6007
6119
  newToken,
@@ -6019,13 +6131,22 @@ async function createNew1155Token({
6019
6131
  account,
6020
6132
  tokenSetupActions
6021
6133
  });
6134
+ const prepareMint = makeOnchainPrepareMintFromCreate({
6135
+ contractAddress,
6136
+ contractVersion,
6137
+ minter,
6138
+ result: newToken.salesConfig,
6139
+ tokenId: nextTokenId,
6140
+ getContractMintFee: async () => mintFee
6141
+ });
6022
6142
  return {
6023
6143
  parameters: request,
6024
6144
  tokenSetupActions,
6025
6145
  newTokenId: nextTokenId,
6026
6146
  newToken,
6027
6147
  contractVersion,
6028
- minter
6148
+ minter,
6149
+ prepareMint
6029
6150
  };
6030
6151
  }
6031
6152
  async function prepareSetupActions({
@@ -6114,7 +6235,7 @@ var sumBalances = (mintAccountBalances) => {
6114
6235
 
6115
6236
  // src/sparks/sparks-contracts.ts
6116
6237
  var import_protocol_deployments11 = require("@zoralabs/protocol-deployments");
6117
- var import_viem10 = require("viem");
6238
+ var import_viem11 = require("viem");
6118
6239
  var addressOrAccountAddress = (address) => typeof address === "string" ? address : address.address;
6119
6240
  var mintWithEthParams = ({
6120
6241
  tokenId,
@@ -6150,7 +6271,7 @@ var encodeCollectOnManager = ({
6150
6271
  minter,
6151
6272
  zoraCreator1155TokenId,
6152
6273
  mintArguments
6153
- }) => (0, import_viem10.encodeFunctionData)({
6274
+ }) => (0, import_viem11.encodeFunctionData)({
6154
6275
  abi: import_protocol_deployments11.zoraMintsManagerImplConfig.abi,
6155
6276
  functionName: "collect",
6156
6277
  args: [
@@ -6270,8 +6391,8 @@ var encodePremintOnManager = ({
6270
6391
  premintConfig,
6271
6392
  premintSignature,
6272
6393
  mintArguments,
6273
- signerContract = import_viem10.zeroAddress
6274
- }) => (0, import_viem10.encodeFunctionData)({
6394
+ signerContract = import_viem11.zeroAddress
6395
+ }) => (0, import_viem11.encodeFunctionData)({
6275
6396
  abi: import_protocol_deployments11.zoraMintsManagerImplConfig.abi,
6276
6397
  functionName: "collectPremintV2",
6277
6398
  args: [
@@ -6338,12 +6459,83 @@ function decodeCallFailedError(error) {
6338
6459
  if (error.data?.errorName !== "CallFailed")
6339
6460
  throw new Error("Not a CallFailed error");
6340
6461
  const internalErrorData = error.data?.args?.[0];
6341
- return (0, import_viem10.decodeErrorResult)({
6462
+ return (0, import_viem11.decodeErrorResult)({
6342
6463
  abi: import_protocol_deployments11.zoraMintsManagerImplABI,
6343
6464
  data: internalErrorData
6344
6465
  });
6345
6466
  }
6346
6467
 
6468
+ // src/create/subgraph-queries.ts
6469
+ function buildContractInfoQuery({
6470
+ contractAddress
6471
+ }) {
6472
+ return {
6473
+ query: `
6474
+ query ($contractAddress: Bytes!) {
6475
+ zoraCreateContract(id: $contractAddress) {
6476
+ contractVersion
6477
+ name
6478
+ mintFeePerQuantity
6479
+ tokens(first: 1, orderBy: tokenId, orderDirection: desc) {
6480
+ tokenId
6481
+ }
6482
+ }
6483
+ }
6484
+ `,
6485
+ variables: {
6486
+ contractAddress: contractAddress.toLowerCase()
6487
+ },
6488
+ parseResponseData: (responseData) => responseData.zoraCreateContract
6489
+ };
6490
+ }
6491
+
6492
+ // src/create/contract-getter.ts
6493
+ var SubgraphContractGetter = class {
6494
+ constructor(chainId, subgraphQuerier) {
6495
+ this.subgraphQuerier = subgraphQuerier || new SubgraphQuerier(httpClient);
6496
+ this.networkConfig = getApiNetworkConfigForChain(chainId);
6497
+ }
6498
+ async querySubgraphWithRetries({
6499
+ query,
6500
+ variables,
6501
+ parseResponseData
6502
+ }) {
6503
+ const responseData = await this.subgraphQuerier.query({
6504
+ subgraphUrl: this.networkConfig.subgraphUrl,
6505
+ query,
6506
+ variables
6507
+ });
6508
+ return parseResponseData(responseData);
6509
+ }
6510
+ async getContractInfo({
6511
+ contractAddress,
6512
+ retries: retries2 = 1
6513
+ }) {
6514
+ const tryFn = async () => {
6515
+ const responseData2 = await this.querySubgraphWithRetries(
6516
+ buildContractInfoQuery({ contractAddress })
6517
+ );
6518
+ if (!responseData2) {
6519
+ console.log("could not find contract");
6520
+ throw new Error("Cannot find contract");
6521
+ }
6522
+ return responseData2;
6523
+ };
6524
+ const responseData = await retriesGeneric({
6525
+ tryFn,
6526
+ maxTries: retries2,
6527
+ linearBackoffMS: 1e3
6528
+ });
6529
+ const nextTokenId = responseData.tokens.length === 0 ? 1n : BigInt(responseData.tokens[0].tokenId) + 1n;
6530
+ return {
6531
+ name: responseData.name,
6532
+ contractVersion: responseData.contractVersion,
6533
+ mintFee: BigInt(responseData.mintFeePerQuantity),
6534
+ nextTokenId
6535
+ };
6536
+ }
6537
+ };
6538
+
6347
6539
  // src/sdk.ts
6348
6540
  function createCreatorClient(clientConfig) {
6349
6541
  const premintClient = new PremintClient({
@@ -6353,7 +6545,8 @@ function createCreatorClient(clientConfig) {
6353
6545
  });
6354
6546
  const create1155CreatorClient = new Create1155Client({
6355
6547
  chainId: clientConfig.chainId,
6356
- publicClient: clientConfig.publicClient
6548
+ publicClient: clientConfig.publicClient,
6549
+ contractGetter: clientConfig.contractGetter || new SubgraphContractGetter(clientConfig.chainId)
6357
6550
  });
6358
6551
  return {
6359
6552
  createPremint: (p) => premintClient.createPremint(p),