CAVYAR

CAVYAR AI

Create Mint in Anchor

Create Mint
Mint
Create NFT

Create a Mint 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)

use anchor_lang::prelude::*; use anchor_spl::token::{Mint, Token}; use mpl_token_metadata::{ instructions::{CreateV1Cpi, CreateV1CpiAccounts, CreateV1InstructionArgs}, types::{PrintSupply, TokenStandard}, }; #[derive(Accounts)] pub struct CreateMint<'info> { #[account(mut)] pub authority: Signer<'info>, // The PDA is both the address of the mint account and the mint authority #[account( init, seeds = [b"my-token-mint"], bump, payer = authority, mint::decimals = 9, mint::authority = my_token_mint, )] 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>, pub token_program: Program<'info, Token>, pub system_program: Program<'info, System>, pub rent: Sysvar<'info, Rent>, //CHECK: checked in CPI pub sysvar_instructions: UncheckedAccount<'info>, } pub fn create_mint( ctx: Context<CreateMint>, name: String, symbol: String, uri: String, ) -> Result<()> { // Confiugure these args as you wish let args = CreateV1InstructionArgs { name, symbol, uri, seller_fee_basis_points: 500, primary_sale_happened: false, is_mutable: true, token_standard: TokenStandard::ProgrammableNonFungible, collection: None, uses: None, collection_details: None, creators: None, rule_set: None, decimals: Some(0), print_supply: Some(PrintSupply::Zero), }; let token_program = &ctx.accounts.token_program.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 authority = &ctx.accounts.authority.to_account_info(); let system_program = &ctx.accounts.system_program.to_account_info(); let sysvar_instructions = &ctx.accounts.sysvar_instructions.to_account_info(); let create_cpi = CreateV1Cpi::new( &token_program, CreateV1CpiAccounts { metadata: metadata, master_edition: None, mint: (my_token_mint, true), authority: authority, payer: authority, update_authority: (authority, true), system_program: system_program, sysvar_instructions: sysvar_instructions, spl_token_program: token_program, }, args, ); // When using pda signer: // create_cpi.invoke_signed() // When signing with authority: create_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 an implementation of a function called create_mint that creates a new mint in the Anchor framework using the mpl-token-metadata crate. It takes in four parameters: name, symbol, uri, and authority.

Here's a breakdown of what the code does:

  1. It defines a struct called CreateMint that represents the accounts required for the mint creation process. The struct contains the following fields:

    • authority: A mutable signer account that represents the authority for the mint.
    • my_token_mint: An initialized account representing the mint itself. It is created using the init attribute and is associated with the PDA (Program Derived Address) my-token-mint.
    • metadata_account: A mutable unchecked account that represents the metadata account associated with the mint. It is associated with the PDA derived from the seed [b"metadata", mpl_token_metadata::ID.as_ref(), my_token_mint.key().as_ref()].
    • token_program: A program account representing the SPL token program.
    • system_program: A program account representing the Solana system program.
    • rent: A sysvar account representing the rent sysvar.
    • sysvar_instructions: An unchecked account representing the sysvar instructions account.
  2. The create_mint function is defined with the Context<CreateMint> context and the four input parameters: name, symbol, uri, and authority.

  3. Inside the function, a CreateV1InstructionArgs struct is created with the provided input parameters and other configuration values.

  4. The necessary accounts for the CPI (Cross-Program Invocation) are extracted from the ctx context using the to_account_info() method.

  5. A CreateV1Cpi instance is created with the extracted accounts, the args struct, and the token program.

  6. Finally, the invoke() method is called on the create_cpi instance to invoke the CPI and create the mint.

In summary, the given code creates a new mint in the Anchor framework using the mpl-token-metadata crate by invoking a CPI with the necessary accounts and arguments.

Ask

Soon™

Created by:

Axax...GsG2

Give this snippet a feedback!

Published on

April 24, 2025

Want to support the author?