Skip to main content

scope/web/api/
discover.rs

1//! Token discovery API handler.
2
3use crate::chains::DexClient;
4use crate::web::AppState;
5use axum::Json;
6use axum::extract::{Query, State};
7use axum::http::StatusCode;
8use axum::response::IntoResponse;
9use serde::Deserialize;
10use std::sync::Arc;
11
12/// Query parameters for token discovery.
13#[derive(Debug, Deserialize)]
14pub struct DiscoverQuery {
15    /// Source: "profiles", "boosts", "top-boosts".
16    #[serde(default = "default_source")]
17    pub source: String,
18    /// Filter by chain (optional).
19    pub chain: Option<String>,
20    /// Max results (default: 15).
21    #[serde(default = "default_limit")]
22    pub limit: u32,
23}
24
25fn default_source() -> String {
26    "profiles".to_string()
27}
28
29fn default_limit() -> u32 {
30    15
31}
32
33/// GET /api/discover — Browse trending/boosted tokens.
34pub async fn handle(
35    State(_state): State<Arc<AppState>>,
36    Query(params): Query<DiscoverQuery>,
37) -> impl IntoResponse {
38    let client = DexClient::new();
39
40    let tokens = match params.source.as_str() {
41        "boosts" => client.get_token_boosts().await,
42        "top-boosts" => client.get_token_boosts_top().await,
43        _ => client.get_token_profiles().await,
44    };
45
46    match tokens {
47        Ok(tokens) => {
48            let filtered: Vec<_> = if let Some(ref chain) = params.chain {
49                let c = chain.to_lowercase();
50                tokens
51                    .into_iter()
52                    .filter(|t| t.chain_id.to_lowercase() == c)
53                    .take(params.limit as usize)
54                    .collect()
55            } else {
56                tokens.into_iter().take(params.limit as usize).collect()
57            };
58            Json(serde_json::json!(filtered)).into_response()
59        }
60        Err(e) => (
61            StatusCode::INTERNAL_SERVER_ERROR,
62            Json(serde_json::json!({ "error": e.to_string() })),
63        )
64            .into_response(),
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn test_deserialize_full() {
74        let json = serde_json::json!({
75            "source": "boosts",
76            "chain": "ethereum",
77            "limit": 25
78        });
79        let req: DiscoverQuery = serde_json::from_value(json).unwrap();
80        assert_eq!(req.source, "boosts");
81        assert_eq!(req.chain, Some("ethereum".to_string()));
82        assert_eq!(req.limit, 25);
83    }
84
85    #[test]
86    fn test_deserialize_minimal() {
87        let json = serde_json::json!({});
88        let req: DiscoverQuery = serde_json::from_value(json).unwrap();
89        assert_eq!(req.source, "profiles");
90        assert_eq!(req.chain, None);
91        assert_eq!(req.limit, 15);
92    }
93
94    #[test]
95    fn test_defaults() {
96        assert_eq!(default_source(), "profiles");
97        assert_eq!(default_limit(), 15);
98    }
99
100    #[test]
101    fn test_with_chain_filter() {
102        let json = serde_json::json!({
103            "chain": "polygon",
104            "limit": 10
105        });
106        let req: DiscoverQuery = serde_json::from_value(json).unwrap();
107        assert_eq!(req.source, "profiles");
108        assert_eq!(req.chain, Some("polygon".to_string()));
109        assert_eq!(req.limit, 10);
110    }
111
112    #[tokio::test]
113    async fn test_handle_discover_profiles() {
114        use crate::chains::DefaultClientFactory;
115        use crate::config::Config;
116        use crate::web::AppState;
117        use axum::extract::{Query, State};
118        use axum::response::IntoResponse;
119
120        let config = Config::default();
121        let factory = DefaultClientFactory {
122            chains_config: config.chains.clone(),
123        };
124        let state = std::sync::Arc::new(AppState { config, factory });
125
126        let params = DiscoverQuery {
127            source: "profiles".to_string(),
128            chain: None,
129            limit: 5,
130        };
131        let response = handle(State(state), Query(params)).await.into_response();
132        let status = response.status();
133        assert!(status.is_success() || status.is_server_error());
134    }
135
136    #[tokio::test]
137    async fn test_handle_discover_boosts() {
138        use crate::chains::DefaultClientFactory;
139        use crate::config::Config;
140        use crate::web::AppState;
141        use axum::extract::{Query, State};
142        use axum::response::IntoResponse;
143
144        let config = Config::default();
145        let factory = DefaultClientFactory {
146            chains_config: config.chains.clone(),
147        };
148        let state = std::sync::Arc::new(AppState { config, factory });
149
150        let params = DiscoverQuery {
151            source: "boosts".to_string(),
152            chain: None,
153            limit: 5,
154        };
155        let response = handle(State(state), Query(params)).await.into_response();
156        let status = response.status();
157        assert!(status.is_success() || status.is_server_error());
158    }
159
160    #[tokio::test]
161    async fn test_handle_discover_top_boosts() {
162        use crate::chains::DefaultClientFactory;
163        use crate::config::Config;
164        use crate::web::AppState;
165        use axum::extract::{Query, State};
166        use axum::response::IntoResponse;
167
168        let config = Config::default();
169        let factory = DefaultClientFactory {
170            chains_config: config.chains.clone(),
171        };
172        let state = std::sync::Arc::new(AppState { config, factory });
173
174        let params = DiscoverQuery {
175            source: "top-boosts".to_string(),
176            chain: None,
177            limit: 5,
178        };
179        let response = handle(State(state), Query(params)).await.into_response();
180        let status = response.status();
181        assert!(status.is_success() || status.is_server_error());
182    }
183
184    #[tokio::test]
185    async fn test_handle_discover_with_chain_filter() {
186        use crate::chains::DefaultClientFactory;
187        use crate::config::Config;
188        use crate::web::AppState;
189        use axum::extract::{Query, State};
190        use axum::response::IntoResponse;
191
192        let config = Config::default();
193        let factory = DefaultClientFactory {
194            chains_config: config.chains.clone(),
195        };
196        let state = std::sync::Arc::new(AppState { config, factory });
197
198        let params = DiscoverQuery {
199            source: "profiles".to_string(),
200            chain: Some("ethereum".to_string()),
201            limit: 10,
202        };
203        let response = handle(State(state), Query(params)).await.into_response();
204        let status = response.status();
205        assert!(status.is_success() || status.is_server_error());
206    }
207
208    #[tokio::test]
209    async fn test_handle_discover_error_path() {
210        use crate::chains::DefaultClientFactory;
211        use crate::config::Config;
212        use crate::web::AppState;
213        use axum::extract::{Query, State};
214        use axum::http::StatusCode;
215        use axum::response::IntoResponse;
216
217        let config = Config::default();
218        let factory = DefaultClientFactory {
219            chains_config: config.chains.clone(),
220        };
221        let state = std::sync::Arc::new(AppState { config, factory });
222
223        let params = DiscoverQuery {
224            source: "unknown-source".to_string(),
225            chain: None,
226            limit: 5,
227        };
228        let response = handle(State(state), Query(params)).await.into_response();
229        let status = response.status();
230        if status == StatusCode::INTERNAL_SERVER_ERROR {
231            let body = axum::body::to_bytes(response.into_body(), 1_000_000)
232                .await
233                .unwrap();
234            let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
235            assert!(json.get("error").is_some());
236        }
237    }
238}