Skip to content

Commit 587f7d1

Browse files
committed
_
1 parent 848b178 commit 587f7d1

File tree

7 files changed

+45
-27
lines changed

7 files changed

+45
-27
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.

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-3759-lightblue)
5+
![Lines Of Code](https://img.shields.io/badge/LoC-3769-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/test.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use v_exchanges::{binance::Binance, bitmex::Bitmex};
2+
3+
#[tokio::main]
4+
async fn main() {
5+
color_eyre::install().unwrap();
6+
v_utils::utils::init_subscriber(v_utils::utils::LogDestination::xdg("v_exchanges"));
7+
8+
let bn = Binance::default();
9+
let lsr = bn.lsr(("BTC", "USDT").into(), "5m".into(), (24 * 12 + 1).into(), "Global".into()).await.unwrap();
10+
dbg!(&lsr[..2]);
11+
}

v_exchanges/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ tracing.workspace = true
4040
v_utils = { workspace = true }
4141

4242
v_exchanges_adapters = { version = "^0.3.0", path = "../v_exchanges_adapters/", features = ["full"] }
43+
async-trait = "0.1.85"
4344

4445
reqwest = { version = "^0.12.12", optional = true }
4546

v_exchanges/src/binance/mod.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ use crate::core::{AssetBalance, Exchange, ExchangeInfo, Klines, RequestRange};
1414
pub struct Binance(pub Client);
1515

1616
//? 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?
17+
#[async_trait::async_trait]
1718
impl Exchange for Binance {
1819
type M = Market;
1920

20-
fn auth<S: Into<String>>(&mut self, key: S, secret: S) {
21-
self.update_default_option(BinanceOption::Key(key.into()));
22-
self.update_default_option(BinanceOption::Secret(secret.into()));
21+
fn auth(&mut self, key: String, secret: String) {
22+
self.update_default_option(BinanceOption::Key(key));
23+
self.update_default_option(BinanceOption::Secret(secret));
2324
}
2425

2526
async fn exchange_info(&self, m: Self::M) -> Result<ExchangeInfo> {
@@ -71,13 +72,11 @@ pub enum Market {
7172
Margin,
7273
}
7374
impl crate::core::MarketTrait for Market {
74-
type Ex = Binance;
75-
76-
fn client(&self) -> Binance {
77-
Binance::default()
75+
fn client(&self) -> Box<dyn Exchange<M = Self>> {
76+
Box::new(Binance::default())
7877
}
7978

8079
fn fmt_abs(&self) -> String {
81-
format!("Binance/{self}")
80+
format!("Binance/{self}")
8281
}
8382
}

v_exchanges/src/bybit/mod.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ use crate::core::{AssetBalance, Exchange, ExchangeInfo, Klines, RequestRange};
1616
pub struct Bybit(pub Client);
1717

1818
//? 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?
19+
#[async_trait::async_trait]
1920
impl Exchange for Bybit {
2021
type M = Market;
2122

22-
fn auth<S: Into<String>>(&mut self, key: S, secret: S) {
23-
self.update_default_option(BybitOption::Key(key.into()));
24-
self.update_default_option(BybitOption::Secret(secret.into()));
23+
fn auth(&mut self, key: String, secret: String) {
24+
self.update_default_option(BybitOption::Key(key));
25+
self.update_default_option(BybitOption::Secret(secret));
2526
}
2627

2728
async fn exchange_info(&self, m: Self::M) -> Result<ExchangeInfo> {
@@ -69,10 +70,8 @@ pub enum Market {
6970
Inverse,
7071
}
7172
impl crate::core::MarketTrait for Market {
72-
type Ex = Bybit;
73-
74-
fn client(&self) -> Bybit {
75-
Bybit::default()
73+
fn client(&self) -> Box<dyn Exchange<M = Self>> {
74+
Box::new(Bybit::default())
7675
}
7776

7877
fn fmt_abs(&self) -> String {

v_exchanges/src/core.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ use v_utils::{
1010
};
1111

1212
//TODO!!!!!!!!!!!!!: klines switch to defining the range via an Enum over either limit either start and end times
13-
13+
#[async_trait::async_trait]
1414
pub trait Exchange {
1515
type M: MarketTrait;
1616

17-
fn auth<S: Into<String>>(&mut self, key: S, secret: S);
17+
fn auth(&mut self, key: String, secret: String);
1818

19-
fn exchange_info(&self, m: Self::M) -> impl std::future::Future<Output = Result<ExchangeInfo>> + Send;
19+
async fn exchange_info(&self, m: Self::M) -> Result<ExchangeInfo>;
2020

2121
//? should I have Self::Pair too? Like to catch the non-existent ones immediately? Although this would increase the error surface on new listings.
22-
fn klines(&self, pair: Pair, tf: Timeframe, range: RequestRange, m: Self::M) -> impl std::future::Future<Output = Result<Klines>> + Send;
22+
async fn klines(&self, pair: Pair, tf: Timeframe, range: RequestRange, m: Self::M) -> Result<Klines>;
2323

2424
/// If no pairs are specified, returns for all;
25-
fn prices(&self, pairs: Option<Vec<Pair>>, m: Self::M) -> impl std::future::Future<Output = Result<Vec<(Pair, f64)>>> + Send;
26-
fn price(&self, pair: Pair, m: Self::M) -> impl std::future::Future<Output = Result<f64>> + Send;
25+
async fn prices(&self, pairs: Option<Vec<Pair>>, m: Self::M) -> Result<Vec<(Pair, f64)>>;
26+
async fn price(&self, pair: Pair, m: Self::M) -> Result<f64>;
2727

2828
// Defined in terms of actors
29-
//TODO!!!: fn spawn_klines_listener(&self, symbol: Pair, tf: Timeframe) -> mpsc::Receiver<Kline>;
29+
//TODO!!!: async fn spawn_klines_listener(&self, symbol: Pair, tf: Timeframe) -> mpsc::Receiver<Kline>;
3030

3131
/// balance of a specific asset
32-
fn asset_balance(&self, asset: Asset, m: Self::M) -> impl std::future::Future<Output = Result<AssetBalance>> + Send;
32+
async fn asset_balance(&self, asset: Asset, m: Self::M) -> Result<AssetBalance>;
3333
/// vec of balances of specific assets
34-
fn balances(&self, m: Self::M) -> impl std::future::Future<Output = Result<Vec<AssetBalance>>> + Send;
34+
async fn balances(&self, m: Self::M) -> Result<Vec<AssetBalance>>;
3535
//? potentially `total_balance`? Would return precompiled USDT-denominated balance of a (bybit::wallet/binance::account)
3636
// balances are defined for each margin type: [futures_balance, spot_balance, margin_balance], but note that on some exchanges, (like bybit), some of these may point to the same exact call
3737
// to negate confusion could add a `total_balance` endpoint
@@ -41,8 +41,7 @@ pub trait Exchange {
4141

4242
// Market {{{
4343
pub trait MarketTrait {
44-
type Ex: Exchange;
45-
fn client(&self) -> Self::Ex;
44+
fn client(&self) -> Box<dyn Exchange<M = Self>>;
4645
fn fmt_abs(&self) -> String;
4746
//TODO; require them to impl Display and FromStr
4847
}
@@ -55,6 +54,14 @@ pub enum Market {
5554
Bybit(crate::bybit::Market),
5655
//TODO
5756
}
57+
//impl Market {
58+
// pub fn client(&self) -> Box<dyn Exchange<M = dyn Market>> {
59+
// match self {
60+
// Market::Binance(m) => m.client(),
61+
// Market::Bybit(m) => m.client(),
62+
// }
63+
// }
64+
//}
5865
//
5966
//impl Default for Market {
6067
// fn default() -> Self {

0 commit comments

Comments
 (0)