2

I'm trying to integrate Raydium into my program. They have different config for devnet and production address based on a feature flag as below:

#[cfg(feature = "devnet")]
declare_id!("CPMDWBwJDtYax9qW7AyRuVC19Cc4L4Vcy4n2BHAbHkCW");
#[cfg(not(feature = "devnet"))]
declare_id!("CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C");

Now I add the Raydium program as a dependency in my own program:

raydium-cp-swap = { git = "https://github.com/raydium-io/raydium-cp-swap", features = [
    "cpi",
    "devnet",
] }

AFAIK, the devnet feature will only be enabled if I manually specify it in the build command, e.g anchor build -- --features "devnet", but in reality it's always enabled, whether I specify the flag or not. The only way for it to not be enabled is to remove the "devnet" flag in the dependency, which is very inconvenient. When I try to print the address, in VSCode it shows the devnet is enabled too:

enter image description here

Does anyone here have a solution to this? basically I want the "devnet" flag to only be enabled if I explicitly specify it in the build command. I want to use the program with both devnet/without devnet configurations.

1 Answer 1

2

You're declaring the Raydium dependency with the devnet feature enabled, which means that it will always be built with the devnet feature.

What you really want is to add a devnet feature in your crate, and conditionally enable the devnet feature in the Raydium dependency if it's enabled in your crate.

Try doing this:

[dependencies]
... all other deps ...
raydium-cp-swap = { git = "https://github.com/raydium-io/raydium-cp-swap", features = ["cpi"] }

[features]
devnet = ["raydium-cp-swap/devnet"]

You can read more about Cargo features at https://doc.rust-lang.org/cargo/reference/features.html

1
  • I did have the devnet feature in my crate, but I wasn't aware that you can conditionally enable the feature like that (thought it was just for optional dependencies). Thank you very much Jon.
    – Kise
    Commented May 23 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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