Search code examples
anchorsolanametaplex

How to test Metaplex instructions with Anchor on localnet?


I'm new to Solana and Anchor. I'm building a program to create a NFT collection and it has an instruction for admin to mint a new NFT for a recipient. But when I call to create_metadata_accounts_v3 and run test. It show error

Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: instruction expected an executable account
      at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:5921:13)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
      at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:5880:20)
      at sendAndConfirmRawTransaction (node_modules/@coral-xyz/anchor/src/provider.ts:370:21)
      at AnchorProvider.sendAndConfirm (node_modules/@coral-xyz/anchor/src/provider.ts:160:14)
      at MethodsBuilder.rpc [as _rpcFn] (node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:29:16)

I think it is because metaplex_metadata_program has not deployed to the localnet. How can I test it in localnet?

It work when I run test on devnet (anchor build, anchor deploy, anchor run test) but it just work on the first time. From the second time, I have to change the program id manually each time and re-deploy because old program has been deployed and some account has been initialized before.

I know that Metaplex has Javascript libraries that can mint NFT without build program but I want to approach the program way.

Thanks.


Solution

  • I finally found a simple solution:

    Step1: dump the target program to .so file from devnet, testnet or mainnet

    solana program dump -u m metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s tests/metaplex_token_metadata_program.so
    

    where -u m is dump from mainnet. The output file is tests/metaplex_token_metadata_program.so. You can custom for your use

    Step2: add config to Anchor.toml

    [[test.genesis]]
    address = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"  
    program = "tests/metaplex_token_metadata_program.so"
    

    where program is .so file you dumped before, address is address of program in localnet

    Step3: run anchor test and enjoy.