1

I'm using anchor framework and I need to check whether an account holds a given NFT or not. According to this answer I can perform this check using mpl-token-metadata crate, but the answer does not work for me. I use mpl-token-metadata = "4.0.0" and id and state are not part of this crate (I've searched for them in docs and nothing was found). Though I'm not sure the only solution is using mpl-token-metadata.

I really appreciate any helpful comment or answer.

1 Answer 1

1

My approach would be to leverage Anchor Account Structs: This is assuming your nft is from a certain collection. Otherwise you can get rid of collection_mint acc and the collection constraints in the metadata account.

    #[account(mut)]
    owner: Signer<'info>,
    nft: InterfaceAccount<'info, Mint>,
    collection_mint: InterfaceAccount<'info, Mint>,
    #[account(
    associated_token::mint = nft,
    associated_token::authority = owner
    )]
    owner_ata: InterfaceAccount<'info, TokenAccount>,
    #[account(
    seeds = [
        b"metadata",
        metadata_program.key().as_ref(),
        nft.key().as_ref()
    ],
    seeds::program = metadata_program.key(),
    bump,
    constraint = metadata.collection.as_ref().unwrap().key.as_ref() == 
    collection_mint.key().as_ref(),
    constraint = metadata.collection.as_ref().unwrap().verified == true,
    )]
    metadata: Account<'info, MetadataAccount>,
    #[account(
    seeds = [
        b"metadata",
        metadata_program.key().as_ref(),
        nft.key().as_ref(),
        b"edition"
    ],
    seeds::program = metadata_program.key(),
    bump,
     )]
    master_edition: Account<'info, MasterEditionAccount>,
    metadata_program: Program<'info, Metadata>,
    token_program: Interface<'info, TokenInterface>,
    associated_token_program: Program<'info, AssociatedToken>,
    system_program: Program<'info, System>

Make sure you import:

   use anchor_spl::{
   token_interface::{TokenAccount, Mint, TokenInterface}, 
   metadata::{Metadata, MetadataAccount, MasterEditionAccount}, 
   associated_token::AssociatedToken
   };

Make sure you are using the metadata feature.

 anchor-spl = { version = "0.29.0", features = ["metadata"] }

Have a nice day

17
  • Thanks for your response, would you please explain a bit your code? I can't see any function in your code, there are only struct definition if I'm not mistaken. Commented Jan 20 at 19:43
  • This should be the accounts needed for your validations. You can do an impl and in ur function u can call this impl. Basically all this accounts allows you to even check if that nft is from specific collection. But if you dont need all that you can use the master_edition without the collection accounts should be enough. Commented Jan 20 at 21:37
  • *this is my approach, probably some has_one constraint would be enough ahahah Commented Jan 20 at 21:46
  • You mean I should use it as an Account struct? I can't understand how can I use it for my validation. I simply want a function receives an account and a NFT and tells me true or false (whether the account is holder of the NFT or not) Commented Jan 22 at 15:13
  • Yes you should do your validations leveraging the anchor account structs. is this nft from a given collection/editions? or a single Mastereditions? Commented Jan 23 at 0:15

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.