Quantcast
Channel: Recent Questions - Solana Stack Exchange
Viewing all articles
Browse latest Browse all 7894

anchor nft mint to collection - How to remove a parameter from a function and make it work properly

$
0
0

I build, deploy, and test on Solana Playground.

First, here is my current code that works fine.

pub fn mint_to_collection(    ctx: Context<MintToCollection>,    id_collection: u64,    id_nft: u64,    name: String,    symbol: String,    uri: String,) -> Result<()> {    let program_state = &mut ctx.accounts.program_state;    require_keys_eq!(program_state.owner, *ctx.accounts.owner.key);    let payer = &ctx.accounts.payer;    let id_bytes = id_collection.to_le_bytes();    let id_nft_bytes = id_nft.to_le_bytes();    let seeds = &["mint".as_bytes(),        id_bytes.as_ref(),        id_nft_bytes.as_ref(),&[ctx.bumps.mint],    ];    mint_to(        CpiContext::new_with_signer(            ctx.accounts.token_program.to_account_info(),            MintTo {                authority: ctx.accounts.authority.to_account_info(),                to: ctx.accounts.token_account.to_account_info(),                mint: ctx.accounts.mint.to_account_info(),            },&[&seeds[..]],        ),        1,    )?;    create_metadata_accounts_v3(        CpiContext::new_with_signer(            ctx.accounts.metadata_program.to_account_info(),            CreateMetadataAccountsV3 {                payer: payer.to_account_info(),                mint: ctx.accounts.mint.to_account_info(),                metadata: ctx.accounts.nft_metadata.to_account_info(),                mint_authority: ctx.accounts.authority.to_account_info(),                update_authority: ctx.accounts.authority.to_account_info(),                system_program: ctx.accounts.system_program.to_account_info(),                rent: ctx.accounts.rent.to_account_info(),            },&[&seeds[..]],        ),        DataV2 {            name,            symbol,            uri,            seller_fee_basis_points: 0,            creators: Some(vec![Creator {                address: payer.key(),                verified: true,                share: 100,            }]),            collection: Some(Collection {                key: ctx.accounts.collection.key(),                verified: false,            }),            uses: None,        },        true,        true,        None,    )?;    create_master_edition_v3(        CpiContext::new_with_signer(            ctx.accounts.metadata_program.to_account_info(),            CreateMasterEditionV3 {                edition: ctx.accounts.master_edition_account.to_account_info(),                payer: payer.to_account_info(),                mint: ctx.accounts.mint.to_account_info(),                metadata: ctx.accounts.nft_metadata.to_account_info(),                mint_authority: ctx.accounts.authority.to_account_info(),                update_authority: ctx.accounts.authority.to_account_info(),                system_program: ctx.accounts.system_program.to_account_info(),                token_program: ctx.accounts.token_program.to_account_info(),                rent: ctx.accounts.rent.to_account_info(),            },&[&seeds[..]],        ),        Some(1),    )?;}#[derive(Accounts)]#[instruction(id_collection: u64, id_nft: u64)]pub struct MintToCollection<'info> {    #[account(mut)]    pub authority: Signer<'info>,    #[account(mut)]    pub payer: Signer<'info>,    #[account(mut)]    pub program_state: Account<'info, ProgramState>,    #[account(mut)]    pub owner: AccountInfo<'info>, // The recipient account    #[account(     init,    payer = payer,     mint::decimals = 0,    mint::authority = authority,    mint::freeze_authority = authority,    seeds = ["mint".as_bytes(),              id_collection.to_le_bytes().as_ref(),             id_nft.to_le_bytes().as_ref()],     bump,    )]    pub mint: Account<'info, Mint>,    #[account(        init_if_needed,        payer = payer,        associated_token::mint = mint,        associated_token::authority = payer,    )]    pub token_account: Account<'info, TokenAccount>,    pub associated_token_program: Program<'info, AssociatedToken>,    pub rent: Sysvar<'info, Rent>,    pub system_program: Program<'info, System>,    pub token_program: Program<'info, Token>,    pub metadata_program: Program<'info, Metadata>,    #[account(        mut,        seeds = [            b"metadata".as_ref(),            metadata_program.key().as_ref(),            mint.key().as_ref(),            b"edition".as_ref(),        ],        bump,        seeds::program = metadata_program.key()    )]    /// CHECK:    pub master_edition_account: UncheckedAccount<'info>,    #[account(        mut,        seeds = [            b"metadata".as_ref(),            metadata_program.key().as_ref(),            mint.key().as_ref(),        ],        bump,        seeds::program = metadata_program.key()    )]    /// CHECK:    pub nft_metadata: UncheckedAccount<'info>,    /// CHECK:    pub collection: UncheckedAccount<'info>,}

enter all the values ​​correctly in the code above, Mint succeeds. However, here I want to mint excluding the id_collection value and id_nft. I thought that since id_collection and id_nft had no effect on actual NFT creation, I could just create a new key and insert it. But I still can't do it.

The first thing I tried was to remove id_collection and id_nft and change mint to #[account(mut)]. Changed new_with_signer inside mint_to_collection to new.

The mint address was randomly generated. I tried creating different addresses several times, but they all failed.

i got

Instruction index: 2Reason: The program expected this account to be already initialized.

and try

#[account(     init,    payer = payer,     mint::decimals = 0;    mint::authority = authority;    mint::freeze_authority = authority)]pub mint: Account<'info, Mint>,

i got

Test 'mintToCollection' failed: Signature verification failed

How can I remove and mint id_collection and id_nft?


Viewing all articles
Browse latest Browse all 7894

Trending Articles