I would like to store the total supply of an NFT collection on-chain. However it seems this value is not in the collection metadata. How can I then obtain this value on-chain ?

1 Answer 1


On a Anchor program, using metadata account the details are stored inside the optional field collection_details

use anchor_lang::prelude::*;
use anchor_spl::metadata::{
    mpl_token_metadata::accounts::Metadata as MplMetadata, MasterEditionAccount, Metadata,

pub mod check_metadata {

    use super::*;

    pub fn get_collection_details(ctx: Context<CheckMetadata>) -> Result<()> {
       let size = ctx.accounts.metadata_account.collection_details.unwrap().

    let CollectionDetails::V1 { size } = ctx.accounts.token_metadata_account.collection_details.as_ref().unwrap();

pub struct CheckMetadata<'info> {
    pub payer: Signer<'info>,

        address = MplMetadata::find_pda(&mint.key()).0
    pub metadata_account: Box<Account<'info, MetadataAccount>>,

    pub system_program: Program<'info, System>,
  • Before approving this response, is it optional in the sense that a collection nft will necessarily have the collection details or is it up to the dev to set the collection details. i.e could there be a collection with no collection details
    – qubitz
    Commented May 31 at 18:33
  • 1
    not really. It's optional because the metadata program handles single-asset metadata accounts and the collection fields will be None and set when the creator mints a collection. But the authority can manipulate the field.
    – Jimii
    Commented May 31 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.