tycho_simulation/rfq/protocols/native/
feed.rs1use std::{collections::HashSet, future::Future, sync::Arc};
2
3use tokio::time::Duration;
4use tycho_common::Bytes;
5
6use crate::{
7 book::{BookFeedConfig, BookSnapshot, ReceivedAt},
8 rfq::{
9 constants::DEFAULT_QUOTE_TIMEOUT,
10 protocols::native::{
11 client::NativeClient, models::NativeSupportedChain, source::NativeBookSource,
12 FALLBACK_PROTOCOL_SYSTEM, PROTOCOL_SYSTEM,
13 },
14 },
15 snapshot_feed::{
16 errors::FeedError,
17 http::{default_http_feed_config, run_http_poll_feed, HttpFeedConfig},
18 Publisher, SnapshotFeed,
19 },
20};
21
22#[derive(Clone, Debug)]
25pub struct NativeFeed {
26 feed_config: HttpFeedConfig,
27 source: NativeBookSource,
28}
29
30pub struct NativeFeedBuilder {
32 protocol_system: &'static str,
33 book_config: BookFeedConfig,
34 all_pairs: bool,
35 usd_quote_tokens: Arc<HashSet<Bytes>>,
36 api_key: String,
37 feed_config: HttpFeedConfig,
38 quote_timeout: Duration,
39}
40
41impl NativeFeedBuilder {
42 pub fn new(
43 book_config: BookFeedConfig,
44 usd_quote_tokens: impl Into<Arc<HashSet<Bytes>>>,
45 api_key: String,
46 ) -> Self {
47 Self {
48 book_config,
49 usd_quote_tokens: usd_quote_tokens.into(),
50 api_key,
51 feed_config: Self::default_feed_config(),
52 quote_timeout: DEFAULT_QUOTE_TIMEOUT,
53 protocol_system: PROTOCOL_SYSTEM,
54 all_pairs: false,
55 }
56 }
57
58 pub fn default_feed_config() -> HttpFeedConfig {
72 default_http_feed_config()
73 }
74
75 pub fn feed_config(mut self, feed_config: HttpFeedConfig) -> Self {
77 self.feed_config = feed_config;
78 self
79 }
80
81 pub fn all_pairs(mut self) -> Self {
87 self.all_pairs = true;
88 self
89 }
90
91 pub fn quote_timeout(mut self, timeout: Duration) -> Self {
93 self.quote_timeout = timeout;
94 self
95 }
96
97 pub fn with_fallback_router(mut self) -> Self {
104 self.protocol_system = FALLBACK_PROTOCOL_SYSTEM;
105 self
106 }
107
108 pub fn build(self) -> Result<NativeFeed, FeedError> {
110 let chain =
111 NativeSupportedChain::try_from(self.book_config.chain).map_err(FeedError::Fatal)?;
112 Ok(NativeFeed {
113 feed_config: self.feed_config,
114 source: NativeBookSource {
115 protocol_system: self.protocol_system,
116 all_pairs: self.all_pairs,
117 client: Arc::new(NativeClient::new(None, chain, self.api_key, self.quote_timeout)),
118 book_config: self.book_config,
119 usd_quote_tokens: self.usd_quote_tokens,
120 },
121 })
122 }
123}
124
125impl SnapshotFeed for NativeFeed {
126 type Snapshot = BookSnapshot<ReceivedAt>;
127 type Error = FeedError;
128
129 fn run(
130 self,
131 publisher: Publisher<Self::Snapshot>,
132 ) -> impl Future<Output = Result<(), FeedError>> + Send + 'static {
133 run_http_poll_feed(self.feed_config, publisher, self.source)
134 }
135}
136
137#[cfg(test)]
138mod tests {
139 use std::collections::HashMap;
140
141 use tycho_common::models::Chain;
142
143 use super::*;
144
145 #[test]
146 fn building_for_a_chain_native_does_not_serve_is_fatal() {
147 let config = BookFeedConfig {
148 chain: Chain::Polygon,
149 tokens: Arc::new(HashMap::new()),
150 min_tvl_usd: 0.0,
151 };
152
153 let error = NativeFeedBuilder::new(config, HashSet::new(), "key".to_string())
154 .build()
155 .unwrap_err();
156
157 assert!(error.is_fatal(), "{error}");
158 }
159}