1

Is there a way have anchor check the correct program ID owner of an account depending on which cluster the program has been deployed to?

Currently I have to manually check both program IDs, the devnet and mainnet program IDs, to see whether the account has the correct owner.

There must be a built in solana or anchor way of switching between the program IDs at compile time?

let owner = *aggregator.owner;
if owner != SWITCHBOARD_V2_DEVNET && owner != SWITCHBOARD_V2_MAINNET {
 return Err(error!(FeedErrorCode::InvalidSwitchboardAccount));
}

1 Answer 1

2

Using Rust's features mechanism and cfg expressions, you can programmatically specify the program/crate ID at build time to avoid the need for such manual checks:

  • for instance in the program's lib.rs (before the #[program] macro) the code below will declare one ID or the other depending on the mainnet feature being enabled or not:
#[cfg(feature = "mainnet")]
declare_id!("8ghymvPffJbkLHqYfSKdE8moRH5gSf4AQav9qtZfu77H");
#[cfg(not(feature = "mainnet"))]
declare_id!("DLANS7Qh31fFWLujEMtn5kyd87H8ZUbhwtfMurrSHYn9");
  • in the program's Cargo.toml the code below will declare the mainnet feature (here disabled by default):
[features]
mainnet = []

This way you can choose to build for mainnet or no by passing the --features "mainnet" option to anchor build. The crate ID baked in will be the relevant one, and Anchor will then be able to perform the check against that only, correct ID for that deployment.

1
  • yes is is how jet also does it i believe, rust features are godsend, helps a lot to test program which have admin features via upgradeable loaders on test environments such as programtest crate where support for upgradeable loaders is not there :( Jul 26 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.