I’m working on building a custom marketplace contract using Anchor and I need to extract specific metadata from NFTs. I’ve attempted a couple of approaches but haven’t been successful yet.
First, I tried accessing the token account data directly, but that didn’t contain the metadata I needed. Then I attempted to parse the token.to_account_info().data by deserializing it into a metadata structure using mpl_token_metadata::state::Metadata, but this approach resulted in deserialization errors.
What I specifically need to retrieve are:
- The
royaltyPercentagefield - The creator information from the metadata
Additionally, I’m wondering if it’s possible to modify the primarySaleHappened flag programmatically? This would be useful for my auction system to track initial sales versus secondary market transactions.
use anchor_lang::prelude::*;
use mpl_token_metadata::state::MetadataAccount;
#[derive(Accounts)]
pub struct ProcessNftSale<'info> {
pub nft_token: Account<'info, TokenAccount>,
pub metadata_account: AccountInfo<'info>,
pub buyer: Signer<'info>,
}
pub fn handle_nft_purchase(ctx: Context<ProcessNftSale>) -> Result<()> {
// Need to access metadata here
// let metadata_info = ???;
Ok(())
}
Any guidance on the correct way to read and potentially update NFT metadata would be greatly appreciated.