Get NFTs by CandyMachineId always returning an empty array

608 views Asked by At

I am trying to write a script to pull NFTs by candy machine id, but it is either failing or returning an empty array each time.

I am using the genesysgo mainnet rpc.

Here is the relevant code.

const rpc = process.env.REACT_APP_RPC_HOST!;
const connection = new Connection(rpc);

export const CANDY_MACHINE_V2_PROGRAM = new anchor.web3.PublicKey(
  "cndy3Z4yapfJBmL3ShUp5exZKqR3z33thTzeNMm2gRZ"
);

export const TOKEN_METADATA_PROGRAM = new anchor.web3.PublicKey(
  "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
);

const MAX_NAME_LENGTH = 32;
const MAX_URI_LENGTH = 200;
const MAX_SYMBOL_LENGTH = 10;
const MAX_CREATOR_LEN = 32 + 1 + 1;
const MAX_CREATOR_LIMIT = 5;
const MAX_DATA_SIZE =
  4 +
  MAX_NAME_LENGTH +
  4 +
  MAX_SYMBOL_LENGTH +
  4 +
  MAX_URI_LENGTH +
  2 +
  1 +
  4 +
  MAX_CREATOR_LIMIT * MAX_CREATOR_LEN;

export const MAX_METADATA_LEN = 1 + 32 + 32 + MAX_DATA_SIZE + 1 + 1 + 9 + 172;
export const CREATOR_ARRAY_START =
  1 +
  32 +
  32 +
  4 +
  MAX_NAME_LENGTH +
  4 +
  MAX_URI_LENGTH +
  4 +
  MAX_SYMBOL_LENGTH +
  2 +
  1 +
  4;

async function getCandyMachineCreator(
  candyMachineId: PublicKey
): Promise<[PublicKey, number]> {
  return await PublicKey.findProgramAddress(
    [Buffer.from("candy_machine"), candyMachineId.toBuffer()],
    CANDY_MACHINE_V2_PROGRAM
  );
}

async function getMintsByCandyMachineId(candyMachineId: StringPublicKey) {
  const key = new PublicKey(candyMachineId);
  const [firstCreatorAddress] = await getCandyMachineCreator(key);
  const metadataAccounts = await connection.getProgramAccounts(
    TOKEN_METADATA_PROGRAM,
    {
      // The mint address is located at byte 33 and lasts for 32 bytes.
      dataSlice: { offset: 33, length: 32 },

      filters: [
        // Only get Metadata accounts.
        { dataSize: MAX_METADATA_LEN },

        // Filter using the first creator.
        {
          memcmp: {
            offset: CREATOR_ARRAY_START,
            bytes: firstCreatorAddress.toBase58(),
          },
        },
      ],
    }
  );

  return metadataAccounts.map((metadataAccountInfo) =>
    bs58.encode(metadataAccountInfo.account.data)
  );
}

I've also tried using Solana NFT Tools and Magic Eden's Hash List Tool. They also return empty arrays. The NFTs are on chain, they're in my wallet, so I'm kind of at a loss.

1

There are 1 answers

1
Sean O On BEST ANSWER

You are probably using the wrong CMID. Find one of your NFTs on solscan and use the first verified creator ID in the metadata as your CMID. (as seen below).

This would explain why the ID also returns an empty array on the magic eden and pentacles sites.

This address is not the same as the CMID in your .cache file. enter image description here