Skip to main content

tycho_simulation/rfq/
constants.rs

1use std::env;
2
3use crate::rfq::errors::RFQError;
4
5pub const DEFAULT_METRIC_API_URL: &str = "http://54.199.103.16:8080";
6
7/// Hashflow authentication configuration
8pub struct HashflowAuth {
9    pub user: String,
10    pub key: String,
11}
12
13/// Bebop authentication configuration
14pub struct BebopAuth {
15    pub key: String,
16}
17
18/// Metric API configuration
19pub struct MetricConfig {
20    pub base_url: String,
21    pub secret_key: Option<String>,
22}
23
24/// Read Hashflow authentication from environment variables
25/// Returns the HASHFLOW_USER and HASHFLOW_KEY environment variables
26pub fn get_hashflow_auth() -> Result<HashflowAuth, RFQError> {
27    let user = env::var("HASHFLOW_USER").map_err(|_| {
28        RFQError::InvalidInput("HASHFLOW_USER environment variable is required".into())
29    })?;
30
31    let key = env::var("HASHFLOW_KEY").map_err(|_| {
32        RFQError::InvalidInput("HASHFLOW_KEY environment variable is required".into())
33    })?;
34
35    Ok(HashflowAuth { user, key })
36}
37
38/// Liquorice authentication configuration
39pub struct LiquoriceAuth {
40    pub solver: String,
41    pub key: String,
42}
43
44/// Read Liquorice authentication from environment variables
45/// Returns the LIQUORICE_USER and LIQUORICE_KEY environment variables
46pub fn get_liquorice_auth() -> Result<LiquoriceAuth, RFQError> {
47    let solver = env::var("LIQUORICE_USER").map_err(|_| {
48        RFQError::InvalidInput("LIQUORICE_USER environment variable is required".into())
49    })?;
50
51    let key = env::var("LIQUORICE_KEY").map_err(|_| {
52        RFQError::InvalidInput("LIQUORICE_KEY environment variable is required".into())
53    })?;
54
55    Ok(LiquoriceAuth { solver, key })
56}
57
58/// Read Bebop authentication from environment variables
59/// Returns the BEBOP_KEY environment variable
60pub fn get_bebop_auth() -> Result<BebopAuth, RFQError> {
61    let key = env::var("BEBOP_KEY")
62        .map_err(|_| RFQError::InvalidInput("BEBOP_KEY environment variable is required".into()))?;
63
64    Ok(BebopAuth { key })
65}
66
67/// Read Metric API configuration from environment variables.
68/// METRIC_API_URL defaults to the public Metric endpoint; METRIC_SECRET_KEY is optional.
69pub fn get_metric_config() -> MetricConfig {
70    let base_url = env::var("METRIC_API_URL")
71        .ok()
72        .filter(|url| !url.trim().is_empty())
73        .unwrap_or_else(|| DEFAULT_METRIC_API_URL.to_string());
74    let secret_key = env::var("METRIC_SECRET_KEY")
75        .ok()
76        .filter(|key| !key.trim().is_empty());
77
78    MetricConfig { base_url, secret_key }
79}
80
81#[cfg(test)]
82mod tests {
83    use std::env;
84
85    use super::*;
86
87    #[test]
88    fn test_hashflow_auth_success() {
89        env::set_var("HASHFLOW_USER", "test_user");
90        env::set_var("HASHFLOW_KEY", "test_key");
91
92        let auth = get_hashflow_auth().unwrap();
93        assert_eq!(auth.user, "test_user");
94        assert_eq!(auth.key, "test_key");
95
96        env::remove_var("HASHFLOW_USER");
97        env::remove_var("HASHFLOW_KEY");
98    }
99
100    #[test]
101    fn test_hashflow_auth_missing_user() {
102        env::remove_var("HASHFLOW_USER");
103        env::set_var("HASHFLOW_KEY", "test_key");
104
105        let result = get_hashflow_auth();
106        assert!(result.is_err());
107
108        env::remove_var("HASHFLOW_KEY");
109    }
110
111    #[test]
112    fn test_hashflow_auth_missing_key() {
113        env::set_var("HASHFLOW_USER", "test_user");
114        env::remove_var("HASHFLOW_KEY");
115
116        let result = get_hashflow_auth();
117        assert!(result.is_err());
118
119        env::remove_var("HASHFLOW_USER");
120    }
121
122    #[test]
123    fn test_bebop_auth_success() {
124        env::set_var("BEBOP_KEY", "test_key");
125
126        let auth = get_bebop_auth().unwrap();
127        assert_eq!(auth.key, "test_key");
128
129        env::remove_var("BEBOP_KEY");
130    }
131
132    #[test]
133    fn test_bebop_auth_missing_key() {
134        env::remove_var("BEBOP_KEY");
135
136        let result = get_bebop_auth();
137        assert!(result.is_err());
138    }
139
140    #[test]
141    fn test_metric_config_defaults_and_reads_env() {
142        env::remove_var("METRIC_API_URL");
143        env::remove_var("METRIC_SECRET_KEY");
144
145        let config = get_metric_config();
146        assert_eq!(config.base_url, DEFAULT_METRIC_API_URL);
147        assert_eq!(config.secret_key, None);
148
149        env::set_var("METRIC_API_URL", "https://metric.example");
150        env::set_var("METRIC_SECRET_KEY", "secret");
151
152        let config = get_metric_config();
153        assert_eq!(config.base_url, "https://metric.example");
154        assert_eq!(config.secret_key.as_deref(), Some("secret"));
155
156        env::remove_var("METRIC_API_URL");
157        env::remove_var("METRIC_SECRET_KEY");
158    }
159}