Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It's not clear how to handle Program Id in a build and deployment process #866

Closed
gisek opened this issue Oct 10, 2021 · 4 comments
Closed
Labels
question Further information is requested

Comments

@gisek
Copy link

gisek commented Oct 10, 2021

Hi, this issue probably falls into the "documentation" category. I'm new to Solana and Anchor and I'm trying to follow your "A Minimal Example" article

We start with a lib.rs file which among others contains this line:

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

Then we go anchor build, the resulting ./target/idl/basic_0.json file looks like that:

{
  "version": "0.0.0",
  "name": "basic_0",
  "instructions": [
    {
      "name": "initialize",
      "accounts": [],
      "args": []
    }
  ]
}

Then we go anchor build and ./target/idl/basic_0.json changes to:

{
  "version": "0.0.0",
  "name": "basic_0",
  "instructions": [
    {
      "name": "initialize",
      "accounts": [],
      "args": []
    }
  ],
  "metadata": {
    "address": "47zS7NKHTP3DAECX6vv1uwB44RgZndUvgAGfWyh1haj7"
  }
}

We also get a log:

To deploy this program:
  $ solana program deploy /devel/anchor-minimal/examples/tutorial/basic-0/target/deploy/basic_0.so
The program address will default to this keypair (override with --program-id):
  /devel/anchor-minimal/examples/tutorial/basic-0/target/deploy/basic_0-keypair.json
[andrzej@andrzej-legion basic-0]$ anchor deploy
Deploying workspace: http://127.0.0.1:8899
Upgrade authority: /home/andrzej/.config/solana/id.json
Deploying program "basic-0"...
Program path: /devel/anchor-minimal/examples/tutorial/basic-0/target/deploy/basic_0.so...
Program Id: 47zS7NKHTP3DAECX6vv1uwB44RgZndUvgAGfWyh1haj7

So the program id, of the newly deployed program was added to the IDL file. But it conflicts with the declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); from lib.rs.

I tried to remove declare_id! line, but the compiler doesn't like it:

[andrzej@andrzej-legion basic-0]$ anchor build
BPF SDK: /home/andrzej/.local/share/solana/install/releases/1.7.12/solana-release/bin/sdk/bpf
Running: rustup toolchain list -v
Running: cargo +bpf build --target bpfel-unknown-unknown --release
   Compiling basic-0 v0.1.0 (/devel/anchor-minimal/examples/tutorial/basic-0/programs/basic-0)
error[E0425]: cannot find value `ID` in this scope
 --> programs/basic-0/src/lib.rs:5:1
  |
5 | #[program]
  | ^^^^^^^^^^ not found in this scope
  |
  = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0425`.
error: could not compile `basic-0`
  1. So what's the role of this declare_id! macro and why do we have to declare the Program Id up front if it's only known upon deployment?
  2. What are the steps do to this correctly?

Thanks

@perlish
Copy link

perlish commented Oct 11, 2021

After first deploy, you can use address in declare_id and upgrade the contract
anchor upgrade --program-id xxx ./target/deploy/yyy.so

If you don't want to upgrade contract, use the anchor_spl::token::ID in Contract and remove declare_id macro

@armaniferrante
Copy link
Member

This should be mostly addressed by #695.

@ghost
Copy link

ghost commented Oct 16, 2021

Thanks @perlish @armaniferrante
Is there a way to specify programId / keypair while using anchor deploy function? anchor build output shows how to do this with pure solana:

Running: cargo +bpf build --target bpfel-unknown-unknown --release
    Finished release [optimized] target(s) in 0.20s
Running: /home/andrzej/.local/share/solana/install/releases/1.7.12/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /devel/App/App.Solana/target/deploy/app.so

To deploy this program:
  $ solana program deploy /devel/App/App.Solana/target/deploy/app.so  //     <-------- *here*
The program address will default to this keypair (override with --program-id):

but if I do it with pure solana CLI, my IDL file isn't updated with:

  "metadata": {
    "address": "47zS7NKHTP3DAECX6vv1uwB44RgZndUvgAGfWyh1haj7"
  } 

@armaniferrante
Copy link
Member

Thanks @perlish @armaniferrante Is there a way to specify programId / keypair while using anchor deploy function? anchor build output shows how to do this with pure solana:

anchor deploy will just use whatever is in the standard target/deploy/<program-name>-keypair.json file. If you want a custom program id keypair file, you can use solana program deploy. You just need to make sure you set the address correctly in your program via declare_id before building and deploying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants