@zoralabs/protocol-sdk 0.3.2 → 0.3.3

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.
@@ -7,9 +7,9 @@ $ tsup
7
7
  CLI Cleaning output folder
8
8
  CJS Build start
9
9
  ESM Build start
10
- CJS dist/index.cjs 35.89 KB
11
- CJS dist/index.cjs.map 65.52 KB
12
- CJS ⚡️ Build success in 25ms
13
- ESM dist/index.js 33.30 KB
14
- ESM dist/index.js.map 65.40 KB
15
- ESM ⚡️ Build success in 25ms
10
+ CJS dist/index.cjs 39.15 KB
11
+ CJS dist/index.cjs.map 73.49 KB
12
+ CJS ⚡️ Build success in 24ms
13
+ ESM dist/index.js 36.55 KB
14
+ ESM dist/index.js.map 73.48 KB
15
+ ESM ⚡️ Build success in 23ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # @zoralabs/protocol-sdk
2
2
 
3
+ ## 0.3.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 97f58b3: `MintAPIClient` is now a class, that takes a chain id and httpClient in the constructor, enabling the httpClient methods `fetch`, `post`, and `retries` to be overridden.
8
+
9
+ new methods on `MintAPIClient`:
10
+
11
+ `getMintableForToken` - takes a token id and token contract address and returns the mintable for it. Easier to use for fetching specific tokens than `getMintable`.
12
+
13
+ `MintClient` now takes the optional `PublicClient` in the constructor instead of in each function, and stores it or creates a default one if none is provided in the constructor. It also takes an optional `httpClient` param in the constructor, allowing the `fetch`, `post`, and `retries` methods to be overridden when using the api. It now internally creates the MintAPIClient.
14
+
15
+ `MintClient.makePrepareMintTokenParams` has the following changes:
16
+
17
+ - returns a `SimulateContractParams`, instead of an object containing it indexed by key
18
+ - no longer takes a `PublicClient` as an argument (it should be specified in the constructor instead)
19
+
20
+ new function `MintClient.getMintCosts` takes a mintable and quantity to mint and returns the mintFee, paidMintPrice, and totalCost.
21
+
22
+ - d02484e: premintClient can have http methods overridable via DI, and now takes publicClient and http overrides in `createPremintClient` function. it no longer takes `publicClient` as an argument in functions, and rather uses them from the constructor. `executePremint` has been renamed ot `makeMintParameters`
23
+
3
24
  ## 0.3.2
4
25
 
5
26
  ### Patch Changes
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Premint SDK
1
+ # Zora Protocol SDK
2
2
 
3
- Protocol SDK allows users to manage zora mints and collects.
3
+ Protocol SDK allows users to create tokens using the Zora Protocol, and mint them.
4
4
 
5
5
  ## Installing
6
6
 
@@ -11,28 +11,175 @@ Protocol SDK allows users to manage zora mints and collects.
11
11
 
12
12
  ### Creating a mint from an on-chain contract:
13
13
 
14
+ #### Using viem
15
+
14
16
  ```ts
15
- import { createMintClient } from "@zoralabs/protocol-sdk";
16
- import type { Address, WalletClient } from "viem";
17
+ import {createMintClient} from "@zoralabs/protocol-sdk";
18
+ import type {Address, PublicClient, WalletClient} from "viem";
17
19
 
18
- async function mintNFT(
19
- walletClient: WalletClient,
20
- address: Address,
21
- tokenId: bigint,
22
- ) {
23
- const mintAPI = createMintClient({ chain: walletClient.chain });
24
- await mintAPI.mintNFT({
25
- walletClient,
26
- address,
20
+ async function mintNFT({
21
+ walletClient,
22
+ publicClient,
23
+ tokenContract,
24
+ tokenId,
25
+ mintToAddress,
26
+ quantityToMint,
27
+ mintReferral,
28
+ }: {
29
+ // wallet client that will submit the transaction
30
+ walletClient: WalletClient;
31
+ // public client that will simulate the transaction
32
+ publicClient: PublicClient;
33
+ // address of the token contract
34
+ tokenContract: Address;
35
+ // id of the token to mint
36
+ tokenId: bigint;
37
+ // address that will receive the minted token
38
+ mintToAddress: Address;
39
+ // quantity of tokens to mint
40
+ quantityToMint: number;
41
+ // optional address that will receive a mint referral reward
42
+ mintReferral?: Address;
43
+ }) {
44
+ const mintClient = createMintClient({chain: walletClient.chain!});
45
+
46
+ // get mintable information about the token.
47
+ const mintable = await mintClient.getMintable({
48
+ tokenContract,
27
49
  tokenId,
50
+ });
51
+
52
+ // prepare the mint transaction, which can be simulated via an rpc with the public client.
53
+ const prepared = await mintClient.makePrepareMintTokenParams({
54
+ // token to mint
55
+ mintable,
28
56
  mintArguments: {
29
- quantityToMint: 23,
30
- mintComment: "Helo",
57
+ // address that will receive the token
58
+ mintToAddress,
59
+ // quantity of tokens to mint
60
+ quantityToMint,
61
+ // comment to include with the mint
62
+ mintComment: "My comment",
63
+ // optional address that will receive a mint referral reward
64
+ mintReferral,
31
65
  },
66
+ // account that is to invoke the mint transaction
67
+ minterAccount: walletClient.account!.address,
32
68
  });
69
+
70
+ // simulate the transaction and get any validation errors
71
+ const { request } = await publicClient.simulateContract(prepared);
72
+
73
+ // submit the transaction to the network
74
+ const txHash = await walletClient.writeContract(request);
75
+
76
+ // wait for the transaction to be complete
77
+ await publicClient.waitForTransactionReceipt({hash: txHash});
33
78
  }
34
79
  ```
35
80
 
81
+ #### Using wagmi
82
+
83
+ ```tsx
84
+ import {createMintClient, Mintable} from "@zoralabs/protocol-sdk";
85
+ import {useEffect, useMemo, useState} from "react";
86
+ import {BaseError, SimulateContractParameters, stringify} from "viem";
87
+ import {Address, useAccount, useContractWrite, useNetwork, usePrepareContractWrite, usePublicClient, useWaitForTransaction} from "wagmi";
88
+
89
+ // custom hook that gets the mintClient for the current chain
90
+ const useMintClient = () => {
91
+ const publicClient = usePublicClient();
92
+
93
+ const {chain} = useNetwork();
94
+
95
+ const mintClient = useMemo(() => chain && createMintClient({chain, publicClient}), [chain, publicClient]);
96
+
97
+ return mintClient;
98
+ };
99
+
100
+ export const Mint = ({tokenId, tokenContract}: {tokenId: string; tokenContract: Address}) => {
101
+ // call custom hook to get the mintClient
102
+ const mintClient = useMintClient();
103
+
104
+ // value will be set by the form
105
+ const [quantityToMint, setQuantityToMint] = useState<number>(1);
106
+
107
+ // fetched mintable info from the sdk
108
+ const [mintable, setMintable] = useState<Mintable>();
109
+
110
+ useEffect(() => {
111
+ // fetch the mintable token info
112
+ const fetchMintable = async () => {
113
+ if (mintClient) {
114
+ const mintable = await mintClient.getMintable({tokenId, tokenContract});
115
+ setMintable(mintable);
116
+ }
117
+ };
118
+
119
+ fetchMintable();
120
+ }, [mintClient, tokenId, tokenContract]);
121
+
122
+ // params for the prepare contract write hook
123
+ const [params, setParams] = useState<SimulateContractParameters>();
124
+
125
+ const {address} = useAccount();
126
+
127
+ useEffect(() => {
128
+ if (!mintable || !mintClient || !address) return;
129
+
130
+ const makeParams = async () => {
131
+ // make the params for the prepare contract write hook
132
+ const params = await mintClient.makePrepareMintTokenParams({
133
+ mintable,
134
+ minterAccount: address,
135
+ mintArguments: {
136
+ mintToAddress: address,
137
+ quantityToMint,
138
+ },
139
+ });
140
+ setParams(params);
141
+ };
142
+
143
+ makeParams();
144
+ }, [mintable, mintClient, address, quantityToMint]);
145
+
146
+ const {config} = usePrepareContractWrite(params);
147
+
148
+ const {write, data, error, isLoading, isError} = useContractWrite(config);
149
+ const {data: receipt, isLoading: isPending, isSuccess} = useWaitForTransaction({hash: data?.hash});
150
+
151
+ return (
152
+ <>
153
+ <h3>Mint a token</h3>
154
+ <form
155
+ onSubmit={(e) => {
156
+ e.preventDefault();
157
+ write?.();
158
+ }}
159
+ >
160
+ {/* input for quantity to mint: */}
161
+ <input placeholder="quantity to mint" onChange={(e) => setQuantityToMint(Number(e.target.value))} />
162
+ <button disabled={!write} type="submit">
163
+ Mint
164
+ </button>
165
+ </form>
166
+
167
+ {isLoading && <div>Check wallet...</div>}
168
+ {isPending && <div>Transaction pending...</div>}
169
+ {isSuccess && (
170
+ <>
171
+ <div>Transaction Hash: {data?.hash}</div>
172
+ <div>
173
+ Transaction Receipt: <pre>{stringify(receipt, null, 2)}</pre>
174
+ </div>
175
+ </>
176
+ )}
177
+ {isError && <div>{(error as BaseError)?.shortMessage}</div>}
178
+ </>
179
+ );
180
+ };
181
+ ```
182
+
36
183
  ### Creating an 1155 contract:
37
184
 
38
185
  If an object with {name, uri} is passed in to this helper, it uses the creatorAccount and those values to either 1) create or 2) mint to that existing contract.
@@ -67,53 +214,71 @@ export async function createContract({
67
214
  }
68
215
  ```
69
216
 
70
- ### Creating a premint:
217
+ ### Creating a token for free (gasless creation):
71
218
 
72
219
  ```ts
73
- import { PremintAPI } from "@zoralabs/protocol-sdk";
74
- import type { Address, WalletClient } from "viem";
220
+ import {createPremintClient} from "@zoralabs/protocol-sdk";
221
+ import type {Address, PublicClient, WalletClient} from "viem";
75
222
 
76
- async function makePremint(walletClient: WalletClient) {
77
- // Create premint
78
- const premint = await createPremintAPI(walletClient.chain).createPremint({
79
- // Extra step to check the signature on-chain before attempting to sign
223
+ async function createForFree({
224
+ walletClient,
225
+ creatorAccount,
226
+ }: {
227
+ // wallet client that will submit the transaction
228
+ walletClient: WalletClient;
229
+ // address of the token contract
230
+ creatorAccount: Address;
231
+ }) {
232
+ const premintClient = createPremintClient({ chain: walletClient.chain! });
233
+
234
+ // create and sign a free token creation.
235
+ const createdPremint = await premintClient.createPremint({
236
+ walletClient,
237
+ account: creatorAccount,
238
+ // if true, will validate that the creator is authorized to create premints on the contract.
80
239
  checkSignature: true,
81
- // Collection information that this premint NFT will exist in once minted.
240
+ // collection info of collection to create
82
241
  collection: {
83
- contractAdmin: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
242
+ contractAdmin: creatorAccount,
84
243
  contractName: "Testing Contract",
85
244
  contractURI:
86
245
  "ipfs://bafkreiainxen4b4wz4ubylvbhons6rembxdet4a262nf2lziclqvv7au3e",
87
246
  },
88
- // WalletClient doing the signature
89
- walletClient,
90
- // Token information, falls back to defaults set in DefaultMintArguments.
247
+ // token info of token to create
91
248
  token: {
92
249
  tokenURI:
93
250
  "ipfs://bafkreice23maski3x52tsfqgxstx3kbiifnt5jotg3a5ynvve53c4soi2u",
94
251
  },
95
252
  });
96
253
 
97
- console.log(`created ZORA premint, link: ${premint.url}`);
98
- return premint;
99
- }
254
+ const premintUid = createdPremint.uid;
255
+ const premintCollectionAddress = createdPremint.verifyingContract;
256
+
257
+ return {
258
+ // unique id of created premint, which can be used later to
259
+ // update or delete the premint
260
+ uid: premintUid,
261
+ tokenContract: premintCollectionAddress,
262
+ }
263
+ }
100
264
  ```
101
265
 
102
- ### Updating a premint:
266
+ ### Updating a token that was created for free (before it was brought onchain):
267
+
268
+ Before a token that was created for free is brought onchain, it can be updated by the original creator of that token, by having that creator sign a message indicating the update. This is useful for updating the tokenURI, other metadata, or token sale configuration (price, duration, limits, etc.):
103
269
 
104
270
  ```ts
105
- import { PremintAPI } from "@zoralabs/premint-sdk";
106
- import type { Address, WalletClient } from "viem";
271
+ import {createPremintClient} from "@zoralabs/protocol-sdk";
272
+ import type {Address, PublicClient, WalletClient} from "viem";
107
273
 
108
- async function updatePremint(walletClient: WalletClient) {
274
+ async function updateCreatedForFreeToken(walletClient: WalletClient, premintUid: number) {
109
275
  // Create premint API object passing in the current wallet chain (only zora and zora testnet are supported currently).
110
- const premintAPI = createPremintAPI(walletClient.chain);
276
+ const premintClient = createPremintClient({ chain: walletClient.chain! });
111
277
 
112
- // Create premint
113
- const premint = await premintAPI.updatePremint({
114
- // Extra step to check the signature on-chain before attempting to sign
278
+ // sign a message to update the created for free token, and store the update
279
+ await premintClient.updatePremint({
115
280
  collection: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
116
- uid: 23,
281
+ uid: premintUid,
117
282
  // WalletClient doing the signature
118
283
  walletClient,
119
284
  // Token information, falls back to defaults set in DefaultMintArguments.
@@ -122,73 +287,72 @@ async function updatePremint(walletClient: WalletClient) {
122
287
  "ipfs://bafkreice23maski3x52tsfqgxstx3kbiifnt5jotg3a5ynvve53c4soi2u",
123
288
  },
124
289
  });
125
-
126
- console.log(`updated ZORA premint, link: ${premint.url}`);
127
- return premint;
128
290
  }
129
291
  ```
130
292
 
131
- ### Deleting a premint:
293
+ ### Deleting a token that was created for free (before it was brought onchain):
294
+
295
+ Before a token that was created for free is brought onchain, it can be deleted by the original creator of that token, by having that creator sign a message indicating the deletion:
132
296
 
133
297
  ```ts
134
- import { PremintAPI } from "@zoralabs/premint-sdk";
135
- import type { Address, WalletClient } from "viem";
298
+ import {createPremintClient} from "@zoralabs/protocol-sdk";
299
+ import type {Address, PublicClient, WalletClient} from "viem";
136
300
 
137
- async function deletePremint(walletClient: WalletClient) {
138
- // Create premint API object passing in the current wallet chain (only zora and zora testnet are supported currently).
139
- const premintAPI = createPremintClient({ chain: walletClient.chain });
301
+ async function deleteCreatedForFreeToken(walletClient: WalletClient) {
302
+ const premintClient = createPremintClient({ chain: walletClient.chain! });
140
303
 
141
- // Create premint
142
- const premint = await premintAPI.deletePremint({
304
+ // sign a message to delete the premint, and store the deletion
305
+ await premintClient.deletePremint({
143
306
  // Extra step to check the signature on-chain before attempting to sign
144
307
  collection: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
145
308
  uid: 23,
146
309
  // WalletClient doing the signature
147
310
  walletClient,
148
311
  });
149
-
150
- console.log(`updated ZORA premint, link: ${premint.url}`);
151
- return premint;
152
312
  }
153
313
  ```
154
314
 
155
- ### Executing a premint:
315
+ ### Minting a token that was created for free (and bringing it onchain):
156
316
 
157
317
  ```ts
158
- import { PremintAPI } from "@zoralabs/premint-sdk";
159
- import type { Address, WalletClient } from "viem";
318
+ import {createPremintClient} from "@zoralabs/protocol-sdk";
319
+ import type {Address, PublicClient, WalletClient} from "viem";
160
320
 
161
- async function executePremint(
321
+ async function mintCreatedForFreeToken(
162
322
  walletClient: WalletClient,
163
- premintAddress: Address,
164
- premintUID: number,
323
+ publicClient: PublicClient,
324
+ minterAccount: Address,
165
325
  ) {
166
- const premintAPI = createPremintClient({ chain: walletClient.chain });
167
-
168
- return await premintAPI.executePremintWithWallet({
169
- data: premintAPI.getPremintData(premintAddress, premintUID),
170
- walletClient,
326
+ const premintClient = createPremintClient({ chain: walletClient.chain! });
327
+
328
+ const simulateContractParameters = await premintClient.makeMintParameters({
329
+ account: minterAccount,
330
+ data: await premintClient.getPremintData({
331
+ address: "0xf8dA7f53c283d898818af7FB9d98103F559bDac2",
332
+ uid: 3,
333
+ }),
171
334
  mintArguments: {
172
335
  quantityToMint: 1,
336
+ mintComment: "",
173
337
  },
174
338
  });
175
- }
176
- ```
177
339
 
178
- ### Deleting a premint:
340
+ // simulate the transaction and get any validation errors
341
+ const { request } = await publicClient.simulateContract(simulateContractParameters);
179
342
 
180
- ```ts
181
- import {PremintAPI} from '@zoralabs/premint-sdk';
182
- import type {Address, WalletClient} from 'viem';
343
+ // submit the transaction to the network
344
+ const txHash = await walletClient.writeContract(request);
183
345
 
184
- async function deletePremint(walletClient: WalletClient, collection: Address, uid: number) {
185
- const premintAPI = createPremintClient({chain: walletClient.chain});
346
+ // wait for the transaction to be complete
347
+ const receipt = await publicClient.waitForTransactionReceipt({hash: txHash});
186
348
 
187
- return await premintAPI.deletePremint({
188
- walletClient,
189
- uid,
190
- collection
191
- });
192
- }
349
+ const { urls } = await premintClient.getDataFromPremintReceipt(receipt);
193
350
 
194
- ```
351
+ // block explorer url:
352
+ console.log(urls.explorer);
353
+ // collect url:
354
+ console.log(urls.zoraCollect);
355
+ // manage url:
356
+ console.log(urls.zoraManage);
357
+ }
358
+ ```
@@ -23,4 +23,10 @@ export declare const get: <T>(url: string) => Promise<T>;
23
23
  */
24
24
  export declare const post: <T>(url: string, data: any) => Promise<T>;
25
25
  export declare const retries: <T>(tryFn: () => T, maxTries?: number, atTry?: number, linearBackoffMS?: number) => Promise<T>;
26
+ export interface IHttpClient {
27
+ get: typeof get;
28
+ post: typeof post;
29
+ retries: typeof retries;
30
+ }
31
+ export declare const httpClient: IHttpClient;
26
32
  //# sourceMappingURL=http-api-base.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"http-api-base.d.ts","sourceRoot":"","sources":["../../src/apis/http-api-base.ts"],"names":[],"mappings":"AAAA,qBAAa,gBAAgB,CAAC,CAAC,GAAG,GAAG,CAAE,SAAQ,KAAK;IAClD,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,CAAC,CAAC;gBACI,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG;CAMvD;AAQD;;;;;;;GAOG;AACH,eAAO,MAAM,GAAG,WAAkB,MAAM,eAcvC,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,IAAI,WAAkB,MAAM,QAAQ,GAAG,eAqBnD,CAAC;AAEF,eAAO,MAAM,OAAO,iCAER,MAAM,UACT,MAAM,oBACI,MAAM,eAexB,CAAC"}
1
+ {"version":3,"file":"http-api-base.d.ts","sourceRoot":"","sources":["../../src/apis/http-api-base.ts"],"names":[],"mappings":"AAAA,qBAAa,gBAAgB,CAAC,CAAC,GAAG,GAAG,CAAE,SAAQ,KAAK;IAClD,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,CAAC,CAAC;gBACI,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG;CAMvD;AAQD;;;;;;;GAOG;AACH,eAAO,MAAM,GAAG,WAAkB,MAAM,eAcvC,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,IAAI,WAAkB,MAAM,QAAQ,GAAG,eAqBnD,CAAC;AAEF,eAAO,MAAM,OAAO,iCAER,MAAM,UACT,MAAM,oBACI,MAAM,eAexB,CAAC;AAEF,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,OAAO,GAAG,CAAC;IAChB,IAAI,EAAE,OAAO,IAAI,CAAC;IAClB,OAAO,EAAE,OAAO,OAAO,CAAC;CACzB;AAED,eAAO,MAAM,UAAU,EAAE,WAIxB,CAAC"}