In my project, I have some code that is unable to run on testnet, so I want to just pass sample data there. I'm wondering if it's possible to detect the environment so that I do not have to run different contracts for each environment.

  • are you trying to deploy a program in some other cluster than before? solana config set --url localhost (can substitute localhost for devnet etc), and solana config get returns your RPC url
    – coco
    Jul 28, 2022 at 16:03

1 Answer 1


You might be able to pre-load an account with configuration data on each of the clusters which just has the name of the cluster in the account data field. Then your program can check that address to make a decision about how to proceed.

When you run your program, you'll still need to include the address of that "config"/"environment" account with each instruction so your program can read from it. As long as you use the same address on all clusters I think it would work.

From a security standpoint, your program obviously shouldn't assume whatever address it receives as configuration/environment is trustworthy.

For a simpler solution, you could also consider just sending in the behaviour you want as part of the instruction call.

  • 1
    That's pretty clever. I'll probably end up just doing it the slow, manual way to avoid the potential security risk, but this is probably the best way
    – web3man
    Jul 30, 2022 at 3:13
  • 1
    Thanks @web3man. If you think it might speed up your workflow I think the on-chain program can just compare the passed-in "config" address with the expected "config" address (like hard-code it in the program). If they match, the program should be able to trust the data it finds there assuming you control the private key. Bonus: if they don't match, the program can refuse to do the "wrong" thing for the environment and just error out.
    – Nate
    Jul 30, 2022 at 5:46

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.