Solving Anchor NFT creation: 'mint' not recognized in Struct initialization

Hey folks, I’m stuck on an Anchor NFT project. I’m trying to set up a struct for NFT initialization, but I’m running into compiler errors. The problem is with the mint reference in the metadata and master edition account addresses. Here’s what I’ve got:

#[derive(Accounts)]
pub struct MintNFT<'info> {
    #[account(mut, signer)]
    pub creator: AccountInfo<'info>,
    #[account(
        init,
        payer = creator,
        mint::decimals = 0,
        mint::authority = creator.key(),
        mint::freeze_authority = creator.key(),
    )]
    pub token_mint: Account<'info, Mint>,
    // ... other fields ...
    #[account(
        mut,
        address=get_metadata_address(&token_mint.key()).0,
    )]
    pub nft_metadata: AccountInfo<'info>,
    #[account(
        mut,
        address=get_edition_address(&token_mint.key()).0,
    )]
    pub nft_edition: AccountInfo<'info>,
    // ... rest of the struct ...
}

The compiler’s complaining that it can’t find mint in scope for those address lines. I’ve double-checked my imports, but no luck. Any ideas on how to fix this? Maybe I’m missing something obvious?

I encountered a similar issue when working on an Anchor NFT project. The problem lies in how you’re referencing the mint within the address constraints. Instead of using mint, you should use the account name directly.

Try modifying your code like this:

#[account(
    mut,
    address = get_metadata_address(&token_mint.key()).0,
)]
pub nft_metadata: AccountInfo<'info>,

#[account(
    mut,
    address = get_edition_address(&token_mint.key()).0,
)]
pub nft_edition: AccountInfo<'info>,

This should resolve the compiler errors you’re experiencing. The mint reference is only used within the init macro for the token_mint account. For other accounts, you directly use the account name when calculating addresses.

Also, ensure that your get_metadata_address and get_edition_address functions are correctly implemented and imported. These are crucial for properly deriving the PDA addresses for metadata and edition accounts.

hey ray84, i had the same headache when i started with anchor nfts. the issue is probably with how ur referencing the mint. try using token_mint.key() directly in ur address constraints instead of mint. also, double check that get_metadata_address and get_edition_address are imported correctly. that should fix it!

oh hey ray84! i’ve been there with anchor nfts, they can be tricky. :upside_down_face: have you considered using the mpl-token-metadata crate? it’s got some handy functions that might make your life easier. something like:

use mpl_token_metadata::state::Metadata;

#[account(
    mut,
    address = Metadata::find_pda(&token_mint.key()).0
)]
pub nft_metadata: AccountInfo<'info>,

this approach is a bit cleaner and less error-prone. plus, it keeps things more standardized if you’re working with metaplex stuff.

btw, what kind of nft project are you working on? sounds interesting! :blush: