1

I'm having some issues with running tests locally against a program I created. Its based off mint_nft.rs from this repo but I've updated it to use the token_2022 program.

use anchor_lang::prelude::*;
use anchor_spl::metadata::mpl_token_metadata::{
    instructions::{
        CreateMasterEditionV3Cpi, CreateMasterEditionV3CpiAccounts,
        CreateMasterEditionV3InstructionArgs, CreateMetadataAccountV3Cpi,
        CreateMetadataAccountV3CpiAccounts, CreateMetadataAccountV3InstructionArgs,
    },
    types::{CollectionDetails, Creator, DataV2},
};
use anchor_spl::{
    associated_token::AssociatedToken,
    metadata::Metadata,
    token_2022,
    token_interface::{Mint, Token2022, TokenAccount},
};

#[derive(Accounts)]
pub struct CreateCollection<'info> {
    #[account(mut)]
    user: Signer<'info>,
    #[account(
    init,
    payer = user,
    mint::decimals = 0,
    mint::authority = mint_authority,
    mint::freeze_authority = mint_authority,
)]
    mint: InterfaceAccount<'info, Mint>,
    #[account(
    seeds = [b"authority"],
    bump,
)]
    /// CHECK: This account is not initialized and is being used for signing purposes only
    pub mint_authority: UncheckedAccount<'info>,
    #[account(mut)]
    /// CHECK: This account will be initialized by the metaplex program
    metadata: UncheckedAccount<'info>,
    #[account(mut)]
    /// CHECK: This account will be initialized by the metaplex program
    master_edition: UncheckedAccount<'info>,
    #[account(
    init,
    payer = user,
    associated_token::mint = mint,
    associated_token::authority = user
)]
    destination: InterfaceAccount<'info, TokenAccount>,
    system_program: Program<'info, System>,
    token_program: Program<'info, Token2022>,
    associated_token_program: Program<'info, AssociatedToken>,
    token_metadata_program: Program<'info, Metadata>,
}

impl<'info> CreateCollection<'info> {
    pub fn create_collection(&mut self, bumps: &CreateCollectionBumps) -> Result<()> {
        let metadata = &self.metadata.to_account_info();
        let master_edition = &self.master_edition.to_account_info();
        let mint = &self.mint.to_account_info();
        let authority = &self.mint_authority.to_account_info();
        let payer = &self.user.to_account_info();
        let system_program = &self.system_program.to_account_info();
        let spl_token_program = &self.token_program.to_account_info();
        let spl_metadata_program = &self.token_metadata_program.to_account_info();

        let seeds = &[&b"authority"[..], &[bumps.mint_authority]];
        let signer_seeds = &[&seeds[..]];

        let cpi_program = self.token_program.to_account_info();
        let cpi_accounts = token_2022::MintTo {
            mint: self.mint.to_account_info(),
            to: self.destination.to_account_info(),
            authority: self.mint_authority.to_account_info(),
        };
        let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer_seeds);
        token_2022::mint_to(cpi_ctx, 1)?;
        msg!("Collection NFT minted!");

        let creator = vec![Creator {
            address: self.mint_authority.key().clone(),
            verified: true,
            share: 100,
        }];

        let metadata_account = CreateMetadataAccountV3Cpi::new(
            spl_metadata_program,
            CreateMetadataAccountV3CpiAccounts {
                metadata,
                mint,
                mint_authority: authority,
                payer,
                update_authority: (authority, true),
                system_program,
                rent: None,
            },
            CreateMetadataAccountV3InstructionArgs {
                data: DataV2 {
                    name: "DummyCollection".to_owned(),
                    symbol: "DC".to_owned(),
                    uri: "".to_owned(),
                    seller_fee_basis_points: 0,
                    creators: Some(creator),
                    collection: None,
                    uses: None,
                },
                is_mutable: true,
                collection_details: Some(CollectionDetails::V1 { size: 0 }),
            },
        );
        metadata_account.invoke_signed(signer_seeds)?;
        msg!("Metadata Account created!");

        let master_edition_account: CreateMasterEditionV3Cpi = CreateMasterEditionV3Cpi::new(
            spl_metadata_program,
            CreateMasterEditionV3CpiAccounts {
                edition: master_edition,
                update_authority: authority,
                mint_authority: authority,
                mint,
                payer,
                metadata,
                token_program: spl_token_program,
                system_program,
                rent: None,
            },
            CreateMasterEditionV3InstructionArgs {
                max_supply: Some(0),
            },
        );
        master_edition_account.invoke_signed(signer_seeds)?;
        msg!("Master Edition Account created");

        Ok(())
    }
}

My test file is practically the same as the repo but this is with TOKEN_2022_PROGRAM_ID being passed into token program here

I can deploy the program so I feel like maybe it's some config issue with my Anchor.toml maybe.

error logs:

  1) mint-nft
       Create Collection NFT:
     
  Error: 
      at Function.parse (node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@coral-xyz/anchor/src/error.ts:250:14)
      at translateError (node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@coral-xyz/anchor/src/error.ts:282:37)
      at MethodsBuilder.rpc [as _rpcFn] (node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:35:29)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)

  2) mint-nft
       Mint NFT:
     
  Error: 
      at Function.parse (node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@coral-xyz/anchor/src/error.ts:250:14)
      at translateError (node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@coral-xyz/anchor/src/error.ts:282:37)
      at MethodsBuilder.rpc [as _rpcFn] (node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:35:29)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)

  3) mint-nft
       Verify Collection:
     
  Error: 
      at Function.parse (node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@coral-xyz/anchor/src/error.ts:250:14)
      at translateError (node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@coral-xyz/anchor/src/error.ts:282:37)
      at MethodsBuilder.rpc [as _rpcFn] (node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:35:29)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)



error Command failed with exit code 3.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Here is the repo if you would like to try it out locally, https://github.com/rtwhite10/token_2022_nft

Appreciate any advice, thanks!!

2
  • 1
    Could you also post the error logs
    – Jimii
    Commented Aug 21 at 7:23
  • 1
    from what i can see, in your git you dont have the metadata program locally. I answered the question below. How to make it work. Commented Aug 24 at 2:35

2 Answers 2

2

getAssociatedTokenAddressSync

The default value is TOKEN_PROGRAM_ID. You should change it to TOKEN_2022_PROGRAM_ID.

export function getAssociatedToken2022AddressSync(
  mint: PublicKey,
  owner: PublicKey,
  allowOwnerOffCurve = false,
  programId = TOKEN_2022_PROGRAM_ID,
  associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID
): PublicKey {
  if (!allowOwnerOffCurve && !PublicKey.isOnCurve(owner.toBuffer()))
    throw new TokenOwnerOffCurveError();

  const [address] = PublicKey.findProgramAddressSync(
    [owner.toBuffer(), programId.toBuffer(), mint.toBuffer()],
    associatedTokenProgramId
  );

  return address;
}
1
  • Thanks @EthanOK! I tried that and it is still failing the test. I might post the repo here in a bit to make it a bit easier to troubleshoot.
    – rtwhite
    Commented Aug 24 at 1:09
1

Hey the reason its failing its because you didnt dumped the metadata program.

When running locally you need to:

first if you haven't dumped the program you do :

solana program dump -u d metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s metadata.so

Then you start your validator with:

solana-test-validator -r --bpf-program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s metadata.so

Have a nice day ser.

2
  • Where you able to get the tests to pass locally? I'm still unable to get the tests to pass even with the metadata dump.
    – rtwhite
    Commented Aug 24 at 21:47
  • well i only checked the files and since u said it u were testing locally and the repo didnt had the metadata program file, it was impossible to work locally. Commented Aug 26 at 3:57

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.