1

I am trying to run some tests in my Solana Anchor project but I get an error saying that the declared program id does not match the actual program id.

Error: AnchorError occurred. Error Code: DeclaredProgramIdMismatch. Error Number: 4100. Error Message: The declared program id does not match the actual program id.

Here's the steps I take:

  1. Build my program (anchor build)
  2. Deploy using local testnet (solana-test-validator -r)
  3. Copy paste the program id that comes out after deployment
  4. Stop the local testnet
  5. Run the tests (anchor test)

My rust program declared id is:

declare_id!("hqgrvUepLLhFbXCb8woduWM62ps5rqap3TmPHbpuK11");

But if i try to paste the client's program id I get this:

ProgramId: PublicKey {
  _bn: <BN: da075cb2ff5ec6817613de530b692a8735477769da47430cbd8154335c4a8327>
}

My question is, how do I further debug this? I've looked for the error inside node_modules to see what is compared that throws such error but I could not find much.

Thank you in advance, Happy coding.

EDIT_1:

Test client:

import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { BoyncAnchorProgram } from "../target/types/boync_anchor_program";

import { PublicKey } from '@solana/web3.js';
import { expect } from "chai";

describe("boync-anchor-program", () => {
  // Configure the client to use the local cluster.
  const provider = anchor.AnchorProvider.env();
  anchor.setProvider(provider);
 
  const program = anchor.workspace.BoyncAnchorProgram as Program<BoyncAnchorProgram>;

  it("Creating user Bob", async () => {
    // Add your test here.
    const [bobUserPDA, _] = await PublicKey
      .findProgramAddress(
        [
          anchor.utils.bytes.utf8.encode('user'),
          provider.wallet.publicKey.toBuffer(),
        ],
        program.programId
      );

    const tx = await program.methods
      .initialize('Bob')
      .accounts({
        user: provider.wallet.publicKey,
        userData: bobUserPDA,
      })
      .rpc();

     ^^^ ERROR at this transaction invocation.

    console.log("[Success] Your transaction signature", tx);

    const bobData = await program.account.boyncUserData.fetch(bobUserPDA);
    expect(bobData.name).to.be.eql('Bob');
  });
});

2 Answers 2

7

I saw you found the solution, but the issue is more related to the Anchor workflow, which is not correctly explained in the Anchor book as far as I know.

When you are compiling a Solana program built with Anchor for the first time, it goes like this:

  1. Run anchor build
    • This generates the target/ folder.
    • Inside this folder, you'll find deploy/ and in there a keypair file. The keypair file is a generated keypair for the program you are deploying.
  2. Run solana-keygen pubkey target/deploy/{keypair_file}.json
    • This will give you the public key of that keypair
  3. Copy that public key and update it both on Anchor.toml and in lib.rs, where the macro declare_id!() resides.
  4. Run anchor build again, in order to re-compile the code with the new program id.
  5. Lastly, if you want to deploy it in localnet, I recommend using anchor localnet instead of the vanilla solana-test-validator -r since it deploys the program located on target/ for you using that keypair.

You only need to do the first 3 steps the first time you build the program, or in case you delete the target/ folder or the keypair inside it changes.

Hope this clarify things :)

0
3

FIXED:

Searched for the name of my program in the project directory and found out that it was a mismatch in the Anchor.toml file.

[features]
seeds = false
[programs.localnet]
boync_anchor_program = "EkvnvRY2prU1sJpVLHBk5qsNMXBryaZeMVacRSE1pcZM"

I changed the pubkey to match the one declared in the program code and everything worked.

A clean rebuild did not worked. I wonder how the Anchor.toml file got stuck with a different programId. A bug probably?

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.