Skip to content

Commit

Permalink
feat: declare program allow overriding address
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrowana committed Dec 17, 2024
1 parent 5767365 commit bc11aa1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
28 changes: 26 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},
Expr, LitStr, Token,
};

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

// Custom keyword for program_id
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![=]>()?;

// Parse the address expression
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

0 comments on commit bc11aa1

Please sign in to comment.