1

I am trying to mint NFTs from a candy machine v3 using the umi library as shown in this https://docs.metaplex.com/programs/candy-machine/minting . I am using the following code -

import React from 'react'
import { Metaplex, walletAdapterIdentity } from "@metaplex-foundation/js";
import { useConnection, useWallet } from "@solana/wallet-adapter-react";
import { useMemo, useState } from "react";
import {
 some,
 generateSigner,
 transactionBuilder,
 } from "@metaplex-foundation/umi";
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
import  {mintV2}  from "@metaplex-foundation/mpl-candy-machine";
import { setComputeUnitLimit } from "@metaplex-foundation/mpl-essentials";
import {
  Connection,
  } from "@solana/web3.js";

 const MintNFT = () => {
 const { publicKey, wallet } = useWallet();
 const [nft, setNft] = useState(null);
 const umi = useMemo(
    () => createUmi(process.env.NEXT_PUBLIC_SOLANA_RPC_HOST),
    []
   );
   const connection = useMemo(
   () => new Connection(process.env.NEXT_PUBLIC_SOLANA_RPC_HOST, "confirmed"),
[]
  );
       const candyMachineAddress = new PublicKey(
       process.env.NEXT_PUBLIC_CANDY_MACHINE_ID
      );
     const metaplex = useMemo(
  () => Metaplex.make(connection).use(walletAdapterIdentity(wallet)),
   [connection, wallet]
   );
  let candyMachine;
  let walletBalance;
 const onClick = async () => {
// Here the actual mint happens. Depending on the guards that you are using you have to run some pre validation beforehand
// Read more: https://docs.metaplex.com/programs/candy-machine/minting#minting-with-pre-validation
// read candy machine state from chain
candyMachine = await metaplex
  .candyMachines()
  .findByAddress({ address: candyMachineAddress });
console.log(candyMachine);
await transactionBuilder()
  .add(setComputeUnitLimit(umi, { units: 800_000 }))
  .add(
    mintV2(umi, {
      candyMachine: candyMachine.address,
      publicKey,
      collectionMint: candyMachine.collectionMintAddress,
      collectionUpdateAuthority: candyMachine.authorityAddress,
      mintArgs: {
        mintLimit: some({ id: 1 }),
        solPayment: some({ destination: candyMachine.authorityAddress }),
      },
    })
  )
  .sendAndConfirm(umi);
  };

  return (
   <div>
  <div>Here we mint the NFT</div>
  <button
    className="bg-blue-400 text-sm text-white rounded-full hover:bg-blue-600 m-0 mt-4 py-2 px-8"
    onClick={onClick}
    
  >
    mint NFT
  </button>
  {nft && (
    <div className={styles.nftPreview}>
      <h1>{nft.name}</h1>
      <img
        src={nft?.json?.image || "/fallbackImage.jpg"}
        alt="The downloaded illustration of the provided NFT address."
      />
    </div>
   )}
   </div>
   );
   }

   export default MintNFT

but on clicking the Mint NFT button I am getting the following error -
caught (in promise) ProgramNotRecognizedError: The provided program name [mplCandyGuard] is not recognized in the [devnet] cluster. Did you forget to register this program? If so, you may use "context.programs.add(myProgram)" to fix this.

Source: SDK Can somebody please suggest what is wrong and how to rectify the error?

1 Answer 1

4

Unfortunately I can not see in your code snippet how you initialise umi, but what you basically need to do to register the guard program is:

umi.use(mplCandyMachine())

In my personal mint ui implementation the umi definition currently looks like this

const umi = createUmi(endpoint) .use(walletAdapterIdentity(wallet)) .use(mplTokenMetadata()) .use(mplCandyMachine())

  • walletAdapterIdentity is needed for the browser wallets to work.
  • mplTokenMetadata To load the nft metadata after the mint.
  • mplCandyMachine to register the candy guard and candy machine programs

Please note, that you should not need @metaplex-foundation/js when using umi. All functions can be replaced with nether new modules. (This can be relevant for size / speed improvements)

2
  • I had the same issue as the OP, but for Fungible tokens. This is was because I didnt not use mplCandyMachine for the fungible token creation and mint. After adding it to both the token creation and mint, it worked. TO be honest, I dont know why mplCandyMachine is needed for Fungible tokens but keeping it for now Commented Mar 10 at 23:38
  • Candy Machine is NOT required for fungible tokens. It is a program to help minting and selling NFT. Commented Apr 3 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.