v_exchanges/bybit/
mod.rs

1mod account;
2mod market;
3
4use adapters::bybit::{BybitOption, BybitOptions};
5use secrecy::SecretString;
6use v_exchanges_adapters::{Client, GetOptions};
7use v_utils::trades::{Asset, Timeframe};
8
9use crate::{
10	Balances, ExchangeName, ExchangeResult, Instrument, OpenInterest, Symbol,
11	core::{AssetBalance, ExchangeImpl, Klines, RequestRange},
12};
13
14#[derive(Clone, Debug, Default, derive_more::Deref, derive_more::DerefMut)]
15pub struct Bybit(pub Client);
16
17//? 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?
18#[async_trait::async_trait]
19impl ExchangeImpl for Bybit {
20	fn name(&self) -> ExchangeName {
21		ExchangeName::Bybit
22	}
23
24	fn auth(&mut self, pubkey: String, secret: SecretString) {
25		self.update_default_option(BybitOption::Pubkey(pubkey));
26		self.update_default_option(BybitOption::Secret(secret));
27	}
28
29	fn set_recv_window(&mut self, recv_window: std::time::Duration) {
30		self.update_default_option(BybitOption::RecvWindow(recv_window));
31	}
32
33	fn default_recv_window(&self) -> Option<std::time::Duration> {
34		GetOptions::<BybitOptions>::default_options(&**self).recv_window
35	}
36
37	async fn klines(&self, symbol: Symbol, tf: Timeframe, range: RequestRange) -> ExchangeResult<Klines> {
38		match symbol.instrument {
39			Instrument::Perp => market::klines(self, symbol, tf.try_into()?, range).await,
40			_ => unimplemented!(),
41		}
42	}
43
44	async fn price(&self, symbol: Symbol) -> ExchangeResult<f64> {
45		match symbol.instrument {
46			Instrument::Perp => market::price(self, symbol.pair).await,
47			_ => unimplemented!(),
48		}
49	}
50
51	async fn open_interest(&self, symbol: Symbol, tf: Timeframe, range: RequestRange) -> ExchangeResult<Vec<OpenInterest>> {
52		match symbol.instrument {
53			Instrument::Perp => market::open_interest(self, symbol, tf.try_into()?, range).await,
54			_ => Err(crate::ExchangeError::Method(crate::MethodError::MethodNotSupported {
55				exchange: self.name(),
56				instrument: symbol.instrument,
57			})),
58		}
59	}
60
61	async fn asset_balance(&self, asset: Asset, _instrument: Instrument, recv_window: Option<std::time::Duration>) -> ExchangeResult<AssetBalance> {
62		account::asset_balance(self, asset, recv_window).await
63	}
64
65	async fn balances(&self, _instrument: Instrument, recv_window: Option<std::time::Duration>) -> ExchangeResult<Balances> {
66		account::balances(self, recv_window).await
67	}
68}
69
70crate::define_provider_timeframe!(BybitInterval, ["1", "3", "5", "15", "30", "60", "120", "240", "360", "720", "D", "W", "M"]);
71crate::define_provider_timeframe!(BybitIntervalTime, ["5min", "15min", "30min", "1h", "4h", "1d"]);