jup_sdk/
router.rs

1/// An abstract module for Jupiter routing.
2use crate::types::QuoteResponse;
3
4/// Route analysis result for comparison and selection of optimal routes
5#[derive(Debug, Clone)]
6pub struct RouteAnalysis {
7    pub best_route: QuoteResponse,
8    pub alternative_routes: Vec<QuoteResponse>,
9    pub estimated_time: f64,
10    pub confidence_score: f64,
11}
12
13impl RouteAnalysis {
14    /// Creates a new RouteAnalysis with the specified best route
15    ///
16    /// # Examples
17    ///
18    /// ```
19    /// use crate::types::QuoteResponse;
20    /// use crate::router::RouteAnalysis;
21    ///
22    /// let quote_response = QuoteResponse::default();
23    /// let analysis = RouteAnalysis::new(quote_response);
24    /// ```
25    pub fn new(best_route: QuoteResponse) -> Self {
26        Self {
27            best_route,
28            alternative_routes: Vec::new(),
29            estimated_time: 0.0,
30            confidence_score: 1.0,
31        }
32    }
33}
34
35/// Route optimizer for selecting and scoring trading routes
36pub struct RouteOptimizer;
37
38impl RouteOptimizer {
39    /// Selects the best route from a list of routes based on weighted criteria
40    ///
41    /// # Arguments
42    ///
43    /// routes - Slice of QuoteResponse to evaluate
44    /// weights - RouteWeights configuration for scoring
45    ///
46    /// # Examples
47    ///
48    /// ```
49    /// use crate::router::{RouteOptimizer, RouteWeights};
50    /// use crate::types::QuoteResponse;
51    ///
52    /// let routes = vec![QuoteResponse::default(), QuoteResponse::default()];
53    /// let weights = RouteWeights::default();
54    /// let best_route = RouteOptimizer::select_best_route(&routes, &weights);
55    /// ```
56    pub fn select_best_route<'a>(
57        routes: &'a [QuoteResponse],
58        weights: &'a RouteWeights,
59    ) -> Option<&'a QuoteResponse> {
60        routes.iter().max_by(|a, b| {
61            let score_a = Self::cal_route_score(a, weights);
62            let score_b = Self::cal_route_score(b, weights);
63            score_a
64                .partial_cmp(&score_b)
65                .unwrap_or(std::cmp::Ordering::Equal)
66        })
67    }
68
69    /// Calculates a comprehensive score for a route based on multiple factors
70    ///
71    /// # Arguments
72    ///
73    /// route - The QuoteResponse to score
74    /// weights - Weight configuration for different scoring factors
75    ///
76    /// # Examples
77    ///
78    /// ```
79    /// use crate::router::{RouteOptimizer, RouteWeights};
80    /// use crate::types::QuoteResponse;
81    ///
82    /// let route = QuoteResponse::default();
83    /// let weights = RouteWeights::default();
84    /// let score = RouteOptimizer::cal_route_score(&route, &weights);
85    /// ```
86    fn cal_route_score(route: &QuoteResponse, weights: &RouteWeights) -> f64 {
87        let mut score = 0.0;
88        if let Ok(price_impact) = route.price_impact_pct.parse::<f64>() {
89            score += (100.0 - price_impact.max(0.0)) * weights.price_impact;
90        }
91        score += (1000.0 - route.time_taken.max(0.0)) * weights.execution_speed;
92        let complexity = 1.0 / (route.route_plan.len() as f64).max(1.0);
93        score += complexity * weights.simplicity;
94        score
95    }
96}
97
98/// Weight configuration for route scoring criteria
99#[derive(Debug, Clone)]
100pub struct RouteWeights {
101    pub price_impact: f64,
102    pub execution_speed: f64,
103    pub simplicity: f64,
104}
105
106impl Default for RouteWeights {
107    /// Creates default RouteWeights with balanced priorities
108    ///
109    /// Default weights prioritize price impact (0.6), followed by execution speed (0.3)
110    /// and simplicity (0.1)
111    fn default() -> Self {
112        Self {
113            price_impact: 0.6,
114            execution_speed: 0.3,
115            simplicity: 0.1,
116        }
117    }
118}