Wonka JS
is the easiest way to mint from Candy Machine and fetch NFTs through JS APIs. You can see an end to end example in Next.js demo project as well as debug using the command line testing tool.
For using Wonka's Solana NFT indexing APIs, checkout Windex Documentation.
Once you have followed the instructions to upload your NFTs, you can use functions below to build your mint flow:
mintCandyMachineToken(..)
getCandyMachineMints(..)
getCandyMachineState(...)
getMintMetadata(...)
updateMintImage(...)
These commands are useful if you need to build a custom facing front end, and don't want to rely on the Candy Machine Minting Site.
npm install @wonka-labs/wonka-js
Returns info about currently available mints in Candy Machine, how many were already minted, how long is left for the auction, etc.
const getCandyMachineState = async () => {
console.log("Getting candy machine state.")
const provider = ...;
const candyMachineId = process.env.REACT_APP_CANDY_MACHINE_ID!;
const wonka = new Wonka(provider, candyMachineId)
const candyMachineState = await wonka.getCandyMachineState()
console.log(candyMachineState)
}
Returns a list of all existing mints on the given candy machine.
const getCandyMachineMints = async() => {
console.log("Getting candy machine mints...")
const provider = ...;
const candyMachineId = process.env.REACT_APP_CANDY_MACHINE_ID!;
const wonka = new Wonka(provider, candyMachineId)
const candyMachineMints = await wonka.getCandyMachineMints()
console.log(candyMachineMints)
}
Mints an NFT; you either get an error with a message or the ID of the mint in return.
const mintCandyMachineToken = async(recipientWallet: PublicKey) => {
const provider = ...;
const candyMachineId = process.env.REACT_APP_CANDY_MACHINE_ID!;
const wonka = new Wonka(provider, candyMachineId)
const candyMachineMintId = await wonka.mintCandyMachineToken(recipientWallet)
console.log(candyMachineMintId)
}
Sometimes you need to load one particular NFT's metadata, here is how you can do it:
const getMintMetadata = async(mintAddress: string) => {
const provider = ...;
const candyMachineId = process.env.REACT_APP_CANDY_MACHINE_ID!;
const wonka = new Wonka(provider, candyMachineId)
const mintMetadata = await wonka.getMintMetadata(mintAddress)
console.log(`Fetched mint metadata:`);
console.log(mintMetadata);
// Can also fetch the data stored inside the metadata:
const metadataDataURIData = await fetch(mintMetadata.uri);
const metadataDataURIDataJSON = await metadataDataURIData.json();
console.log(`Fetched mint metadata's URI data:`);
console.log(metadataDataURIDataJSON);
}
This is a bit more advanced, but if you have a wallet with the update authority, you can actually update the NFT's png. Since this requires access to arweave's private key, it's probably better to create a backend API that uses these functions. Here is what's happening at a high-level:
- You create an ArweaveUploader with a private key that has enough money in it for uploading files.
- You create a wonka with a provider that's attached to a wallet that has permission to update the NFT's metadata.
- You give Wonka a base64 encoded PNG and a mint address. That's it!
const updateMintImage = async(mintAddress: PublicKey, b64image: string) => {
console.log("Getting mint metadata...")
const provider = ...;
const candyMachineId = process.env.REACT_APP_CANDY_MACHINE_ID!;
const wonka = new Wonka(provider, candyMachineId)
const arweaveUploader = new ArweaveUploader(process.env.ARWEAVE_KEY!);
const txn = await wonka.updateMintImage(
b64image,
arweaveUploader,
provider.wallet,
mintAddress,
{ image_updated: true }
);
}
The question is where to store information like candy machine ID, etc. If you're using React or Next.js, you can easily use the .env file so that the code above looks more like:
const candyMachineId = process.env.NEXT_PUBLIC_CANDY_MACHINE_ID!; // For Next.js
const candyMachineId = process.env.REACT_APP_CANDY_MACHINE_ID!; // For React
Read more about these in the docs: React .env and Next.js .env.
*
By default, fetching NFTs by Wallet, Collection, or ID requires fetching a series of Solana accounts and external JSON metadata, which can be slow and bandwidth intensive. The Wonka Index (windex) is a backend cache that enables blazing fast metadata fetches. You can use the following queries to easily fetch NFTs. For more details, visit WINDEX.md.
To display all NFTs in a collection, you can query Windex by Candy Machine ID or Collection ID (the collection id is the first verified creator in the NFT's metadata).
const fetchNFTsByCandyMachine = async(candyMachineId: PublicKey) => {
const nfts = await Windex.fetchNFTsByCandyMachineID(candyMachineId, 20, Windex.DEVNET_ENDPOINT);
console.log(`Retrieved ${nfts.length} NFTs!`);
}
const fetchNFTsByCollection = async(collectionId: PublicKey) => {
const nfts = await Windex.fetchNFTsByCollectionID(collectionId, 20, Windex.DEVNET_ENDPOINT);
console.log(`Retrieved ${nfts.length} NFTs!`);
}
const fetchNFTsByWallet = async(walletAddress: PublicKey) => {
const nfts = await Windex.fetchNFTsByWallet(walletAddress, 20, Windex.DEVNET_ENDPOINT);
console.log(`Retrieved ${nfts.length} NFTs in ${walletAddress}'s wallet!`);
}
const fetchNFTsByMintAddress = async(mintAddress: PublicKey) => {
const nft = await Windex.fetchNFTByMintAddress(mintAddress, Windex.DEVNET_ENDPOINT);
if (!nft) {
console.log("nft not found!");
} else {
console.log(`Fetched ${nft.address}: ${nft.name}`);
}
}