Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions crates/exchanges/binance/src/convert/market_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ pub(crate) fn market_info_from_exchange_symbol(
.quote_asset
.ok_or_else(|| crate::error::missing_field(operation, "quoteAsset"))?,
)
.base_asset_precision(symbol_definition.base_asset_precision)
.quote_precision(symbol_definition.quote_precision)
.quote_asset_precision(symbol_definition.quote_asset_precision)
.trading_permissions(trading_permissions)
.trading_constraints(trading_constraints)
.build()
Expand Down Expand Up @@ -458,3 +461,61 @@ pub(crate) fn klines_from_rows(
})
.collect()
}

#[cfg(test)]
mod tests {
use super::market_info_from_exchange_symbol;
use binance_sdk::spot::rest_api::{
ExchangeInfoResponseSymbolsInner, ExchangeInfoSymbolStatusEnum, LotSizeFilter, PriceFilter,
SymbolFilters,
};
use rust_decimal::Decimal;
use std::str::FromStr;

#[test]
fn market_info_preserves_symbol_precision_fields() {
let symbol = ExchangeInfoResponseSymbolsInner {
symbol: Some(String::from("BTCUSDT")),
status: Some(ExchangeInfoSymbolStatusEnum::Trading.as_str().to_string()),
base_asset: Some(String::from("BTC")),
base_asset_precision: Some(8),
quote_asset: Some(String::from("USDT")),
quote_precision: Some(8),
quote_asset_precision: Some(8),
order_types: Some(vec![String::from("MARKET")]),
quote_order_qty_market_allowed: Some(true),
is_spot_trading_allowed: Some(true),
filters: Some(vec![
SymbolFilters::PriceFilter(Box::new(PriceFilter {
filter_type: Some(String::from("PRICE_FILTER")),
price_exponent: None,
min_price: Some(String::from("0.01000000")),
max_price: Some(String::from("1000000.00000000")),
tick_size: Some(String::from("0.01000000")),
})),
SymbolFilters::LotSize(Box::new(LotSizeFilter {
filter_type: Some(String::from("LOT_SIZE")),
qty_exponent: None,
min_qty: Some(String::from("0.00001000")),
max_qty: Some(String::from("9000.00000000")),
step_size: Some(String::from("0.00001000")),
})),
]),
..ExchangeInfoResponseSymbolsInner::new()
};

let market = market_info_from_exchange_symbol(symbol, "spot.exchange_info")
.expect("market should convert");

assert_eq!(market.base_asset_precision, Some(8));
assert_eq!(market.quote_precision, Some(8));
assert_eq!(market.quote_asset_precision, Some(8));
assert_eq!(
market
.trading_constraints
.price_filter
.and_then(|filter| filter.tick_size),
Some(Decimal::from_str("0.01").expect("decimal"))
);
}
}
41 changes: 33 additions & 8 deletions crates/exchanges/binance/src/market_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,8 @@ impl BinanceMarketData {
pub(crate) fn new(inner: Arc<BinanceInner>) -> Self {
Self { inner }
}
}

#[async_trait]
impl MarketData for BinanceMarketData {
async fn markets(&self) -> Result<Vec<MarketInfo>> {
let params = ExchangeInfoParams::builder()
.show_permission_sets(false)
.build()
.map_err(|err| error::adapter_error(EXCHANGE_INFO_OPERATION, err.to_string()))?;
async fn exchange_info(&self, params: ExchangeInfoParams) -> Result<Vec<MarketInfo>> {
let response = self
.inner
.spot_rest
Expand All @@ -51,6 +44,38 @@ impl MarketData for BinanceMarketData {
})
.collect()
}
}

#[async_trait]
impl MarketData for BinanceMarketData {
async fn markets(&self) -> Result<Vec<MarketInfo>> {
let params = ExchangeInfoParams::builder()
.show_permission_sets(false)
.build()
.map_err(|err| error::adapter_error(EXCHANGE_INFO_OPERATION, err.to_string()))?;
self.exchange_info(params).await
}

async fn market(&self, symbol: &Symbol) -> Result<Option<MarketInfo>> {
let params = ExchangeInfoParams::builder()
.symbol(convert::require_spot_symbol(
symbol,
EXCHANGE_INFO_OPERATION,
)?)
.show_permission_sets(false)
.build()
.map_err(|err| error::adapter_error(EXCHANGE_INFO_OPERATION, err.to_string()))?;
let mut markets = self.exchange_info(params).await?;
match markets.len() {
0 => Ok(None),
1 => Ok(markets.pop()),
_ => Err(error::invalid_field(
EXCHANGE_INFO_OPERATION,
"symbol",
"expected a single-symbol exchange info response",
)),
}
}

async fn last_price(&self, symbol: &Symbol) -> Result<LastPrice> {
let mut prices = self.last_prices(Some(std::slice::from_ref(symbol))).await?;
Expand Down
4 changes: 4 additions & 0 deletions crates/mkt-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ secrecy.workspace = true
strum.workspace = true
strum_macros.workspace = true
thiserror.workspace = true

[dev-dependencies]
time.workspace = true
tokio.workspace = true
2 changes: 2 additions & 0 deletions crates/mkt-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod capabilities;
mod config;
pub mod error;
mod handle;
mod market_data_ext;
mod stream;
mod traits;

Expand All @@ -14,6 +15,7 @@ pub use capabilities::{
pub use config::{ApiCredentials, ExchangeConfig, SecretString};
pub use error::{CapabilityUnavailableReason, Error, ErrorKind, Result};
pub use handle::{Builder, ExchangeHandle};
pub use market_data_ext::MarketDataExt;
pub use secrecy::ExposeSecret;
pub use stream::{
EventStream, MarketDataEvent, PrivateEvent, PrivateEventStream, PrivateSubscription,
Expand Down
Loading
Loading