lang: Add const of program ID to `declare_id!` and `declare_program!` (#3019)

This commit is contained in:
cryptopapi997 2024-06-18 22:48:58 +02:00 committed by GitHub
parent 64c52c66e3
commit 3c5483f0d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 0 deletions

View File

@ -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

View File

@ -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() {

View File

@ -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,)*]);
}
}