@zoralabs/protocol-sdk 0.5.17 → 0.7.0

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 (84) hide show
  1. package/.turbo/turbo-build.log +6 -6
  2. package/CHANGELOG.md +19 -0
  3. package/README.md +1 -416
  4. package/dist/constants.d.ts +0 -1
  5. package/dist/constants.d.ts.map +1 -1
  6. package/dist/create/1155-create-helper.d.ts +16 -55
  7. package/dist/create/1155-create-helper.d.ts.map +1 -1
  8. package/dist/create/contract-setup.d.ts +14 -0
  9. package/dist/create/contract-setup.d.ts.map +1 -0
  10. package/dist/create/token-setup.d.ts +27 -0
  11. package/dist/create/token-setup.d.ts.map +1 -0
  12. package/dist/create/types.d.ts +45 -0
  13. package/dist/create/types.d.ts.map +1 -0
  14. package/dist/index.cjs +1745 -891
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.ts +3 -1
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +1698 -858
  19. package/dist/index.js.map +1 -1
  20. package/dist/mint/mint-client.d.ts +48 -51
  21. package/dist/mint/mint-client.d.ts.map +1 -1
  22. package/dist/mint/mint-queries.d.ts +38 -0
  23. package/dist/mint/mint-queries.d.ts.map +1 -0
  24. package/dist/mint/mint-transactions.d.ts +20 -0
  25. package/dist/mint/mint-transactions.d.ts.map +1 -0
  26. package/dist/mint/subgraph-mint-getter.d.ts +24 -0
  27. package/dist/mint/subgraph-mint-getter.d.ts.map +1 -0
  28. package/dist/mint/subgraph-queries.d.ts +55 -0
  29. package/dist/mint/subgraph-queries.d.ts.map +1 -0
  30. package/dist/mint/types.d.ts +177 -0
  31. package/dist/mint/types.d.ts.map +1 -0
  32. package/dist/mint/utils.d.ts +2 -0
  33. package/dist/mint/utils.d.ts.map +1 -0
  34. package/dist/mints/mints-contracts.d.ts +27 -4518
  35. package/dist/mints/mints-contracts.d.ts.map +1 -1
  36. package/dist/premint/contract-types.d.ts +4 -4
  37. package/dist/premint/contract-types.d.ts.map +1 -1
  38. package/dist/premint/conversions.d.ts +15 -16
  39. package/dist/premint/conversions.d.ts.map +1 -1
  40. package/dist/premint/premint-api-client.d.ts +32 -16
  41. package/dist/premint/premint-api-client.d.ts.map +1 -1
  42. package/dist/premint/premint-client.d.ts +76 -50
  43. package/dist/premint/premint-client.d.ts.map +1 -1
  44. package/dist/premint/preminter.d.ts +20 -8
  45. package/dist/premint/preminter.d.ts.map +1 -1
  46. package/dist/sdk.d.ts +45 -0
  47. package/dist/sdk.d.ts.map +1 -0
  48. package/dist/types.d.ts +3 -0
  49. package/dist/types.d.ts.map +1 -1
  50. package/dist/utils.d.ts +24 -6869
  51. package/dist/utils.d.ts.map +1 -1
  52. package/package.json +3 -2
  53. package/src/constants.ts +0 -36
  54. package/src/create/1155-create-helper.test.ts +236 -55
  55. package/src/create/1155-create-helper.ts +141 -309
  56. package/src/create/contract-setup.ts +88 -0
  57. package/src/create/token-setup.ts +379 -0
  58. package/src/create/types.ts +57 -0
  59. package/src/index.ts +5 -1
  60. package/src/mint/mint-client.test.ts +116 -78
  61. package/src/mint/mint-client.ts +130 -234
  62. package/src/mint/mint-queries.ts +320 -0
  63. package/src/mint/mint-transactions.ts +253 -0
  64. package/src/mint/subgraph-mint-getter.ts +247 -0
  65. package/src/mint/subgraph-queries.ts +170 -0
  66. package/src/mint/types.ts +239 -0
  67. package/src/mint/utils.ts +14 -0
  68. package/src/mints/mints-contracts.test.ts +1 -1
  69. package/src/mints/mints-contracts.ts +4 -4
  70. package/src/premint/contract-types.ts +4 -4
  71. package/src/premint/conversions.ts +38 -4
  72. package/src/premint/premint-api-client.ts +92 -48
  73. package/src/premint/premint-client.test.ts +100 -84
  74. package/src/premint/premint-client.ts +186 -185
  75. package/src/premint/preminter.test.ts +4 -5
  76. package/src/premint/preminter.ts +63 -14
  77. package/src/sdk.ts +101 -0
  78. package/src/types.ts +18 -0
  79. package/src/utils.ts +54 -46
  80. package/test-integration/setup-test-contracts.ts +96 -0
  81. package/dist/mint/mint-api-client.d.ts +0 -35
  82. package/dist/mint/mint-api-client.d.ts.map +0 -1
  83. package/src/mint/mint-api-client.ts +0 -177
  84. package/test-integration/premint-client.test.ts +0 -148
@@ -29,13 +29,15 @@ import {
29
29
  PremintConfigWithVersion,
30
30
  TokenCreationConfig,
31
31
  } from "@zoralabs/protocol-deployments";
32
- import { MintCosts } from "src/mint/mint-client";
33
32
  import { PublicClient } from "src/utils";
34
33
  import {
35
34
  ContractCreationConfigAndAddress,
36
35
  ContractCreationConfigOrAddress,
37
36
  ContractCreationConfigWithOptionalAdditionalAdmins,
38
37
  } from "./contract-types";
38
+ import { IPremintGetter } from "./premint-api-client";
39
+ import { isPremintConfigV1, isPremintConfigV2 } from "./conversions";
40
+ import { MintCosts } from "src/mint/types";
39
41
 
40
42
  export const getPremintExecutorAddress = () =>
41
43
  zoraCreator1155PremintExecutorImplAddress[999] as Address;
@@ -271,8 +273,8 @@ export const supportsPremintVersion = async ({
271
273
 
272
274
  export async function getPremintCollectionAddress({
273
275
  publicClient,
274
- collection,
275
- collectionAddress,
276
+ contract: collection,
277
+ contractAddress: collectionAddress,
276
278
  }: {
277
279
  publicClient: PublicClient;
278
280
  } & ContractCreationConfigOrAddress): Promise<Address> {
@@ -355,25 +357,72 @@ export async function getPremintMintFee({
355
357
  }
356
358
  }
357
359
 
360
+ type GetMintCostsParams = {
361
+ tokenContract: Address;
362
+ tokenPrice: bigint;
363
+ quantityToMint: bigint | number;
364
+ publicClient: PublicClient;
365
+ };
366
+
358
367
  export async function getPremintMintCosts({
359
368
  publicClient,
360
369
  tokenContract,
361
370
  tokenPrice,
362
371
  quantityToMint,
363
- }: {
364
- tokenContract: Address;
365
- tokenPrice: bigint;
366
- quantityToMint: bigint;
367
- publicClient: PublicClient;
368
- }): Promise<MintCosts> {
372
+ }: GetMintCostsParams): Promise<MintCosts> {
369
373
  const mintFee = await getPremintMintFee({ tokenContract, publicClient });
370
374
 
375
+ const quantityToMintBigInt = BigInt(quantityToMint);
376
+
371
377
  return {
372
- mintFee: mintFee * quantityToMint,
373
- tokenPurchaseCost: tokenPrice * quantityToMint,
374
- totalCost: (mintFee + tokenPrice) * quantityToMint,
378
+ mintFee: mintFee * quantityToMintBigInt,
379
+ totalPurchaseCost: tokenPrice * quantityToMintBigInt,
380
+ totalCostEth: (mintFee + tokenPrice) * quantityToMintBigInt,
375
381
  };
376
382
  }
383
+ export async function getPremintPricePerToken({
384
+ collection,
385
+ uid,
386
+ premintGetter,
387
+ }: {
388
+ collection: Address;
389
+ uid: number;
390
+ premintGetter: IPremintGetter;
391
+ }) {
392
+ const { premint: premintConfigWithVersion } = await premintGetter.get({
393
+ collectionAddress: collection,
394
+ uid,
395
+ });
396
+
397
+ if (
398
+ isPremintConfigV1(premintConfigWithVersion) ||
399
+ isPremintConfigV2(premintConfigWithVersion)
400
+ ) {
401
+ return premintConfigWithVersion.premintConfig.tokenConfig.pricePerToken;
402
+ }
403
+
404
+ throw new Error("Premint version not supported to get price");
405
+ }
406
+
407
+ export async function getPremintMintCostsWithUnknownTokenPrice({
408
+ premintGetter,
409
+ uid,
410
+ ...rest
411
+ }: Omit<GetMintCostsParams, "tokenPrice"> & {
412
+ premintGetter: IPremintGetter;
413
+ uid: number;
414
+ }) {
415
+ const pricePerToken = await getPremintPricePerToken({
416
+ uid,
417
+ premintGetter,
418
+ collection: rest.tokenContract,
419
+ });
420
+
421
+ return await getPremintMintCosts({
422
+ ...rest,
423
+ tokenPrice: pricePerToken,
424
+ });
425
+ }
377
426
 
378
427
  export function makeMintRewardsRecipient({
379
428
  mintReferral = zeroAddress,
@@ -416,13 +465,13 @@ export const toContractCreationConfigOrAddress = ({
416
465
  }) => {
417
466
  if (typeof collection !== "undefined") {
418
467
  return {
419
- collection,
468
+ contract: collection,
420
469
  };
421
470
  }
422
471
 
423
472
  if (typeof collectionAddress !== "undefined") {
424
473
  return {
425
- collectionAddress,
474
+ contractAddress: collectionAddress,
426
475
  };
427
476
  }
428
477
 
package/src/sdk.ts ADDED
@@ -0,0 +1,101 @@
1
+ import {
2
+ PremintClient,
3
+ getDataFromPremintReceipt,
4
+ } from "./premint/premint-client";
5
+ import { Create1155Client } from "./create/1155-create-helper";
6
+ import { MintClient } from "./mint/mint-client";
7
+ import { ClientConfig } from "./utils";
8
+ import { IPremintAPI, PremintAPIClient } from "./premint/premint-api-client";
9
+ import { SubgraphMintGetter } from "./mint/subgraph-mint-getter";
10
+ import { IOnchainMintGetter } from "./mint/types";
11
+
12
+ export type CreatorClient = {
13
+ createPremint: PremintClient["createPremint"];
14
+ updatePremint: PremintClient["updatePremint"];
15
+ deletePremint: PremintClient["deletePremint"];
16
+ create1155: Create1155Client["createNew1155Token"];
17
+ };
18
+
19
+ export type CollectorClient = {
20
+ getPremint: PremintClient["getPremint"];
21
+ getCollectDataFromPremintReceipt: PremintClient["getDataFromPremintReceipt"];
22
+ mint: MintClient["mint"];
23
+ getMintCosts: MintClient["getMintCosts"];
24
+ getToken: MintClient["get"];
25
+ getTokensOfContract: MintClient["getOfContract"];
26
+ };
27
+
28
+ export type CreatorClientConfig = ClientConfig & {
29
+ /** API for submitting and getting premints. Defaults to the Zora Premint API */
30
+ premintApi?: IPremintAPI;
31
+ };
32
+
33
+ /**
34
+ * Builds the sdk for creating/managing 1155 contracts and tokens
35
+ *
36
+ * @param clientConfig - Configuration for the client {@link CreatorClientConfig}
37
+ * @returns CreatorClient {@link CreatorClient}
38
+ * */
39
+ export function createCreatorClient(
40
+ clientConfig: CreatorClientConfig,
41
+ ): CreatorClient {
42
+ const premintClient = new PremintClient({
43
+ chainId: clientConfig.chainId,
44
+ publicClient: clientConfig.publicClient,
45
+ premintApi:
46
+ clientConfig.premintApi || new PremintAPIClient(clientConfig.chainId),
47
+ });
48
+
49
+ const create1155CreatorClient = new Create1155Client({
50
+ chainId: clientConfig.chainId,
51
+ publicClient: clientConfig.publicClient,
52
+ });
53
+
54
+ return {
55
+ createPremint: (p) => premintClient.createPremint(p),
56
+ updatePremint: (p) => premintClient.updatePremint(p),
57
+ deletePremint: (p) => premintClient.deletePremint(p),
58
+ create1155: (p) => create1155CreatorClient.createNew1155Token(p),
59
+ };
60
+ }
61
+
62
+ export type CollectorClientConfig = ClientConfig & {
63
+ /** API for getting premints. Defaults to the Zora Premint API */
64
+ premintGetter?: IPremintAPI;
65
+ /** API for getting onchain mints. Defaults to the Zora Creator Subgraph */
66
+ mintGetter?: IOnchainMintGetter;
67
+ };
68
+
69
+ /**
70
+ * Builds the sdk for collecting Premints, 1155, and 721 tokens.
71
+ *
72
+ * @param clientConfig - Configuration for the client {@link CollectorClientConfig}
73
+ * @returns CollectorClient {@link CollectorClient}
74
+ */
75
+ export function createCollectorClient(
76
+ params: CollectorClientConfig,
77
+ ): CollectorClient {
78
+ const premintGetterToUse =
79
+ params.premintGetter || new PremintAPIClient(params.chainId);
80
+ const mintGetterToUse =
81
+ params.mintGetter || new SubgraphMintGetter(params.chainId);
82
+ const mintClient = new MintClient({
83
+ publicClient: params.publicClient,
84
+ premintGetter: premintGetterToUse,
85
+ mintGetter: mintGetterToUse,
86
+ });
87
+
88
+ return {
89
+ getPremint: (p) =>
90
+ premintGetterToUse.get({
91
+ collectionAddress: p.address,
92
+ uid: p.uid,
93
+ }),
94
+ getCollectDataFromPremintReceipt: (p) =>
95
+ getDataFromPremintReceipt(p, params.chainId),
96
+ getToken: (p) => mintClient.get(p),
97
+ getTokensOfContract: (p) => mintClient.getOfContract(p),
98
+ mint: (p) => mintClient.mint(p),
99
+ getMintCosts: (p) => mintClient.getMintCosts(p),
100
+ };
101
+ }
package/src/types.ts CHANGED
@@ -1 +1,19 @@
1
+ import {
2
+ Account,
3
+ Address,
4
+ PublicClient,
5
+ SimulateContractParameters,
6
+ } from "viem";
7
+
1
8
  export type GenericTokenIdTypes = number | bigint | string;
9
+
10
+ export type IPublicClient = Pick<PublicClient, "readContract">;
11
+
12
+ export type SimulateContractParametersWithAccount = SimulateContractParameters<
13
+ any,
14
+ any,
15
+ any,
16
+ any,
17
+ any,
18
+ Account | Address
19
+ >;
package/src/utils.ts CHANGED
@@ -1,55 +1,63 @@
1
- import {
2
- Abi,
3
- Account,
4
- Address,
5
- Chain,
6
- ContractFunctionArgs,
7
- ContractFunctionName,
8
- PublicClient as BasePublicClient,
9
- SimulateContractParameters,
10
- Transport,
11
- createPublicClient,
12
- http,
13
- } from "viem";
14
- import {
15
- IHttpClient,
16
- httpClient as defaultHttpClient,
17
- } from "./apis/http-api-base";
18
-
19
- export const makeSimulateContractParamaters = <
20
- const abi extends Abi | readonly unknown[],
21
- functionName extends ContractFunctionName<abi, "nonpayable" | "payable">,
22
- args extends ContractFunctionArgs<
23
- abi,
24
- "nonpayable" | "payable",
25
- functionName
26
- >,
27
- chainOverride extends Chain | undefined,
28
- accountOverride extends Account | Address | undefined = undefined,
29
- >(
30
- args: SimulateContractParameters<
31
- abi,
32
- functionName,
33
- args,
34
- Chain,
35
- chainOverride,
36
- accountOverride
37
- >,
1
+ import { Account, Address, PublicClient as BasePublicClient } from "viem";
2
+ import { IHttpClient } from "./apis/http-api-base";
3
+ import { SimulateContractParametersWithAccount } from "./types";
4
+
5
+ export const makeContractParameters = (
6
+ args: SimulateContractParametersWithAccount,
38
7
  ) => args;
39
8
 
40
- export type PublicClient = BasePublicClient<Transport, Chain>;
9
+ export type PublicClient = Pick<BasePublicClient, "readContract">;
41
10
 
42
11
  export type ClientConfig = {
43
- chain: Chain;
44
- publicClient?: PublicClient;
45
- httpClient?: IHttpClient;
12
+ /** The chain that the client is to run on. */
13
+ chainId: number;
14
+ /** Optional public client for the chain. If not provide, it is created. */
15
+ publicClient: PublicClient;
46
16
  };
47
17
 
48
- export function setupClient({ chain, httpClient, publicClient }: ClientConfig) {
18
+ export function setupClient({ chainId, publicClient }: ClientConfig) {
49
19
  return {
50
- chain,
51
- httpClient: httpClient || defaultHttpClient,
52
- publicClient:
53
- publicClient || createPublicClient({ chain, transport: http() }),
20
+ chainId,
21
+ publicClient,
54
22
  };
55
23
  }
24
+
25
+ export function mintRecipientOrAccount({
26
+ mintRecipient,
27
+ minterAccount,
28
+ }: {
29
+ mintRecipient?: Address;
30
+ minterAccount: Address | Account;
31
+ }): Address {
32
+ return (
33
+ mintRecipient ||
34
+ (typeof minterAccount === "string" ? minterAccount : minterAccount.address)
35
+ );
36
+ }
37
+
38
+ export type Concrete<Type> = {
39
+ [Property in keyof Type]-?: Type[Property];
40
+ };
41
+
42
+ export async function querySubgraphWithRetries({
43
+ httpClient,
44
+ subgraphUrl,
45
+ query,
46
+ variables,
47
+ }: {
48
+ httpClient: IHttpClient;
49
+ subgraphUrl: string;
50
+ query: string;
51
+ variables: any;
52
+ }) {
53
+ const { retries, post } = httpClient;
54
+
55
+ const result = await retries(async () => {
56
+ return await post<any>(subgraphUrl, {
57
+ query,
58
+ variables,
59
+ });
60
+ });
61
+
62
+ return result?.data;
63
+ }
@@ -0,0 +1,96 @@
1
+ import {
2
+ createCollectorClient,
3
+ createCreatorClient,
4
+ getPremintCollectionAddress,
5
+ } from "src";
6
+ import {
7
+ LocalAccount,
8
+ createPublicClient,
9
+ createWalletClient,
10
+ http,
11
+ parseEther,
12
+ } from "viem";
13
+ import { privateKeyToAccount } from "viem/accounts";
14
+ import { zoraSepolia } from "viem/chains";
15
+
16
+ const publicClient = createPublicClient({
17
+ chain: zoraSepolia,
18
+ transport: http(),
19
+ });
20
+
21
+ const walletClient = createWalletClient({
22
+ chain: zoraSepolia,
23
+ transport: http(),
24
+ });
25
+
26
+ const creatorClient = createCreatorClient({
27
+ chainId: zoraSepolia.id,
28
+ publicClient,
29
+ });
30
+
31
+ const collectorClient = createCollectorClient({
32
+ chainId: zoraSepolia.id,
33
+ publicClient,
34
+ });
35
+
36
+ export const createPremintsOnContract = async ({
37
+ creatorAccount,
38
+ }: {
39
+ creatorAccount: LocalAccount;
40
+ }) => {
41
+ const collectionAddress = await getPremintCollectionAddress({
42
+ publicClient,
43
+ contract: {
44
+ contractAdmin: creatorAccount.address,
45
+ contractName: "testContract",
46
+ contractURI:
47
+ "ipfs://bafkreiainxen4b4wz4ubylvbhons6rembxdet4a262nf2lziclqvv7au3e",
48
+ },
49
+ });
50
+
51
+ console.log({ collectionAddress });
52
+
53
+ const { typedDataDefinition, submit } = await creatorClient.createPremint({
54
+ contract: {
55
+ contractAdmin: creatorAccount.address,
56
+ contractName: "testContract",
57
+ contractURI:
58
+ "ipfs://bafkreiainxen4b4wz4ubylvbhons6rembxdet4a262nf2lziclqvv7au3e",
59
+ },
60
+ token: {
61
+ payoutRecipient: creatorAccount.address,
62
+ tokenURI:
63
+ "ipfs://bafkreice23maski3x52tsfqgxstx3kbiifnt5jotg3a5ynvve53c4soi2u",
64
+ pricePerToken: parseEther("0.001"),
65
+ },
66
+ });
67
+
68
+ const signature = await walletClient.signTypedData({
69
+ account: creatorAccount,
70
+ ...typedDataDefinition,
71
+ });
72
+
73
+ await submit({
74
+ signature,
75
+ });
76
+
77
+ const mints = await collectorClient.getTokensOfContract({
78
+ tokenContract: collectionAddress,
79
+ });
80
+
81
+ console.log({ mints: mints.map((x) => x.mintable) });
82
+ };
83
+
84
+ const setupTestContracts = async () => {
85
+ const creatorAccount = privateKeyToAccount(
86
+ // random private key created by cast
87
+ "0x0d32fcabfe28c779974a77dc635163f062be2bc0b10eea62994235617b44092f",
88
+ );
89
+
90
+ // create 2 premints on a contract
91
+ await createPremintsOnContract({
92
+ creatorAccount,
93
+ });
94
+ };
95
+
96
+ setupTestContracts();
@@ -1,35 +0,0 @@
1
- import { Address } from "viem";
2
- import { IHttpClient } from "../apis/http-api-base";
3
- import { NetworkConfig } from "src/apis/chain-constants";
4
- import { GenericTokenIdTypes } from "src/types";
5
- export type SaleType = "fixedPrice" | "erc20";
6
- type SaleStrategy<T extends SaleType> = {
7
- saleType: T;
8
- address: Address;
9
- pricePerToken: bigint;
10
- saleEnd: string;
11
- saleStart: string;
12
- maxTokensPerAddress: bigint;
13
- };
14
- type FixedPriceSaleStrategy = SaleStrategy<"fixedPrice">;
15
- type ERC20SaleStrategy = SaleStrategy<"erc20"> & {
16
- currency: Address;
17
- };
18
- type SaleStrategies = FixedPriceSaleStrategy | ERC20SaleStrategy;
19
- export type SalesConfigAndTokenInfo = {
20
- salesConfig: SaleStrategies;
21
- mintFeePerQuantity: bigint;
22
- };
23
- export declare const getApiNetworkConfigForChain: (chainId: number) => NetworkConfig;
24
- export declare class MintAPIClient {
25
- httpClient: IHttpClient;
26
- networkConfig: NetworkConfig;
27
- constructor(chainId: number, httpClient?: IHttpClient);
28
- getSalesConfigAndTokenInfo({ tokenAddress, tokenId, saleType, }: {
29
- tokenAddress: Address;
30
- tokenId?: GenericTokenIdTypes;
31
- saleType?: SaleType;
32
- }): Promise<SalesConfigAndTokenInfo>;
33
- }
34
- export {};
35
- //# sourceMappingURL=mint-api-client.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mint-api-client.d.ts","sourceRoot":"","sources":["../../src/mint/mint-api-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAEL,WAAW,EACZ,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAwB,MAAM,0BAA0B,CAAC;AAC/E,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAGhD,MAAM,MAAM,QAAQ,GAAG,YAAY,GAAG,OAAO,CAAC;AAiC9C,KAAK,YAAY,CAAC,CAAC,SAAS,QAAQ,IAAI;IACtC,QAAQ,EAAE,CAAC,CAAC;IACZ,OAAO,EAAE,OAAO,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,mBAAmB,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF,KAAK,sBAAsB,GAAG,YAAY,CAAC,YAAY,CAAC,CAAC;AAEzD,KAAK,iBAAiB,GAAG,YAAY,CAAC,OAAO,CAAC,GAAG;IAC/C,QAAQ,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,KAAK,cAAc,GAAG,sBAAsB,GAAG,iBAAiB,CAAC;AAEjE,MAAM,MAAM,uBAAuB,GAAG;IACpC,WAAW,EAAE,cAAc,CAAC;IAC5B,kBAAkB,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,eAAO,MAAM,2BAA2B,YAAa,MAAM,KAAG,aAK7D,CAAC;AAEF,qBAAa,aAAa;IACxB,UAAU,EAAE,WAAW,CAAC;IACxB,aAAa,EAAE,aAAa,CAAC;gBAEjB,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,WAAW;IAK/C,0BAA0B,CAAC,EAC/B,YAAY,EACZ,OAAO,EACP,QAAQ,GACT,EAAE;QACD,YAAY,EAAE,OAAO,CAAC;QACtB,OAAO,CAAC,EAAE,mBAAmB,CAAC;QAC9B,QAAQ,CAAC,EAAE,QAAQ,CAAC;KACrB,GAAG,OAAO,CAAC,uBAAuB,CAAC;CAwFrC"}
@@ -1,177 +0,0 @@
1
- import { Address } from "viem";
2
- import {
3
- httpClient as defaultHttpClient,
4
- IHttpClient,
5
- } from "../apis/http-api-base";
6
- import { NetworkConfig, networkConfigByChain } from "src/apis/chain-constants";
7
- import { GenericTokenIdTypes } from "src/types";
8
- import { NFT_SALE_QUERY } from "src/constants";
9
-
10
- export type SaleType = "fixedPrice" | "erc20";
11
-
12
- type FixedPriceSaleStrategyResult = {
13
- address: Address;
14
- pricePerToken: string;
15
- saleEnd: string;
16
- saleStart: string;
17
- maxTokensPerAddress: string;
18
- };
19
-
20
- type ERC20SaleStrategyResult = FixedPriceSaleStrategyResult & {
21
- currency: Address;
22
- };
23
-
24
- type SalesStrategyResult =
25
- | {
26
- type: "FIXED_PRICE";
27
- fixedPrice: FixedPriceSaleStrategyResult;
28
- }
29
- | {
30
- type: "ERC_20_MINTER";
31
- erc20Minter: ERC20SaleStrategyResult;
32
- };
33
-
34
- type TokenQueryResult = {
35
- tokenId?: string;
36
- salesStrategies?: SalesStrategyResult[];
37
- contract: {
38
- mintFeePerQuantity: "string";
39
- salesStrategies: SalesStrategyResult[];
40
- };
41
- };
42
-
43
- type SaleStrategy<T extends SaleType> = {
44
- saleType: T;
45
- address: Address;
46
- pricePerToken: bigint;
47
- saleEnd: string;
48
- saleStart: string;
49
- maxTokensPerAddress: bigint;
50
- };
51
-
52
- type FixedPriceSaleStrategy = SaleStrategy<"fixedPrice">;
53
-
54
- type ERC20SaleStrategy = SaleStrategy<"erc20"> & {
55
- currency: Address;
56
- };
57
-
58
- type SaleStrategies = FixedPriceSaleStrategy | ERC20SaleStrategy;
59
-
60
- export type SalesConfigAndTokenInfo = {
61
- salesConfig: SaleStrategies;
62
- mintFeePerQuantity: bigint;
63
- };
64
-
65
- export const getApiNetworkConfigForChain = (chainId: number): NetworkConfig => {
66
- if (!networkConfigByChain[chainId]) {
67
- throw new Error(`chain id ${chainId} network not configured `);
68
- }
69
- return networkConfigByChain[chainId]!;
70
- };
71
-
72
- export class MintAPIClient {
73
- httpClient: IHttpClient;
74
- networkConfig: NetworkConfig;
75
-
76
- constructor(chainId: number, httpClient?: IHttpClient) {
77
- this.httpClient = httpClient || defaultHttpClient;
78
- this.networkConfig = getApiNetworkConfigForChain(chainId);
79
- }
80
-
81
- async getSalesConfigAndTokenInfo({
82
- tokenAddress,
83
- tokenId,
84
- saleType,
85
- }: {
86
- tokenAddress: Address;
87
- tokenId?: GenericTokenIdTypes;
88
- saleType?: SaleType;
89
- }): Promise<SalesConfigAndTokenInfo> {
90
- const { retries, post } = this.httpClient;
91
- return retries(async () => {
92
- const response = await post<any>(this.networkConfig.subgraphUrl, {
93
- query: NFT_SALE_QUERY,
94
- variables: {
95
- id:
96
- tokenId !== undefined
97
- ? // Generic Token ID types all stringify down to the base numeric equivalent.
98
- `${tokenAddress.toLowerCase()}-${tokenId}`
99
- : `${tokenAddress.toLowerCase()}-0`,
100
- },
101
- });
102
-
103
- const token = response.data?.zoraCreateToken as TokenQueryResult;
104
-
105
- if (!token) {
106
- throw new Error("Cannot find a token to mint");
107
- }
108
-
109
- const allStrategies =
110
- (typeof tokenId !== "undefined"
111
- ? token.salesStrategies
112
- : token.contract.salesStrategies) || [];
113
-
114
- const saleStrategies = allStrategies.sort((a, b) =>
115
- BigInt(
116
- a.type === "ERC_20_MINTER"
117
- ? a.erc20Minter.saleEnd
118
- : a.fixedPrice.saleEnd,
119
- ) >
120
- BigInt(
121
- b.type === "FIXED_PRICE"
122
- ? b.fixedPrice.saleEnd
123
- : b.erc20Minter.saleEnd,
124
- )
125
- ? 1
126
- : -1,
127
- );
128
-
129
- let targetStrategy: SalesStrategyResult | undefined;
130
-
131
- if (!saleType) {
132
- targetStrategy = saleStrategies[0];
133
- if (!targetStrategy) {
134
- throw new Error("Cannot find sale strategy");
135
- }
136
- } else {
137
- const mappedSaleType =
138
- saleType === "erc20" ? "ERC_20_MINTER" : "FIXED_PRICE";
139
- targetStrategy = saleStrategies.find(
140
- (strategy: SalesStrategyResult) => strategy.type === mappedSaleType,
141
- );
142
- if (!targetStrategy) {
143
- throw new Error(`Cannot find sale strategy for ${mappedSaleType}`);
144
- }
145
- }
146
-
147
- if (targetStrategy.type === "FIXED_PRICE") {
148
- return {
149
- salesConfig: {
150
- saleType: "fixedPrice",
151
- ...targetStrategy.fixedPrice,
152
- maxTokensPerAddress: BigInt(
153
- targetStrategy.fixedPrice.maxTokensPerAddress,
154
- ),
155
- pricePerToken: BigInt(targetStrategy.fixedPrice.pricePerToken),
156
- },
157
- mintFeePerQuantity: BigInt(token.contract.mintFeePerQuantity),
158
- };
159
- }
160
- if (targetStrategy.type === "ERC_20_MINTER") {
161
- return {
162
- salesConfig: {
163
- saleType: "erc20",
164
- ...targetStrategy.erc20Minter,
165
- maxTokensPerAddress: BigInt(
166
- targetStrategy.erc20Minter.maxTokensPerAddress,
167
- ),
168
- pricePerToken: BigInt(targetStrategy.erc20Minter.pricePerToken),
169
- },
170
- mintFeePerQuantity: BigInt(token.contract.mintFeePerQuantity),
171
- };
172
- }
173
-
174
- throw new Error("Invalid saleType");
175
- });
176
- }
177
- }