5

I moved the app from devnet to mainnet. After building and deploying the app with anchor, received a new Program ID. I put this new Program ID in the source code in all the places needed.

I now try to call the app from the NodeJS API and I receive this error: AnchorError occurred. Error Code: DeclaredProgramIdMismatch. Error Number: 4100. Error Message: The declared program id does not match the actual program id.

I then built again the same app but with the new Program ID provided and deployed it. During this operation, the deploy command returned another Program Id, which actually was the OLD Program ID that I had on Devnet.

I now try to use the Old and the New Program Ids as addresses in the NodeJS app, but still I received the same error in both cases.

Code Repo: https://github.com/The-Community-Atlas/solana-client-and-program

4
  • 1
    try running anchor keys list and seeing if the output is the address you expect for your program ID
    – john
    Commented Jan 31, 2023 at 21:08
  • By running that command i see the Old Key that i had when deployed in Devnet. Even if i put that key as the address in idl.json file for the Node.js app, i still get the same error. here: const idl = JSON.parse(fs.readFileSync('./idl.json')); const programId = new PublicKey(idl.metadata.address); Commented Feb 1, 2023 at 15:19
  • Were you able to solve this? I'm running into the same problem.
    – Nelis.sol
    Commented Feb 19, 2023 at 15:37
  • Not yet. How many times have you tried to deploy the Program? Commented Feb 21, 2023 at 11:17

5 Answers 5

6

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

This means that you need to check 2 things.

  1. Check that declare_id! (usually in lib.rs) has the same key as the program keypair you are deploying with. You can get the pubkey of your program keypair by running solana-keygen pubkey target/deploy/my_program-keypair.json.
  2. Check that the program keypair has 0 SOL! Programs cannot be deployed to if they have SOL. If you have accidentally transferred SOL to the program, then send it back to your wallet, and then try redeploying. You can check the SOL balance of your program with solana balance $(solana-keygen pubkey target/deploy/my_program-keypair.json) --url mainnet-beta.

Lastly, I recommend against using anchor deploy for versions before 0.27.0, since it can be extremely difficult to debug for folks new to Solana. Instead, I recommend manually deploying your program & then manually deploying the IDL. There are also 2 steps for this.

  1. Deploy your built program with: solana deploy target/deploy/my_program.so ./target/deploy/my_program-keypair.json -k <path/to/your-wallet-keypair>.json --url mainnet-beta

  2. Deploy your anchor IDL with: anchor idl init $(solana-keygen pubkey target/deploy/my_program-keypair.json) --filepath target/idl/my_program.json --provider.cluster mainnet

Note that target/deploy folder is created via anchor build or cargo build-sbf and you will lose your program's keypair if you ever run cargo clean or anchor clean, so always save target/deploy/my_program-keypair.json if you're planning on using it later.

Best of luck 🙏

1
  • 1
    Thanks a lot for your help. I managed to deploy the Program in Mainnet :) Commented Mar 17, 2023 at 22:53
2

try running Solana Program deploy instead of anchor with the program id specified in --program-id flag

solana program deploy --program-id <PROGRAM_ID>
2

For me anchor clean fix me problem!

0
1

I was facing the same issue when running anchor test. I tried anchor clean and changing the pubkey in the declare_id macro to match the one in target/deploy/[my_program_name].json, but this didn't work.

What fixed it was checking inside Anchor.toml and changing the following field to my new pubkey:

[programs.localnet]
my_program_name = "<insert new pubkey here in base58>"
0

I solve with following command, it change my code of "declare_id!" section. I am not sure whether it's best practice.

anchor keys sync
New contributor
ZhongLiDe Shen is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.