Skip to content

Commit

Permalink
lang: Add const of program ID to declare_id! and declare_program! (
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptopapi997 authored Jun 18, 2024
1 parent 64c52c6 commit 3c5483f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions lang/attribute/account/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions lang/attribute/program/src/declare_program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,)*]);
}
}

0 comments on commit 3c5483f

Please sign in to comment.