use solana_pubkey::Pubkey;
use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum LightconeEnv {
Local,
Staging,
#[default]
Prod,
}
impl LightconeEnv {
pub fn api_url(&self) -> String {
if let Ok(override_url) = std::env::var("SDK_API_URL") {
return override_url;
}
match self {
Self::Local => "https://api.local.lightcone.xyz",
Self::Staging => "https://api.staging.lightcone.xyz",
Self::Prod => "https://api.lightcone.xyz",
}
.to_string()
}
pub fn ws_url(&self) -> String {
if let Ok(override_url) = std::env::var("SDK_WS_URL") {
return override_url;
}
match self {
Self::Local => "wss://ws.local.lightcone.xyz/ws",
Self::Staging => "wss://ws.staging.lightcone.xyz/ws",
Self::Prod => "wss://ws.lightcone.xyz/ws",
}
.to_string()
}
pub fn rpc_url(&self) -> String {
if let Ok(override_url) = std::env::var("SDK_RPC_URL") {
return override_url;
}
match self {
Self::Local => "https://api.devnet.solana.com",
Self::Staging => "https://api.devnet.solana.com",
Self::Prod => "https://api.devnet.solana.com",
}
.to_string()
}
pub fn program_id(&self) -> Pubkey {
if let Ok(override_id) = std::env::var("SDK_PROGRAM_ID") {
return Pubkey::from_str(&override_id).expect("SDK_PROGRAM_ID must be a valid pubkey");
}
match self {
Self::Local => Pubkey::from_str("HQZW84F7WbpDLDdd6eaDsBh6LjDQ2uCxpkZgkLakcago")
.expect("valid program id"),
Self::Staging => Pubkey::from_str("FAq4NbwPVWNzoaNjcJGhWz4VFT5CbdysLPo7ZWWiWuuE")
.expect("valid program id"),
Self::Prod => Pubkey::from_str("B9rCvafkkjh749284jfDu5UB268pHeRLkzFpFf7t4mxK")
.expect("valid program id"),
}
}
}
impl fmt::Display for LightconeEnv {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Local => write!(formatter, "local"),
Self::Staging => write!(formatter, "staging"),
Self::Prod => write!(formatter, "prod"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn local_environment_targets_local_backend() {
let environment = LightconeEnv::Local;
assert_eq!(
environment.api_url(),
"https://api.local.lightcone.xyz".to_string()
);
assert_eq!(
environment.ws_url(),
"wss://ws.local.lightcone.xyz/ws".to_string()
);
}
#[test]
fn local_environment_uses_deployed_program() {
let program_id = LightconeEnv::Local.program_id();
let (exchange_pda, _) = crate::program::pda::get_exchange_pda(&program_id);
assert_eq!(
program_id,
Pubkey::from_str("HQZW84F7WbpDLDdd6eaDsBh6LjDQ2uCxpkZgkLakcago").unwrap()
);
assert_eq!(
exchange_pda,
Pubkey::from_str("B6Y3DF25exUTk2j7ocjYfoBY6r3tc6shyXTVhnHhrbk9").unwrap()
);
}
}