Skip to content

Commit

Permalink
Switch connector to async
Browse files Browse the repository at this point in the history
  • Loading branch information
noahbclarkson committed Dec 2, 2024
1 parent 2215195 commit b6822a7
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 368 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
binance = { git = "https://github.com/wisespace-io/binance-rs.git" }
binance-rs-async = "1.3.3"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"
chrono = "0.4"
Expand All @@ -24,6 +24,5 @@ linfa-pls = "0.7"
ndarray = "0.15"
derive_builder = "0.20"
genevo = "0.7"

[dev-dependencies]
tempfile = "3.14"
tokio = { version = "1.0", features = ["full"] }
futures = "0.3"
10 changes: 4 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl KryptoConfig {
///
/// A `Result` containing the `KryptoConfig` on success or an `Error` on failure.
#[instrument(level = "info", skip(filename))]
pub fn read_config<P: AsRef<Path>>(filename: Option<P>) -> Result<Self, KryptoError> {
pub async fn read_config<P: AsRef<Path>>(filename: Option<P>) -> Result<Self, KryptoError> {
let path = filename
.map(|p| p.as_ref().to_path_buf())
.unwrap_or_else(|| Path::new("config.yml").to_path_buf());
Expand All @@ -138,16 +138,14 @@ impl KryptoConfig {
let file = File::open(&path)?;
let reader = BufReader::new(file);
let config: Self = from_reader(reader)?;
let account: Account = config.get_binance();
let account = config.get_binance::<Account>();
if config.api_key.is_some() || config.api_secret.is_some() {
let account_info = account.get_account().map_err(|e| {
let account_info = account.get_account().await.map_err(|e| {
error!("Failed to get account info: {}", e);
KryptoError::BinanceApiError(e.to_string())
})?;
for asset in account_info.balances {
let free = asset.free.parse::<f64>().unwrap_or(0.0);
let locked = asset.locked.parse::<f64>().unwrap_or(0.0);
if free + locked > 0.0 {
if asset.free + asset.locked > 0.0 {
info!(
"Asset: {}, Free: {}, Locked: {}",
asset.asset, asset.free, asset.locked
Expand Down
149 changes: 7 additions & 142 deletions src/data/candlestick.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use binance::model::{KlineSummaries, KlineSummary};
use binance::rest_model::{KlineSummaries, KlineSummary};
use chrono::{DateTime, TimeZone, Utc};
use derive_builder::Builder;

Expand Down Expand Up @@ -48,29 +48,11 @@ impl Candlestick {
Ok(Self {
open_time,
close_time,
open: summary.open.parse().map_err(|_| KryptoError::ParseError {
value_name: "open".to_string(),
timestamp: summary.open_time,
})?,
high: summary.high.parse().map_err(|_| KryptoError::ParseError {
value_name: "high".to_string(),
timestamp: summary.open_time,
})?,
low: summary.low.parse().map_err(|_| KryptoError::ParseError {
value_name: "low".to_string(),
timestamp: summary.open_time,
})?,
close: summary.close.parse().map_err(|_| KryptoError::ParseError {
value_name: "close".to_string(),
timestamp: summary.open_time,
})?,
volume: summary
.volume
.parse()
.map_err(|_| KryptoError::ParseError {
value_name: "volume".to_string(),
timestamp: summary.open_time,
})?,
open: summary.open,
high: summary.high,
low: summary.low,
close: summary.close,
volume: summary.volume,
})
}

Expand Down Expand Up @@ -139,121 +121,4 @@ impl PartialOrd for Candlestick {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.open_time.partial_cmp(&other.open_time)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::error::KryptoError;
use chrono::TimeZone;

#[test]
fn test_candlestick_from_summary() {
let summary = KlineSummary {
open_time: 1618185600000,
close_time: 1618185659999,
open: "0.00000000".to_string(),
high: "0.00000000".to_string(),
low: "0.00000000".to_string(),
close: "0.00000000".to_string(),
volume: "0.00000000".to_string(),
quote_asset_volume: "0.00000000".to_string(),
number_of_trades: 0,
taker_buy_base_asset_volume: "0.00000000".to_string(),
taker_buy_quote_asset_volume: "0.00000000".to_string(),
};

let candlestick = Candlestick::from_summary(summary).unwrap();
assert_eq!(
candlestick.open_time,
Utc.timestamp_millis_opt(1618185600000).single().unwrap()
);
assert_eq!(
candlestick.close_time,
Utc.timestamp_millis_opt(1618185659999).single().unwrap()
);
assert_eq!(candlestick.open, 0.0);
assert_eq!(candlestick.high, 0.0);
assert_eq!(candlestick.low, 0.0);
assert_eq!(candlestick.close, 0.0);
assert_eq!(candlestick.volume, 0.0);
}

#[test]
fn test_candlestick_from_summary_invalid_open_time() {
let summary = KlineSummary {
open_time: 16181856599999998,
close_time: 16181856599999999,
open: "0.00000000".to_string(),
high: "0.00000000".to_string(),
low: "0.00000000".to_string(),
close: "0.00000000".to_string(),
volume: "0.00000000".to_string(),
quote_asset_volume: "0.00000000".to_string(),
number_of_trades: 0,
taker_buy_base_asset_volume: "0.00000000".to_string(),
taker_buy_quote_asset_volume: "0.00000000".to_string(),
};

let result = Candlestick::from_summary(summary);
assert!(matches!(
result,
Err(KryptoError::InvalidCandlestickDateTime {
when: When::Open,
timestamp: 16181856599999998
})
));
}

#[test]
fn test_candlestick_from_summary_invalid_close_time() {
let summary = KlineSummary {
open_time: 1618185600000,
close_time: 16181856599999999,
open: "0.00000000".to_string(),
high: "0.00000000".to_string(),
low: "0.00000000".to_string(),
close: "0.00000000".to_string(),
volume: "0.00000000".to_string(),
quote_asset_volume: "0.00000000".to_string(),
number_of_trades: 0,
taker_buy_base_asset_volume: "0.00000000".to_string(),
taker_buy_quote_asset_volume: "0.00000000".to_string(),
};

let result = Candlestick::from_summary(summary);
assert!(matches!(
result,
Err(KryptoError::InvalidCandlestickDateTime {
when: When::Close,
timestamp: 16181856599999999
})
));
}

#[test]
fn test_candlestick_from_summary_open_time_greater_than_close_time() {
let summary = KlineSummary {
open_time: 1618185659999,
close_time: 1618185600000,
open: "0.00000000".to_string(),
high: "0.00000000".to_string(),
low: "0.00000000".to_string(),
close: "0.00000000".to_string(),
volume: "0.00000000".to_string(),
quote_asset_volume: "0.00000000".to_string(),
number_of_trades: 0,
taker_buy_base_asset_volume: "0.00000000".to_string(),
taker_buy_quote_asset_volume: "0.00000000".to_string(),
};

let result = Candlestick::from_summary(summary);
assert!(matches!(
result,
Err(KryptoError::OpenTimeGreaterThanCloseTime {
open_time: 1618185659999,
close_time: 1618185600000
})
));
}
}
}
Loading

0 comments on commit b6822a7

Please sign in to comment.