So I want to be able to read the fields of an NFT metadata account from within my instruction logic.

I'm passing in the NFT as an unchecked account in my accounts context struct like so (note the collection_mint and collection_metadata fields):

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

        payer = payer,
        space = 8 + Pair::SIZE
    pub pair: Account<'info, Pair>,

    /// CHECK: validated in access control logic
    pub nft_collection_mint: UncheckedAccount<'info>,
    /// CHECK: validated in access control logic
    pub nft_collection_metadata: UncheckedAccount<'info>,

    pub quote_token_mint: Account<'info, Mint>,

        payer = payer,
        token::mint = quote_token_mint,
        token::authority = program_as_signer,
        seeds = [b"quote", pair.key().as_ref()],
    pub quote_token_vault: Account<'info, TokenAccount>,

    pub system_program: Program<'info, System>,
    pub token_program: Program<'info, Token>,
    pub rent: Sysvar<'info, Rent>,
    pub token_metadata: Program<'info, TokenMetadata>,

    #[account(seeds = [b"program", b"signer"], bump)]
    pub program_as_signer: UncheckedAccount<'info>,

Assume I know that the collection_metadata is valid and exists (i have not included that logic here to keep this short), I want to read the collection and collection_details fields on the Metadata account. Not sure how to access this. I imagine I need to deserialize the account data somehow?

1 Answer 1


Let's assume that you have correctly implemented all the logic to ensure that nft_collection_metadata is a Metadata account from the provided mint. So in order to decode this Metadata account you will need to add the following dependency to your anchor.TOML file:

mpl-token-metadata={version = "1.3.4", features = [ "no-entrypoint" ]}

Then you will need to use the Metadata state in order to transform the UncheckedAccount into Metadata account, so add this to your code:

use mpl_token_metadata::state::{TokenMetadataAccount, Metadata};

Note here that you need to add TokenMetadataAccount too, because this state has the deserialization implementation that you will use.

Finally, you just have to use the following code to get the metadata account:

let metadata: Metadata = Metadata::from_account_info(&ctx.accounts.nft_collection_metadata.to_account_info())?;

Note that this is has to be done on the instructions. Also, if you want to use metadata.collection or metadata.collection_details you need to make sure that those fields are not null.

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.