use std::{collections::HashSet, future::Future, sync::Arc};
use tokio::time::Duration;
use tycho_common::Bytes;
use crate::{
book::{
all_pairs::{ComponentLayout, QuoteRule},
chain_id, BookFeedConfig, BookSnapshot, ReceivedAt,
},
rfq::{
constants::DEFAULT_QUOTE_TIMEOUT,
protocols::liquorice::{
client::LiquoriceClient, source::LiquoriceBookSource, FALLBACK_PROTOCOL_SYSTEM,
PROTOCOL_SYSTEM,
},
},
snapshot_feed::{
errors::FeedError,
http::{default_http_feed_config, run_http_poll_feed, HttpFeedConfig},
Publisher, SnapshotFeed,
},
};
#[derive(Clone, Debug)]
pub struct LiquoriceFeed {
feed_config: HttpFeedConfig,
source: LiquoriceBookSource,
}
pub struct LiquoriceFeedBuilder {
protocol_system: &'static str,
book_config: BookFeedConfig,
component_layout: ComponentLayout<QuoteRule>,
usd_quote_tokens: Arc<HashSet<Bytes>>,
auth_solver: String,
auth_key: String,
feed_config: HttpFeedConfig,
quote_timeout: Duration,
quote_expiry_secs: u64,
}
impl LiquoriceFeedBuilder {
pub fn new(
book_config: BookFeedConfig,
usd_quote_tokens: impl Into<Arc<HashSet<Bytes>>>,
auth_solver: String,
auth_key: String,
) -> Self {
Self {
book_config,
usd_quote_tokens: usd_quote_tokens.into(),
auth_solver,
auth_key,
feed_config: Self::default_feed_config(),
quote_timeout: DEFAULT_QUOTE_TIMEOUT,
component_layout: ComponentLayout::PerPair,
quote_expiry_secs: 300,
protocol_system: PROTOCOL_SYSTEM,
}
}
pub fn default_feed_config() -> HttpFeedConfig {
default_http_feed_config()
}
pub fn feed_config(mut self, feed_config: HttpFeedConfig) -> Self {
self.feed_config = feed_config;
self
}
pub fn all_pairs(mut self, rule: QuoteRule) -> Self {
self.component_layout = ComponentLayout::AllPairs(rule);
self
}
pub fn quote_timeout(mut self, timeout: Duration) -> Self {
self.quote_timeout = timeout;
self
}
pub fn quote_expiry_secs(mut self, secs: u64) -> Self {
self.quote_expiry_secs = secs;
self
}
pub fn with_fallback_router(mut self) -> Self {
self.protocol_system = FALLBACK_PROTOCOL_SYSTEM;
self
}
pub fn build(self) -> Result<LiquoriceFeed, FeedError> {
let chain_id = chain_id(self.book_config.chain)?;
Ok(LiquoriceFeed {
feed_config: self.feed_config,
source: LiquoriceBookSource {
protocol_system: self.protocol_system,
component_layout: self.component_layout,
client: Arc::new(LiquoriceClient::new(
None,
chain_id,
self.auth_solver,
self.auth_key,
self.quote_timeout,
self.quote_expiry_secs,
)),
book_config: self.book_config,
usd_quote_tokens: self.usd_quote_tokens,
},
})
}
}
impl SnapshotFeed for LiquoriceFeed {
type Snapshot = BookSnapshot<ReceivedAt>;
type Error = FeedError;
fn run(
self,
publisher: Publisher<Self::Snapshot>,
) -> impl Future<Output = Result<(), FeedError>> + Send + 'static {
run_http_poll_feed(self.feed_config, publisher, self.source)
}
}