Skip to content

Commit 69609a1

Browse files
committed
refactor: _
1 parent 0cdc302 commit 69609a1

File tree

6 files changed

+53
-29
lines changed

6 files changed

+53
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
![Minimum Supported Rust Version](https://img.shields.io/badge/nightly-1.85+-ab6000.svg)
33
[<img alt="crates.io" src="https://img.shields.io/crates/v/v_exchanges.svg?color=fc8d62&logo=rust" height="20" style=flat-square>](https://crates.io/crates/v_exchanges)
44
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs&style=flat-square" height="20">](https://docs.rs/v_exchanges)
5-
![Lines Of Code](https://img.shields.io/badge/LoC-3864-lightblue)
5+
![Lines Of Code](https://img.shields.io/badge/LoC-3894-lightblue)
66
<br>
77
[<img alt="ci errors" src="https://img.shields.io/github/actions/workflow/status/valeratrades/v_exchanges/errors.yml?branch=master&style=for-the-badge&style=flat-square&label=errors&labelColor=420d09" height="20">](https://github.com/valeratrades/v_exchanges/actions?query=branch%3Amaster) <!--NB: Won't find it if repo is private-->
88
[<img alt="ci warnings" src="https://img.shields.io/github/actions/workflow/status/valeratrades/v_exchanges/warnings.yml?branch=master&style=for-the-badge&style=flat-square&label=warnings&labelColor=d16002" height="20">](https://github.com/valeratrades/v_exchanges/actions?query=branch%3Amaster) <!--NB: Won't find it if repo is private-->

examples/binance/market_futures.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::env;
22

3-
use v_exchanges::{
4-
prelude::*,
5-
};
3+
use v_exchanges::prelude::*;
64

75
#[tokio::main]
86
async fn main() {
@@ -12,6 +10,8 @@ async fn main() {
1210
let m: AbsMarket = "Binance/Futures".into();
1311
let mut c = m.client();
1412

13+
println!("m: {m}");
14+
1515
let exchange_info = c.exchange_info(m).await.unwrap();
1616
dbg!(&exchange_info.pairs.iter().take(2).collect::<Vec<_>>());
1717

v_exchanges/src/binance/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Binance {
3838
});
3939
let params = join_params(base_json, range_json);
4040
let r: serde_json::Value = self
41-
.0
41+
.client
4242
.get(&format!("/futures/data/{ending}"), &params, [BinanceOption::HttpUrl(BinanceHttpUrl::FuturesUsdM)])
4343
.await?;
4444
let r: Vec<LsrResponse> = serde_json::from_value(r).unwrap();

v_exchanges/src/binance/mod.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@ use v_utils::trades::{Asset, Pair, Timeframe};
1111
use crate::core::{AbsMarket, AssetBalance, Exchange, ExchangeInfo, Klines, RequestRange, WrongExchangeError};
1212

1313
#[derive(Clone, Debug, Default, Deref, DerefMut)]
14-
pub struct Binance(pub Client);
14+
pub struct Binance {
15+
#[deref_mut]
16+
#[deref]
17+
client: Client,
18+
source_market: AbsMarket,
19+
}
1520

1621
//? currently client ends up importing this from crate::binance, but could it be possible to lift the [Client] reexport up, and still have the ability to call all exchange methods right on it?
1722
#[async_trait::async_trait]
1823
impl Exchange for Binance {
24+
fn source_market(&self) -> AbsMarket {
25+
self.source_market
26+
}
27+
1928
fn exchange_name(&self) -> &'static str {
20-
"Binance"
29+
self.source_market().exchange_name()
2130
}
2231

2332
fn auth(&mut self, key: String, secret: String) {
@@ -28,7 +37,7 @@ impl Exchange for Binance {
2837
async fn exchange_info(&self, am: AbsMarket) -> Result<ExchangeInfo> {
2938
match am {
3039
AbsMarket::Binance(m) => match m {
31-
Market::Futures => futures::general::exchange_info(&self.0).await,
40+
Market::Futures => futures::general::exchange_info(&self.client).await,
3241
_ => unimplemented!(),
3342
},
3443
_ => Err(WrongExchangeError::new(self.exchange_name(), am).into()),
@@ -37,15 +46,15 @@ impl Exchange for Binance {
3746

3847
async fn klines(&self, pair: Pair, tf: Timeframe, range: RequestRange, am: AbsMarket) -> Result<Klines> {
3948
match am {
40-
AbsMarket::Binance(m) => market::klines(&self.0, pair, tf, range, m).await,
49+
AbsMarket::Binance(m) => market::klines(&self.client, pair, tf, range, m).await,
4150
_ => Err(WrongExchangeError::new(self.exchange_name(), am).into()),
4251
}
4352
}
4453

4554
async fn prices(&self, pairs: Option<Vec<Pair>>, am: AbsMarket) -> Result<Vec<(Pair, f64)>> {
4655
match am {
4756
AbsMarket::Binance(m) => match m {
48-
Market::Spot => spot::market::prices(&self.0, pairs).await,
57+
Market::Spot => spot::market::prices(&self.client, pairs).await,
4958
_ => unimplemented!(),
5059
},
5160
_ => Err(WrongExchangeError::new(self.exchange_name(), am).into()),
@@ -55,8 +64,8 @@ impl Exchange for Binance {
5564
async fn price(&self, pair: Pair, am: AbsMarket) -> Result<f64> {
5665
match am {
5766
AbsMarket::Binance(m) => match m {
58-
Market::Spot => spot::market::price(&self.0, pair).await,
59-
Market::Futures => futures::market::price(&self.0, pair).await,
67+
Market::Spot => spot::market::price(&self.client, pair).await,
68+
Market::Futures => futures::market::price(&self.client, pair).await,
6069
_ => unimplemented!(),
6170
},
6271
_ => Err(WrongExchangeError::new(self.exchange_name(), am).into()),
@@ -76,7 +85,7 @@ impl Exchange for Binance {
7685
async fn balances(&self, am: AbsMarket) -> Result<Vec<AssetBalance>> {
7786
match am {
7887
AbsMarket::Binance(m) => match m {
79-
Market::Futures => futures::account::balances(&self.0).await,
88+
Market::Futures => futures::account::balances(&self.client).await,
8089
_ => unimplemented!(),
8190
},
8291
_ => Err(WrongExchangeError::new(self.exchange_name(), am).into()),

v_exchanges/src/bybit/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@ use v_utils::trades::{Asset, Pair, Timeframe};
1313
use crate::core::{AbsMarket, AssetBalance, Exchange, ExchangeInfo, Klines, RequestRange, WrongExchangeError};
1414

1515
#[derive(Clone, Debug, Default, Deref, DerefMut)]
16-
pub struct Bybit(pub Client);
16+
pub struct Bybit {
17+
#[deref_mut]
18+
#[deref]
19+
client: Client,
20+
source_market: AbsMarket,
21+
}
1722

1823
//? currently client ends up importing this from crate::binance, but could it be possible to lift the [Client] reexport up, and still have the ability to call all exchange methods right on it?
1924
#[async_trait::async_trait]
2025
impl Exchange for Bybit {
26+
fn source_market(&self) -> AbsMarket {
27+
todo!()
28+
}
2129
fn exchange_name(&self) -> &'static str {
22-
"Bybit"
30+
self.source_market().exchange_name()
2331
}
2432

2533
fn auth(&mut self, key: String, secret: String) {
@@ -37,7 +45,7 @@ impl Exchange for Bybit {
3745
async fn klines(&self, pair: Pair, tf: Timeframe, range: RequestRange, am: AbsMarket) -> Result<Klines> {
3846
match am {
3947
AbsMarket::Bybit(m) => match m {
40-
Market::Linear => market::klines(&self.0, pair, tf, range).await,
48+
Market::Linear => market::klines(&self.client, pair, tf, range).await,
4149
_ => unimplemented!(),
4250
},
4351
_ => Err(WrongExchangeError::new(self.exchange_name(), am).into()),
@@ -47,7 +55,7 @@ impl Exchange for Bybit {
4755
async fn price(&self, pair: Pair, am: AbsMarket) -> Result<f64> {
4856
match am {
4957
AbsMarket::Bybit(m) => match m {
50-
Market::Linear => market::price(&self.0, pair).await,
58+
Market::Linear => market::price(&self.client, pair).await,
5159
_ => unimplemented!(),
5260
},
5361
_ => Err(WrongExchangeError::new(self.exchange_name(), am).into()),
@@ -64,7 +72,7 @@ impl Exchange for Bybit {
6472
async fn asset_balance(&self, asset: Asset, am: AbsMarket) -> Result<AssetBalance> {
6573
match am {
6674
AbsMarket::Bybit(m) => match m {
67-
Market::Linear => account::asset_balance(&self.0, asset).await,
75+
Market::Linear => account::asset_balance(&self.client, asset).await,
6876
_ => unimplemented!(),
6977
},
7078
_ => Err(WrongExchangeError::new(self.exchange_name(), am).into()),
@@ -74,7 +82,7 @@ impl Exchange for Bybit {
7482
async fn balances(&self, am: AbsMarket) -> Result<Vec<AssetBalance>> {
7583
match am {
7684
AbsMarket::Bybit(m) => match m {
77-
Market::Linear => account::balances(&self.0).await,
85+
Market::Linear => account::balances(&self.client).await,
7886
_ => unimplemented!()
7987
},
8088
_ => Err(WrongExchangeError::new(self.exchange_name(), am).into()),

v_exchanges/src/core.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use v_utils::{
1212
//TODO!!!!!!!!!!!!!: klines switch to defining the range via an Enum over either limit either start and end times
1313
#[async_trait::async_trait]
1414
pub trait Exchange: std::fmt::Debug {
15+
fn source_market(&self) -> AbsMarket;
1516
fn exchange_name(&self) -> &'static str;
1617
fn auth(&mut self, key: String, secret: String);
1718

@@ -57,7 +58,7 @@ pub trait MarketTrait {
5758
//TODO; require them to impl Display and FromStr
5859
}
5960

60-
#[derive(Debug, Clone, Copy, derive_more::Display/*TODO:, derive_more::FromStr*/)]
61+
#[derive(Debug, Clone, Copy)]
6162
pub enum AbsMarket {
6263
Binance(crate::binance::Market),
6364
Bybit(crate::bybit::Market),
@@ -70,21 +71,27 @@ impl AbsMarket {
7071
Self::Bybit(m) => m.client(),
7172
}
7273
}
74+
pub fn exchange_name(&self) -> &'static str {
75+
match self {
76+
Self::Binance(_) => "Binance",
77+
Self::Bybit(_) => "Bybit",
78+
}
79+
}
7380
}
7481
impl Default for AbsMarket {
7582
fn default() -> Self {
7683
Self::Binance(crate::binance::Market::default())
7784
}
7885
}
79-
//impl std::fmt::Display for Market {
80-
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81-
// match self {
82-
// Market::Binance(m) => write!(f, "Binance/{}", m),
83-
// Market::Bybit(m) => write!(f, "Bybit/{}", m),
84-
// }
85-
// }
86-
//}
87-
//
86+
impl std::fmt::Display for AbsMarket {
87+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88+
match self {
89+
Self::Binance(m) => write!(f, "Binance/{}", m),
90+
Self::Bybit(m) => write!(f, "Bybit/{}", m),
91+
}
92+
}
93+
}
94+
8895
impl std::str::FromStr for AbsMarket {
8996
type Err = eyre::Error;
9097

0 commit comments

Comments
 (0)