Skip to content

Commit 62c8ff7

Browse files
committed
let's gooooo
1 parent 7eba013 commit 62c8ff7

File tree

11 files changed

+105
-61
lines changed

11 files changed

+105
-61
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v_exchanges/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ignored = ["derive-new", "eyre", "serde", "tokio", "v_utils"]
1313

1414
[dependencies]
1515
v_exchanges_deser = { path = "../v_exchanges_deser/", features = ["full"] }
16+
v_exchanges_adapters = { path = "../v_exchanges_adapters/", features = ["full"] }
1617

1718
tokio.workspace = true
1819
tracing.workspace = true

v_exchanges/src/binance/mod.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

v_exchanges/src/lib.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

v_exchanges/src/main.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
fn main() {
2-
println!("Hello, world!");
1+
use v_exchanges_adapters::binance::{BinanceHttpUrl, BinanceOption};
2+
use v_exchanges_deser::{binance::Client, core::Exchange};
3+
4+
#[tokio::main]
5+
async fn main() {
6+
color_eyre::install().unwrap();
7+
v_utils::utils::init_subscriber(v_utils::utils::LogDestination::xdg("v_exchanges"));
8+
9+
let mut client = Client::new();
10+
client.update_default_option(BinanceOption::HttpUrl(BinanceHttpUrl::FuturesUsdM));
11+
12+
let klines = client.klines(("BTC", "USDT").into(), "1m".into(), None, None, None).await.unwrap();
13+
dbg!(&klines);
314
}

v_exchanges_deser/src/binance/futures/core.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,6 @@ pub struct KlineNamed {
5353
#[serde(rename = "B")]
5454
__ignore: Option<Value>,
5555
}
56-
/** # Ex: ```json
57-
[1731448080000,\"88591.90\",\"88630.90\",\"88560.00\",\"88574.10\",\"173.581\",1731448139999,\"15378315.48720\",2800,\"113.654\",\"10069629.84420\",\"0\"]
58-
```
59-
**/
60-
#[serde_as]
61-
#[derive(Debug, Serialize, Deserialize, Clone)]
62-
pub struct Kline {
63-
pub open_time: i64,
64-
#[serde_as(as = "DisplayFromStr")]
65-
pub open: f64,
66-
#[serde_as(as = "DisplayFromStr")]
67-
pub close: f64,
68-
#[serde_as(as = "DisplayFromStr")]
69-
pub high: f64,
70-
#[serde_as(as = "DisplayFromStr")]
71-
pub low: f64,
72-
#[serde_as(as = "DisplayFromStr")]
73-
pub volume: f64,
74-
pub close_time: i64,
75-
#[serde_as(as = "DisplayFromStr")]
76-
pub quote_asset_volume: f64,
77-
pub number_of_trades: i64,
78-
#[serde_as(as = "DisplayFromStr")]
79-
pub taker_buy_base_asset_volume: f64,
80-
#[serde_as(as = "DisplayFromStr")]
81-
pub taker_buy_quote_asset_volume: f64,
82-
83-
__ignore: Option<Value>,
84-
}
8556
//
8657

8758
#[serde_as]
@@ -127,12 +98,3 @@ pub struct FullKlines {
12798
#[serde(skip, rename = "B")]
12899
pub __ignore: u64,
129100
}
130-
131-
#[cfg(test)]
132-
mod tests {
133-
#[test]
134-
fn kline_core() {
135-
let raw_str = "[1731448080000,\"88591.90\",\"88630.90\",\"88560.00\",\"88574.10\",\"173.581\",1731448139999,\"15378315.48720\",2800,\"113.654\",\"10069629.84420\",\"0\"]";
136-
let _: super::Kline = serde_json::from_str(raw_str).unwrap();
137-
}
138-
}
Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
use chrono::{DateTime, TimeZone, Utc};
12
//HACK: Methods should be implemented on the central interface struct, following <https://github.com/wisespace-io/binance-rs>.
2-
use v_exchanges_adapters::binance::BinanceOption;
3-
43
use color_eyre::eyre::Result;
5-
use v_utils::trades::{Pair, Timeframe};
6-
use serde::Serialize;
4+
use serde::{Deserialize, Serialize};
5+
use serde_json::Value;
6+
use serde_with::{DisplayFromStr, serde_as};
7+
use v_exchanges_adapters::binance::BinanceOption;
8+
use v_utils::trades::{Kline, Ohlc, Pair, Timeframe};
79

8-
use crate::binance::futures::core::*;
10+
use crate::{binance::futures::core::*, core::Klines};
911

10-
//? should the `symbol` and `tf` be strs, while only the top-most level interface takes in the general interfaces?
11-
pub async fn klines(generic_client: &v_exchanges_adapters::Client, pair: Pair, tf: Timeframe, limit: Option<u16>, start_time: Option<u64>, end_time: Option<u64>) -> Result<Vec<Kline>> {
12+
pub async fn klines(client: &v_exchanges_adapters::Client, pair: Pair, tf: Timeframe, limit: Option<u32>, start_time: Option<u64>, end_time: Option<u64>) -> Result<Klines> {
1213
#[derive(Serialize)]
1314
pub struct KlineParams {
1415
pub symbol: String,
1516
pub interval: String,
1617
#[serde(skip_serializing_if = "Option::is_none")]
17-
pub limit: Option<u16>,
18+
pub limit: Option<u32>,
1819
#[serde(rename = "startTime", skip_serializing_if = "Option::is_none")]
1920
pub start_time: Option<u64>,
2021
#[serde(rename = "endTime", skip_serializing_if = "Option::is_none")]
@@ -29,7 +30,66 @@ pub async fn klines(generic_client: &v_exchanges_adapters::Client, pair: Pair, t
2930
end_time,
3031
};
3132

32-
//TODO!!!: have the function take the vec of options, instead of hardcoding `[BinanceOption::Default]`
33-
let klines: Vec<Kline> = generic_client.get("/fapi/v1/klines", Some(&params), [BinanceOption::Default]).await.unwrap();
34-
Ok(klines)
33+
let kline_responses: Vec<KlineResponse> = client.get("/fapi/v1/klines", Some(&params), [BinanceOption::Default]).await.unwrap();
34+
let klines: Vec<Kline> = kline_responses.into_iter().map(Kline::from).collect();
35+
36+
Ok(Klines { v: klines, tf, oi: Vec::new() })
37+
}
38+
39+
/** # Ex: ```json
40+
[1731448080000,\"88591.90\",\"88630.90\",\"88560.00\",\"88574.10\",\"173.581\",1731448139999,\"15378315.48720\",2800,\"113.654\",\"10069629.84420\",\"0\"]
41+
```
42+
**/
43+
#[serde_as]
44+
#[derive(Debug, Serialize, Deserialize, Clone)]
45+
pub struct KlineResponse {
46+
pub open_time: i64,
47+
#[serde_as(as = "DisplayFromStr")]
48+
pub open: f64,
49+
#[serde_as(as = "DisplayFromStr")]
50+
pub close: f64,
51+
#[serde_as(as = "DisplayFromStr")]
52+
pub high: f64,
53+
#[serde_as(as = "DisplayFromStr")]
54+
pub low: f64,
55+
#[serde_as(as = "DisplayFromStr")]
56+
pub volume: f64,
57+
pub close_time: i64,
58+
#[serde_as(as = "DisplayFromStr")]
59+
pub quote_asset_volume: f64,
60+
pub number_of_trades: usize,
61+
#[serde_as(as = "DisplayFromStr")]
62+
pub taker_buy_base_asset_volume: f64,
63+
#[serde_as(as = "DisplayFromStr")]
64+
pub taker_buy_quote_asset_volume: f64,
65+
66+
__ignore: Option<Value>,
67+
}
68+
impl From<KlineResponse> for Kline {
69+
fn from(k: KlineResponse) -> Self {
70+
let ohlc = Ohlc {
71+
open: k.open,
72+
high: k.high,
73+
low: k.low,
74+
close: k.close,
75+
};
76+
Kline {
77+
open_time: DateTime::from_timestamp_millis(k.open_time).unwrap(),
78+
ohlc,
79+
volume_quote: k.quote_asset_volume,
80+
//TODO!!!!!!: before adding check that it is not less than start_time + tf
81+
trades: Some(k.number_of_trades),
82+
taker_buy_volume_quote: Some(k.taker_buy_quote_asset_volume),
83+
close_time: Some(Utc.timestamp_millis_opt(k.close_time).unwrap()),
84+
}
85+
}
86+
}
87+
88+
#[cfg(test)]
89+
mod tests {
90+
#[test]
91+
fn kline_core() {
92+
let raw_str = "[1731448080000,\"88591.90\",\"88630.90\",\"88560.00\",\"88574.10\",\"173.581\",1731448139999,\"15378315.48720\",2800,\"113.654\",\"10069629.84420\",\"0\"]";
93+
let _: super::Kline = serde_json::from_str(raw_str).unwrap();
94+
}
3595
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
11
pub mod futures;
2+
use color_eyre::eyre::Result;
3+
use futures::market::klines;
4+
pub use v_exchanges_adapters::Client; // re-export
5+
use v_exchanges_adapters::binance;
6+
use v_utils::trades::{Pair, Timeframe};
7+
8+
use crate::core::{Exchange, Klines};
9+
10+
impl Exchange<binance::BinanceOptions> for Client {
11+
async fn klines(&self, symbol: Pair, tf: Timeframe, limit: Option<u32>, start_time: Option<u64>, end_time: Option<u64>) -> Result<Klines> {
12+
klines(&self, symbol, tf, limit, start_time, end_time).await
13+
}
14+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use chrono::{DateTime, Utc};
22
use color_eyre::eyre::Result;
33
use tokio::sync::mpsc;
4+
use v_exchanges_adapters::traits::HandlerOptions;
45
use v_utils::trades::{Kline, Pair, Timeframe};
56

67
#[derive(Clone, Debug, Default, derive_new::new, Copy)]
@@ -18,10 +19,9 @@ pub struct Klines {
1819
pub oi: Vec<Oi>,
1920
}
2021

21-
pub trait Exchange {
22+
pub trait Exchange<O: HandlerOptions> {
2223
//? should I have Self::Pair too? Like to catch the non-existent ones immediately? Although this would increase the error surface on new listings.
23-
type Timeframe;
24-
fn klines(&self, symbol: Pair, tf: Self::Timeframe, limit: u32) -> impl std::future::Future<Output = Result<Klines>> + Send;
24+
fn klines(&self, symbol: Pair, tf: Timeframe, limit: Option<u32>, start_time: Option<u64>, end_time: Option<u64>) -> impl std::future::Future<Output = Result<Klines>> + Send;
2525
// Defined in terms of actors
26-
//TODO!!!: fn spawn_klines_listener(&self, symbol: Pair, tf: Self::Timeframe) -> mpsc::Receiver<Kline>;
26+
//TODO!!!: fn spawn_klines_listener(&self, symbol: Pair, tf: Timeframe) -> mpsc::Receiver<Kline>;
2727
}

v_exchanges_deser/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod binance;
2+
pub mod core;

0 commit comments

Comments
 (0)