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