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

feat: declare program allow overriding address #3435

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 25 additions & 2 deletions lang/attribute/program/src/declare_program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ mod mods;
use anchor_lang_idl::{convert::convert_idl, types::Idl};
use anyhow::anyhow;
use quote::{quote, ToTokens};
use syn::parse::{Parse, ParseStream};
use syn::{
parse::{Parse, ParseStream},
LitStr, Token,
};

use common::gen_docs;
use mods::{
Expand All @@ -18,10 +21,30 @@ pub struct DeclareProgram {
idl: Idl,
}

// Custom keyword for address
mod kw {
syn::custom_keyword!(address);
}

impl Parse for DeclareProgram {
fn parse(input: ParseStream) -> syn::Result<Self> {
let name = input.parse()?;
let idl = get_idl(&name).map_err(|e| syn::Error::new(name.span(), e))?;

let address = if input.peek(Token![,]) {
input.parse::<Token![,]>()?;
input.parse::<kw::address>()?;
input.parse::<Token![=]>()?;

Some(input.parse::<LitStr>()?)
} else {
None
};

let mut idl = get_idl(&name).map_err(|e| syn::Error::new(name.span(), e))?;

if let Some(address) = address {
idl.address = address.value();
}
Ok(Self { name, idl })
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/declare-program/programs/declare-program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use anchor_lang::prelude::*;

declare_id!("Dec1areProgram11111111111111111111111111111");

declare_program!(external);
declare_program!(
external,
address = "Externa111111111111111111111111111111111111"
);
use external::program::External;

// Compilation check for legacy IDL (pre Anchor `0.30`)
Expand Down
Loading