use std::sync::Arc;
use crate::clock::Clock;
use crate::error::{Error, Result};
use crate::recorder::RawTape;
use crate::types::{Symbol, VenueId};
use crate::venue::kraken::Precision;
use crate::venue::{FeedEvent, HttpFetch, Venue};
#[derive(Debug, Clone)]
pub struct VenueConfig {
pub symbols: Vec<Symbol>,
pub kraken_depth: usize,
pub kraken_precision: Option<Precision>,
pub bybit_depth: usize,
pub binance_rest_depth: usize,
pub bitstamp_book_level: crate::types::BookLevel,
}
impl Default for VenueConfig {
fn default() -> Self {
VenueConfig {
symbols: vec![Symbol::new("BTC", "USD")],
kraken_depth: 10,
kraken_precision: None,
bybit_depth: 50,
binance_rest_depth: 1000,
bitstamp_book_level: crate::types::BookLevel::L2,
}
}
}
impl VenueConfig {
pub fn with_symbols(mut self, symbols: Vec<Symbol>) -> Self {
self.symbols = symbols;
self
}
pub fn default_symbol(venue: VenueId) -> Symbol {
match venue {
VenueId::Coinbase | VenueId::Kraken | VenueId::Bitstamp => Symbol::new("BTC", "USD"),
VenueId::Okx | VenueId::Bybit | VenueId::BinanceUs => Symbol::new("BTC", "USDT"),
}
}
pub fn quotes_supported(venue: VenueId, symbol: &Symbol) -> bool {
let quote = symbol.quote();
match venue {
VenueId::Coinbase | VenueId::Kraken | VenueId::Bitstamp => quote != "USDT",
VenueId::Okx | VenueId::Bybit | VenueId::BinanceUs => quote != "USD",
}
}
}
pub async fn build(
id: VenueId,
config: &VenueConfig,
http: Arc<dyn HttpFetch>,
clock: Arc<dyn Clock>,
) -> Result<Arc<dyn Venue>> {
use crate::venue::{
binance_us::BinanceUs, bitstamp::Bitstamp, bybit::Bybit, coinbase::Coinbase,
kraken::Kraken, okx::Okx,
};
let symbols = &config.symbols;
Ok(match id {
VenueId::Coinbase => Arc::new(Coinbase::new(http, clock).with_symbols(symbols)),
VenueId::Kraken => {
let mut kraken = Kraken::new(http, clock, config.kraken_depth)?.with_symbols(symbols);
match config.kraken_precision {
Some(precision) => {
for symbol in symbols {
kraken = kraken.with_precision(symbol.clone(), precision);
}
}
None => kraken.load_precisions(symbols).await?,
}
Arc::new(kraken)
}
VenueId::Okx => Arc::new(Okx::new(http, clock).with_symbols(symbols)),
VenueId::Bybit => {
Arc::new(Bybit::new(http, clock, config.bybit_depth)?.with_symbols(symbols))
}
VenueId::BinanceUs => Arc::new(
BinanceUs::new(http, clock)
.with_rest_depth(config.binance_rest_depth)
.with_symbols(symbols),
),
VenueId::Bitstamp => Arc::new(
Bitstamp::new(http, clock)
.with_book_level(config.bitstamp_book_level)
.with_symbols(symbols),
),
})
}
pub fn build_offline(
id: VenueId,
config: &VenueConfig,
http: Arc<dyn HttpFetch>,
clock: Arc<dyn Clock>,
) -> Result<Arc<dyn Venue>> {
if id == VenueId::Kraken && config.kraken_precision.is_none() {
return Err(Error::Other(
"an offline Kraken needs kraken_precision; its checksum cannot be computed without \
the pair's price and quantity precision"
.to_string(),
));
}
futures_util::future::FutureExt::now_or_never(build(id, config, http, clock))
.ok_or_else(|| Error::Other(format!("{id} needed the network to build offline")))?
}
pub fn delta_frame_indices(venue: &dyn Venue, tape: &RawTape) -> Vec<usize> {
tape.frames
.iter()
.enumerate()
.filter(|(_, recorded)| {
venue
.parse_delta(&recorded.to_raw())
.map(|events| events.iter().any(|e| matches!(e, FeedEvent::Delta(_))))
.unwrap_or(false)
})
.map(|(i, _)| i)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::clock::ManualClock;
use crate::transport::CannedFetch;
fn offline(id: VenueId) -> Arc<dyn Venue> {
let config = VenueConfig::default()
.with_symbols(vec![VenueConfig::default_symbol(id)])
.tap_precision();
build_offline(
id,
&config,
CannedFetch::new().shared(),
Arc::new(ManualClock::default()),
)
.unwrap_or_else(|e| panic!("{id} failed to build offline: {e}"))
}
impl VenueConfig {
fn tap_precision(mut self) -> Self {
self.kraken_precision = Some(Precision { price: 1, qty: 8 });
self
}
}
#[test]
fn every_venue_builds_offline_and_reports_its_own_id() {
for id in VenueId::ALL {
assert_eq!(offline(*id).id(), *id);
}
}
#[test]
fn kraken_refuses_to_build_offline_without_its_precision() {
let err = match build_offline(
VenueId::Kraken,
&VenueConfig::default(),
CannedFetch::new().shared(),
Arc::new(ManualClock::default()),
) {
Ok(_) => panic!("kraken built without the precision it needs"),
Err(e) => e,
};
assert!(err.to_string().contains("kraken_precision"), "{err}");
}
#[test]
fn the_dollar_pair_a_venue_lists_is_stated_not_assumed() {
assert_eq!(
VenueConfig::default_symbol(VenueId::Coinbase),
Symbol::new("BTC", "USD")
);
assert_eq!(
VenueConfig::default_symbol(VenueId::Bybit),
Symbol::new("BTC", "USDT")
);
assert!(VenueConfig::quotes_supported(
VenueId::Kraken,
&Symbol::new("BTC", "USD")
));
assert!(!VenueConfig::quotes_supported(
VenueId::Kraken,
&Symbol::new("BTC", "USDT")
));
assert!(!VenueConfig::quotes_supported(
VenueId::Okx,
&Symbol::new("BTC", "USD")
));
}
}