From 4edcd6a1c6b30caa080ff6756deba172001ee081 Mon Sep 17 00:00:00 2001 From: Dan Oved Date: Tue, 28 Nov 2023 14:45:50 -0800 Subject: [PATCH] updated readme with better examples. renamed premint mint function --- packages/protocol-sdk/README.md | 145 ++++++++++-------- .../src/premint/premint-client.test.ts | 4 +- .../src/premint/premint-client.ts | 10 +- 3 files changed, 87 insertions(+), 72 deletions(-) diff --git a/packages/protocol-sdk/README.md b/packages/protocol-sdk/README.md index bab6150ad..d231a7e8d 100644 --- a/packages/protocol-sdk/README.md +++ b/packages/protocol-sdk/README.md @@ -214,53 +214,71 @@ export async function createContract({ } ``` -### Creating a premint: +### Creating a token for free (gasless creation): ```ts -import { PremintAPI } from "@zoralabs/protocol-sdk"; -import type { Address, WalletClient } from "viem"; +import {createPremintClient} from "@zoralabs/protocol-sdk"; +import type {Address, PublicClient, WalletClient} from "viem"; -async function makePremint(walletClient: WalletClient) { - // Create premint - const premint = await createPremintAPI(walletClient.chain).createPremint({ - // Extra step to check the signature on-chain before attempting to sign +async function createForFree({ + walletClient, + creatorAccount, +}: { + // wallet client that will submit the transaction + walletClient: WalletClient; + // address of the token contract + creatorAccount: Address; +}) { + const premintClient = createPremintClient({ chain: walletClient.chain! }); + + // create and sign a free token creation. + const createdPremint = await premintClient.createPremint({ + walletClient, + account: creatorAccount, + // if true, will validate that the creator is authorized to create premints on the contract. checkSignature: true, - // Collection information that this premint NFT will exist in once minted. + // collection info of collection to create collection: { - contractAdmin: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + contractAdmin: creatorAccount, contractName: "Testing Contract", contractURI: "ipfs://bafkreiainxen4b4wz4ubylvbhons6rembxdet4a262nf2lziclqvv7au3e", }, - // WalletClient doing the signature - walletClient, - // Token information, falls back to defaults set in DefaultMintArguments. + // token info of token to create token: { tokenURI: "ipfs://bafkreice23maski3x52tsfqgxstx3kbiifnt5jotg3a5ynvve53c4soi2u", }, }); - console.log(`created ZORA premint, link: ${premint.url}`); - return premint; -} + const premintUid = createdPremint.uid; + const premintCollectionAddress = createdPremint.verifyingContract; + + return { + // unique id of created premint, which can be used later to + // update or delete the premint + uid: premintUid, + tokenContract: premintCollectionAddress, + } +} ``` -### Updating a premint: +### Updating a token that was created for free (before it was brought onchain): + +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.): ```ts -import { PremintAPI } from "@zoralabs/premint-sdk"; -import type { Address, WalletClient } from "viem"; +import {createPremintClient} from "@zoralabs/protocol-sdk"; +import type {Address, PublicClient, WalletClient} from "viem"; -async function updatePremint(walletClient: WalletClient) { +async function updateCreatedForFreeToken(walletClient: WalletClient, premintUid: number) { // Create premint API object passing in the current wallet chain (only zora and zora testnet are supported currently). - const premintAPI = createPremintAPI(walletClient.chain); + const premintClient = createPremintClient({ chain: walletClient.chain! }); - // Create premint - const premint = await premintAPI.updatePremint({ - // Extra step to check the signature on-chain before attempting to sign + // sign a message to update the created for free token, and store the update + await premintClient.updatePremint({ collection: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", - uid: 23, + uid: premintUid, // WalletClient doing the signature walletClient, // Token information, falls back to defaults set in DefaultMintArguments. @@ -269,73 +287,72 @@ async function updatePremint(walletClient: WalletClient) { "ipfs://bafkreice23maski3x52tsfqgxstx3kbiifnt5jotg3a5ynvve53c4soi2u", }, }); - - console.log(`updated ZORA premint, link: ${premint.url}`); - return premint; } ``` -### Deleting a premint: +### Deleting a token that was created for free (before it was brought onchain): + +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: ```ts -import { PremintAPI } from "@zoralabs/premint-sdk"; -import type { Address, WalletClient } from "viem"; +import {createPremintClient} from "@zoralabs/protocol-sdk"; +import type {Address, PublicClient, WalletClient} from "viem"; -async function deletePremint(walletClient: WalletClient) { - // Create premint API object passing in the current wallet chain (only zora and zora testnet are supported currently). - const premintAPI = createPremintClient({ chain: walletClient.chain }); +async function deleteCreatedForFreeToken(walletClient: WalletClient) { + const premintClient = createPremintClient({ chain: walletClient.chain! }); - // Create premint - const premint = await premintAPI.deletePremint({ + // sign a message to delete the premint, and store the deletion + await premintClient.deletePremint({ // Extra step to check the signature on-chain before attempting to sign collection: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", uid: 23, // WalletClient doing the signature walletClient, }); - - console.log(`updated ZORA premint, link: ${premint.url}`); - return premint; } ``` -### Executing a premint: +### Minting a token that was created for free (and bringing it onchain): ```ts -import { PremintAPI } from "@zoralabs/premint-sdk"; -import type { Address, WalletClient } from "viem"; +import {createPremintClient} from "@zoralabs/protocol-sdk"; +import type {Address, PublicClient, WalletClient} from "viem"; -async function executePremint( +async function mintCreatedForFreeToken( walletClient: WalletClient, - premintAddress: Address, - premintUID: number, + publicClient: PublicClient, + minterAccount: Address, ) { - const premintAPI = createPremintClient({ chain: walletClient.chain }); - - return await premintAPI.executePremintWithWallet({ - data: premintAPI.getPremintData(premintAddress, premintUID), - walletClient, + const premintClient = createPremintClient({ chain: walletClient.chain! }); + + const simulateContractParameters = await premintClient.makeMintParameters({ + account: minterAccount, + data: await premintClient.getPremintData({ + address: "0xf8dA7f53c283d898818af7FB9d98103F559bDac2", + uid: 3, + }), mintArguments: { quantityToMint: 1, + mintComment: "", }, }); -} -``` -### Deleting a premint: + // simulate the transaction and get any validation errors + const { request } = await publicClient.simulateContract(simulateContractParameters); -```ts -import {PremintAPI} from '@zoralabs/premint-sdk'; -import type {Address, WalletClient} from 'viem'; + // submit the transaction to the network + const txHash = await walletClient.writeContract(request); -async function deletePremint(walletClient: WalletClient, collection: Address, uid: number) { - const premintAPI = createPremintClient({chain: walletClient.chain}); + // wait for the transaction to be complete + const receipt = await publicClient.waitForTransactionReceipt({hash: txHash}); - return await premintAPI.deletePremint({ - walletClient, - uid, - collection - }); -} + const { urls } = await premintClient.getDataFromPremintReceipt(receipt); -``` + // block explorer url: + console.log(urls.explorer); + // collect url: + console.log(urls.zoraCollect); + // manage url: + console.log(urls.zoraManage); +} +``` \ No newline at end of file diff --git a/packages/protocol-sdk/src/premint/premint-client.test.ts b/packages/protocol-sdk/src/premint/premint-client.test.ts index 075f67ceb..7b6c07216 100644 --- a/packages/protocol-sdk/src/premint/premint-client.test.ts +++ b/packages/protocol-sdk/src/premint/premint-client.test.ts @@ -145,7 +145,7 @@ describe("ZoraCreator1155Premint", () => { }); premintClient.apiClient.postSignature = vi.fn(); - const { request } = await premintClient.executePremint({ + const simulateContractParameters = await premintClient.makeMintParameters({ account: deployerAccount!, data: await premintClient.getPremintData({ address: "0xf8dA7f53c283d898818af7FB9d98103F559bDac2", @@ -157,7 +157,7 @@ describe("ZoraCreator1155Premint", () => { }, }); const { request: simulateRequest } = - await publicClient.simulateContract(request); + await publicClient.simulateContract(simulateContractParameters); const hash = await walletClient.writeContract(simulateRequest); const receipt = await publicClient.waitForTransactionReceipt({ hash }); const { premintedLog, urls } = diff --git a/packages/protocol-sdk/src/premint/premint-client.ts b/packages/protocol-sdk/src/premint/premint-client.ts index 1c024f448..84a39cf23 100644 --- a/packages/protocol-sdk/src/premint/premint-client.ts +++ b/packages/protocol-sdk/src/premint/premint-client.ts @@ -542,7 +542,7 @@ class PremintClient { * @param settings.publicClient Optional public client for preflight checks. * @returns receipt, log, zoraURL */ - async executePremint({ + async makeMintParameters({ data, account, mintArguments, @@ -553,7 +553,7 @@ class PremintClient { quantityToMint: number; mintComment?: string; }; - }): Promise<{ request: SimulateContractParameters }> { + }): Promise { if (mintArguments && mintArguments?.quantityToMint < 1) { throw new Error("Quantity to mint cannot be below 1"); } @@ -574,7 +574,7 @@ class PremintClient { const value = numberToMint * REWARD_PER_TOKEN; - const request = { + const request: SimulateContractParameters = { account, abi: zoraCreator1155PremintExecutorImplABI, functionName: "premint", @@ -583,9 +583,7 @@ class PremintClient { args, }; - return { - request, - }; + return request; } }