Skip to main content

fynd_core/algorithm/
request.rs

1//! What an algorithm is given to solve one order.
2
3use std::sync::Arc;
4
5use crate::{
6    derived::SharedDerivedDataRef,
7    feed::market_data::{MarketData, StateLabel},
8    types::{quote::RouteExclusions, Order},
9};
10
11/// A [`SolveRequest`] taken apart, so an algorithm owns the market and the derived data.
12pub struct SolveParts<'a, G> {
13    /// The graph to search, in the algorithm's own `GraphType`.
14    pub graph: &'a G,
15    /// The order to solve.
16    pub order: &'a Order,
17    /// The market to read state from. An algorithm takes its own lock.
18    pub market: MarketData,
19    /// The overlay to read market state through, if the request named one.
20    pub label: Option<StateLabel>,
21    /// The derived data the algorithm declared it needs.
22    pub derived: Option<SharedDerivedDataRef>,
23    /// The pools and tokens this solve must not use.
24    pub exclusions: Arc<RouteExclusions>,
25}
26
27/// One order to solve, and everything the algorithm reads to solve it.
28pub struct SolveRequest<'a, G> {
29    graph: &'a G,
30    market: MarketData,
31    order: &'a Order,
32    label: Option<StateLabel>,
33    derived: Option<SharedDerivedDataRef>,
34    exclusions: Arc<RouteExclusions>,
35}
36
37impl<'a, G> SolveRequest<'a, G> {
38    /// The request's parts, moved out of it.
39    #[must_use]
40    pub fn into_parts(self) -> SolveParts<'a, G> {
41        SolveParts {
42            graph: self.graph,
43            order: self.order,
44            market: self.market,
45            label: self.label,
46            derived: self.derived,
47            exclusions: self.exclusions,
48        }
49    }
50
51    /// A solve against the live market state, with no derived data and nothing excluded.
52    pub fn new(graph: &'a G, market: MarketData, order: &'a Order) -> Self {
53        Self {
54            graph,
55            market,
56            order,
57            label: None,
58            derived: None,
59            exclusions: Arc::new(RouteExclusions::default()),
60        }
61    }
62
63    /// The solve reads market state through this overlay, so the request's component overrides
64    /// apply.
65    #[must_use]
66    pub fn with_label(mut self, label: StateLabel) -> Self {
67        self.label = Some(label);
68        self
69    }
70
71    /// The derived data the algorithm may read: token prices, component depths.
72    #[must_use]
73    pub fn with_derived(mut self, derived: SharedDerivedDataRef) -> Self {
74        self.derived = Some(derived);
75        self
76    }
77
78    /// Liquidity this solve must not route through.
79    #[must_use]
80    pub fn with_exclusions(mut self, exclusions: RouteExclusions) -> Self {
81        self.exclusions = Arc::new(exclusions);
82        self
83    }
84
85    pub(crate) fn with_shared_exclusions(mut self, exclusions: Arc<RouteExclusions>) -> Self {
86        self.exclusions = exclusions;
87        self
88    }
89
90    /// The graph to search, in the algorithm's own `GraphType`.
91    pub fn graph(&self) -> &'a G {
92        self.graph
93    }
94
95    /// The market to read state from. Algorithms take their own locks.
96    pub fn market(&self) -> &MarketData {
97        &self.market
98    }
99
100    /// The order to solve.
101    pub fn order(&self) -> &'a Order {
102        self.order
103    }
104
105    /// The overlay to read market state through, if the request named one.
106    pub fn label(&self) -> Option<&StateLabel> {
107        self.label.as_ref()
108    }
109
110    /// The derived data, if the caller passed any.
111    pub fn derived(&self) -> Option<&SharedDerivedDataRef> {
112        self.derived.as_ref()
113    }
114
115    /// The pools and tokens this solve request must not use.
116    pub fn exclusions(&self) -> &RouteExclusions {
117        &self.exclusions
118    }
119}