Mint 1155s, 721s, and Premints
The Collector Client can be used to prepare transactions for minting 1155s, 721s, and premints with
a function mint()
. The type of item to mint is determined by the mintType
parameter, which
can be set to either 1155
, 721
, or premint
. Depending on the mintType
additional parameters
must be passed.
import {
useChainId,
usePublicClient,
useWriteContract } from 'wagmi';
import { createCollectorClient } from "@zoralabs/protocol-sdk";
const chainId = useChainId();
const publicClient = usePublicClient();
// set to the chain you want to interact with
const collectorClient = createCollectorClient({ chainId, publicClient });
const { parameters } = await collectorClient.mint({
// collection address to mint
tokenContract: "0x1234567890123456789012345678901234567890",
// quantity of tokens to mint
quantityToMint: 5,
// can be set to 1155, 721, or premint
mintType: "1155721premint",
});
const { writeContract } = useWriteContract();
//Clicking the button writes the mint transaction to the network
<button onClick={() => writeContract(parameters)}>Create</button>
Mint Premints
Once a Premint has been created and submitted to the Zora Premint API, it can be collected by calling mint()
with mintType
set to premint
, and the uid
set to the premint's uid:
import { createCollectorClient } from "@zoralabs/protocol-sdk";
import { collection, uid } from "./createPremint";
import { useChainId, usePublicClient, useWriteContract } from "wagmi";
const chainId = useChainId();
const publicClient = usePublicClient()!;
// initialize the collect sdk with the chain configuration
const collectorClient = createCollectorClient({ chainId, publicClient });
// get parameters to mint a premint, which can be used to simulate and submit the transaction
const { parameters } = await collectorClient.mint({
// the deterministic premint collection address
tokenContract: collection,
// type of item to mint
mintType: "premint",
// the uid of the premint to mint
uid,
// how many tokens to mint
quantityToMint: 3,
// Comment to attach to the mint
mintComment: "Mint comment",
// the account to execute the transaction
minterAccount: "0xf69fEc6d858c77e969509843852178bd24CAd2B6",
});
const { writeContract } = useWriteContract();
writeContract(parameters);
Mint 1155s
1155s can be minted by calling mint()
with mintType
set to 1155
, and the tokenId
set to the token id to mint:
import {
useAccount,
useChainId,
usePublicClient,
useWriteContract,
} from "wagmi";
import { createCollectorClient } from "@zoralabs/protocol-sdk";
const chainId = useChainId();
const publicClient = usePublicClient()!;
const { address } = useAccount();
const collectorClient = createCollectorClient({ chainId, publicClient });
// prepare the mint transaction
const { parameters } = await collectorClient.mint({
// 1155 contract address
tokenContract: "0xCD860870DE521cDb0C5ae89E80bBa057Cd30Bf8C",
// type of item to mint
mintType: "1155",
// 1155 token id to mint
tokenId: 1n,
// quantity of tokens to mint
quantityToMint: 3,
// optional comment to include with the mint
mintComment: "My comment",
// optional address that will receive a mint referral reward
mintReferral: "0x0C8596Ee50e06Ce710237c9c905D4aB63A132207",
// account that is to invoke the mint transaction
minterAccount: address!,
});
const { writeContract } = useWriteContract();
// write the mint transaction to the network
writeContract(parameters);
Mint ERC20 based 1155s
When minting ERC20 based 1155s, an additional approval to transfer ERC20s to an address must be executed before minting.
In the case that the 1155 to mint is an ERC20 based mint, the mint
function will return an erc20Approval
which contains
information about which ERC20 token to approve, the amount to approve, and the address to approve to.
Before executing the mint transaction, the approval transaction must be executed:
import { publicClient, walletClient, chainId, minterAccount } from "./config";
import { createCollectorClient } from "@zoralabs/protocol-sdk";
import { erc20Abi } from "viem";
const collectorClient = createCollectorClient({ chainId, publicClient });
// prepare the mint transaction. If it is an erc20 based mint, the erc20Approval object will be returned
// with info for necessary erc20 approvals
const { parameters, erc20Approval } = await collectorClient.mint({
tokenContract: "0xCD860870DE521cDb0C5ae89E80bBa057Cd30Bf8C",
mintType: "1155",
tokenId: 1n,
quantityToMint: 3,
mintComment: "My comment",
minterAccount,
});
// request necessary erc20 approvals as returned from the sdk's mint call
const approveHash = await walletClient.writeContract({
abi: erc20Abi,
address: erc20Approval!.erc20,
functionName: "approve",
args: [erc20Approval!.approveTo, erc20Approval!.quantity],
account: minterAccount,
});
const receipt = await publicClient.waitForTransactionReceipt({
hash: approveHash,
});
if (receipt.status !== "success") {
throw new Error("ERC20 Approval failed");
}
// once the erc20 approval is successful, write the mint transaction to the network
const mintHash = await walletClient.writeContract(parameters);
const mintReceipt = await publicClient.waitForTransactionReceipt({
hash: mintHash,
});
if (mintReceipt.status !== "success") {
throw new Error("Mint failed");
}
Mint 721s
721s can be minted by calling mint()
with mintType
set to 721
. No tokenId
is set as the token id is determined by auto-incrementing token ids; one tokenId will be created & minted for each quantityToMint
:
import { createCollectorClient } from "@zoralabs/protocol-sdk";
import { publicClient, walletClient, chainId, minterAccount } from "./config";
const collectorClient = createCollectorClient({ chainId, publicClient });
// prepare the mint transaction, which can be simulated via an rpc with the public client.
const { parameters } = await collectorClient.mint({
// 1155 contract address
tokenContract: "0x7aae7e67515A2CbB8585C707Ca6db37BDd3EA839",
// type of item to mint
mintType: "721",
// quantity of tokens to mint
quantityToMint: 3,
// optional comment to include with the mint
mintComment: "My comment",
// optional address that will receive a mint referral reward
mintReferral: "0x0C8596Ee50e06Ce710237c9c905D4aB63A132207",
// account that is to invoke the mint transaction
minterAccount: minterAccount,
});
// execute the transaction
await walletClient.writeContract(parameters);
Earning Mint Referral Rewards
To earn a mint referral reward, pass in the mintReferral
argument to the mint
function:
import { collection, uid } from "./createPremint";
import {
useAccount,
useChainId,
usePublicClient,
useWriteContract,
} from "wagmi";
import { createCollectorClient } from "@zoralabs/protocol-sdk";
const chainId = useChainId();
const publicClient = usePublicClient()!;
const { address } = useAccount();
const collectorClient = createCollectorClient({ chainId, publicClient });
const { parameters } = await collectorClient.mint({
tokenContract: collection,
mintType: "premint",
uid,
quantityToMint: 3,
minterAccount: address!,
// mintReferral address will get mint referral reward
mintReferral: "0x64B585fabf03B932D637B15112cDa02C77b5cef9",
});
const { writeContract } = useWriteContract();
writeContract(parameters);