getMetaplexCollection method

Future<List<ProgramAccount>> getMetaplexCollection(
  1. Pubkey collectionMint, {
  2. required int numberOfCreators,
  3. AccountEncoding encoding = AccountEncoding.base64,
  4. DataSlice? dataSlice,
  5. int? minContextSlot,
})

Returns all token Metadata accounts in collectionMint.

The metadata account structure can be found here.

The numberOfCreators must include the candy machine creator (if any). For example, if your collection has 2 creators and you've used candy machine, numberOfCreators will be 3.

Implementation

Future<List<ProgramAccount>> getMetaplexCollection(
  final Pubkey collectionMint, {
  required final int numberOfCreators,
  final AccountEncoding encoding = AccountEncoding.base64,
  final DataSlice? dataSlice,
  final int? minContextSlot,
}) async {
  const int maxCreators = 5;
  assert(numberOfCreators >= 0 && numberOfCreators <= maxCreators);

  // See https://docs.metaplex.com/programs/token-metadata/accounts#metadata fields for data sizes
  // and offsets.
  const int keySize = 1;
  const int updateAuthoritySize = 32;
  const int mintSize = 32;
  const int nameSize = 32;
  const int symbolSize = 10;
  const int uriSize = 200;
  const int sellerFeeBasisPointsSize = 2;
  const int creatorsAddressSize = 32;
  const int creatorsVerifiedSize = 1;
  const int creatorsShareSize = 1;
  const int primarySaleHappenedSize = 1;
  const int isMutableSize = 1;
  const int editionNonceSize = 1;
  const int tokenStandardSize = 1;
  const int collectionVerifiedSize = 1;
  const int collectionKeySize = 32;
  const int usesUseMethodSize = 1;
  const int usesRemainingSize = 8;
  const int usesTotalSize = 8;
  const int collectionDetailsSize = 9;
  const int programmableConfigSize = 34;

  const int encodedLengthSize = 4;
  const int optionFlagSize = 1;

  const int creatorsOffset = keySize +
      updateAuthoritySize +
      mintSize +
      encodedLengthSize +
      nameSize +
      encodedLengthSize +
      symbolSize +
      encodedLengthSize +
      uriSize +
      sellerFeeBasisPointsSize +
      optionFlagSize +
      encodedLengthSize; // Creators array option flag (1) + encoded length (4).

  const int creatorsSize =
      creatorsAddressSize + creatorsVerifiedSize + creatorsShareSize;
  final int currentCreatorsSize = numberOfCreators * creatorsSize;
  const int maxCreatorsSize = maxCreators * creatorsSize;

  final int collectionOffset = creatorsOffset +
      currentCreatorsSize +
      primarySaleHappenedSize +
      isMutableSize +
      optionFlagSize +
      editionNonceSize +
      optionFlagSize +
      tokenStandardSize +
      optionFlagSize; // Collection field's option flag (1).

  final int maxDataSize = collectionOffset +
      collectionVerifiedSize +
      collectionKeySize +
      optionFlagSize +
      usesUseMethodSize +
      usesRemainingSize +
      usesTotalSize +
      optionFlagSize +
      collectionDetailsSize +
      optionFlagSize +
      programmableConfigSize +
      80 +
      (maxCreatorsSize -
          currentCreatorsSize); // padding (80) + offset adjustment.

  return getProgramAccounts(
    MetaplexTokenMetadataProgram.programId,
    config: GetProgramAccountsConfig(
      commitment: commitment,
      encoding: encoding,
      dataSlice: dataSlice,
      minContextSlot: minContextSlot,
      filters: [
        DataSize(
          dataSize: maxDataSize,
        ),
        MemCmp(
          offset: collectionOffset + collectionVerifiedSize,
          bytes: collectionMint.toBase58(),
        ),
      ],
    ),
  );
}