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

WIP: Try killing declare_id! by moving it into the #[program] macro. #868

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions examples/tutorial/basic-1/programs/basic-1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
mod basic_1 {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions lang/syn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ serde_json = "1.0"
sha2 = "0.9.2"
thiserror = "1.0"
bs58 = "0.3.1"
toml = "0.5.8"
4 changes: 4 additions & 0 deletions lang/syn/src/codegen/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ mod dispatch;
mod entry;
mod handlers;
mod instruction;
mod program_id;

pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let mod_name = &program.name;

let program_id = program_id::generate(program);
let entry = entry::generate(program);
let dispatch = dispatch::generate(program);
let handlers = handlers::generate(program);
Expand All @@ -21,6 +23,8 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let accounts = accounts::generate(program);

quote! {
#program_id

// TODO: remove once we allow segmented paths in `Accounts` structs.
use #mod_name::*;

Expand Down
17 changes: 17 additions & 0 deletions lang/syn/src/codegen/program/program_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use quote::quote;

use crate::Program;

pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let anchor_toml = std::fs::read_to_string("./Anchor.toml")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should happen if the program id isn't in the Anchor.toml?

I think it would convenient if it looked for the default deploy key in the local filesystem. I.e. target/deploy/<program-name>-keypair.json, which is what solana program deploy uses by default. Then everything would work by default, i.e., you can clone, test, and deploy without any modifications--since you would have the keypair locally for the deploy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, was wondering about all the error handling 😬

So the desired behavior is: check for the id in Anchor.toml first, otherwise fall back to target/deploy/-keypair.json? (I realized working on this that I need to learn more about the Anchor.toml format, what each part means/entails etc.)

Copy link
Contributor Author

@cqfd cqfd Oct 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having an issue including solana-sdk as a dependency so this macro can use Keypair, when I try to use anchor build etc. I get a ring 0.16.20 build error. Not sure why yet (maybe because anchor build uses build-bpf whereas other places that use solana-sdk, e.g. the cli, don't?).

Will try again tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ Have gotten around that for now by just snagging the final 32 bytes of the on-disk keypair and bs58-ing it 🙃

.map(|s| s.parse::<toml::Value>())
.unwrap()
.unwrap();
let cluster = std::env::var("ANCHOR_CLUSTER").unwrap_or("localnet".to_string());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ This env var is WIP, I was thinking the anchor command could feed it along if you specify --provider.cluster?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm

let program_id = anchor_toml["programs"][cluster][&program.name.to_string()].to_string();
// strip quotes, urgh
let program_id = &program_id[1..program_id.len() - 1];
quote! {
::anchor_lang::declare_id!(#program_id);
}
}