use std::{collections::HashSet, future::Future, sync::Arc};
use tokio::time::Duration;
use tycho_common::{models::Chain, Bytes};
use crate::{
book::{BookFeedConfig, BookSnapshot, ReceivedAt},
rfq::{
constants::DEFAULT_QUOTE_TIMEOUT,
protocols::euclid::{
client::{EuclidClient, EuclidLevelsClient, EUCLID_API_URL},
models::{EuclidPrices, EuclidSupportedChain},
source::{EuclidBookAssembler, EuclidBookSource, EuclidPricesSource},
},
},
snapshot_feed::{
errors::FeedError,
ws::{default_ws_feed_config, run_ws_feed, WsFeedConfig},
Publisher, SnapshotFeed,
},
};
#[derive(Clone, Debug)]
pub struct EuclidFeed {
feed_config: WsFeedConfig,
source: EuclidBookSource,
}
pub struct EuclidFeedBuilder {
assembler_builder: EuclidBookAssemblerBuilder,
assembler: Option<EuclidBookAssembler>,
feed_config: WsFeedConfig,
}
impl EuclidFeedBuilder {
pub fn new(
book_config: BookFeedConfig,
usd_quote_tokens: impl Into<Arc<HashSet<Bytes>>>,
) -> Self {
Self {
assembler_builder: EuclidBookAssemblerBuilder::new(book_config, usd_quote_tokens),
assembler: None,
feed_config: Self::default_feed_config(),
}
}
pub fn with_assembler(mut self, assembler: EuclidBookAssembler) -> Self {
self.assembler = Some(assembler);
self
}
pub fn all_pairs(mut self) -> Self {
self.assembler_builder = self.assembler_builder.all_pairs();
self
}
pub fn api_url(mut self, api_url: String) -> Self {
self.assembler_builder = self.assembler_builder.api_url(api_url);
self
}
pub fn quote_timeout(mut self, timeout: Duration) -> Self {
self.assembler_builder = self
.assembler_builder
.quote_timeout(timeout);
self
}
pub fn default_feed_config() -> WsFeedConfig {
default_ws_feed_config()
}
pub fn feed_config(mut self, feed_config: WsFeedConfig) -> Self {
self.feed_config = feed_config;
self
}
pub fn build(self) -> Result<EuclidFeed, FeedError> {
let EuclidPricesFeed { feed_config, source: prices } =
EuclidPricesFeedBuilder::new(self.assembler_builder.book_config.chain)
.api_url(self.assembler_builder.api_url.clone())
.feed_config(self.feed_config)
.build()?;
let assembler = match self.assembler {
Some(assembler) => assembler,
None => self.assembler_builder.build()?,
};
Ok(EuclidFeed { feed_config, source: EuclidBookSource { prices, assembler } })
}
}
pub struct EuclidBookAssemblerBuilder {
book_config: BookFeedConfig,
usd_quote_tokens: Arc<HashSet<Bytes>>,
api_url: String,
quote_timeout: Duration,
all_pairs: bool,
}
impl EuclidBookAssemblerBuilder {
pub fn new(
book_config: BookFeedConfig,
usd_quote_tokens: impl Into<Arc<HashSet<Bytes>>>,
) -> Self {
Self {
book_config,
usd_quote_tokens: usd_quote_tokens.into(),
api_url: EUCLID_API_URL.to_string(),
quote_timeout: DEFAULT_QUOTE_TIMEOUT,
all_pairs: false,
}
}
pub fn all_pairs(mut self) -> Self {
self.all_pairs = true;
self
}
pub fn api_url(mut self, api_url: String) -> Self {
self.api_url = api_url;
self
}
pub fn quote_timeout(mut self, timeout: Duration) -> Self {
self.quote_timeout = timeout;
self
}
pub fn build(self) -> Result<EuclidBookAssembler, FeedError> {
let chain =
EuclidSupportedChain::try_from(self.book_config.chain).map_err(FeedError::Fatal)?;
Ok(EuclidBookAssembler {
chain,
book_config: self.book_config,
usd_quote_tokens: self.usd_quote_tokens,
client: Arc::new(EuclidClient::new(chain, &self.api_url, self.quote_timeout)),
all_pairs: self.all_pairs,
})
}
}
impl SnapshotFeed for EuclidFeed {
type Snapshot = BookSnapshot<ReceivedAt>;
type Error = FeedError;
fn run(
self,
publisher: Publisher<Self::Snapshot>,
) -> impl Future<Output = Result<(), FeedError>> + Send + 'static {
run_ws_feed(self.feed_config, publisher, self.source)
}
}
#[derive(Clone, Debug)]
pub struct EuclidPricesFeed {
feed_config: WsFeedConfig,
source: EuclidPricesSource,
}
pub struct EuclidPricesFeedBuilder {
chain: Chain,
api_url: String,
feed_config: WsFeedConfig,
}
impl EuclidPricesFeedBuilder {
pub fn new(chain: Chain) -> Self {
Self {
chain,
api_url: EUCLID_API_URL.to_string(),
feed_config: EuclidFeedBuilder::default_feed_config(),
}
}
pub fn api_url(mut self, api_url: String) -> Self {
self.api_url = api_url;
self
}
pub fn feed_config(mut self, feed_config: WsFeedConfig) -> Self {
self.feed_config = feed_config;
self
}
pub fn build(self) -> Result<EuclidPricesFeed, FeedError> {
let chain = EuclidSupportedChain::try_from(self.chain).map_err(FeedError::Fatal)?;
Ok(EuclidPricesFeed {
feed_config: self.feed_config,
source: EuclidPricesSource::new(EuclidLevelsClient::new(chain, &self.api_url)),
})
}
}
impl SnapshotFeed for EuclidPricesFeed {
type Snapshot = EuclidPrices;
type Error = FeedError;
fn run(
self,
publisher: Publisher<Self::Snapshot>,
) -> impl Future<Output = Result<(), FeedError>> + Send + 'static {
run_ws_feed(self.feed_config, publisher, self.source)
}
}
#[cfg(test)]
mod tests {
use std::{net::SocketAddr, str::FromStr};
use futures::{SinkExt, StreamExt};
use num_bigint::BigUint;
use tokio::net::TcpListener;
use tokio_tungstenite::{accept_async, tungstenite::Message};
use tycho_common::models::protocol::GetAmountOutParams;
use super::*;
use crate::{
book::{
assert_same_books,
component::pair_component_id,
levels::{Levels, PriceLevel},
test_token_map, BookFeedEvent, BookFeedStream,
},
rfq::protocols::euclid::{
models::EuclidBook,
test_support::{LEVELS_FRAME, USDC, WETH},
PROTOCOL_SYSTEM,
},
snapshot_feed::{SnapshotFeedEvent, SnapshotFeedStream},
};
fn book_config(chain: Chain) -> BookFeedConfig {
let (weth, usdc) = (Bytes::from_str(WETH).unwrap(), Bytes::from_str(USDC).unwrap());
BookFeedConfig {
chain,
tokens: Arc::new(test_token_map(&[(&weth, "WETH", 18), (&usdc, "USDC", 6)])),
min_tvl_usd: 0.0,
}
}
fn usd_quote_tokens() -> HashSet<Bytes> {
HashSet::from([Bytes::from_str(USDC).unwrap()])
}
fn weth_usdc_prices() -> EuclidPrices {
let levels = |price| Levels::new(vec![PriceLevel { price, quantity: 1.0 }]).unwrap();
EuclidPrices {
chain_id: 1,
pairs: vec![EuclidBook {
base: Bytes::from_str(WETH).unwrap(),
quote: Bytes::from_str(USDC).unwrap(),
updated_at_ms: 1_757_500_000_000,
bids: levels(3000.0),
asks: levels(3001.0),
}],
}
}
async fn serve_one_frame() -> SocketAddr {
let listener = TcpListener::bind("127.0.0.1:0")
.await
.unwrap();
let address = listener.local_addr().unwrap();
tokio::spawn(async move {
let (stream, _) = listener.accept().await.unwrap();
let mut ws = accept_async(stream).await.unwrap();
ws.send(Message::Text(LEVELS_FRAME.into()))
.await
.unwrap();
tokio::time::sleep(Duration::from_secs(5)).await;
});
address
}
#[test]
fn building_for_a_chain_euclid_does_not_serve_is_fatal() {
let error = EuclidFeedBuilder::new(book_config(Chain::Base), usd_quote_tokens())
.build()
.unwrap_err();
assert!(matches!(error, FeedError::Fatal(ref message) if message.contains("Euclid")));
}
#[test]
fn building_a_book_assembler_for_a_chain_euclid_does_not_serve_is_fatal() {
let error = EuclidBookAssemblerBuilder::new(book_config(Chain::Base), usd_quote_tokens())
.build()
.unwrap_err();
assert!(error.is_fatal(), "{error}");
}
#[test]
fn the_prices_feed_refuses_a_chain_euclid_does_not_serve() {
let error = EuclidPricesFeedBuilder::new(Chain::Base)
.build()
.unwrap_err();
assert!(error.is_fatal(), "{error}");
}
#[test]
fn the_prices_feed_runs_on_the_feed_tuning_it_is_given() {
let tuned = WsFeedConfig {
read_idle_timeout: Duration::from_secs(7),
..EuclidFeedBuilder::default_feed_config()
};
let feed = EuclidPricesFeedBuilder::new(Chain::Ethereum)
.feed_config(tuned)
.build()
.unwrap();
assert_eq!(feed.feed_config.read_idle_timeout, Duration::from_secs(7));
}
#[test]
fn the_api_url_reaches_the_levels_stream_and_the_quote_client() {
let feed = EuclidFeedBuilder::new(book_config(Chain::Ethereum), usd_quote_tokens())
.api_url("https://rfq.example.com/tycho".to_string())
.build()
.unwrap();
let rendered = format!("{feed:?}");
assert!(rendered.contains("wss://rfq.example.com/tycho/levels?chainId=1"), "{rendered}");
assert!(rendered.contains("https://rfq.example.com/tycho/firm"), "{rendered}");
}
#[test]
fn the_assembler_builder_makes_the_books_the_feed_builder_does() {
let feed = EuclidFeedBuilder::new(book_config(Chain::Ethereum), usd_quote_tokens())
.all_pairs()
.build()
.unwrap();
let assembler =
EuclidBookAssemblerBuilder::new(book_config(Chain::Ethereum), usd_quote_tokens())
.all_pairs()
.build()
.unwrap();
let books = assembler.books(weth_usdc_prices());
assert_eq!(books.len(), 1);
assert_same_books(
&books,
&feed
.source
.assembler
.books(weth_usdc_prices()),
);
}
#[test]
fn the_feed_makes_its_books_with_the_assembler_it_is_given() {
let assembler =
EuclidBookAssemblerBuilder::new(book_config(Chain::Ethereum), usd_quote_tokens())
.all_pairs()
.build()
.unwrap();
let feed = EuclidFeedBuilder::new(book_config(Chain::Ethereum), usd_quote_tokens())
.with_assembler(assembler.clone())
.build()
.unwrap();
assert!(feed.source.assembler.all_pairs);
assert_same_books(
&feed
.source
.assembler
.books(weth_usdc_prices()),
&assembler.books(weth_usdc_prices()),
);
}
#[tokio::test]
async fn a_pushed_frame_is_published_as_a_snapshot() {
let address = serve_one_frame().await;
let feed = EuclidFeedBuilder::new(book_config(Chain::Ethereum), usd_quote_tokens())
.api_url(format!("http://{address}/tycho"))
.build()
.unwrap();
let mut stream = BookFeedStream::spawn(PROTOCOL_SYSTEM, feed);
let event = tokio::time::timeout(Duration::from_secs(5), stream.next())
.await
.expect("a snapshot within five seconds")
.expect("the feed is still running");
let BookFeedEvent::Published(snapshot) = event else {
panic!("expected a published snapshot, got {event:?}");
};
let id = pair_component_id(
PROTOCOL_SYSTEM,
&Bytes::from_str(WETH).unwrap(),
&Bytes::from_str(USDC).unwrap(),
);
let book = &snapshot.books[&id.to_string()];
let [base, quote] = book.component.tokens.as_slice() else {
panic!("expected exactly two tokens")
};
assert!(
book.state
.spot_price(base, quote)
.unwrap() >
0.0
);
}
#[tokio::test]
async fn the_prices_feed_publishes_the_frame_it_receives() {
let address = serve_one_frame().await;
let feed = EuclidPricesFeedBuilder::new(Chain::Ethereum)
.api_url(format!("http://{address}/tycho"))
.build()
.unwrap();
let mut stream = SnapshotFeedStream::spawn(PROTOCOL_SYSTEM, feed);
let event = tokio::time::timeout(Duration::from_secs(5), stream.next())
.await
.expect("prices within five seconds")
.expect("the feed is still running");
let SnapshotFeedEvent::Published(prices) = event else {
panic!("expected published prices, got {event:?}");
};
assert_eq!(prices.chain_id, 1);
assert_eq!(prices.pairs.len(), 1);
assert_eq!(prices.pairs[0].base, Bytes::from_str(WETH).unwrap());
}
#[tokio::test]
#[ignore = "hits Euclid's live gateway"]
async fn euclid_live_levels_and_quote() {
let config = book_config(Chain::Ethereum);
let feed = EuclidFeedBuilder::new(config.clone(), usd_quote_tokens())
.build()
.unwrap();
let mut stream = BookFeedStream::spawn(PROTOCOL_SYSTEM, feed);
let event = tokio::time::timeout(Duration::from_secs(15), stream.next())
.await
.expect("a snapshot within fifteen seconds")
.expect("the feed is still running");
let BookFeedEvent::Published(snapshot) = event else {
panic!("expected a published snapshot, got {event:?}");
};
let book = snapshot
.books
.values()
.next()
.expect("Euclid prices at least one pair");
let router = Bytes::from_str("0x1644D2477f809cc2C71bCCFd6Dc9497E3F83210d").unwrap();
let quote = book
.state
.as_indicatively_priced()
.unwrap()
.request_signed_quote(GetAmountOutParams {
amount_in: BigUint::from(3_000_000_000_000_000u64),
token_in: book.component.tokens[0].address.clone(),
token_out: book.component.tokens[1].address.clone(),
sender: router.clone(),
receiver: router,
origin: None,
})
.await
.unwrap();
assert!(quote.amount_out > BigUint::ZERO);
assert_eq!(quote.quote_attributes["calldata"][..4], [0x5a, 0x09, 0x98, 0x43]);
}
}