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:
- 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.
- This generates the
- Run
solana-keygen pubkey target/deploy/{keypair_file}.json
- This will give you the public key of that keypair
- Copy that public key and update it both on
Anchor.toml
and inlib.rs
, where the macrodeclare_id!()
resides. - Run
anchor build
again, in order to re-compile the code with the new program id. - Lastly, if you want to deploy it in localnet, I recommend using
anchor localnet
instead of the vanillasolana-test-validator -r
since it deploys the program located ontarget/
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 :)