Skip to content

Commit 4e68e9c

Browse files
committed
fix: _
1 parent 099d935 commit 4e68e9c

File tree

5 files changed

+69
-31
lines changed

5 files changed

+69
-31
lines changed

examples/binance/market.rs

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

3-
use v_exchanges::{binance::Client, core::Exchange};
3+
use v_exchanges::{binance::Binance, core::Exchange};
44
use v_exchanges_adapters::binance::{self, BinanceHttpUrl, BinanceOption};
55

66
#[tokio::main]
77
async fn main() {
88
color_eyre::install().unwrap();
99
v_utils::utils::init_subscriber(v_utils::utils::LogDestination::xdg("v_exchanges"));
1010

11-
let mut client = Client::new();
11+
let mut b = Binance::new();
1212

13-
client.update_default_option(BinanceOption::HttpUrl(BinanceHttpUrl::FuturesUsdM));
13+
b.update_default_option(BinanceOption::HttpUrl(BinanceHttpUrl::FuturesUsdM));
1414

1515
//before implementing the trait for bybit too, was able to just do eg: `let klines = client.futures_klines(("BTC", "USDT").into(), "1m".into(), 2, None, None).await.unwrap();`
1616

17-
let klines = <Client as Exchange<binance::BinanceOptions>>::futures_klines(&client, ("BTC", "USDT").into(), "1m".into(), 2, None, None)
17+
let klines = b.futures_klines(("BTC", "USDT").into(), "1m".into(), 2, None, None)
1818
.await
1919
.unwrap();
20-
let price = <Client as Exchange<binance::BinanceOptions>>::futures_price(&client, ("BTC", "USDT").into()).await.unwrap();
20+
let price = b.futures_price(("BTC", "USDT").into()).await.unwrap();
2121
dbg!(&klines, price);
2222

2323
if let (Ok(key), Ok(secret)) = (env::var("BINANCE_TIGER_READ_KEY"), env::var("BINANCE_TIGER_READ_SECRET")) {
24-
client.update_default_option(BinanceOption::Key(key));
25-
client.update_default_option(BinanceOption::Secret(secret));
26-
let balance = <Client as Exchange<binance::BinanceOptions>>::futures_asset_balance(&client, "USDT".into()).await.unwrap();
24+
b.update_default_option(BinanceOption::Key(key));
25+
b.update_default_option(BinanceOption::Secret(secret));
26+
let balance = b.futures_asset_balance("USDT".into()).await.unwrap();
2727
dbg!(&balance);
2828
} else {
2929
eprintln!("BINANCE_TIGER_READ_KEY or BINANCE_TIGER_READ_SECRET is missing, skipping private API methods.");

examples/bybit/market.rs

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

3-
use v_exchanges::{bybit::Client, core::Exchange};
3+
use v_exchanges::{bybit::Bybit, core::Exchange};
44
use v_exchanges_adapters::bybit::{BybitHttpAuth, BybitHttpUrl, BybitOption};
55

66
#[tokio::main]
77
async fn main() {
88
color_eyre::install().unwrap();
99
v_utils::utils::init_subscriber(v_utils::utils::LogDestination::xdg("v_exchanges"));
1010

11-
let mut client = Client::new();
11+
let mut bb = Bybit::new();
1212

13-
let ticker: serde_json::Value = client
13+
let ticker: serde_json::Value = bb.0
1414
.get("/v5/market/tickers", &[("category", "spot"), ("symbol", "BTCUSDT")], [BybitOption::Default])
1515
.await
1616
.expect("failed to get ticker");
1717
println!("Ticker:\n{ticker}");
1818

1919
if let (Ok(key), Ok(secret)) = (env::var("BYBIT_TIGER_READ_KEY"), env::var("BYBIT_TIGER_READ_SECRET")) {
20-
client.update_default_option(BybitOption::Key(key));
21-
client.update_default_option(BybitOption::Secret(secret));
22-
private(&client).await;
20+
bb.0.update_default_option(BybitOption::Key(key));
21+
bb.0.update_default_option(BybitOption::Secret(secret));
22+
private(&mut bb).await;
2323
} else {
2424
eprintln!("BYBIT_TIGER_READ_KEY or BYBIT_TIGER_READ_SECRET is missing, skipping private API methods.");
2525
}
@@ -39,8 +39,8 @@ async fn main() {
3939
//}
4040
}
4141

42-
async fn private(client: &Client) {
43-
let balance: serde_json::Value = client
42+
async fn private(bb: &mut Bybit) {
43+
let balance: serde_json::Value = bb
4444
.get("/v5/account/wallet-balance", &[("accountType", "UNIFIED")], [BybitOption::HttpAuth(BybitHttpAuth::V3AndAbove)])
4545
.await
4646
.expect("failed to get balance");

v_exchanges/src/binance/mod.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
11
pub mod futures;
22
use color_eyre::eyre::Result;
3-
pub use v_exchanges_adapters::Client; // re-export
4-
use v_exchanges_adapters::binance;
3+
use v_exchanges_adapters::{Client, binance};
54
use v_utils::trades::{Asset, Pair, Timeframe};
65

76
use crate::core::{AssetBalance, Exchange, Klines};
87

8+
#[derive(Clone, Debug, Default)]
9+
pub struct Binance(pub Client);
10+
impl Binance {
11+
pub fn new() -> Self {
12+
Self(Client::new())
13+
}
14+
}
15+
impl std::ops::Deref for Binance {
16+
type Target = Client;
17+
18+
fn deref(&self) -> &Self::Target {
19+
&self.0
20+
}
21+
}
22+
impl std::ops::DerefMut for Binance {
23+
fn deref_mut(&mut self) -> &mut Self::Target {
24+
&mut self.0
25+
}
26+
}
27+
928
//? 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?
10-
impl Exchange<binance::BinanceOptions> for Client {
29+
impl Exchange for Binance {
1130
async fn futures_klines(&self, symbol: Pair, tf: Timeframe, limit: u32, start_time: Option<u64>, end_time: Option<u64>) -> Result<Klines> {
12-
futures::market::klines(&self, symbol, tf, limit, start_time, end_time).await
31+
futures::market::klines(&self.0, symbol, tf, limit, start_time, end_time).await
1332
}
1433

1534
async fn futures_price(&self, symbol: Pair) -> Result<f64> {
16-
futures::market::price(&self, symbol).await
35+
futures::market::price(&self.0, symbol).await
1736
}
1837

1938
async fn futures_asset_balance(&self, asset: Asset) -> Result<AssetBalance> {
20-
futures::account::asset_balance(&self, asset).await
39+
futures::account::asset_balance(&self.0, asset).await
2140
}
2241

2342
async fn futures_balances(&self) -> Result<Vec<AssetBalance>> {
24-
futures::account::balances(&self).await
43+
futures::account::balances(&self.0).await
2544
}
2645

2746
//DO: async fn balance(&self,

v_exchanges/src/bybit/mod.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,51 @@
1-
pub use v_exchanges_adapters::Client; // re-export //? not sure if this is the correct place to do this
2-
31
pub mod account;
42

53
use color_eyre::eyre::Result;
4+
use v_exchanges_adapters::Client;
65
use v_exchanges_adapters::bybit;
76
use v_utils::trades::{Asset, Pair, Timeframe};
87

98
use crate::core::{AssetBalance, Exchange, Klines};
109

10+
pub struct Bybit(pub Client);
11+
impl Bybit {
12+
pub fn new() -> Self {
13+
Self(Client::new())
14+
}
15+
}
16+
impl std::ops::Deref for Bybit {
17+
type Target = Client;
18+
19+
fn deref(&self) -> &Self::Target {
20+
&self.0
21+
}
22+
}
23+
impl std::ops::DerefMut for Bybit {
24+
fn deref_mut(&mut self) -> &mut Self::Target {
25+
&mut self.0
26+
}
27+
}
28+
29+
1130
//? 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?
12-
impl Exchange<bybit::BybitOptions> for Client {
31+
impl Exchange for Bybit {
1332
async fn futures_klines(&self, symbol: Pair, tf: Timeframe, limit: u32, start_time: Option<u64>, end_time: Option<u64>) -> Result<Klines> {
14-
//futures::market::klines(&self, symbol, tf, limit, start_time, end_time).await
33+
//futures::market::klines(&self.0, symbol, tf, limit, start_time, end_time).await
1534
todo!();
1635
}
1736

1837
async fn futures_price(&self, symbol: Pair) -> Result<f64> {
19-
//futures::market::price(&self, symbol).await
38+
//futures::market::price(&self.0, symbol).await
2039
todo!();
2140
}
2241

2342
async fn futures_asset_balance(&self, asset: Asset) -> Result<AssetBalance> {
24-
//futures::account::asset_balance(&self, asset).await
43+
//futures::account::asset_balance(&self.0, asset).await
2544
todo!();
2645
}
2746

2847
async fn futures_balances(&self) -> Result<Vec<AssetBalance>> {
29-
account::balances(&self).await
48+
account::balances(&self.0).await
3049
}
3150

3251
//DO: async fn balance(&self,

v_exchanges/src/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use tokio::sync::mpsc;
44
use v_exchanges_adapters::traits::HandlerOptions;
55
use v_utils::trades::{Asset, Kline, Pair, Timeframe};
66

7-
pub trait Exchange<O: HandlerOptions> {
7+
pub trait Exchange {
88
//? should I have Self::Pair too? Like to catch the non-existent ones immediately? Although this would increase the error surface on new listings.
99
fn futures_klines(&self, symbol: Pair, tf: Timeframe, limit: u32, start_time: Option<u64>, end_time: Option<u64>) -> impl std::future::Future<Output = Result<Klines>> + Send;
1010
fn futures_price(&self, symbol: Pair) -> impl std::future::Future<Output = Result<f64>> + Send;

0 commit comments

Comments
 (0)