@zoralabs/protocol-sdk 0.6.0 → 0.7.1

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 (86) hide show
  1. package/.turbo/turbo-build.log +6 -6
  2. package/CHANGELOG.md +12 -0
  3. package/dist/constants.d.ts +0 -1
  4. package/dist/constants.d.ts.map +1 -1
  5. package/dist/index.cjs +2023 -431
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.ts +1 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +2001 -412
  10. package/dist/index.js.map +1 -1
  11. package/dist/ipfs/arweave.d.ts +3 -0
  12. package/dist/ipfs/arweave.d.ts.map +1 -0
  13. package/dist/ipfs/gateway.d.ts +4 -0
  14. package/dist/ipfs/gateway.d.ts.map +1 -0
  15. package/dist/ipfs/index.d.ts +4 -0
  16. package/dist/ipfs/index.d.ts.map +1 -0
  17. package/dist/ipfs/ipfs.d.ts +5 -0
  18. package/dist/ipfs/ipfs.d.ts.map +1 -0
  19. package/dist/ipfs/mimeTypes.d.ts +25 -0
  20. package/dist/ipfs/mimeTypes.d.ts.map +1 -0
  21. package/dist/ipfs/text-metadata.d.ts +5 -0
  22. package/dist/ipfs/text-metadata.d.ts.map +1 -0
  23. package/dist/ipfs/token-metadata.d.ts +14 -0
  24. package/dist/ipfs/token-metadata.d.ts.map +1 -0
  25. package/dist/ipfs/types.d.ts +49 -0
  26. package/dist/ipfs/types.d.ts.map +1 -0
  27. package/dist/mint/mint-client.d.ts +28 -4071
  28. package/dist/mint/mint-client.d.ts.map +1 -1
  29. package/dist/mint/mint-queries.d.ts +38 -0
  30. package/dist/mint/mint-queries.d.ts.map +1 -0
  31. package/dist/mint/mint-transactions.d.ts +20 -0
  32. package/dist/mint/mint-transactions.d.ts.map +1 -0
  33. package/dist/mint/subgraph-mint-getter.d.ts +14 -7
  34. package/dist/mint/subgraph-mint-getter.d.ts.map +1 -1
  35. package/dist/mint/subgraph-queries.d.ts +55 -0
  36. package/dist/mint/subgraph-queries.d.ts.map +1 -0
  37. package/dist/mint/types.d.ts +117 -19
  38. package/dist/mint/types.d.ts.map +1 -1
  39. package/dist/mint/utils.d.ts +2 -0
  40. package/dist/mint/utils.d.ts.map +1 -0
  41. package/dist/mints/mints-contracts.d.ts +3 -4494
  42. package/dist/mints/mints-contracts.d.ts.map +1 -1
  43. package/dist/premint/conversions.d.ts +12 -15
  44. package/dist/premint/conversions.d.ts.map +1 -1
  45. package/dist/premint/premint-api-client.d.ts +14 -11
  46. package/dist/premint/premint-api-client.d.ts.map +1 -1
  47. package/dist/premint/premint-client.d.ts +17 -6
  48. package/dist/premint/premint-client.d.ts.map +1 -1
  49. package/dist/premint/preminter.d.ts +1 -1
  50. package/dist/premint/preminter.d.ts.map +1 -1
  51. package/dist/sdk.d.ts +4 -2
  52. package/dist/sdk.d.ts.map +1 -1
  53. package/dist/types.d.ts +3 -0
  54. package/dist/types.d.ts.map +1 -1
  55. package/dist/utils.d.ts +10 -2
  56. package/dist/utils.d.ts.map +1 -1
  57. package/package.json +2 -1
  58. package/src/constants.ts +0 -36
  59. package/src/create/1155-create-helper.test.ts +9 -7
  60. package/src/index.ts +2 -0
  61. package/src/ipfs/arweave.ts +5 -0
  62. package/src/ipfs/gateway.ts +48 -0
  63. package/src/ipfs/index.ts +7 -0
  64. package/src/ipfs/ipfs.ts +82 -0
  65. package/src/ipfs/mimeTypes.ts +141 -0
  66. package/src/ipfs/text-metadata.ts +128 -0
  67. package/src/ipfs/token-metadata.ts +99 -0
  68. package/src/ipfs/types.ts +54 -0
  69. package/src/mint/mint-client.test.ts +96 -47
  70. package/src/mint/mint-client.ts +75 -343
  71. package/src/mint/mint-queries.ts +320 -0
  72. package/src/mint/mint-transactions.ts +253 -0
  73. package/src/mint/subgraph-mint-getter.ts +216 -123
  74. package/src/mint/subgraph-queries.ts +170 -0
  75. package/src/mint/types.ts +140 -23
  76. package/src/mint/utils.ts +14 -0
  77. package/src/premint/conversions.ts +26 -2
  78. package/src/premint/premint-api-client.ts +48 -16
  79. package/src/premint/premint-client.test.ts +29 -23
  80. package/src/premint/premint-client.ts +73 -37
  81. package/src/premint/preminter.ts +2 -3
  82. package/src/sdk.ts +7 -4
  83. package/src/types.ts +18 -0
  84. package/src/utils.ts +29 -28
  85. package/test-integration/setup-test-contracts.ts +96 -0
  86. package/test-integration/premint-client.test.ts +0 -148
@@ -1,43 +1,23 @@
1
+ import { Address, Account, SimulateContractParameters } from "viem";
2
+ import { IPublicClient } from "src/types";
1
3
  import {
2
- Address,
3
- encodeAbiParameters,
4
- parseAbiParameters,
5
- zeroAddress,
6
- Account,
7
- SimulateContractParameters,
8
- erc20Abi,
9
- } from "viem";
10
- import {
11
- erc20MinterABI,
12
- erc20MinterAddress,
13
- zoraCreator1155ImplABI,
14
- zoraCreatorFixedPriceSaleStrategyAddress,
15
- } from "@zoralabs/protocol-deployments";
16
- import { zora721Abi } from "src/constants";
17
- import { GenericTokenIdTypes } from "src/types";
18
- import {
19
- makeContractParameters,
20
- PublicClient,
21
- mintRecipientOrAccount,
22
- } from "src/utils";
23
- import {
24
- IMintGetter,
25
- SalesConfigAndTokenInfo,
26
- isErc20SaleStrategy,
4
+ GetMintParameters,
5
+ IOnchainMintGetter,
6
+ MintCosts,
7
+ PrepareMintReturn,
8
+ SaleType,
27
9
  } from "./types";
28
10
  import {
29
11
  MakeMintParametersArguments,
30
- MakeMintParametersArgumentsBase,
31
12
  Make1155MintArguments,
32
- MakePremintMintParametersArguments,
33
13
  Make721MintArguments,
34
14
  GetMintCostsParameters,
35
- isOnChainMint,
36
15
  is1155Mint,
37
16
  } from "./types";
38
- import { collectPremint } from "src/premint/premint-client";
39
17
  import { IPremintGetter } from "src/premint/premint-api-client";
40
- import { getPremintMintCostsWithUnknownTokenPrice } from "src/premint/preminter";
18
+
19
+ import { makeOnchainMintCall } from "./mint-transactions";
20
+ import { getMint, getMintCosts, getMintsOfContract } from "./mint-queries";
41
21
 
42
22
  class MintError extends Error {}
43
23
  class MintInactiveError extends Error {}
@@ -48,23 +28,19 @@ export const Errors = {
48
28
  };
49
29
 
50
30
  export class MintClient {
51
- private readonly chainId: number;
52
- private readonly publicClient: PublicClient;
53
- private readonly mintGetter: IMintGetter;
31
+ private readonly publicClient: IPublicClient;
32
+ private readonly mintGetter: IOnchainMintGetter;
54
33
  private readonly premintGetter: IPremintGetter;
55
34
 
56
35
  constructor({
57
- chainId,
58
36
  publicClient,
59
37
  premintGetter,
60
38
  mintGetter,
61
39
  }: {
62
- chainId: number;
63
- publicClient: PublicClient;
40
+ publicClient: IPublicClient;
64
41
  premintGetter: IPremintGetter;
65
- mintGetter: IMintGetter;
42
+ mintGetter: IOnchainMintGetter;
66
43
  }) {
67
- this.chainId = chainId;
68
44
  this.publicClient = publicClient;
69
45
  this.mintGetter = mintGetter;
70
46
  this.premintGetter = premintGetter;
@@ -75,18 +51,52 @@ export class MintClient {
75
51
  * Works with premint, onchain 1155, and onchain 721.
76
52
  *
77
53
  * @param parameters - Parameters for collecting the token {@link MakeMintParametersArguments}
78
- * @returns Parameters for simulating/executing the mint transaction
54
+ * @returns Parameters for simulating/executing the mint transaction, any necessary erc20 approval, and costs to mint
79
55
  */
80
- async mint(parameters: MakeMintParametersArguments) {
56
+ async mint(
57
+ parameters: MakeMintParametersArguments,
58
+ ): Promise<PrepareMintReturn> {
81
59
  return mint({
82
60
  parameters,
83
- chainId: this.chainId,
84
61
  publicClient: this.publicClient,
85
62
  mintGetter: this.mintGetter,
86
63
  premintGetter: this.premintGetter,
87
64
  });
88
65
  }
89
66
 
67
+ /**
68
+ * Gets an 1155, 721, or premint, and returns both information about it, and a function
69
+ * that can be used to build a mint transaction for a quantity of items to mint.
70
+ * @param parameters - Token to get {@link GetMintParameters}
71
+ * @Returns Information about the mint and a function to build a mint transaction {@link MintableReturn}
72
+ */
73
+ async get(parameters: GetMintParameters) {
74
+ return getMint({
75
+ params: parameters,
76
+ mintGetter: this.mintGetter,
77
+ premintGetter: this.premintGetter,
78
+ publicClient: this.publicClient,
79
+ });
80
+ }
81
+
82
+ /**
83
+ * Gets onchain and premint tokens of an 1155 contract. For each token returns both information about it, and a function
84
+ * that can be used to build a mint transaction for a quantity of items to mint.
85
+ * @param parameters - Contract address to get tokens for {@link GetMintsOfContractParameters}
86
+ * @Returns Array of tokens, each containing information about the token and a function to build a mint transaction.
87
+ */
88
+ async getOfContract(params: {
89
+ tokenContract: Address;
90
+ preferredSaleType?: SaleType;
91
+ }) {
92
+ return getMintsOfContract({
93
+ params,
94
+ mintGetter: this.mintGetter,
95
+ premintGetter: this.premintGetter,
96
+ publicClient: this.publicClient,
97
+ });
98
+ }
99
+
90
100
  /**
91
101
  * Gets the costs to mint the quantity of tokens specified for a mint.
92
102
  * @param parameters - Parameters for the mint {@link GetMintCostsParameters}
@@ -94,7 +104,7 @@ export class MintClient {
94
104
  */
95
105
  async getMintCosts(parameters: GetMintCostsParameters): Promise<MintCosts> {
96
106
  return getMintCosts({
97
- ...parameters,
107
+ params: parameters,
98
108
  mintGetter: this.mintGetter,
99
109
  premintGetter: this.premintGetter,
100
110
  publicClient: this.publicClient,
@@ -102,106 +112,32 @@ export class MintClient {
102
112
  }
103
113
  }
104
114
 
105
- function isPremintCollect(
106
- parameters: MakeMintParametersArguments,
107
- ): parameters is MakePremintMintParametersArguments {
108
- return parameters.mintType === "premint";
109
- }
110
-
111
- function is721Collect(
112
- parameters: MakeMintParametersArguments,
113
- ): parameters is Make721MintArguments {
114
- return parameters.mintType === "721";
115
- }
116
-
117
115
  async function mint({
118
116
  parameters,
119
- chainId,
120
117
  publicClient,
121
118
  mintGetter,
122
119
  premintGetter,
123
120
  }: {
124
121
  parameters: MakeMintParametersArguments;
125
- publicClient: PublicClient;
126
- mintGetter: IMintGetter;
122
+ publicClient: IPublicClient;
123
+ mintGetter: IOnchainMintGetter;
127
124
  premintGetter: IPremintGetter;
128
- chainId: number;
129
- }) {
130
- if (isPremintCollect(parameters)) {
131
- return {
132
- parameters: await collectPremint({
133
- ...parameters,
134
- premintGetter: premintGetter,
135
- publicClient,
136
- }),
137
- };
138
- }
139
-
140
- return {
141
- parameters: await collectOnchain({
142
- ...parameters,
143
- mintGetter: mintGetter,
144
- chainId,
145
- }),
146
- };
147
- }
148
-
149
- export function requestErc20ApprovalForMint({
150
- account,
151
- tokenAddress,
152
- quantityErc20,
153
- erc20MinterAddress,
154
- }: {
155
- account: Account | Address;
156
- tokenAddress: Address;
157
- quantityErc20: bigint;
158
- erc20MinterAddress: Address;
159
- }) {
160
- return makeContractParameters({
161
- abi: erc20Abi,
162
- address: tokenAddress,
163
- account,
164
- functionName: "approve",
165
- args: [erc20MinterAddress, quantityErc20],
125
+ }): Promise<PrepareMintReturn> {
126
+ const { prepareMint } = await getMint({
127
+ params: parameters,
128
+ mintGetter,
129
+ premintGetter,
130
+ publicClient,
166
131
  });
167
- }
168
132
 
169
- export async function getNecessaryErc20Approval({
170
- chainId,
171
- account,
172
- tokenAddress,
173
- quantityErc20,
174
- publicClient,
175
- }: {
176
- chainId: number;
177
- account: Account | Address;
178
- tokenAddress: Address;
179
- quantityErc20: bigint;
180
- publicClient: PublicClient;
181
- }) {
182
- const destination =
183
- erc20MinterAddress[chainId as keyof typeof erc20MinterAddress];
184
- const allowance = await publicClient.readContract({
185
- abi: erc20Abi,
186
- address: tokenAddress,
187
- functionName: "allowance",
188
- account,
189
- args: [
190
- destination,
191
- erc20MinterAddress[chainId as keyof typeof erc20MinterAddress],
192
- ],
133
+ return prepareMint({
134
+ minterAccount: parameters.minterAccount,
135
+ quantityToMint: parameters.quantityToMint,
136
+ firstMinter: parameters.firstMinter,
137
+ mintComment: parameters.mintComment,
138
+ mintRecipient: parameters.mintRecipient,
139
+ mintReferral: parameters.mintReferral,
193
140
  });
194
-
195
- if (allowance < quantityErc20) {
196
- return requestErc20ApprovalForMint({
197
- erc20MinterAddress: destination,
198
- account,
199
- tokenAddress,
200
- quantityErc20: quantityErc20 - allowance,
201
- });
202
- }
203
-
204
- return undefined;
205
141
  }
206
142
 
207
143
  export async function collectOnchain({
@@ -209,226 +145,22 @@ export async function collectOnchain({
209
145
  mintGetter,
210
146
  ...parameters
211
147
  }: (Make1155MintArguments | Make721MintArguments) & {
212
- mintGetter: IMintGetter;
148
+ mintGetter: IOnchainMintGetter;
213
149
  chainId: number;
214
150
  }): Promise<
215
151
  SimulateContractParameters<any, any, any, any, any, Account | Address>
216
152
  > {
217
- const { tokenContract: tokenContract, saleType } = parameters;
153
+ const { tokenContract: tokenContract, preferredSaleType: saleType } =
154
+ parameters;
218
155
  const tokenId = is1155Mint(parameters) ? parameters.tokenId : undefined;
219
- const salesConfigAndTokenInfo = await mintGetter.getSalesConfigAndTokenInfo({
156
+ const salesConfigAndTokenInfo = await mintGetter.getMintable({
220
157
  tokenId,
221
158
  tokenAddress: tokenContract,
222
- saleType,
159
+ preferredSaleType: saleType,
223
160
  });
224
161
 
225
- if (is721Collect(parameters)) {
226
- return makePrepareMint721TokenParams({
227
- salesConfigAndTokenInfo,
228
- ...parameters,
229
- });
230
- }
231
-
232
- return makePrepareMint1155TokenParams({
233
- salesConfigAndTokenInfo,
234
- chainId,
235
- ...parameters,
236
- });
237
- }
238
-
239
- async function makePrepareMint721TokenParams({
240
- salesConfigAndTokenInfo,
241
- minterAccount,
242
- tokenContract,
243
- mintComment,
244
- mintReferral,
245
- mintRecipient,
246
- quantityToMint,
247
- }: {
248
- salesConfigAndTokenInfo: SalesConfigAndTokenInfo;
249
- } & Pick<
250
- MakeMintParametersArgumentsBase,
251
- | "minterAccount"
252
- | "tokenContract"
253
- | "mintComment"
254
- | "mintReferral"
255
- | "quantityToMint"
256
- | "mintRecipient"
257
- >) {
258
- const actualQuantityToMint = BigInt(quantityToMint || 1);
259
- const mintValue = parseMintCosts({
260
- salesConfigAndTokenInfo,
261
- quantityToMint: actualQuantityToMint,
262
- }).totalCostEth;
263
-
264
- return makeContractParameters({
265
- abi: zora721Abi,
266
- address: tokenContract,
267
- account: minterAccount,
268
- functionName: "mintWithRewards",
269
- value: mintValue,
270
- args: [
271
- mintRecipientOrAccount({ mintRecipient, minterAccount }),
272
- actualQuantityToMint,
273
- mintComment || "",
274
- mintReferral || zeroAddress,
275
- ],
276
- });
277
- }
278
-
279
- export type MintCosts = {
280
- /** The total of the mint fee, in eth */
281
- mintFee: bigint;
282
- /** If it is a paid or erc20 mint, the total price of the paid or erc20 mint in eth or erc20 value correspondingly. */
283
- totalPurchaseCost: bigint;
284
- /** If it is an erc20 mint, the erc20 address */
285
- totalPurchaseCostCurrency?: Address;
286
- /** The total cost in eth (mint fee + purchase cost) to mint */
287
- totalCostEth: bigint;
288
- };
289
-
290
- export function parseMintCosts({
291
- salesConfigAndTokenInfo,
292
- quantityToMint,
293
- }: {
294
- salesConfigAndTokenInfo: SalesConfigAndTokenInfo;
295
- quantityToMint: bigint;
296
- }): MintCosts {
297
- const mintFeeForTokens =
298
- salesConfigAndTokenInfo.mintFeePerQuantity * quantityToMint;
299
-
300
- const tokenPurchaseCost =
301
- BigInt(salesConfigAndTokenInfo.salesConfig.pricePerToken) * quantityToMint;
302
-
303
- const totalPurchaseCostCurrency = isErc20SaleStrategy(
304
- salesConfigAndTokenInfo.salesConfig,
305
- )
306
- ? salesConfigAndTokenInfo.salesConfig.currency
307
- : undefined;
308
-
309
- const totalPurchaseCostEth = totalPurchaseCostCurrency
310
- ? 0n
311
- : tokenPurchaseCost;
312
-
313
- return {
314
- mintFee: mintFeeForTokens,
315
- totalPurchaseCost: tokenPurchaseCost,
316
- totalPurchaseCostCurrency,
317
- totalCostEth: mintFeeForTokens + totalPurchaseCostEth,
318
- };
319
- }
320
-
321
- export async function getMintCosts(
322
- params: GetMintCostsParameters & {
323
- mintGetter: IMintGetter;
324
- premintGetter: IPremintGetter;
325
- publicClient: PublicClient;
326
- },
327
- ) {
328
- const { quantityMinted: quantityToMint, collection, publicClient } = params;
329
- if (isOnChainMint(params)) {
330
- const tokenId = is1155Mint(params) ? params.tokenId : undefined;
331
- const salesConfigAndTokenInfo =
332
- await params.mintGetter.getSalesConfigAndTokenInfo({
333
- tokenId,
334
- tokenAddress: collection,
335
- });
336
-
337
- return parseMintCosts({
338
- salesConfigAndTokenInfo,
339
- quantityToMint: BigInt(quantityToMint),
340
- });
341
- }
342
-
343
- return getPremintMintCostsWithUnknownTokenPrice({
344
- premintGetter: params.premintGetter,
345
- publicClient: publicClient,
346
- quantityToMint: BigInt(quantityToMint),
347
- uid: params.uid,
348
- tokenContract: collection,
162
+ return makeOnchainMintCall({
163
+ mintParams: parameters,
164
+ token: salesConfigAndTokenInfo,
349
165
  });
350
166
  }
351
-
352
- export function makePrepareMint1155TokenParams({
353
- tokenContract: tokenContract,
354
- tokenId,
355
- salesConfigAndTokenInfo,
356
- minterAccount,
357
- mintComment,
358
- mintReferral,
359
- mintRecipient,
360
- quantityToMint,
361
- chainId,
362
- }: {
363
- salesConfigAndTokenInfo: SalesConfigAndTokenInfo;
364
- chainId: number;
365
- tokenId: GenericTokenIdTypes;
366
- } & Pick<
367
- MakeMintParametersArgumentsBase,
368
- | "minterAccount"
369
- | "tokenContract"
370
- | "mintComment"
371
- | "mintReferral"
372
- | "quantityToMint"
373
- | "mintRecipient"
374
- >) {
375
- const mintQuantity = BigInt(quantityToMint || 1);
376
-
377
- const mintTo = mintRecipientOrAccount({ mintRecipient, minterAccount });
378
-
379
- const saleType = salesConfigAndTokenInfo.salesConfig.saleType;
380
-
381
- if (saleType === "fixedPrice") {
382
- const mintValue = parseMintCosts({
383
- salesConfigAndTokenInfo,
384
- quantityToMint: mintQuantity,
385
- }).totalCostEth;
386
-
387
- return makeContractParameters({
388
- abi: zoraCreator1155ImplABI,
389
- functionName: "mintWithRewards",
390
- account: minterAccount,
391
- value: mintValue,
392
- address: tokenContract,
393
- /* args: minter, tokenId, quantity, minterArguments, mintReferral */
394
- args: [
395
- (salesConfigAndTokenInfo.salesConfig.address ||
396
- zoraCreatorFixedPriceSaleStrategyAddress[
397
- chainId as keyof typeof zoraCreatorFixedPriceSaleStrategyAddress
398
- ]) as Address,
399
- BigInt(tokenId),
400
- mintQuantity,
401
- encodeAbiParameters(parseAbiParameters("address, string"), [
402
- mintTo,
403
- mintComment || "",
404
- ]),
405
- mintReferral || zeroAddress,
406
- ],
407
- });
408
- }
409
-
410
- if (saleType === "erc20") {
411
- return makeContractParameters({
412
- abi: erc20MinterABI,
413
- functionName: "mint",
414
- account: minterAccount,
415
- address: (salesConfigAndTokenInfo?.salesConfig.address ||
416
- erc20MinterAddress[
417
- chainId as keyof typeof erc20MinterAddress
418
- ]) as Address,
419
- /* args: mintTo, quantity, tokenAddress, tokenId, totalValue, currency, mintReferral, comment */
420
- args: [
421
- mintTo,
422
- mintQuantity,
423
- tokenContract,
424
- BigInt(tokenId),
425
- salesConfigAndTokenInfo.salesConfig.pricePerToken * mintQuantity,
426
- salesConfigAndTokenInfo.salesConfig.currency,
427
- mintReferral || zeroAddress,
428
- mintComment || "",
429
- ],
430
- });
431
- }
432
-
433
- throw new MintError("Unsupported sale type");
434
- }