Retrieving the most recent NFT minted on a candy machine

I’m trying to show users the NFT they just minted. How can I do this?

I’m using Metaplex’s Candy Machine UI. The mintOneToken function only gives me a confirmation code. That’s not enough info.

const result = await mintOneToken(candyMachine, userWallet.publicKey);
console.log(result); // Just shows 'confirmed'

I tried getting all NFTs owned by the user’s wallet. But it seems this method doesn’t catch the newest one right away.

async function getNFTs() {
  if (userWallet.isConnected) {
    const owner = userWallet.publicKey;
    const nftData = await Metadata.findDataByOwner(connection, owner);
    
    if (nftData.length > 0) {
      console.log(nftData[nftData.length - 1]);
    }
  }
}

How can I get the latest NFT info to show users? Should I add a delay? Any ideas?

hey there, i’ve been messing around with nft minting too and ran into similar headaches! :sweat_smile: have you tried using the getProgramAccounts method from the Solana web3.js library? it might help you grab that fresh nft data quicker. something like this could work:

const { Connection, PublicKey } = require('@solana/web3.js');
const { programs } = require('@metaplex/js');

async function getLatestNFT(connection, walletAddress) {
  const tokenAccounts = await connection.getProgramAccounts(
    new PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'),
    {
      filters: [
        {
          dataSize: 165,  // size of token account
        },
        {
          memcmp: {
            offset: 32,
            bytes: walletAddress.toBase58(),
          },
        },
      ],
    }
  );

  // Sort accounts by creation time (newest first)
  tokenAccounts.sort((a, b) => b.account.data.readUInt64LE(0) - a.account.data.readUInt64LE(0));

  // Get metadata for the newest token
  const latestToken = tokenAccounts[0];
  const tokenMint = new PublicKey(latestToken.account.data.slice(0, 32));
  const tokenMetadata = await programs.metadata.Metadata.findByMint(connection, tokenMint);

  return tokenMetadata;
}

this should grab the latest nft pretty quickly after minting. what do you think? have you tried anything like this before?

yo, i feel ur pain! :sweat_smile: have u tried using the getAssetsByOwner method from @metaplex-foundation/js? it’s pretty sweet for grabbing the latest nft. something like:

const assets = await metaplex.nfts().findAllByOwner({ owner: wallet.publicKey });
const latestNFT = assets[assets.length - 1];

this might catch that fresh mint faster than other methods. worth a shot, right?

I’ve encountered a similar issue when working with Candy Machine mints. One effective approach is to leverage the getMintInfo function from the Metaplex SDK. After minting, you can use the mint address returned by mintOneToken to fetch the NFT details:

const mintResult = await mintOneToken(candyMachine, userWallet.publicKey);
const mintAddress = mintResult.mintAddress;
const nftInfo = await getMintInfo(connection, mintAddress);

This method retrieves the most up-to-date information about the newly minted NFT, including its metadata. It’s more reliable than querying all NFTs owned by the wallet, which can indeed have delays in reflecting the latest mint.

Remember to handle potential errors and add appropriate loading states in your UI while fetching the NFT info. This approach should give you the immediate feedback you’re looking for without resorting to arbitrary delays.