Search code examples
solanaanchor-solana

How can I correctly declare the program id inside a Solana program?


I created and tested a Solana contract using Anchor. I am now trying to deploy it on localnet.

When I successfuly run anchor deploy, the resulting program id is different than the one declared inside the contract. Any attempt to interact with the contract results with the following error:

The declared program id does not match the actual program id

I am slightly confused. Judging from the Anchor documentation, I assumed that the program id is deterministic and will match whatever I declare inside the contract. How can I properly declare the program id to get rid of the error above?


Solution

  • I ended up finding an answer here.

    After running:

    1. anchor build
    2. anchor keys list

    I was able to get the new string that I used to replace whatever was inside declare_id!(...). I also had to replace it inside Anchor.toml, just under [programs.localnet].

    After replacing the strings, I ran anchor build once again and I was able to deploy & migrate without any errors.