@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
@@ -1,32 +1,23 @@
1
+ import { Address, Account, SimulateContractParameters } from "viem";
2
+ import { IPublicClient } from "src/types";
1
3
  import {
2
- Address,
3
- Chain,
4
- encodeAbiParameters,
5
- parseAbiParameters,
6
- zeroAddress,
7
- Account,
8
- SimulateContractParameters,
9
- } from "viem";
10
- import {
11
- erc20MinterABI,
12
- erc20MinterAddress,
13
- zoraCreator1155ImplABI,
14
- zoraCreatorFixedPriceSaleStrategyAddress,
15
- } from "@zoralabs/protocol-deployments";
16
- import { IHttpClient } from "src/apis/http-api-base";
17
- import { zora721Abi } from "src/constants";
18
- import { GenericTokenIdTypes } from "src/types";
19
- import {
20
- MintAPIClient,
21
- SalesConfigAndTokenInfo,
4
+ GetMintParameters,
5
+ IOnchainMintGetter,
6
+ MintCosts,
7
+ PrepareMintReturn,
22
8
  SaleType,
23
- } from "./mint-api-client";
9
+ } from "./types";
24
10
  import {
25
- makeSimulateContractParamaters,
26
- ClientConfig,
27
- setupClient,
28
- PublicClient,
29
- } from "src/utils";
11
+ MakeMintParametersArguments,
12
+ Make1155MintArguments,
13
+ Make721MintArguments,
14
+ GetMintCostsParameters,
15
+ is1155Mint,
16
+ } from "./types";
17
+ import { IPremintGetter } from "src/premint/premint-api-client";
18
+
19
+ import { makeOnchainMintCall } from "./mint-transactions";
20
+ import { getMint, getMintCosts, getMintsOfContract } from "./mint-queries";
30
21
 
31
22
  class MintError extends Error {}
32
23
  class MintInactiveError extends Error {}
@@ -36,235 +27,140 @@ export const Errors = {
36
27
  MintInactiveError,
37
28
  };
38
29
 
39
- type MintArguments = {
40
- /** Quantity of tokens to mint */
41
- quantityToMint: number;
42
- /** Comment to add to the mint */
43
- mintComment?: string;
44
- /** Optional address to receive the mint referral reward */
45
- mintReferral?: Address;
46
- /** Address to receive the minted tokens */
47
- mintToAddress: Address;
48
- saleType?: SaleType;
49
- };
50
-
51
- type MintTokenParams = {
52
- /** Account to execute the mint */
53
- minterAccount: Address | Account;
54
- /** Contract address of token to mint */
55
- tokenAddress: Address;
56
- /** Id of token to mint */
57
- tokenId?: GenericTokenIdTypes;
58
- /** Mint settings */
59
- mintArguments: MintArguments;
60
- };
61
-
62
- class MintClient {
63
- readonly apiClient: MintAPIClient;
64
- readonly publicClient: PublicClient;
65
-
66
- constructor(
67
- chain: Chain,
68
- publicClient: PublicClient,
69
- httpClient: IHttpClient,
70
- ) {
71
- this.apiClient = new MintAPIClient(chain.id, httpClient);
30
+ export class MintClient {
31
+ private readonly publicClient: IPublicClient;
32
+ private readonly mintGetter: IOnchainMintGetter;
33
+ private readonly premintGetter: IPremintGetter;
34
+
35
+ constructor({
36
+ publicClient,
37
+ premintGetter,
38
+ mintGetter,
39
+ }: {
40
+ publicClient: IPublicClient;
41
+ premintGetter: IPremintGetter;
42
+ mintGetter: IOnchainMintGetter;
43
+ }) {
72
44
  this.publicClient = publicClient;
45
+ this.mintGetter = mintGetter;
46
+ this.premintGetter = premintGetter;
73
47
  }
74
48
 
75
49
  /**
76
50
  * Returns the parameters needed to prepare a transaction mint a token.
51
+ * Works with premint, onchain 1155, and onchain 721.
77
52
  *
78
- * @param parameters - Parameters for collecting the token {@link MintTokenParams}
79
- * @returns Parameters for simulating/executing the mint transaction
53
+ * @param parameters - Parameters for collecting the token {@link MakeMintParametersArguments}
54
+ * @returns Parameters for simulating/executing the mint transaction, any necessary erc20 approval, and costs to mint
80
55
  */
81
- async makePrepareMintTokenParams(parameters: MintTokenParams) {
82
- return makePrepareMintTokenParams({
83
- ...parameters,
84
- apiClient: this.apiClient,
56
+ async mint(
57
+ parameters: MakeMintParametersArguments,
58
+ ): Promise<PrepareMintReturn> {
59
+ return mint({
60
+ parameters,
85
61
  publicClient: this.publicClient,
62
+ mintGetter: this.mintGetter,
63
+ premintGetter: this.premintGetter,
86
64
  });
87
65
  }
88
- }
89
-
90
- /**
91
- * Creates a new MintClient.
92
- * @param param0.chain The chain to use for the mint client.
93
- * @param param0.publicClient Optional viem public client
94
- * @param param0.httpClient Optional http client to override post, get, and retry methods
95
- * @returns
96
- */
97
- export function createMintClient(clientConfig: ClientConfig) {
98
- const { chain, publicClient, httpClient } = setupClient(clientConfig);
99
- return new MintClient(chain, publicClient, httpClient);
100
- }
101
66
 
102
- export type TMintClient = ReturnType<typeof createMintClient>;
103
-
104
- async function makePrepareMintTokenParams({
105
- publicClient,
106
- apiClient,
107
- tokenId,
108
- tokenAddress,
109
- mintArguments,
110
- ...rest
111
- }: {
112
- publicClient: PublicClient;
113
- minterAccount: Address | Account;
114
- tokenId?: GenericTokenIdTypes;
115
- tokenAddress: Address;
116
- mintArguments: MintArguments;
117
- apiClient: MintAPIClient;
118
- }): Promise<
119
- SimulateContractParameters<any, any, any, any, any, Account | Address>
120
- > {
121
- const salesConfigAndTokenInfo = await apiClient.getSalesConfigAndTokenInfo({
122
- tokenId,
123
- tokenAddress,
124
- saleType: mintArguments.saleType,
125
- });
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
+ }
126
81
 
127
- if (tokenId === undefined) {
128
- return makePrepareMint721TokenParams({
129
- salesConfigAndTokenInfo,
130
- tokenAddress,
131
- mintArguments,
132
- ...rest,
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,
133
97
  });
134
98
  }
135
99
 
136
- return makePrepareMint1155TokenParams({
137
- salesConfigAndTokenInfo,
138
- tokenAddress,
139
- tokenId,
140
- mintArguments,
141
- ...rest,
142
- });
100
+ /**
101
+ * Gets the costs to mint the quantity of tokens specified for a mint.
102
+ * @param parameters - Parameters for the mint {@link GetMintCostsParameters}
103
+ * @returns Costs to mint the quantity of tokens specified
104
+ */
105
+ async getMintCosts(parameters: GetMintCostsParameters): Promise<MintCosts> {
106
+ return getMintCosts({
107
+ params: parameters,
108
+ mintGetter: this.mintGetter,
109
+ premintGetter: this.premintGetter,
110
+ publicClient: this.publicClient,
111
+ });
112
+ }
143
113
  }
144
114
 
145
- async function makePrepareMint721TokenParams({
146
- tokenAddress,
147
- salesConfigAndTokenInfo,
148
- minterAccount,
149
- mintArguments,
115
+ async function mint({
116
+ parameters,
117
+ publicClient,
118
+ mintGetter,
119
+ premintGetter,
150
120
  }: {
151
- tokenAddress: Address;
152
- salesConfigAndTokenInfo: SalesConfigAndTokenInfo;
153
- minterAccount: Address | Account;
154
- mintArguments: MintArguments;
155
- }) {
156
- const mintValue = getMintCosts({
157
- salesConfigAndTokenInfo,
158
- quantityToMint: BigInt(mintArguments.quantityToMint),
159
- }).totalCost;
160
-
161
- return makeSimulateContractParamaters({
162
- abi: zora721Abi,
163
- address: tokenAddress,
164
- account: minterAccount,
165
- functionName: "mintWithRewards",
166
- value: mintValue,
167
- args: [
168
- mintArguments.mintToAddress,
169
- BigInt(mintArguments.quantityToMint),
170
- mintArguments.mintComment || "",
171
- mintArguments.mintReferral || zeroAddress,
172
- ],
121
+ parameters: MakeMintParametersArguments;
122
+ publicClient: IPublicClient;
123
+ mintGetter: IOnchainMintGetter;
124
+ premintGetter: IPremintGetter;
125
+ }): Promise<PrepareMintReturn> {
126
+ const { prepareMint } = await getMint({
127
+ params: parameters,
128
+ mintGetter,
129
+ premintGetter,
130
+ publicClient,
173
131
  });
174
- }
175
-
176
- export type MintCosts = {
177
- mintFee: bigint;
178
- tokenPurchaseCost: bigint;
179
- totalCost: bigint;
180
- };
181
132
 
182
- export function getMintCosts({
183
- salesConfigAndTokenInfo,
184
- quantityToMint,
185
- }: {
186
- salesConfigAndTokenInfo: SalesConfigAndTokenInfo;
187
- quantityToMint: bigint;
188
- }): MintCosts {
189
- const mintFeeForTokens =
190
- salesConfigAndTokenInfo.mintFeePerQuantity * quantityToMint;
191
- const tokenPurchaseCost =
192
- BigInt(salesConfigAndTokenInfo.salesConfig.pricePerToken) * quantityToMint;
193
-
194
- return {
195
- mintFee: mintFeeForTokens,
196
- tokenPurchaseCost,
197
- totalCost: mintFeeForTokens + tokenPurchaseCost,
198
- };
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,
140
+ });
199
141
  }
200
142
 
201
- async function makePrepareMint1155TokenParams({
202
- tokenId,
203
- salesConfigAndTokenInfo,
204
- minterAccount,
205
- tokenAddress,
206
- mintArguments,
207
- }: {
208
- salesConfigAndTokenInfo: SalesConfigAndTokenInfo;
209
- tokenId: GenericTokenIdTypes;
210
- minterAccount: Address | Account;
211
- tokenAddress: Address;
212
- mintArguments: MintArguments;
213
- }) {
214
- const mintQuantity = BigInt(mintArguments.quantityToMint);
215
-
216
- const mintValue = getMintCosts({
217
- salesConfigAndTokenInfo,
218
- quantityToMint: mintQuantity,
219
- }).totalCost;
220
-
221
- switch (salesConfigAndTokenInfo.salesConfig.saleType) {
222
- case "fixedPrice":
223
- const fixedPriceArgs = mintArguments;
224
-
225
- return makeSimulateContractParamaters({
226
- abi: zoraCreator1155ImplABI,
227
- functionName: "mintWithRewards",
228
- account: minterAccount,
229
- value: mintValue,
230
- address: tokenAddress,
231
- /* args: minter, tokenId, quantity, minterArguments, mintReferral */
232
- args: [
233
- (salesConfigAndTokenInfo.salesConfig.address ||
234
- zoraCreatorFixedPriceSaleStrategyAddress[999999999]) as Address,
235
- BigInt(tokenId),
236
- mintQuantity,
237
- encodeAbiParameters(parseAbiParameters("address, string"), [
238
- fixedPriceArgs.mintToAddress,
239
- fixedPriceArgs.mintComment || "",
240
- ]),
241
- fixedPriceArgs.mintReferral || zeroAddress,
242
- ],
243
- });
244
-
245
- case "erc20":
246
- const erc20Args = mintArguments;
247
-
248
- return makeSimulateContractParamaters({
249
- abi: erc20MinterABI,
250
- functionName: "mint",
251
- account: minterAccount,
252
- address: (salesConfigAndTokenInfo?.salesConfig.address ||
253
- erc20MinterAddress[999999999]) as Address,
254
- /* args: mintTo, quantity, tokenAddress, tokenId, totalValue, currency, mintReferral, comment */
255
- args: [
256
- mintArguments.mintToAddress,
257
- mintQuantity,
258
- tokenAddress,
259
- BigInt(tokenId),
260
- salesConfigAndTokenInfo.salesConfig.pricePerToken,
261
- salesConfigAndTokenInfo.salesConfig.currency,
262
- erc20Args.mintReferral || zeroAddress,
263
- erc20Args.mintComment || "",
264
- ],
265
- });
143
+ export async function collectOnchain({
144
+ chainId,
145
+ mintGetter,
146
+ ...parameters
147
+ }: (Make1155MintArguments | Make721MintArguments) & {
148
+ mintGetter: IOnchainMintGetter;
149
+ chainId: number;
150
+ }): Promise<
151
+ SimulateContractParameters<any, any, any, any, any, Account | Address>
152
+ > {
153
+ const { tokenContract: tokenContract, preferredSaleType: saleType } =
154
+ parameters;
155
+ const tokenId = is1155Mint(parameters) ? parameters.tokenId : undefined;
156
+ const salesConfigAndTokenInfo = await mintGetter.getMintable({
157
+ tokenId,
158
+ tokenAddress: tokenContract,
159
+ preferredSaleType: saleType,
160
+ });
266
161
 
267
- default:
268
- throw new MintError("Unsupported sale type");
269
- }
162
+ return makeOnchainMintCall({
163
+ mintParams: parameters,
164
+ token: salesConfigAndTokenInfo,
165
+ });
270
166
  }