use std::{future::Future, num::NonZeroUsize, time::Duration};
use reqwest::Url;
use tycho_common::models::Chain;
use crate::{
book::{chain_id, BookFeedConfig, BookSnapshot, ReceivedAt},
pamm::protocols::metric::{
client::{MetricClient, DEFAULT_BID_ASK_CONCURRENCY},
models::MetricPrices,
source::{MetricBookAssembler, MetricBookSource, MetricPricesSource},
FALLBACK_PROTOCOL_SYSTEM, PROTOCOL_SYSTEM,
},
snapshot_feed::{
errors::FeedError,
http::{default_http_feed_config, run_http_poll_feed, HttpFeedConfig},
Publisher, SnapshotFeed,
},
};
#[derive(Debug, Clone)]
pub struct MetricFeed {
feed_config: HttpFeedConfig,
source: MetricBookSource,
}
pub struct MetricFeedBuilder {
assembler_builder: MetricBookAssemblerBuilder,
assembler: Option<MetricBookAssembler>,
base_url: Option<String>,
api_key: String,
bid_ask_concurrency: NonZeroUsize,
feed_config: HttpFeedConfig,
}
impl MetricFeedBuilder {
pub fn new(book_config: BookFeedConfig, api_key: String) -> Self {
Self {
assembler_builder: MetricBookAssemblerBuilder::new(book_config),
assembler: None,
base_url: None,
api_key,
bid_ask_concurrency: DEFAULT_BID_ASK_CONCURRENCY,
feed_config: Self::default_feed_config(),
}
}
pub fn with_assembler(mut self, assembler: MetricBookAssembler) -> Self {
self.assembler = Some(assembler);
self
}
pub fn base_url(mut self, base_url: String) -> Self {
self.base_url = Some(base_url);
self
}
pub fn bid_ask_concurrency(mut self, requests_in_flight: NonZeroUsize) -> Self {
self.bid_ask_concurrency = requests_in_flight;
self
}
pub fn default_feed_config() -> HttpFeedConfig {
HttpFeedConfig {
poll_interval: Duration::from_secs(2),
request_timeout: Duration::from_secs(5),
max_snapshot_age: Some(Duration::from_secs(15)),
..default_http_feed_config()
}
}
pub fn with_fallback_router(mut self) -> Self {
self.assembler_builder = self
.assembler_builder
.with_fallback_router();
self
}
pub fn feed_config(mut self, feed_config: HttpFeedConfig) -> Self {
self.feed_config = feed_config;
self
}
pub fn build(self) -> Result<MetricFeed, FeedError> {
let chain = self.assembler_builder.book_config.chain;
let mut prices_builder = MetricPricesFeedBuilder::new(chain, self.api_key)
.bid_ask_concurrency(self.bid_ask_concurrency)
.feed_config(self.feed_config);
if let Some(base_url) = self.base_url {
prices_builder = prices_builder.base_url(base_url);
}
let MetricPricesFeed { feed_config, source: prices } = prices_builder.build()?;
let assembler = match self.assembler {
Some(assembler) => assembler,
None => self.assembler_builder.build(),
};
Ok(MetricFeed { feed_config, source: MetricBookSource { prices, assembler } })
}
}
pub struct MetricBookAssemblerBuilder {
protocol_system: &'static str,
book_config: BookFeedConfig,
}
impl MetricBookAssemblerBuilder {
pub fn new(book_config: BookFeedConfig) -> Self {
Self { protocol_system: PROTOCOL_SYSTEM, book_config }
}
pub fn with_fallback_router(mut self) -> Self {
self.protocol_system = FALLBACK_PROTOCOL_SYSTEM;
self
}
pub fn build(self) -> MetricBookAssembler {
MetricBookAssembler { protocol_system: self.protocol_system, book_config: self.book_config }
}
}
impl SnapshotFeed for MetricFeed {
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)
}
}
fn check_api_key(api_key: &str) -> Result<(), FeedError> {
if api_key.is_empty() {
return Err(FeedError::InvalidInput("Metric trading key is empty".to_string()));
}
Ok(())
}
fn check_base_url(base_url: &str) -> Result<(), FeedError> {
Url::parse(base_url)
.map(drop)
.map_err(|error| FeedError::InvalidInput(format!("Metric base URL {base_url}: {error}")))
}
#[derive(Debug, Clone)]
pub struct MetricPricesFeed {
feed_config: HttpFeedConfig,
source: MetricPricesSource,
}
pub struct MetricPricesFeedBuilder {
chain: Chain,
api_key: String,
base_url: Option<String>,
bid_ask_concurrency: NonZeroUsize,
feed_config: HttpFeedConfig,
}
impl MetricPricesFeedBuilder {
pub fn new(chain: Chain, api_key: String) -> Self {
Self {
chain,
api_key,
base_url: None,
bid_ask_concurrency: DEFAULT_BID_ASK_CONCURRENCY,
feed_config: MetricFeedBuilder::default_feed_config(),
}
}
pub fn base_url(mut self, base_url: String) -> Self {
self.base_url = Some(base_url);
self
}
pub fn bid_ask_concurrency(mut self, requests_in_flight: NonZeroUsize) -> Self {
self.bid_ask_concurrency = requests_in_flight;
self
}
pub fn feed_config(mut self, feed_config: HttpFeedConfig) -> Self {
self.feed_config = feed_config;
self
}
pub fn build(self) -> Result<MetricPricesFeed, FeedError> {
check_api_key(&self.api_key)?;
let chain_id = chain_id(self.chain)?;
if let Some(base_url) = &self.base_url {
check_base_url(base_url)?;
}
let client = MetricClient::new(
self.base_url.as_deref(),
chain_id,
self.api_key,
self.bid_ask_concurrency,
);
Ok(MetricPricesFeed {
feed_config: self.feed_config,
source: MetricPricesSource::new(client),
})
}
}
impl SnapshotFeed for MetricPricesFeed {
type Snapshot = MetricPrices;
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)
}
}
#[cfg(test)]
mod tests {
use std::{collections::HashMap, sync::Arc};
use futures::StreamExt;
use tokio::time::timeout;
use super::*;
use crate::{
book::{assert_same_books, chain_without_id},
pamm::protocols::metric::test_support::{
pool_server, pools_of, prices_source, weth_usdc_book_config, BELOW_FLOOR, FIRST_POOL,
UNQUOTABLE,
},
snapshot_feed::{http::HttpSource, SnapshotFeedEvent, SnapshotFeedStream},
};
fn builder(chain: Chain) -> MetricFeedBuilder {
MetricFeedBuilder::new(
BookFeedConfig { chain, tokens: Arc::new(HashMap::new()), min_tvl_usd: 0.0 },
"secret_key".to_string(),
)
}
#[test]
fn debug_output_omits_the_api_key() {
let feed = builder(Chain::Ethereum)
.base_url("https://metric.example".to_string())
.build()
.unwrap();
let rendered = format!("{feed:?}");
assert!(!rendered.contains("secret_key"));
assert!(rendered.contains("metric.example"));
}
#[test]
fn a_base_url_that_is_not_one_is_rejected() {
let error = builder(Chain::Ethereum)
.base_url("api.metric.xyz".to_string())
.build()
.unwrap_err();
assert!(matches!(error, FeedError::InvalidInput(_)), "{error}");
}
#[test]
fn an_empty_trading_key_is_rejected() {
let config = BookFeedConfig {
chain: Chain::Ethereum,
tokens: Arc::new(HashMap::new()),
min_tvl_usd: 0.0,
};
let error = MetricFeedBuilder::new(config, String::new())
.build()
.unwrap_err();
assert!(matches!(error, FeedError::InvalidInput(_)), "{error}");
}
#[tokio::test]
async fn the_prices_feed_publishes_each_poll() {
let server = pool_server().await;
let feed = MetricPricesFeedBuilder::new(Chain::Ethereum, "key".to_string())
.base_url(server.url())
.build()
.unwrap();
let mut stream = SnapshotFeedStream::spawn("test", feed);
let event = timeout(Duration::from_secs(5), stream.next())
.await
.expect("the feed publishes within the timeout")
.expect("the stream is not over");
let SnapshotFeedEvent::Published(prices) = event else {
panic!("expected the first poll to be published");
};
assert_eq!(pools_of(&prices), [FIRST_POOL, UNQUOTABLE, BELOW_FLOOR]);
}
#[tokio::test]
async fn the_assembler_builder_makes_the_books_the_feed_builder_does() {
let server = pool_server().await;
let prices = prices_source(Chain::Ethereum, &server.url(), "key")
.fetch()
.await
.unwrap();
let feed = MetricFeedBuilder::new(weth_usdc_book_config(), "key".to_string())
.with_fallback_router()
.build()
.unwrap();
let assembler = MetricBookAssemblerBuilder::new(weth_usdc_book_config())
.with_fallback_router()
.build();
let books = assembler.books(prices.clone());
assert_eq!(books.len(), 1);
assert_same_books(&books, &feed.source.assembler.books(prices));
}
#[test]
fn the_feed_makes_its_books_with_the_assembler_it_is_given() {
let assembler = MetricBookAssemblerBuilder::new(weth_usdc_book_config())
.with_fallback_router()
.build();
let feed = MetricFeedBuilder::new(weth_usdc_book_config(), "key".to_string())
.with_assembler(assembler)
.build()
.unwrap();
assert_eq!(feed.source.assembler.protocol_system, FALLBACK_PROTOCOL_SYSTEM);
}
#[test]
fn the_prices_feed_rejects_an_empty_key() {
let error = MetricPricesFeedBuilder::new(Chain::Ethereum, String::new())
.build()
.unwrap_err();
assert!(matches!(error, FeedError::InvalidInput(_)), "{error}");
}
#[test]
fn the_prices_feed_refuses_a_chain_with_no_numeric_chain_id() {
let error = MetricPricesFeedBuilder::new(chain_without_id(), "key".to_string())
.build()
.unwrap_err();
assert!(error.is_fatal(), "{error}");
assert!(
error
.to_string()
.contains("no chain id"),
"{error}"
);
}
#[test]
fn the_prices_feed_runs_on_the_feed_tuning_it_is_given() {
let tuned = HttpFeedConfig {
poll_interval: Duration::from_secs(7),
..MetricFeedBuilder::default_feed_config()
};
let feed = MetricPricesFeedBuilder::new(Chain::Ethereum, "key".to_string())
.feed_config(tuned)
.build()
.unwrap();
assert_eq!(feed.feed_config.poll_interval, Duration::from_secs(7));
}
#[test]
fn the_prices_feed_rejects_an_invalid_base_url() {
let error = MetricPricesFeedBuilder::new(Chain::Ethereum, "secret_key".to_string())
.base_url("api.metric.xyz".to_string())
.build()
.unwrap_err();
assert!(matches!(error, FeedError::InvalidInput(_)), "{error}");
}
#[test]
fn the_prices_feed_debug_output_omits_the_api_key() {
let feed = MetricPricesFeedBuilder::new(Chain::Ethereum, "secret_key".to_string())
.base_url("https://metric.example".to_string())
.build()
.unwrap();
let rendered = format!("{feed:?}");
assert!(!rendered.contains("secret_key"));
assert!(rendered.contains("metric.example"));
}
}