1

I'm trying to mint an NFT using Anchor but the current docs are out of date. Does anyone have a simple example?

2 Answers 2

2

Hey there with anchor you can add mpl-token-metadata to your cargo.

mpl-token-metadata = { version = "*"}

make sure you import:

use mpl_token_metadata::{
    state::{Creator, Collection},
    instruction::{create_metadata_accounts_v3, create_master_edition_v3}
};

So a very basic example would be:

...
#[account(mut)]
initializer: Signer<'info>,
#[account(
    init,
    payer = initializer,
    mint::authority = auth,
    mint::freeze_authority = auth,
    mint::decimals = 0
)]
nft: InterfaceAccount<'info, Mint>
#[account(
    init,
    payer = initializer,
    associated_token::mint = nft,
    associated_token::authority = initializer,
)]
nft_ata: InterfaceAccount<'info, TokenAccount>,
#[account(mut)]
///CHECK: account is initialized in the ix
metadata: UncheckedAccount<'info>,
#[account(mut)]
///CHECK: account is initialized in the ix
master_edition: UncheckedAccount<'info>,
#[account(
    mut,
    seeds=[b"auth", config.key().as_ref()],
    bump = config.auth_bump
)]
///CHECK: This is safe. It's just used to sign things
auth: UncheckedAccount<'info>,
mpl_program: Program<'info, Metadata>,
....


impl<'info> ...<'info> {
    pub fn issue_nft(
    &self
) -> Result<()> {
    let accounts = MintTo {
        mint: self.nft.to_account_info(),
        to: self.nft_ata.to_account_info(),
        authority: self.auth.to_account_info()
    };

    let seeds = &[
        &b"auth"[..],
        &self.config.key().to_bytes()[..],
        &[self.config.auth_bump],
    ];

    let signer_seeds = &[&seeds[..]];

    let ctx = CpiContext::new_with_signer(
        self.system_program.to_account_info(),
        accounts,
        signer_seeds
    );

// Mint NFT
    mint_to(ctx, 1)
}

// Create Metadata
    let create_metadata_ix = create_metadata_accounts_v3(
.....
//In case you have a collection and you added a collection acc
Some(Collection { verified: false, key: self.collection.key() }),
...


 // Create Master Edition
    let create_master_edition_ix = create_master_edition_v3(
....

If you want to do a collection, you would need to add a collection account To the account struct. Mint this collection nft, create the metadata and master edition( its existence is proof of the Non-Fungibility of the token). Then after minting the nfts you need to pass the collection account, set the collection when creating the metadata.

2

I've created a simple localhost example repo.

Specific setups have been broken down into separate commits.

Note: It uses older mpl-token-metadata (v1.13.1) and has the NFT metadata JSON stored on GitHub as centralized storage for modification.

Open to PRs to get this to use latest version!

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.