通过 CandyMachineId 获取 NFT 总是返回空数组

发布于 2025-01-12 10:48:52 字数 2312 浏览 0 评论 0原文

我正在尝试编写一个脚本来通过糖果机 ID 提取 NFT,但它要么失败,要么每次都返回一个空数组。

我正在使用 Genesysgo 主网 rpc。

这是相关代码。

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)
  );
}

我还尝试过使用 Solana NFT ToolsMagic Eden 的哈希列表工具。它们还返回空数组。 NFT 是在链上的,它们在我的钱包里,所以我有点不知所措。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2025-01-19 10:48:52

您可能使用了错误的 CMID。在 solscan 上找到您的一个 NFT,并使用元数据中第一个经过验证的创建者 ID 作为您的 CMID。 (如下所示)。

这可以解释为什么 ID 在 magic eden 和 pentacles 站点上也返回一个空数组。

此地址与 .cache 文件中的 CMID 不同。
输入图片此处描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文