Skip to content

Commit d02484e

Browse files
authored
PremintClient updates: PremintApiClient is now a class, createPremintClient now follows pattern of createMintClient. executePremint renamed to makeMintParemeters (#390)
* premint client now follows standard mint client functinoality * added changeset * updated readme with better examples. renamed premint mint function * lint fix
1 parent 97f58b3 commit d02484e

File tree

5 files changed

+216
-148
lines changed

5 files changed

+216
-148
lines changed

.changeset/pink-turtles-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zoralabs/protocol-sdk": patch
3+
---
4+
5+
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`

packages/protocol-sdk/README.md

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -214,53 +214,71 @@ export async function createContract({
214214
}
215215
```
216216

217-
### Creating a premint:
217+
### Creating a token for free (gasless creation):
218218

219219
```ts
220-
import { PremintAPI } from "@zoralabs/protocol-sdk";
221-
import type { Address, WalletClient } from "viem";
220+
import {createPremintClient} from "@zoralabs/protocol-sdk";
221+
import type {Address, PublicClient, WalletClient} from "viem";
222222

223-
async function makePremint(walletClient: WalletClient) {
224-
// Create premint
225-
const premint = await createPremintAPI(walletClient.chain).createPremint({
226-
// 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.
227239
checkSignature: true,
228-
// Collection information that this premint NFT will exist in once minted.
240+
// collection info of collection to create
229241
collection: {
230-
contractAdmin: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
242+
contractAdmin: creatorAccount,
231243
contractName: "Testing Contract",
232244
contractURI:
233245
"ipfs://bafkreiainxen4b4wz4ubylvbhons6rembxdet4a262nf2lziclqvv7au3e",
234246
},
235-
// WalletClient doing the signature
236-
walletClient,
237-
// Token information, falls back to defaults set in DefaultMintArguments.
247+
// token info of token to create
238248
token: {
239249
tokenURI:
240250
"ipfs://bafkreice23maski3x52tsfqgxstx3kbiifnt5jotg3a5ynvve53c4soi2u",
241251
},
242252
});
243253

244-
console.log(`created ZORA premint, link: ${premint.url}`);
245-
return premint;
246-
}
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+
}
247264
```
248265

249-
### 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.):
250269

251270
```ts
252-
import { PremintAPI } from "@zoralabs/premint-sdk";
253-
import type { Address, WalletClient } from "viem";
271+
import {createPremintClient} from "@zoralabs/protocol-sdk";
272+
import type {Address, PublicClient, WalletClient} from "viem";
254273

255-
async function updatePremint(walletClient: WalletClient) {
274+
async function updateCreatedForFreeToken(walletClient: WalletClient, premintUid: number) {
256275
// Create premint API object passing in the current wallet chain (only zora and zora testnet are supported currently).
257-
const premintAPI = createPremintAPI(walletClient.chain);
276+
const premintClient = createPremintClient({ chain: walletClient.chain! });
258277

259-
// Create premint
260-
const premint = await premintAPI.updatePremint({
261-
// 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({
262280
collection: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
263-
uid: 23,
281+
uid: premintUid,
264282
// WalletClient doing the signature
265283
walletClient,
266284
// Token information, falls back to defaults set in DefaultMintArguments.
@@ -269,73 +287,72 @@ async function updatePremint(walletClient: WalletClient) {
269287
"ipfs://bafkreice23maski3x52tsfqgxstx3kbiifnt5jotg3a5ynvve53c4soi2u",
270288
},
271289
});
272-
273-
console.log(`updated ZORA premint, link: ${premint.url}`);
274-
return premint;
275290
}
276291
```
277292

278-
### 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:
279296

280297
```ts
281-
import { PremintAPI } from "@zoralabs/premint-sdk";
282-
import type { Address, WalletClient } from "viem";
298+
import {createPremintClient} from "@zoralabs/protocol-sdk";
299+
import type {Address, PublicClient, WalletClient} from "viem";
283300

284-
async function deletePremint(walletClient: WalletClient) {
285-
// Create premint API object passing in the current wallet chain (only zora and zora testnet are supported currently).
286-
const premintAPI = createPremintClient({ chain: walletClient.chain });
301+
async function deleteCreatedForFreeToken(walletClient: WalletClient) {
302+
const premintClient = createPremintClient({ chain: walletClient.chain! });
287303

288-
// Create premint
289-
const premint = await premintAPI.deletePremint({
304+
// sign a message to delete the premint, and store the deletion
305+
await premintClient.deletePremint({
290306
// Extra step to check the signature on-chain before attempting to sign
291307
collection: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
292308
uid: 23,
293309
// WalletClient doing the signature
294310
walletClient,
295311
});
296-
297-
console.log(`updated ZORA premint, link: ${premint.url}`);
298-
return premint;
299312
}
300313
```
301314

302-
### Executing a premint:
315+
### Minting a token that was created for free (and bringing it onchain):
303316

304317
```ts
305-
import { PremintAPI } from "@zoralabs/premint-sdk";
306-
import type { Address, WalletClient } from "viem";
318+
import {createPremintClient} from "@zoralabs/protocol-sdk";
319+
import type {Address, PublicClient, WalletClient} from "viem";
307320

308-
async function executePremint(
321+
async function mintCreatedForFreeToken(
309322
walletClient: WalletClient,
310-
premintAddress: Address,
311-
premintUID: number,
323+
publicClient: PublicClient,
324+
minterAccount: Address,
312325
) {
313-
const premintAPI = createPremintClient({ chain: walletClient.chain });
314-
315-
return await premintAPI.executePremintWithWallet({
316-
data: premintAPI.getPremintData(premintAddress, premintUID),
317-
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+
}),
318334
mintArguments: {
319335
quantityToMint: 1,
336+
mintComment: "",
320337
},
321338
});
322-
}
323-
```
324339

325-
### Deleting a premint:
340+
// simulate the transaction and get any validation errors
341+
const { request } = await publicClient.simulateContract(simulateContractParameters);
326342

327-
```ts
328-
import {PremintAPI} from '@zoralabs/premint-sdk';
329-
import type {Address, WalletClient} from 'viem';
343+
// submit the transaction to the network
344+
const txHash = await walletClient.writeContract(request);
330345

331-
async function deletePremint(walletClient: WalletClient, collection: Address, uid: number) {
332-
const premintAPI = createPremintClient({chain: walletClient.chain});
346+
// wait for the transaction to be complete
347+
const receipt = await publicClient.waitForTransactionReceipt({hash: txHash});
333348

334-
return await premintAPI.deletePremint({
335-
walletClient,
336-
uid,
337-
collection
338-
});
339-
}
349+
const { urls } = await premintClient.getDataFromPremintReceipt(receipt);
340350

341-
```
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+
```
Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { post, retries, get } from "../apis/http-api-base";
1+
import {
2+
IHttpClient,
3+
httpClient as defaultHttpClient,
4+
} from "../apis/http-api-base";
25
import { components, paths } from "../apis/generated/premint-api-types";
36
import { ZORA_API_BASE } from "../constants";
7+
import { NetworkConfig } from "src/apis/chain-constants";
8+
import { getApiNetworkConfigForChain } from "src/mint/mint-api-client";
49

510
type SignaturePostType = paths["/signature"]["post"];
611
type PremintSignatureRequestBody =
@@ -22,36 +27,85 @@ type PremintSignatureGetPathParameters =
2227
export type PremintSignatureGetResponse =
2328
SignaturePremintGetType["responses"][200]["content"]["application/json"];
2429

30+
export type PremintCollection = PremintSignatureGetResponse["collection"];
31+
2532
export type BackendChainNames = components["schemas"]["ChainName"];
2633

27-
const postSignature = async (
28-
data: PremintSignatureRequestBody,
29-
): Promise<PremintSignatureResponse> =>
34+
const postSignature = async ({
35+
httpClient: { post, retries } = defaultHttpClient,
36+
...data
37+
}: PremintSignatureRequestBody & {
38+
httpClient?: Pick<IHttpClient, "retries" | "post">;
39+
}): Promise<PremintSignatureResponse> =>
3040
retries(() =>
3141
post<PremintSignatureResponse>(`${ZORA_API_BASE}premint/signature`, data),
3242
);
3343

34-
const getNextUID = async (
35-
path: PremintNextUIDGetPathParameters,
36-
): Promise<PremintNextUIDGetResponse> =>
44+
const getNextUID = async ({
45+
chain_name,
46+
collection_address,
47+
httpClient: { retries, get } = defaultHttpClient,
48+
}: PremintNextUIDGetPathParameters & {
49+
httpClient?: Pick<IHttpClient, "retries" | "get">;
50+
}): Promise<PremintNextUIDGetResponse> =>
3751
retries(() =>
3852
get<PremintNextUIDGetResponse>(
39-
`${ZORA_API_BASE}premint/signature/${path.chain_name}/${path.collection_address}/next_uid`,
53+
`${ZORA_API_BASE}premint/signature/${chain_name}/${collection_address}/next_uid`,
4054
),
4155
);
4256

43-
const getSignature = async (
44-
path: PremintSignatureGetPathParameters,
45-
): Promise<PremintSignatureGetResponse> =>
57+
const getSignature = async ({
58+
collection_address,
59+
uid,
60+
chain_name,
61+
httpClient: { retries, get } = defaultHttpClient,
62+
}: PremintSignatureGetPathParameters & {
63+
httpClient?: Pick<IHttpClient, "retries" | "get">;
64+
}): Promise<PremintSignatureGetResponse> =>
4665
retries(() =>
4766
get<PremintSignatureGetResponse>(
48-
`${ZORA_API_BASE}premint/signature/${path.chain_name}/${path.collection_address}/${path.uid}`,
67+
`${ZORA_API_BASE}premint/signature/${chain_name}/${collection_address}/${uid}`,
4968
),
5069
);
5170

52-
export const PremintAPIClient = {
53-
postSignature,
54-
getSignature,
55-
getNextUID,
56-
};
57-
export { ZORA_API_BASE };
71+
type OmitChainName<T> = Omit<T, "chain_name">;
72+
73+
class PremintAPIClient {
74+
httpClient: IHttpClient;
75+
networkConfig: NetworkConfig;
76+
77+
constructor(chainId: number, httpClient?: IHttpClient) {
78+
this.httpClient = httpClient || defaultHttpClient;
79+
this.networkConfig = getApiNetworkConfigForChain(chainId);
80+
}
81+
postSignature = async (
82+
data: OmitChainName<PremintSignatureRequestBody>,
83+
): Promise<PremintSignatureResponse> =>
84+
postSignature({
85+
...data,
86+
chain_name: this.networkConfig.zoraBackendChainName,
87+
httpClient: this.httpClient,
88+
});
89+
90+
getNextUID = async (
91+
path: OmitChainName<PremintNextUIDGetPathParameters>,
92+
): Promise<PremintNextUIDGetResponse> =>
93+
getNextUID({
94+
...path,
95+
chain_name: this.networkConfig.zoraBackendChainName,
96+
httpClient: this.httpClient,
97+
});
98+
99+
getSignature = async ({
100+
collection_address,
101+
uid,
102+
}: OmitChainName<PremintSignatureGetPathParameters>): Promise<PremintSignatureGetResponse> =>
103+
getSignature({
104+
collection_address,
105+
uid,
106+
chain_name: this.networkConfig.zoraBackendChainName,
107+
httpClient: this.httpClient,
108+
});
109+
}
110+
111+
export { ZORA_API_BASE, PremintAPIClient };

0 commit comments

Comments
 (0)