简体   繁体   English

获取 NFT 集合的所有元数据

[英]Fetch all the metadata of an NFT collection

On Solana, we can obtain all tokens owned by a public key thanks to that method:在 Solana 上,由于该方法,我们可以获得公钥拥有的所有令牌:

connection
.getParsedTokenAccountsByOwner(
  new PublicKey(publicKey.toBase58()),
  {
    programId: TOKEN_PROGRAM_ID
  }
)
.then((b) => {
  const owner = b?.value?.[0].account.owner;
  const pb = b?.value?.[0].pubkey;

  const nonZeroAccounts = b?.value?.filter(
    (obj) => obj.account.data.parsed.info.tokenAmount.uiAmount > 0
  );
  setTokens(JSON.stringify(nonZeroAccounts, null, 2));

and we can retrieve the metadata of that token using this:我们可以使用以下方法检索该令牌的元数据:

import * as metadata from "@metaplex-foundation/mpl-token-metadata";
const nftsmetadata:metadata.MetadataData[] = await metadata.Metadata.findDataByOwner(connection, publicKey);

But before we can access this metadata, the token need to be minted.但在我们可以访问此元数据之前,需要创建令牌。 Is there a way to retrieve all the metadata (minted NFTs and unminted NFTs) of a collection using the CandyMachine ID or Token Metadata Program?有没有办法使用 CandyMachine ID 或令牌元数据程序检索集合的所有元数据(铸造的 NFT 和未铸造的 NFT)?

Replacing ENTER_YOUR_CANDY_MACHINE_ID_HERE with the candy machine you want info from, this will get all the mints associated.ENTER_YOUR_CANDY_MACHINE_ID_HERE替换为您要从中获取信息的糖果机,这将获得所有关联的薄荷糖。

import { Connection, clusterApiUrl, PublicKey } from '@solana/web3.js';
import bs58 from 'bs58';

const connection = new Connection(clusterApiUrl('mainnet-beta'));
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;
const MAX_METADATA_LEN = 1 + 32 + 32 + MAX_DATA_SIZE + 1 + 1 + 9 + 172;
const CREATOR_ARRAY_START = 1 + 32 + 32 + 4 + MAX_NAME_LENGTH + 4 + MAX_URI_LENGTH + 4 + MAX_SYMBOL_LENGTH + 2 + 1 + 4;

const TOKEN_METADATA_PROGRAM = new PublicKey('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s');
const CANDY_MACHINE_V2_PROGRAM = new PublicKey('cndy3Z4yapfJBmL3ShUp5exZKqR3z33thTzeNMm2gRZ');
const candyMachineId = new PublicKey('ENTER_YOUR_CANDY_MACHINE_ID_HERE');

const getMintAddresses = async (firstCreatorAddress: PublicKey) => {
  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)
  ));
};

const getCandyMachineCreator = async (candyMachine: PublicKey): Promise<[PublicKey, number]> => (
  PublicKey.findProgramAddress(
    [Buffer.from('candy_machine'), candyMachine.toBuffer()],
    CANDY_MACHINE_V2_PROGRAM,
  )
);

(async () => {

  const candyMachineCreator = await getCandyMachineCreator(candyMachineId);
  getMintAddresses(candyMachineCreator[0]);

})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM