CAVYAR

CAVYAR AI

Mint Token in Anchor

Mint Token
Create Token
Create NFT
Mint NFT

Mint a token in Anchor using the mpl-token-metadata crate. This might not work for you if your version is < 3.0 (Example uses 3.0.2). You need to add this to enable init_if_needed to your Cargo.toml file: anchor-lang = { version = "x.xx.x", features = ["init-if-needed"] }

use anchor_lang::prelude::*; use anchor_spl::{ associated_token::AssociatedToken, token::{Mint, Token, TokenAccount}, }; use mpl_token_metadata::instructions::{MintV1Cpi, MintV1CpiAccounts, MintV1InstructionArgs}; #[derive(Accounts)] pub struct MintToken<'info> { #[account(mut)] pub authority: Signer<'info>, // The PDA is both the address of the mint account and the mint authority #[account( mut, seeds = [b"my-token-mint"], bump, )] pub my_token_mint: Account<'info, Mint>, ///CHECK: Using "address" constraint to validate metadata account address #[account( mut, seeds = [b"metadata", mpl_token_metadata::ID.as_ref(), my_token_mint.key().as_ref()], seeds::program = mpl_token_metadata::ID, bump, )] pub metadata_account: UncheckedAccount<'info>, #[account( init_if_needed, payer = authority, associated_token::mint = my_token_mint, associated_token::authority = authority, )] pub my_associated_token_account: Account<'info, TokenAccount>, //CHECK: checked in CPI pub sysvar_instructions: UncheckedAccount<'info>, pub associated_token_program: Program<'info, AssociatedToken>, pub token_program: Program<'info, Token>, pub system_program: Program<'info, System>, } pub fn mint_token(ctx: Context<MintToken>) -> Result<()> { // Confiugure these args as you wish let args = MintV1InstructionArgs { amount: 1, // Mint 1 token authorization_data: None, }; let authority = &ctx.accounts.authority.to_account_info(); let metadata = &ctx.accounts.metadata_account.to_account_info(); let my_token_mint = &ctx.accounts.my_token_mint.to_account_info(); let spl_token_program = &ctx.accounts.token_program.to_account_info(); let system_program = &ctx.accounts.system_program.to_account_info(); let sysvar_instructions = &ctx.accounts.sysvar_instructions.to_account_info(); let token = &ctx.accounts.my_associated_token_account.to_account_info(); let spl_ata_program = &ctx.accounts.my_associated_token_account.to_account_info(); // Configure the accounts as you need let mint_cpi = MintV1Cpi::new( spl_token_program, MintV1CpiAccounts { authority, metadata, mint: my_token_mint, payer: authority, spl_ata_program, spl_token_program, system_program, sysvar_instructions, token, authorization_rules: None, authorization_rules_program: None, delegate_record: None, master_edition: None, token_owner: None, token_record: None, }, args, ); // When using pda signer: // mint_cpi.invoke_signed() // When signing with authority: mint_cpi.invoke()?; Ok(()) }

CAVYAR AI

CAVYAR AI is in its earliest stages of development and the quality of generated descriptions will improve massively over time. At this stage, there is a risk of false information being generated.

The given code snippet is used to mint a token in Anchor using the mpl-token-metadata crate. It creates a new token and associates it with an associated token account.

Explanation:

  • The MintToken struct defines the accounts required for the minting process. It includes the authority account, the mint account, the metadata account, the associated token account, and various program accounts.
  • The mint_token function is the entry point for the minting process. It takes a Context<MintToken> as an argument and returns a Result<()>.
  • The args variable is used to configure the minting process. In this example, it specifies that 1 token should be minted and sets the authorization data to None.
  • The authority, metadata, my_token_mint, spl_token_program, system_program, sysvar_instructions, token, and spl_ata_program variables are used to reference the respective accounts in the MintV1CpiAccounts struct.
  • The mint_cpi variable is created using the MintV1Cpi::new function. It takes the necessary program accounts, the MintV1CpiAccounts struct, and the args as arguments.
  • Finally, the mint_cpi.invoke() function is called to invoke the minting process. This will mint the specified number of tokens and associate them with the associated token account.

Note: The code snippet assumes that the mpl-token-metadata crate is already imported and available in the project. Make sure to import the necessary dependencies and configure the Cargo.toml file accordingly.

Ask

Soon™

Created by:

Axax...GsG2

Give this snippet a feedback!

Published on

April 24, 2025

Want to support the author?