getToken
The Collector Client can be used to get token 1155s, 721s, and premints with
a function getToken()
. The type of token get 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. The getToken()
function returns both information about the token, in the token
object, and a function
prepareMint
which takes a quantity to mint and returns the transaction parameters to mint the token, costs to mint the token, and any necessary
erc20 approvals that must be executed before minting the token.
Usage
import {
useChainId,
usePublicClient,
useAccount,
useWriteContract
} from 'wagmi';
import { createCollectorClient } from "@zoralabs/protocol-sdk";
const chainId = useChainId();
const publicClient = usePublicClient();
// initiate the collector client
const collectorClient = createCollectorClient({ chainId, publicClient });
// get the item that can be minted, and a function to prepare
// a mint transaction
const { token, prepareMint } = await collectorClient.getToken({
// contract address token belongs to
tokenContract: "0x1234567890123456789012345678901234567890",
// can be set to 1155, 721, or premint
mintType: "1155721premint",
});
// the function returns a `token` object
// containing information about the item that can be minted
token.contractcreatormaxSupplymintTypetokenURItotalMinted
// The `prepareMint` function can be used to prepare a transaction
// to mint x quantity of tokens to a recipient
const { address } = useAccount();
const { parameters, costs } = prepareMint({
minterAccount: address!,
quantityToMint: 3n
});
const { writeContract } = useWriteContract();
// When the button is clicked, the transaction
// to mint 3 tokens is written to the network
<button onClick={() => writeContract(parameters)}></button>
// costs to mint the 3 tokens can be retrieved
// from the returned `cost` object
costs.mintFeetotalCostEthtotalPurchaseCosttotalPurchaseCostCurrency
Minting the returned token
The getToken()
function returns a prepareMint function that can be used to prepare a mint transaction. The prepareMint
function takes a minterAccount
and quantityToMint
parameter and returns the transaction parameters to mint the token, costs to mint the token, and any necessary erc20 approvals that must be executed before minting the token.
The returned parameters
object can be passed to a WalletClient
's writeContract
function to mint the token.
import React from 'react';
// ---cut---
import {
useChainId,
usePublicClient,
useAccount,
useWriteContract
} from 'wagmi';
import { createCollectorClient } from "@zoralabs/protocol-sdk";
const chainId = useChainId();
const publicClient = usePublicClient()!;
const collectorClient = createCollectorClient({ chainId, publicClient });
// get the item that can be minted, and a function to prepare
// a mint transaction
const { prepareMint } = await collectorClient.getToken({
// contract address token belongs to
tokenContract: "0x1234567890123456789012345678901234567890",
// can be set to 1155, 721, or premint
mintType: "1155",
tokenId: 1n
});
// The `prepareMint` function can be used to prepare a transaction
// to mint x quantity of tokens to a recipient
const { address } = useAccount();
const { parameters, costs } = prepareMint({
minterAccount: address!,
quantityToMint: 3n
});
const { writeContract } = useWriteContract();
// When the button is clicked, the transaction
// to mint 3 tokens is written to the network
<button onClick={() => writeContract(parameters)}></button>
Parameters
import { type GetMintParameters } from '@zoralabs/protocol-sdk'
tokenContract
Address
The address of the contract that the token belongs to.
mintType
"1155" | "721" | "premint"
The type of the collection to get the token from.
tokenId
bigint | undefined
The token id to get, only applicable for 1155s.
uid
number | undefined
The uid to get, only applicable for premints.
preferredSaleType (optional)
"fixedPrice" | "erc20" | "allowlist" | "premint" | "timed"
Optional preferred sale type of the minter to use for the token, only applicable for onchain 1155s.
Usage Examples
For ERC1155 Mint
const params: GetMintParameters = {
tokenContract: "0x1234567890123456789012345678901234567890",
mintType: "1155",
tokenId: 1n
};
For ERC721 Mint
const params: GetMintParameters = {
tokenContract: "0x1234567890123456789012345678901234567890",
mintType: "721"
};
For Premint
const params: GetMintParameters = {
tokenContract: "0x1234567890123456789012345678901234567890",
mintType: "premint",
uid: 5
};
Return Type
import { type MintableReturn } from '@zoralabs/protocol-sdk'
Promise<MintableReturn>
Where MintableReturn
is defined as:
type MintableReturn = {
/** Token information */
token: SalesConfigAndTokenInfo;
/** If the primary mint is active, the end time of the primary mint, if there is an end time */
primaryMintEnd?: bigint;
/** If the secondary market is active */
secondaryMarketActive: boolean;
} & (
| {
primaryMintActive: true;
/** Function that takes a quantity of items to mint and returns a prepared transaction and the costs to mint that quantity. If the primary mint is not active, it will be undefined. */
prepareMint: PrepareMint;
}
| {
primaryMintActive: false;
prepareMint: undefined;
}
);
token
OnchainSalesConfigAndTokenInfo | PremintSalesConfigAndTokenInfo
An object containing information about the token that can be minted, including contract details, sales configuration, and token-specific information.
primaryMintActive
boolean
Indicates whether the primary mint is currently active.
primaryMintEnd
number | undefined
The timestamp when the primary mint ends, if applicable.
secondaryMarketActive
boolean
Indicates whether the secondary market is currently active. If so buy1155OnSecondary
and sell1155OnSecondary
will be active on the token.
prepareMint
PrepareMint | undefined
A function that prepares the mint transaction, if the primary mint is active. Otherwise, it is undefined.
import { type MintParametersBase, type PrepareMintReturn } from "@zoralabs/protocol-sdk";
type PrepareMint = (params: MintParametersBase) => PrepareMintReturn;
It takes a MintParametersBase
as the argument, which contains the following properties:
minterAccount
: The account that will execute the mint transaction.quantityToMint
: The quantity of tokens to mint. Defaults to 1.mintComment
: An optional comment to add to the mint transaction.mintReferral
: (optional) address that will receive the mint referral reward.mintRecipient
: (optional) address to receive the minted tokens. Defaults to the minting account.firstMinter
: (optional) address to receive the first minter reward, if the mint is a premint.allowListEntry
: (optional) allow list entry to use for the mint, if the mint is an allow list mint.
It returns a PrepareMintReturn
object with the following properties:
parameters
: The transaction parameters to mint the token.erc20Approval
: Any necessary ERC20 approvals that must be executed before minting the token (if applicable).costs
: The costs associated with minting the token.