From dbe2444d1aedddec22c5ae00724d8c82f6699fde Mon Sep 17 00:00:00 2001 From: Dan Oved Date: Mon, 13 Nov 2023 15:08:04 -0800 Subject: [PATCH] Make SDK factory client patterns (#356) duplicate of #353 but with proper commit history (and rebasing off of a stacked diff) --- packages/protocol-sdk/README.md | 53 ++++- .../src/create/1155-create-helper.test.ts | 66 ++++-- .../src/create/1155-create-helper.ts | 223 ++++++++++-------- .../protocol-sdk/src/mint/mint-client.test.ts | 6 +- packages/protocol-sdk/src/mint/mint-client.ts | 14 +- .../src/premint/premint-client.test.ts | 31 ++- .../src/premint/premint-client.ts | 60 +++-- 7 files changed, 268 insertions(+), 185 deletions(-) diff --git a/packages/protocol-sdk/README.md b/packages/protocol-sdk/README.md index be448ca39..4e5fad4ea 100644 --- a/packages/protocol-sdk/README.md +++ b/packages/protocol-sdk/README.md @@ -12,7 +12,7 @@ Protocol SDK allows users to manage zora mints and collects. ### Creating a mint from an on-chain contract: ```ts -import { MintAPI } from "@zoralabs/protocol-sdk"; +import { createMintClient } from "@zoralabs/protocol-sdk"; import type { Address, WalletClient } from "viem"; async function mintNFT( @@ -20,7 +20,7 @@ async function mintNFT( address: Address, tokenId: bigint, ) { - const mintAPI = new MintAPI(walletClient.chain); + const mintAPI = createMintClient({ chain: walletClient.chain }); await mintAPI.mintNFT({ walletClient, address, @@ -33,6 +33,40 @@ async function mintNFT( } ``` +### Creating an 1155 contract: + +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. + +If you wish to mint on an existing contract, pass that contract in the contract field. The return value is the prepared transaction that you can use viem or wagmi to send. + +```ts +import type { PublicClient } from "viem"; +import { create1155CreatorClient } from "@zoralabs/protocol-sdk"; + +export async function createContract({ + publicClient, + walletClient, +}: { + publicClient: PublicClient; + walletClient: WalletClient; +}) { + const creatorClient = create1155CreatorClient({ publicClient }); + const { request } = await creatorClient.createNew1155Token({ + contract: { + name: "testContract", + uri: demoContractMetadataURI, + }, + tokenMetadataURI: demoTokenMetadataURI, + account: creatorAccount, + mintToCreatorCount: 1, + }); + const { request: simulateRequest } = publicClient.simulateContract(request); + const hash = await walletClient.writeContract(simulateRequest); + const receipt = await publicClient.waitForTransactionReceipt({ hash }); + return receipt; +} +``` + ### Creating a premint: ```ts @@ -40,11 +74,8 @@ import { PremintAPI } from "@zoralabs/protocol-sdk"; import type { Address, WalletClient } from "viem"; async function makePremint(walletClient: WalletClient) { - // Create premint API object passing in the current wallet chain (only zora and zora testnet are supported currently). - const premintAPI = new PremintAPI(walletClient.chain); - // Create premint - const premint = await premintAPI.createPremint({ + const premint = await createPremintAPI(walletClient.chain).createPremint({ // Extra step to check the signature on-chain before attempting to sign checkSignature: true, // Collection information that this premint NFT will exist in once minted. @@ -76,7 +107,7 @@ import type { Address, WalletClient } from "viem"; async function updatePremint(walletClient: WalletClient) { // Create premint API object passing in the current wallet chain (only zora and zora testnet are supported currently). - const premintAPI = new PremintAPI(walletClient.chain); + const premintAPI = createPremintAPI(walletClient.chain); // Create premint const premint = await premintAPI.updatePremint({ @@ -105,7 +136,7 @@ import type { Address, 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 = new PremintAPI(walletClient.chain); + const premintAPI = createPremintClient({ chain: walletClient.chain }); // Create premint const premint = await premintAPI.deletePremint({ @@ -132,7 +163,7 @@ async function executePremint( premintAddress: Address, premintUID: number, ) { - const premintAPI = new PremintAPI(walletClient.chain); + const premintAPI = createPremintClient({ chain: walletClient.chain }); return await premintAPI.executePremintWithWallet({ data: premintAPI.getPremintData(premintAddress, premintUID), @@ -146,12 +177,12 @@ async function executePremint( ### Deleting a premint: -```js +```ts import {PremintAPI} from '@zoralabs/premint-sdk'; import type {Address, WalletClient} from 'viem'; async function deletePremint(walletClient: WalletClient, collection: Address, uid: number) { - const premintAPI = new PremintAPI(walletClient.chain); + const premintAPI = createPremintClient({chain: walletClient.chain}); return await premintAPI.deletePremint({ walletClient, diff --git a/packages/protocol-sdk/src/create/1155-create-helper.test.ts b/packages/protocol-sdk/src/create/1155-create-helper.test.ts index bf303cba5..a8ae366e1 100644 --- a/packages/protocol-sdk/src/create/1155-create-helper.test.ts +++ b/packages/protocol-sdk/src/create/1155-create-helper.test.ts @@ -1,7 +1,7 @@ import { parseEther } from "viem"; import { describe, expect } from "vitest"; import { - createNew1155Token, + create1155CreatorClient, getTokenIdFromCreateReceipt, } from "./1155-create-helper"; import { anvilTest } from "src/anvil"; @@ -19,8 +19,10 @@ describe("create-helper", () => { address: creatorAddress, value: parseEther("1"), }); - const new1155TokenRequest = await createNew1155Token({ - publicClient, + const creatorClient = create1155CreatorClient({ + publicClient: publicClient, + }); + const { request } = await creatorClient.createNew1155Token({ contract: { name: "testContract", uri: demoContractMetadataURI, @@ -29,7 +31,9 @@ describe("create-helper", () => { account: creatorAddress, mintToCreatorCount: 1, }); - const hash = await new1155TokenRequest.send(walletClient); + const { request: simulationResponse } = + await publicClient.simulateContract(request); + const hash = await walletClient.writeContract(simulationResponse); const receipt = await publicClient.waitForTransactionReceipt({ hash }); expect(receipt).not.toBeNull(); expect(receipt.to).to.equal("0x777777c338d93e2c7adf08d102d45ca7cc4ed021"); @@ -43,41 +47,51 @@ describe("create-helper", () => { const addresses = await walletClient.getAddresses(); const creatorAccount = addresses[0]!; - const new1155TokenRequest = await createNew1155Token({ - publicClient, - contract: { - name: "testContract2", - uri: demoContractMetadataURI, - }, - tokenMetadataURI: demoTokenMetadataURI, - account: creatorAccount, - mintToCreatorCount: 1, + const creatorClient = create1155CreatorClient({ + publicClient: publicClient, }); - expect(new1155TokenRequest.contractAddress).to.be.equal( + + const { request, contractAddress, contractExists } = + await creatorClient.createNew1155Token({ + contract: { + name: "testContract2", + uri: demoContractMetadataURI, + }, + tokenMetadataURI: demoTokenMetadataURI, + account: creatorAccount, + mintToCreatorCount: 1, + }); + expect(contractAddress).to.be.equal( "0xb1A8928dF830C21eD682949Aa8A83C1C215194d3", ); - expect(new1155TokenRequest.contractExists).to.be.false; - const hash = await new1155TokenRequest.send(walletClient); + expect(contractExists).to.be.false; + const { request: simulateResponse } = + await publicClient.simulateContract(request); + const hash = await walletClient.writeContract(simulateResponse); const receipt = await publicClient.waitForTransactionReceipt({ hash }); const firstTokenId = getTokenIdFromCreateReceipt(receipt); expect(firstTokenId).to.be.equal(1n); expect(receipt).not.toBeNull(); - const newTokenOnExistingContract = await createNew1155Token({ - publicClient, - contract: { - name: "testContract2", - uri: demoContractMetadataURI, + const newTokenOnExistingContract = await creatorClient.createNew1155Token( + { + contract: { + name: "testContract2", + uri: demoContractMetadataURI, + }, + tokenMetadataURI: demoTokenMetadataURI, + account: creatorAccount, + mintToCreatorCount: 1, }, - tokenMetadataURI: demoTokenMetadataURI, - account: creatorAccount, - mintToCreatorCount: 1, - }); + ); expect(newTokenOnExistingContract.contractAddress).to.be.equal( "0xb1A8928dF830C21eD682949Aa8A83C1C215194d3", ); expect(newTokenOnExistingContract.contractExists).to.be.true; - const newHash = await newTokenOnExistingContract.send(walletClient); + const { request: simulateRequest } = await publicClient.simulateContract( + newTokenOnExistingContract.request, + ); + const newHash = await walletClient.writeContract(simulateRequest); const newReceipt = await publicClient.waitForTransactionReceipt({ hash: newHash, }); diff --git a/packages/protocol-sdk/src/create/1155-create-helper.ts b/packages/protocol-sdk/src/create/1155-create-helper.ts index 66b8f3340..84f9b276d 100644 --- a/packages/protocol-sdk/src/create/1155-create-helper.ts +++ b/packages/protocol-sdk/src/create/1155-create-helper.ts @@ -8,8 +8,8 @@ import type { Address, Hex, PublicClient, + SimulateContractParameters, TransactionReceipt, - WalletClient, } from "viem"; import { decodeEventLog, encodeFunctionData, zeroAddress } from "viem"; import { OPEN_EDITION_MINT_SIZE } from "../constants"; @@ -101,11 +101,6 @@ export function create1155TokenSetupArgs({ }; const setupActions = [ - encodeFunctionData({ - abi: zoraCreator1155ImplABI, - functionName: "addPermission", - args: [0n, fixedPriceMinterAddress, PERMISSION_BIT_MINTER], - }), encodeFunctionData({ abi: zoraCreator1155ImplABI, functionName: "assumeLastTokenIdMatches", @@ -122,6 +117,11 @@ export function create1155TokenSetupArgs({ functionName: "setupNewToken", args: [tokenMetadataURI, maxSupply], }), + encodeFunctionData({ + abi: zoraCreator1155ImplABI, + functionName: "addPermission", + args: [0n, fixedPriceMinterAddress, PERMISSION_BIT_MINTER], + }), encodeFunctionData({ abi: zoraCreator1155ImplABI, functionName: "callSale", @@ -226,117 +226,134 @@ async function getContractExists( }; } -// Create new 1155 token -export async function createNew1155Token({ +type CreateNew1155TokenReturn = { + request: SimulateContractParameters; + tokenSetupActions: Hex[]; + contractAddress: Address; + contractExists: boolean; +}; + +export function create1155CreatorClient({ publicClient, - contract, - tokenMetadataURI, - mintToCreatorCount = 1, - salesConfig = {}, - maxSupply, - account, - royaltySettings, - getAdditionalSetupActions, }: { publicClient: PublicClient; - account: Address; - maxSupply?: bigint | number; - royaltySettings?: RoyaltySettingsType; - royaltyBPS?: number; - contract: ContractType; - tokenMetadataURI: string; - mintToCreatorCount?: bigint | number; - salesConfig?: SalesConfigParamsType; - getAdditionalSetupActions?: (args: { - tokenId: bigint; - contractAddress: Address; - }) => Hex[]; }) { - // Check if contract exists either from metadata or the static address passed in. - // If a static address is passed in, this fails if that contract does not exist. - const { contractExists, contractAddress } = await getContractExists( - publicClient, + async function createNew1155Token({ contract, - account, - ); - - // Assume the next token id is the first token available for a new contract. - let nextTokenId = 1n; - - if (contractExists) { - nextTokenId = await publicClient.readContract({ - abi: zoraCreator1155ImplABI, - functionName: "nextTokenId", - address: contractAddress, - }); - } - - // Get the fixed price minter to use within the new token to set the sales configuration. - const fixedPriceMinterAddress = await publicClient.readContract({ - abi: zoraCreator1155FactoryImplABI, - address: zoraCreator1155FactoryImplAddress[999], - functionName: "fixedPriceMinter", - }); - - let tokenSetupActions = create1155TokenSetupArgs({ tokenMetadataURI, - nextTokenId, - salesConfig, + mintToCreatorCount = 1, + salesConfig = {}, maxSupply, - fixedPriceMinterAddress, account, - mintToCreatorCount, royaltySettings, - }); - if (getAdditionalSetupActions) { - tokenSetupActions = [ - ...getAdditionalSetupActions({ tokenId: nextTokenId, contractAddress }), - ...tokenSetupActions, - ]; - } + getAdditionalSetupActions, + }: { + account: Address; + maxSupply?: bigint | number; + royaltySettings?: RoyaltySettingsType; + royaltyBPS?: number; + contract: ContractType; + tokenMetadataURI: string; + mintToCreatorCount?: bigint | number; + salesConfig?: SalesConfigParamsType; + getAdditionalSetupActions?: (args: { + tokenId: bigint; + contractAddress: Address; + }) => Hex[]; + }): Promise { + // Check if contract exists either from metadata or the static address passed in. + // If a static address is passed in, this fails if that contract does not exist. + const { contractExists, contractAddress } = await getContractExists( + publicClient, + contract, + account, + ); - if (!contractAddress && typeof contract === "string") { - throw new Error("Invariant: contract cannot be missing and an address"); - } - if (!contractExists && typeof contract !== "string") { - const { request } = await publicClient.simulateContract({ + // Assume the next token id is the first token available for a new contract. + let nextTokenId = 1n; + + if (contractExists) { + nextTokenId = await publicClient.readContract({ + abi: zoraCreator1155ImplABI, + functionName: "nextTokenId", + address: contractAddress, + }); + } + + // Get the fixed price minter to use within the new token to set the sales configuration. + const fixedPriceMinterAddress = await publicClient.readContract({ abi: zoraCreator1155FactoryImplABI, - functionName: "createContractDeterministic", - account, address: zoraCreator1155FactoryImplAddress[999], - args: [ - contract.uri, - contract.name, - { - // deprecated - royaltyMintSchedule: 0, - royaltyBPS: royaltySettings?.royaltyBPS || ROYALTY_BPS_DEFAULT, - royaltyRecipient: royaltySettings?.royaltyRecipient || account, - }, - contract.defaultAdmin || account, - tokenSetupActions, - ], + functionName: "fixedPriceMinter", }); - return { - send: (walletClient: WalletClient) => walletClient.writeContract(request), - tokenSetupActions, - contractAddress, - contractExists, - }; - } else if (contractExists) { - const { request } = await publicClient.simulateContract({ - abi: zoraCreator1155ImplABI, - functionName: "multicall", + + let tokenSetupActions = create1155TokenSetupArgs({ + tokenMetadataURI, + nextTokenId, + salesConfig, + maxSupply, + fixedPriceMinterAddress, account, - address: contractAddress, - args: [tokenSetupActions], + mintToCreatorCount, + royaltySettings, }); - return { - send: (walletClient: WalletClient) => walletClient.writeContract(request), - tokenSetupActions, - contractAddress, - contractExists, - }; + if (getAdditionalSetupActions) { + tokenSetupActions = [ + ...getAdditionalSetupActions({ tokenId: nextTokenId, contractAddress }), + ...tokenSetupActions, + ]; + } + + if (!contractAddress && typeof contract === "string") { + throw new Error("Invariant: contract cannot be missing and an address"); + } + if (!contractExists && typeof contract !== "string") { + const request: SimulateContractParameters< + typeof zoraCreator1155FactoryImplABI, + "createContractDeterministic" + > = { + abi: zoraCreator1155FactoryImplABI, + functionName: "createContractDeterministic", + account, + address: zoraCreator1155FactoryImplAddress[999], + args: [ + contract.uri, + contract.name, + { + // deprecated + royaltyMintSchedule: 0, + royaltyBPS: royaltySettings?.royaltyBPS || ROYALTY_BPS_DEFAULT, + royaltyRecipient: royaltySettings?.royaltyRecipient || account, + }, + contract.defaultAdmin || account, + tokenSetupActions, + ], + }; + return { + request, + tokenSetupActions, + contractAddress, + contractExists, + }; + } else if (contractExists) { + const request: SimulateContractParameters< + typeof zoraCreator1155ImplABI, + "multicall" + > = { + abi: zoraCreator1155ImplABI, + functionName: "multicall", + account, + address: contractAddress, + args: [tokenSetupActions], + }; + return { + request, + tokenSetupActions, + contractAddress, + contractExists, + }; + } + throw new Error("Unsupported contract argument type"); } - throw new Error("Unsupported contract argument type"); + return { createNew1155Token }; } diff --git a/packages/protocol-sdk/src/mint/mint-client.test.ts b/packages/protocol-sdk/src/mint/mint-client.test.ts index 58c3b2514..696e5ac0a 100644 --- a/packages/protocol-sdk/src/mint/mint-client.test.ts +++ b/packages/protocol-sdk/src/mint/mint-client.test.ts @@ -1,7 +1,7 @@ import { parseAbi, parseEther } from "viem"; import { zora } from "viem/chains"; import { describe, expect } from "vitest"; -import { MintClient } from "./mint-client"; +import { createMintClient } from "./mint-client"; import { zoraCreator1155ImplABI } from "@zoralabs/protocol-deployments"; import { anvilTest } from "src/anvil"; @@ -21,7 +21,7 @@ describe("mint-helper", () => { }); const targetContract = "0xa2fea3537915dc6c7c7a97a82d1236041e6feb2e"; const targetTokenId = 1n; - const minter = new MintClient(zora); + const minter = createMintClient({ chain: zora }); const { simulateContractParameters: params } = await minter.makePrepareMintTokenParams({ @@ -73,7 +73,7 @@ describe("mint-helper", () => { const targetContract = "0x7aae7e67515A2CbB8585C707Ca6db37BDd3EA839"; const targetTokenId = undefined; - const minter = new MintClient(zora); + const minter = createMintClient({ chain: zora }); const { simulateContractParameters: prepared } = await minter.makePrepareMintTokenParams({ diff --git a/packages/protocol-sdk/src/mint/mint-client.ts b/packages/protocol-sdk/src/mint/mint-client.ts index c7d2f7a30..c56b27fb4 100644 --- a/packages/protocol-sdk/src/mint/mint-client.ts +++ b/packages/protocol-sdk/src/mint/mint-client.ts @@ -42,7 +42,7 @@ const zora721Abi = parseAbi([ "function zoraFeeForAmount(uint256 amount) public view returns (address, uint256)", ] as const); -export class MintClient extends ClientBase { +class MintClient extends ClientBase { apiClient: typeof MintAPIClient; constructor(chain: Chain, apiClient?: typeof MintAPIClient) { @@ -80,7 +80,7 @@ export class MintClient extends ClientBase { mintable: MintableGetTokenResponse; minterAccount: Address; mintArguments: MintArguments; - }): Promise<{simulateContractParameters: SimulateContractParameters}> { + }): Promise<{ simulateContractParameters: SimulateContractParameters }> { if (!mintable) { throw new MintError("No mintable found"); } @@ -216,3 +216,13 @@ export class MintClient extends ClientBase { return result; } } + +export function createMintClient({ + chain, + mintAPIClient, +}: { + chain: Chain; + mintAPIClient?: typeof MintAPIClient; +}) { + return new MintClient(chain, mintAPIClient); +} diff --git a/packages/protocol-sdk/src/premint/premint-client.test.ts b/packages/protocol-sdk/src/premint/premint-client.test.ts index 7b119b368..076ed7af0 100644 --- a/packages/protocol-sdk/src/premint/premint-client.test.ts +++ b/packages/protocol-sdk/src/premint/premint-client.test.ts @@ -1,6 +1,6 @@ import { foundry } from "viem/chains"; import { describe, expect, vi } from "vitest"; -import { PremintClient } from "./premint-client"; +import { createPremintClient } from "./premint-client"; import { anvilTest } from "src/anvil"; import { BackendChainNamesLookup } from "src/apis/chain-constants"; @@ -9,7 +9,7 @@ describe("ZoraCreator1155Premint", () => { "can sign on the forked premint contract", async ({ viemClients: { walletClient, publicClient } }) => { const [deployerAccount] = await walletClient.getAddresses(); - const premintClient = new PremintClient(foundry); + const premintClient = createPremintClient({ chain: foundry }); premintClient.apiClient.getNextUID = vi .fn() @@ -71,7 +71,7 @@ describe("ZoraCreator1155Premint", () => { anvilTest( "can validate premint on network", async ({ viemClients: { publicClient } }) => { - const premintClient = new PremintClient(foundry); + const premintClient = createPremintClient({ chain: foundry }); const premintData = { collection: { @@ -116,7 +116,7 @@ describe("ZoraCreator1155Premint", () => { "can execute premint on network", async ({ viemClients: { walletClient, publicClient } }) => { const [deployerAccount] = await walletClient.getAddresses(); - const premintClient = new PremintClient(foundry); + const premintClient = createPremintClient({ chain: foundry }); premintClient.apiClient.getSignature = vi.fn().mockResolvedValue({ chain_name: "ZORA-TESTNET", @@ -149,21 +149,34 @@ describe("ZoraCreator1155Premint", () => { }); premintClient.apiClient.postSignature = vi.fn(); - const premint = await premintClient.executePremintWithWallet({ + const { request } = await premintClient.executePremint({ + account: deployerAccount!, data: await premintClient.getPremintData({ address: "0xf8dA7f53c283d898818af7FB9d98103F559bDac2", uid: 3, }), - account: deployerAccount, - walletClient, - publicClient, mintArguments: { quantityToMint: 1, mintComment: "", }, }); + const { request: simulateRequest } = + await publicClient.simulateContract(request); + const hash = await walletClient.writeContract(simulateRequest); + const receipt = await publicClient.waitForTransactionReceipt({ hash }); + const { premintedLog, urls } = + premintClient.getDataFromPremintReceipt(receipt); + + expect(urls).toEqual({ + explorer: + "https://undefined/token/0xf8dA7f53c283d898818af7FB9d98103F559bDac2/instance/1", + zoraCollect: + "https://testnet.zora.co/collect/zgor:0xf8dA7f53c283d898818af7FB9d98103F559bDac2/1", + zoraManage: + "https://testnet.zora.co/collect/zgor:0xf8dA7f53c283d898818af7FB9d98103F559bDac2/1", + }); - expect(premint.premintedLog).toEqual({ + expect(premintedLog).toEqual({ contractAddress: "0xf8dA7f53c283d898818af7FB9d98103F559bDac2", contractConfig: { contractAdmin: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", diff --git a/packages/protocol-sdk/src/premint/premint-client.ts b/packages/protocol-sdk/src/premint/premint-client.ts index c708359a4..a56065173 100644 --- a/packages/protocol-sdk/src/premint/premint-client.ts +++ b/packages/protocol-sdk/src/premint/premint-client.ts @@ -5,6 +5,7 @@ import type { Chain, Hex, PublicClient, + SimulateContractParameters, TransactionReceipt, WalletClient, } from "viem"; @@ -55,12 +56,6 @@ type SignedPremintResponse = { premint: PremintSignatureResponse; }; -type ExecutedPremintResponse = { - receipt: TransactionReceipt; - premintedLog?: PremintedLogType; - urls: URLSReturnType; -}; - export const DefaultMintArguments = { maxSupply: OPEN_EDITION_MINT_SIZE, maxTokensPerAddress: 0n, @@ -148,7 +143,7 @@ export const encodePremintForAPI = ({ * Preminter API to access ZORA Premint functionality. * Currently only supports V1 premints. */ -export class PremintClient extends ClientBase { +class PremintClient extends ClientBase { apiClient: typeof PremintAPIClient; constructor(chain: Chain, apiClient?: typeof PremintAPIClient) { @@ -181,6 +176,17 @@ export class PremintClient extends ClientBase { return zoraCreatorFixedPriceSaleStrategyAddress[999]; } + getDataFromPremintReceipt(receipt: TransactionReceipt) { + const premintedLog = getPremintedLogFromReceipt(receipt); + return { + premintedLog, + urls: this.makeUrls({ + address: premintedLog?.contractAddress, + tokenId: premintedLog?.tokenId, + }), + }; + } + /** * Update existing premint given collection address and UID of existing premint. * @@ -553,24 +559,18 @@ export class PremintClient extends ClientBase { * @param settings.publicClient Optional public client for preflight checks. * @returns receipt, log, zoraURL */ - async executePremintWithWallet({ + async executePremint({ data, account, - walletClient, mintArguments, - publicClient, }: { data: PremintSignatureGetResponse; - walletClient: WalletClient; - account?: Account | Address; + account: Account | Address; mintArguments?: { quantityToMint: number; mintComment?: string; }; - publicClient?: PublicClient; - }): Promise { - publicClient = this.getPublicClient(publicClient); - + }): Promise<{request: SimulateContractParameters}> { if (mintArguments && mintArguments?.quantityToMint < 1) { throw new Error("Quantity to mint cannot be below 1"); } @@ -585,35 +585,33 @@ export class PremintClient extends ClientBase { mintArguments?.mintComment || "", ] as const; - if (!account) { - account = walletClient.account; - } - if (!account) { throw new Error("Wallet not passed in"); } const value = numberToMint * REWARD_PER_TOKEN; - const { request } = await publicClient.simulateContract({ + const request = { account, abi: zoraCreator1155PremintExecutorImplABI, functionName: "premint", value, address: targetAddress, args, - }); - const hash = await walletClient.writeContract(request); - const receipt = await publicClient.waitForTransactionReceipt({ hash }); - const premintedLog = getPremintedLogFromReceipt(receipt); + }; return { - receipt, - premintedLog, - urls: this.makeUrls({ - address: premintedLog?.contractAddress, - tokenId: premintedLog?.tokenId, - }), + request, }; } } + +export function createPremintClient({ + chain, + premintAPIClient, +}: { + chain: Chain; + premintAPIClient?: typeof PremintAPIClient; +}) { + return new PremintClient(chain, premintAPIClient); +}