@layerzerolabs/program-id-solana-contracts 1.2.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Anchor.toml +5 -0
- package/Cargo.toml +6 -0
- package/README.md +55 -0
- package/lib/Cargo.toml +16 -0
- package/lib/src/lib.rs +57 -0
- package/package.json +42 -0
- package/rust-toolchain.toml +3 -0
- package/rustfmt.toml +23 -0
package/Anchor.toml
ADDED
package/Cargo.toml
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @layerzerolabs/program-id-solana-contracts
|
|
2
|
+
|
|
3
|
+
A cargo-tracked replacement for `solana_helper::program_id_from_env!`, for Solana programs whose
|
|
4
|
+
address is supplied at build time.
|
|
5
|
+
|
|
6
|
+
## Usage
|
|
7
|
+
|
|
8
|
+
```rust
|
|
9
|
+
use anchor_lang::prelude::*;
|
|
10
|
+
use program_id::declare_program_id;
|
|
11
|
+
|
|
12
|
+
declare_program_id!("OFT_ID", "9UovNrJD8pQyBLheeHNayuG1wJSEAoxkmM14vw5gcsTT");
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Takes the program ID from `$OFT_ID`, falling back to the literal. Expand it in a crate that has
|
|
16
|
+
`anchor_lang::prelude::*` in scope.
|
|
17
|
+
|
|
18
|
+
## Problem
|
|
19
|
+
|
|
20
|
+
`program_id_from_env!` is a proc macro that calls `std::env::var` while it expands. Cargo's
|
|
21
|
+
fingerprint cannot observe that read, so a rebuild with a different value recompiles nothing and
|
|
22
|
+
re-emits the program ID from the previous build. Nothing fails loudly — the `.so` carries the wrong
|
|
23
|
+
address.
|
|
24
|
+
|
|
25
|
+
That artifact cannot be repaired after the fact. A program ID is folded into `.text` as BPF `lddw`
|
|
26
|
+
immediates at ~150 sites, and Anchor const-evaluates the `__event_authority` bump from it, so
|
|
27
|
+
changing it is a compilation process, not a patch.
|
|
28
|
+
|
|
29
|
+
## How it works
|
|
30
|
+
|
|
31
|
+
`declare_program_id!` expands the `declare_id!` alongside an `option_env!($var)` marker. rustc
|
|
32
|
+
records that marker as an environment dependency of the calling crate, so cargo rebuilds whenever
|
|
33
|
+
the variable changes.
|
|
34
|
+
|
|
35
|
+
The crate does not depend on `anchor-lang`, so it cannot force an anchor version on a consumer.
|
|
36
|
+
|
|
37
|
+
## Adopting it
|
|
38
|
+
|
|
39
|
+
- **Repair the IDL.** `anchor idl build` recovers the address by text-matching `declare_id!`, so any
|
|
40
|
+
non-literal form yields a garbage `address` field. Chain `add_address_to_idl.py --idl-dir <dir>`
|
|
41
|
+
(present in the anchor image) after it, which re-derives the address via `cargo expand`.
|
|
42
|
+
- **Pass the variable to every step.** The compile and the IDL build are separate `lz-tool`
|
|
43
|
+
invocations; both need the same value or the `.so` and the IDL disagree.
|
|
44
|
+
- **Pass it with `lz-tool --env`, not cargo's `[env]` table.** `[env]` does not override a variable
|
|
45
|
+
already set in the process environment, and `--config` cannot express `force = true`.
|
|
46
|
+
- **Expect a new `.so`.** The marker adds no bytes to the artifact, but replacing a literal
|
|
47
|
+
`declare_id!` shifts source lines, which moves `panic::Location` constants.
|
|
48
|
+
|
|
49
|
+
## Tests
|
|
50
|
+
|
|
51
|
+
- `pnpm test:rebuild` — builds `tests/probe` twice with different IDs without cleaning, and fails if
|
|
52
|
+
the second build does not follow. Deleting the `option_env!` marker makes it fail.
|
|
53
|
+
- `pnpm test:idl-shape` — expands the probe and asserts `add_address_to_idl.py`'s matcher recovers
|
|
54
|
+
the probe's declared ID.
|
|
55
|
+
- `pnpm test:unit` — default fallback and base58 decoding.
|
package/lib/Cargo.toml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "program-id"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Cargo-tracked program IDs for Solana programs whose address is supplied at build time"
|
|
5
|
+
edition = "2021"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
|
|
8
|
+
[lib]
|
|
9
|
+
crate-type = ["lib"]
|
|
10
|
+
name = "program_id"
|
|
11
|
+
|
|
12
|
+
[dependencies]
|
|
13
|
+
solana-helper = "0.1.0"
|
|
14
|
+
|
|
15
|
+
[dev-dependencies]
|
|
16
|
+
bs58 = "0.5.1"
|
package/lib/src/lib.rs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
//! Cargo-tracked program IDs for Solana programs whose address is supplied at build time.
|
|
2
|
+
//!
|
|
3
|
+
//! `solana_helper::program_id_from_env!` reads the variable inside a proc macro, which cargo's
|
|
4
|
+
//! fingerprint cannot observe: a rebuild with a changed value re-emits the previous program ID.
|
|
5
|
+
//! `declare_program_id!` pairs that read with an `option_env!` marker, which rustc records as an
|
|
6
|
+
//! environment dependency of the calling crate, so cargo rebuilds when the variable changes.
|
|
7
|
+
//!
|
|
8
|
+
//! ```ignore
|
|
9
|
+
//! use anchor_lang::prelude::*;
|
|
10
|
+
//! use program_id::declare_program_id;
|
|
11
|
+
//!
|
|
12
|
+
//! declare_program_id!("OFT_ID", "9UovNrJD8pQyBLheeHNayuG1wJSEAoxkmM14vw5gcsTT");
|
|
13
|
+
//! ```
|
|
14
|
+
//!
|
|
15
|
+
//! No `anchor-lang` dependency: `declare_id!` and `Pubkey` resolve at the call site.
|
|
16
|
+
|
|
17
|
+
#[doc(hidden)]
|
|
18
|
+
pub use solana_helper::program_id_from_env as __program_id_from_env;
|
|
19
|
+
|
|
20
|
+
/// Declares the program ID from `$var`, falling back to `$default`, and registers `$var` with
|
|
21
|
+
/// cargo so that changing it forces a rebuild.
|
|
22
|
+
///
|
|
23
|
+
/// Expand this in a crate that has `anchor_lang::prelude::*` in scope.
|
|
24
|
+
#[macro_export]
|
|
25
|
+
macro_rules! declare_program_id {
|
|
26
|
+
($var:literal, $default:literal) => {
|
|
27
|
+
// Sibling item: this expansion is matched under `cargo expand` to repair the IDL address.
|
|
28
|
+
const _: Option<&str> = option_env!($var);
|
|
29
|
+
declare_id!(Pubkey::new_from_array($crate::__program_id_from_env!($var, $default)));
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[cfg(test)]
|
|
34
|
+
mod tests {
|
|
35
|
+
use super::__program_id_from_env as program_id_from_env;
|
|
36
|
+
|
|
37
|
+
// `const` position: `declare_id!` consumes the bytes in const context.
|
|
38
|
+
|
|
39
|
+
#[test]
|
|
40
|
+
fn falls_back_to_the_default_when_the_variable_is_absent() {
|
|
41
|
+
const ID: [u8; 32] = program_id_from_env!(
|
|
42
|
+
"LZ_PROGRAM_ID_ABSENT_IN_TESTS",
|
|
43
|
+
"11111111111111111111111111111111"
|
|
44
|
+
);
|
|
45
|
+
assert_eq!(ID, [0u8; 32]);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#[test]
|
|
49
|
+
fn decodes_the_default_from_base58() {
|
|
50
|
+
const DEFAULT: &str = "9UovNrJD8pQyBLheeHNayuG1wJSEAoxkmM14vw5gcsTT";
|
|
51
|
+
const ID: [u8; 32] = program_id_from_env!(
|
|
52
|
+
"LZ_PROGRAM_ID_ABSENT_IN_TESTS",
|
|
53
|
+
"9UovNrJD8pQyBLheeHNayuG1wJSEAoxkmM14vw5gcsTT"
|
|
54
|
+
);
|
|
55
|
+
assert_eq!(ID.to_vec(), bs58::decode(DEFAULT).into_vec().unwrap());
|
|
56
|
+
}
|
|
57
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@layerzerolabs/program-id-solana-contracts",
|
|
3
|
+
"version": "1.2.64",
|
|
4
|
+
"private": false,
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib",
|
|
9
|
+
"Anchor.toml",
|
|
10
|
+
"Cargo.toml",
|
|
11
|
+
"rust-toolchain.toml",
|
|
12
|
+
"rustfmt.toml"
|
|
13
|
+
],
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@layerzerolabs/vm-tooling-solana": "1.2.64"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "restricted",
|
|
19
|
+
"registry": "https://registry.npmjs.org/"
|
|
20
|
+
},
|
|
21
|
+
"externalRepoConfig": {
|
|
22
|
+
"targets": [
|
|
23
|
+
"audit-external",
|
|
24
|
+
"onesig-client"
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"allow-read-write": "[ -z \"$CI\" ] || sudo chmod -R a+rwx ./target",
|
|
29
|
+
"build": "pnpm exec lz-tool --script 'cargo build --locked -p program-id' anchor && pnpm allow-read-write",
|
|
30
|
+
"build:native": "pnpm exec lz-tool --docker-platform native --script 'cargo build --locked -p program-id' anchor && pnpm allow-read-write",
|
|
31
|
+
"check": "pnpm exec lz-tool --script 'cargo check --workspace --locked' anchor",
|
|
32
|
+
"clean": "rm -rf target",
|
|
33
|
+
"format": "pnpm exec lz-tool --script 'cargo +nightly-2025-06-01 fmt --all' anchor",
|
|
34
|
+
"format:check": "pnpm exec lz-tool --script 'cargo +nightly-2025-06-01 fmt --all --check' anchor",
|
|
35
|
+
"lint": "pnpm format:check && pnpm lint:rs",
|
|
36
|
+
"lint:rs": "pnpm exec lz-tool --script 'cargo clippy --workspace --all-targets --locked -- -D warnings' anchor",
|
|
37
|
+
"test": "pnpm test:unit && pnpm test:rebuild && pnpm test:idl-shape",
|
|
38
|
+
"test:idl-shape": "pnpm exec lz-tool --script './tests/assert-idl-shape.sh' anchor",
|
|
39
|
+
"test:rebuild": "pnpm exec lz-tool --script './tests/assert-rebuild.sh' anchor",
|
|
40
|
+
"test:unit": "pnpm exec lz-tool --script 'cargo test --locked -p program-id --lib' anchor"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/rustfmt.toml
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Basic
|
|
2
|
+
hard_tabs = false
|
|
3
|
+
max_width = 100
|
|
4
|
+
use_small_heuristics = "Max"
|
|
5
|
+
# Imports
|
|
6
|
+
imports_granularity = "Crate"
|
|
7
|
+
reorder_imports = true
|
|
8
|
+
# Consistency
|
|
9
|
+
newline_style = "Unix"
|
|
10
|
+
# Format comments
|
|
11
|
+
comment_width = 100
|
|
12
|
+
wrap_comments = true
|
|
13
|
+
# Misc
|
|
14
|
+
chain_width = 80
|
|
15
|
+
spaces_around_ranges = false
|
|
16
|
+
binop_separator = "Front"
|
|
17
|
+
reorder_impl_items = false
|
|
18
|
+
match_arm_leading_pipes = "Preserve"
|
|
19
|
+
match_arm_blocks = true
|
|
20
|
+
match_block_trailing_comma = true
|
|
21
|
+
trailing_comma = "Vertical"
|
|
22
|
+
trailing_semicolon = true
|
|
23
|
+
use_field_init_shorthand = true
|