diff --git a/CHANGELOG.md b/CHANGELOG.md index 3af26fe0e..7161132da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ The minor version will be incremented upon a breaking change and the patch versi - lang: Add `anchor_lang::pubkey` macro for declaring `Pubkey` const values ([#3021](https://github.com/coral-xyz/anchor/pull/3021)). - cli: Sync program ids on the initial build ([#3023](https://github.com/coral-xyz/anchor/pull/3023)). - idl: Remove `anchor-syn` dependency ([#3030](https://github.com/coral-xyz/anchor/pull/3030)). +- lang: Add `const` of program ID to `declare_id!` and `declare_program!` ([#3019](https://github.com/coral-xyz/anchor/pull/3019)). ### Fixes diff --git a/lang/attribute/account/src/id.rs b/lang/attribute/account/src/id.rs index c45680c1c..74c9ac885 100644 --- a/lang/attribute/account/src/id.rs +++ b/lang/attribute/account/src/id.rs @@ -46,6 +46,9 @@ fn id_to_tokens( /// The static program ID pub static ID: #pubkey_type = #id; + /// Const version of `ID` + pub const ID_CONST: #pubkey_type = #id; + /// Confirms that a given pubkey is equivalent to the program ID pub fn check_id(id: &#pubkey_type) -> bool { id == &ID @@ -56,6 +59,11 @@ fn id_to_tokens( ID } + /// Const version of `ID` + pub const fn id_const() -> #pubkey_type { + ID_CONST + } + #[cfg(test)] #[test] fn test_id() { diff --git a/lang/attribute/program/src/declare_program/mod.rs b/lang/attribute/program/src/declare_program/mod.rs index b6e5df3f2..82c9002b2 100644 --- a/lang/attribute/program/src/declare_program/mod.rs +++ b/lang/attribute/program/src/declare_program/mod.rs @@ -114,8 +114,12 @@ fn gen_id(idl: &Idl) -> proc_macro2::TokenStream { #[doc = #doc] pub static ID: Pubkey = __ID; + /// Const version of `ID` + pub const ID_CONST: Pubkey = __ID_CONST; + /// The name is intentionally prefixed with `__` in order to reduce to possibility of name /// clashes with the crate's `ID`. static __ID: Pubkey = Pubkey::new_from_array([#(#address_bytes,)*]); + const __ID_CONST : Pubkey = Pubkey::new_from_array([#(#address_bytes,)*]); } }