v_exchanges 0.17.3

Implementations of HTTP/HTTPS/WebSocket API methods for some crypto exchanges, using [crypto-botters](<https://github.com/negi-grass/crypto-botters>) framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
use std::collections::{BTreeMap, VecDeque};

use adapters::{
	Client, HttpClient,
	generics::{RetryConfig, ws::WsError},
};
use derive_more::{Deref, DerefMut};
use jiff::Timestamp;
use secrecy::SecretString;
use serde_json::json;
use v_utils::{
	prelude::*,
	trades::{Asset, Kline, Pair, Timeframe, Usd},
	utils::filter_nulls,
};

use crate::error::{ExchangeError, ExchangeResult, MethodError, OutOfRangeError, RequestRangeError};

const MAX_RECV_WINDOW: std::time::Duration = std::time::Duration::from_secs(10 * 60); // 10 minutes

/// Main trait for all standardized exchange interactions.
///
/// //NB: NEVER implement this trait manually. It is auto-implemented via blanket impl for all `ExchangeImpl` implementors.
/// The blanket impl ensures that this trait can only be implemented within this crate.
#[async_trait::async_trait]
pub trait Exchange: std::fmt::Debug + Send + Sync + std::ops::Deref<Target = Client> + std::ops::DerefMut {
	fn name(&self) -> ExchangeName;
	fn auth(&mut self, pubkey: String, secret: SecretString);
	fn set_recv_window(&mut self, recv_window: std::time::Duration);
	fn set_timeout(&mut self, timeout: std::time::Duration);
	fn set_retry_config(&mut self, config: RetryConfig);
	fn set_use_testnet(&mut self, b: bool);
	fn set_cache_testnet_calls(&mut self, duration: Option<std::time::Duration>);
	async fn exchange_info(&self, instrument: Instrument) -> ExchangeResult<ExchangeInfo>;
	async fn klines(&self, symbol: Symbol, tf: Timeframe, range: RequestRange) -> ExchangeResult<Klines>;
	async fn prices(&self, pairs: Option<Vec<Pair>>, instrument: Instrument) -> ExchangeResult<BTreeMap<Pair, f64>>;
	async fn price(&self, symbol: Symbol) -> ExchangeResult<f64>;
	async fn open_interest(&self, symbol: Symbol, tf: Timeframe, range: RequestRange) -> ExchangeResult<Vec<OpenInterest>>;
	async fn personal_info(&self, instrument: Instrument, recv_window: Option<std::time::Duration>) -> ExchangeResult<PersonalInfo>;
	fn ws_trades(&self, pairs: Vec<Pair>, instrument: Instrument) -> ExchangeResult<Box<dyn ExchangeStream<Item = Trade>>>;
	fn ws_book(&self, pairs: Vec<Pair>, instrument: Instrument) -> ExchangeResult<Box<dyn ExchangeStream<Item = BookUpdate>>>;
}
/// Concerns itself with exact types.
#[async_trait::async_trait]
pub trait ExchangeStream: std::fmt::Debug + Send + Sync {
	type Item;

	async fn next(&mut self) -> eyre::Result<Self::Item, WsError>;
}
#[async_trait::async_trait]
pub trait SubscribeOrder {
	type Order;

	async fn place_and_subscribe(&mut self, topics: Vec<Self::Order>) -> Result<(), WsError>;
}
/// most exchanges default to returning OI value in asset quantity, not quote. Exception would be Inverse on Bybit.
/// Which actually makes sense, as same endpoints accept things like "BTCETH", where quote value would be irrelevant.
#[derive(Clone, Copy, Debug, Default)]
pub struct OpenInterest {
	pub val_asset: f64,
	pub val_quote: Option<f64>,
	/// Binance's /futures/data/openInterestHist returns CMC's MC as well
	pub marketcap: Option<f64>,
	pub timestamp: Timestamp,
}
/// Does not have any gaps in the data, (as klines are meant to be indexed naively when used). TODO: enforce this.
///
/// # Arch
/// the greater the index, the newer the value
#[derive(Clone, Debug, Default, Deref, DerefMut, derive_new::new)]
pub struct Klines {
	#[deref_mut]
	#[deref]
	pub v: VecDeque<Kline>,
	pub tf: Timeframe,
}
#[derive(Clone, Copy, Debug)]
pub enum RequestRange {
	/// Preferred way of defining the range
	Span { since: Timestamp, until: Option<Timestamp> },
	/// For quick and dirty
	//TODO!: have it contain an enum, with either exact value, either just `Max`, then each exchange matches on it
	Limit(u32),
}
impl RequestRange {
	pub fn ensure_allowed(&self, allowed: std::ops::RangeInclusive<u32>, tf: &Timeframe) -> Result<(), RequestRangeError> {
		match self {
			RequestRange::Span { since: start, until: end } =>
				if let Some(end) = end {
					if start > end {
						return Err(eyre!("Start time is greater than end time").into());
					}
					let effective_limit =
						((*end - *start).get_milliseconds() / tf.duration().as_millis() as i64/*ok to downcast, because i64 will be sufficient for entirety of my lifetime*/) as u32;
					if effective_limit > *allowed.end() {
						return Err(OutOfRangeError::new(allowed, effective_limit).into());
					}
				},
			RequestRange::Limit(limit) =>
				if !allowed.contains(limit) {
					return Err(OutOfRangeError::new(allowed, *limit).into());
				},
		}
		Ok(())
	}

	pub fn serialize(&self, exchange: ExchangeName) -> serde_json::Value {
		match exchange {
			#[cfg(feature = "binance")]
			ExchangeName::Binance => self.serialize_common(),
			#[cfg(feature = "bybit")]
			ExchangeName::Bybit => self.serialize_common(),
			_ => unimplemented!(),
		}
	}

	fn serialize_common(&self) -> serde_json::Value {
		filter_nulls(match self {
			RequestRange::Span { since: start, until: end } => json!({
				"startTime": start.as_millisecond(),
				"endTime": end.map(|dt| dt.as_millisecond()),
			}),
			RequestRange::Limit(limit) => json!({
				"limit": limit,
			}),
		})
	}
}

#[derive(Clone, Copy, Debug, Default, derive_more::Deref, derive_more::DerefMut)]
pub struct AssetBalance {
	pub asset: Asset,
	pub underlying: f64,
	/// Optional, as for most exchanges appending it costs another call to `price{s}` endpoint
	#[deref_mut]
	#[deref]
	pub usd: Option<Usd>,
	// Binance
	//cross_wallet_balance: f64,
	//cross_unrealized_pnl: f64,
	//available_balance: f64,
	//max_withdraw_amount: f64,
	//margin_available: bool,
	// Mexc
	//available_balance: f64,
	//available_cash: f64,
	//available_open: f64,
	//bonus: f64,
	//cash_balance: f64,
	//currency: String,
	//equity: f64,
	//frozen_balance: f64,
	//position_margin: f64,
	//unrealized: f64,
}
#[derive(Clone, Debug, Default, derive_more::Deref, derive_more::DerefMut, derive_new::new)]
pub struct Balances {
	#[deref_mut]
	#[deref]
	v: Vec<AssetBalance>,
	/// breaks zero-cost of the abstraction, but I assume that most calls to this actually want usd, so it's warranted.
	pub total: Usd,
}
#[derive(Clone, Debug, Default)]
pub struct ApiKeyInfo {
	/// `None` means no expiry set (key is permanent)
	pub expire_time: Option<Timestamp>,
	/// Empty means the exchange doesn't expose permissions via this endpoint.
	pub permissions: Vec<KeyPermission>,
}

#[derive(Clone, Debug, strum::Display, Eq, PartialEq)]
#[non_exhaustive]
pub enum KeyPermission {
	/// Read-only access (market data, account info queries)
	Read,
	/// Spot trading
	SpotTrade,
	/// Futures/perpetual trading
	Futures,
	/// Options trading
	Options,
	/// Margin trading
	Margin,
	/// Withdrawals
	Withdraw,
	/// Asset transfers (internal, cross-account, sub-account)
	Transfer,
	/// Earn/savings products
	Earn,
	/// Anything not covered above
	Other(String),
}
impl KeyPermission {
	#[cfg(feature = "kucoin")]
	pub(crate) fn from_kucoin(s: &str) -> Self {
		match s {
			"General" => Self::Read,
			"Spot" => Self::SpotTrade,
			"Futures" => Self::Futures,
			"Options" => Self::Options,
			"Margin" => Self::Margin,
			"Withdrawal" => Self::Withdraw,
			"FlexTransfers" => Self::Transfer,
			"Earn" => Self::Earn,
			other => Self::Other(other.to_owned()),
		}
	}
}
#[derive(Clone, Debug)]
pub struct PersonalInfo {
	pub api: ApiKeyInfo,
	pub balances: Balances,
}
#[derive(Clone, Debug, Default)]
pub struct ExchangeInfo {
	pub server_time: Timestamp,
	pub pairs: BTreeMap<Pair, PairInfo>,
}
impl ExchangeInfo {
	pub fn usdt_pairs(&self) -> impl Iterator<Item = Pair> {
		self.pairs.keys().filter(|p| p.is_usdt()).copied()
	}
}

#[derive(Clone, Debug, Default)]
pub struct PairInfo {
	pub price_precision: u8,
	pub qty_precision: u8,
	/// `None` means perpetual (no expiry). Only set for dated futures.
	pub delivery_date: Option<Timestamp>,
}
#[derive(Clone, Debug, strum::Display, strum::EnumString, Eq, Hash, PartialEq)]
#[strum(serialize_all = "lowercase")]
#[non_exhaustive]
pub enum ExchangeName {
	Binance,
	Bybit,
	Kucoin,
	Mexc,
	BitFlyer,
	Coincheck,
	Yahoo,
}
impl ExchangeName {
	pub fn init_client(&self) -> Box<dyn Exchange> {
		match self {
			#[cfg(feature = "binance")]
			Self::Binance => Box::new(crate::Binance(Client::default())),
			#[cfg(feature = "bybit")]
			Self::Bybit => Box::new(crate::Bybit(Client::default())),
			#[cfg(feature = "kucoin")]
			Self::Kucoin => Box::new(crate::Kucoin(Client::default())),
			#[cfg(feature = "mexc")]
			Self::Mexc => Box::new(crate::Mexc(Client::default())),
			_ => unimplemented!(),
		}
	}

	pub fn init_mock_client(&self) -> Box<dyn Exchange> {
		match self {
			#[cfg(feature = "binance")]
			Self::Binance => Box::new(crate::Binance(Client::new_mock())),
			#[cfg(feature = "bybit")]
			Self::Bybit => Box::new(crate::Bybit(Client::new_mock())),
			#[cfg(feature = "kucoin")]
			Self::Kucoin => Box::new(crate::Kucoin(Client::new_mock())),
			#[cfg(feature = "mexc")]
			Self::Mexc => Box::new(crate::Mexc(Client::new_mock())),
			_ => unimplemented!(),
		}
	}
}

#[derive(Clone, Copy, Debug, Default, serde::Deserialize, strum::Display, strum::EnumString, Eq, Hash, PartialEq, serde::Serialize)]
#[non_exhaustive]
pub enum Instrument {
	#[default]
	#[strum(serialize = "")]
	Spot,
	#[strum(serialize = ".P")]
	Perp,
	#[strum(serialize = ".M")]
	Margin, //Q: do we care for being able to parse spot/margin diff from ticker defs?
	#[strum(serialize = ".PERP_INVERSE")]
	PerpInverse,
	#[strum(serialize = ".OPTIONS")]
	Options,
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Ticker {
	pub symbol: Symbol,
	pub exchange_name: ExchangeName,
}
#[derive(Clone, Copy, Debug, Default, serde::Deserialize, Eq, Hash, PartialEq, serde::Serialize, derive_new::new)]
pub struct Symbol {
	pub pair: Pair,
	pub instrument: Instrument,
}
#[derive(Clone, Debug, Default)]
pub struct Trade {
	pub time: Timestamp,
	pub qty_asset: f64,
	pub price: f64,
}

/// (price, qty) levels for one side of an orderbook.
/// Asks are sorted ascending, bids descending.
#[derive(Clone, Debug, Default)]
pub struct BookShape {
	pub time: Timestamp,
	pub asks: Vec<(f64, f64)>,
	pub bids: Vec<(f64, f64)>,
}

/// Distinguishes full snapshots from incremental deltas.
/// For deltas: qty=0 means remove that price level.
#[derive(Clone, Debug)]
pub enum BookUpdate {
	Snapshot(BookShape),
	Delta(BookShape),
}

/// Internal trait for exchange implementations.
/// Exchange implementations should implement this trait, not `Exchange` directly.
///
/// Each **private** method allows to specify `recv_window`.
///
/// # Other
/// - has too many methods, so for dev purposes most default to `unimplemented!()`.
#[async_trait::async_trait]
pub(crate) trait ExchangeImpl: std::fmt::Debug + Send + Sync + std::ops::Deref<Target = Client> + std::ops::DerefMut {
	fn name(&self) -> ExchangeName;
	// Config {{{
	fn auth(&mut self, pubkey: String, secret: SecretString);
	/// Set number of **milliseconds** the request is valid for. Recv Window of over a minute does not make sense, thus it's expressed as u16.
	///
	/// **WARNING:** This sets a global default and should only be used as a crutch when you can't pass `recv_window` per-request.
	/// Prefer using the `recv_window` parameter in individual method calls instead.
	fn set_recv_window(&mut self, recv_window: std::time::Duration);
	/// Get the default recv_window configured for this exchange, if any.
	fn default_recv_window(&self) -> Option<std::time::Duration>;
	//,}}}

	//Q: do we actually want to return a `MethodNotSupported` error, or should we just `unimplemented!()`?

	#[allow(unused_variables)]
	async fn exchange_info(&self, instrument: Instrument) -> ExchangeResult<ExchangeInfo> {
		Err(ExchangeError::Method(MethodError::new_method_not_supported(self.name(), instrument)))
	}

	//? should I have Self::Pair too? Like to catch the non-existent ones immediately? Although this would increase the error surface on new listings.
	#[allow(unused_variables)]
	async fn klines(&self, symbol: Symbol, tf: Timeframe, range: RequestRange) -> ExchangeResult<Klines> {
		Err(ExchangeError::Method(MethodError::new_method_not_supported(self.name(), symbol.instrument)))
	}

	/// If no pairs are specified, returns for all;
	#[allow(unused_variables)]
	async fn prices(&self, pairs: Option<Vec<Pair>>, instrument: Instrument) -> ExchangeResult<BTreeMap<Pair, f64>> {
		Err(ExchangeError::Method(MethodError::new_method_not_supported(self.name(), instrument)))
	}

	#[allow(unused_variables)]
	/// NB: not perf-critical, so literally just calls `prices`, incurring cost of making a vec and a BTreeMap for no reason
	async fn price(&self, symbol: Symbol) -> ExchangeResult<f64> {
		self.prices(Some(vec![symbol.pair]), symbol.instrument).await.map(|m| m[&symbol.pair])
	}

	/// Get Open Interest data
	/// in output vec: greater the index, fresher the data
	#[allow(unused_variables)]
	async fn open_interest(&self, symbol: Symbol, tf: Timeframe, range: RequestRange) -> ExchangeResult<Vec<OpenInterest>> {
		Err(ExchangeError::Method(MethodError::new_method_not_supported(self.name(), symbol.instrument)))
	}

	// Authenticated {{{
	#[allow(unused_variables)]
	async fn personal_info(&self, instrument: Instrument, recv_window: Option<std::time::Duration>) -> ExchangeResult<PersonalInfo> {
		Err(ExchangeError::Method(MethodError::new_method_not_supported(self.name(), instrument)))
	}
	//,}}}

	//? potentially `total_balance`? Would return precompiled USDT-denominated balance of a (bybit::wallet/binance::account)
	// 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
	// to negate confusion could add a `total_balance` endpoint

	//? could implement many things that are _explicitly_ combinatorial. I can imagine several cases, where knowing that say the specified limit for the klines is wayyy over the max and that you may be opting into a long wait by calling it, could be useful.

	// Websocket {{{
	// Start a websocket connection for individual trades
	#[allow(unused_variables)]
	fn ws_trades(&self, pairs: Vec<Pair>, instrument: Instrument) -> ExchangeResult<Box<dyn ExchangeStream<Item = Trade>>> {
		Err(ExchangeError::Method(MethodError::new_method_not_supported(self.name(), instrument)))
	}

	/// Start a websocket connection for orderbook depth updates (max depth only).
	#[allow(unused_variables)]
	fn ws_book(&self, pairs: Vec<Pair>, instrument: Instrument) -> ExchangeResult<Box<dyn ExchangeStream<Item = BookUpdate>>> {
		Err(ExchangeError::Method(MethodError::new_method_not_supported(self.name(), instrument)))
	}
	//,}}}
}
/// Validates recv_window parameters and warns if using global default.
/// Returns an error if either the provided or default recv_window exceeds MAX_RECV_WINDOW.
fn validate_recv_window(recv_window: Option<std::time::Duration>, default_recv_window: Option<std::time::Duration>) -> ExchangeResult<()> {
	if let Some(rw) = recv_window
		&& rw > MAX_RECV_WINDOW
	{
		return Err(ExchangeError::Other(eyre!("recv_window of {rw:?} exceeds maximum allowed duration of {MAX_RECV_WINDOW:?}")));
	}

	if let Some(rw) = default_recv_window
		&& rw > MAX_RECV_WINDOW
	{
		return Err(ExchangeError::Other(eyre!(
			"client's default recv_window of {rw:?} exceeds maximum allowed duration of {MAX_RECV_WINDOW:?}"
		)));
	}

	if recv_window.is_none() && default_recv_window.is_some() {
		tracing::warn!("called without recv_window, using global default (not recommended)");
	}

	Ok(())
}

/// Blanket impl: any type implementing ExchangeImpl automatically gets Exchange.
/// This enforces that Exchange can only be implemented within this crate (since ExchangeImpl is pub(crate)).
#[async_trait::async_trait]
impl<T: ExchangeImpl> Exchange for T {
	fn name(&self) -> ExchangeName {
		ExchangeImpl::name(self)
	}

	fn auth(&mut self, pubkey: String, secret: SecretString) {
		ExchangeImpl::auth(self, pubkey, secret)
	}

	fn set_recv_window(&mut self, recv_window: std::time::Duration) {
		ExchangeImpl::set_recv_window(self, recv_window)
	}

	fn set_timeout(&mut self, timeout: std::time::Duration) {
		self.http_client_mut().config.timeout = timeout;
	}

	fn set_retry_config(&mut self, config: RetryConfig) {
		self.http_client_mut().config.retry = config;
	}

	fn set_use_testnet(&mut self, b: bool) {
		self.http_client_mut().config.use_testnet = b;
	}

	fn set_cache_testnet_calls(&mut self, duration: Option<std::time::Duration>) {
		self.http_client_mut().config.cache_testnet_calls = duration;
	}

	async fn exchange_info(&self, instrument: Instrument) -> ExchangeResult<ExchangeInfo> {
		ExchangeImpl::exchange_info(self, instrument).await
	}

	async fn klines(&self, symbol: Symbol, tf: Timeframe, range: RequestRange) -> ExchangeResult<Klines> {
		ExchangeImpl::klines(self, symbol, tf, range).await
	}

	async fn prices(&self, pairs: Option<Vec<Pair>>, instrument: Instrument) -> ExchangeResult<BTreeMap<Pair, f64>> {
		ExchangeImpl::prices(self, pairs, instrument).await
	}

	async fn price(&self, symbol: Symbol) -> ExchangeResult<f64> {
		ExchangeImpl::price(self, symbol).await
	}

	async fn open_interest(&self, symbol: Symbol, tf: Timeframe, range: RequestRange) -> ExchangeResult<Vec<OpenInterest>> {
		ExchangeImpl::open_interest(self, symbol, tf, range).await
	}

	async fn personal_info(&self, instrument: Instrument, recv_window: Option<std::time::Duration>) -> ExchangeResult<PersonalInfo> {
		validate_recv_window(recv_window, ExchangeImpl::default_recv_window(self))?;
		ExchangeImpl::personal_info(self, instrument, recv_window).await
	}

	// Websocket connections are NOT rate-limited by the semaphore
	fn ws_trades(&self, pairs: Vec<Pair>, instrument: Instrument) -> ExchangeResult<Box<dyn ExchangeStream<Item = Trade>>> {
		ExchangeImpl::ws_trades(self, pairs, instrument)
	}

	fn ws_book(&self, pairs: Vec<Pair>, instrument: Instrument) -> ExchangeResult<Box<dyn ExchangeStream<Item = BookUpdate>>> {
		ExchangeImpl::ws_book(self, pairs, instrument)
	}
}

// Open Interest {{{
//,}}}

// Klines {{{

//Q: maybe add a `vectorize` method? Should add, question is really if it should be returning a) df b) all fields, including optional and oi c) t, o, h, l, c, v
// probably should figure out rust-typed dataframes for this first
impl Iterator for Klines {
	type Item = Kline;

	fn next(&mut self) -> Option<Self::Item> {
		self.v.pop_front()
	}
}
//,}}}

// RequestRange {{{
impl Default for RequestRange {
	fn default() -> Self {
		RequestRange::Span {
			since: Timestamp::default(),
			until: None,
		}
	}
}
impl From<Timestamp> for RequestRange {
	fn from(value: Timestamp) -> Self {
		RequestRange::Span { since: value, until: None }
	}
}
impl From<jiff::Span> for RequestRange {
	fn from(time_delta: jiff::Span) -> Self {
		let now = Timestamp::now();
		RequestRange::Span {
			since: now - time_delta,
			until: None,
		}
	}
}
impl From<usize> for RequestRange {
	fn from(value: usize) -> Self {
		RequestRange::Limit(value as u32)
	}
}
impl From<u32> for RequestRange {
	fn from(value: u32) -> Self {
		RequestRange::Limit(value)
	}
}
impl From<i32> for RequestRange {
	fn from(value: i32) -> Self {
		RequestRange::Limit(value as u32)
	}
}
impl From<u16> for RequestRange {
	fn from(value: u16) -> Self {
		RequestRange::Limit(value as u32)
	}
}
impl From<u8> for RequestRange {
	fn from(value: u8) -> Self {
		RequestRange::Limit(value as u32)
	}
}
impl From<(Timestamp, Timestamp)> for RequestRange {
	fn from(value: (Timestamp, Timestamp)) -> Self {
		RequestRange::Span {
			since: value.0,
			until: Some(value.1),
		}
	}
}
impl From<(i64, i64)> for RequestRange {
	fn from(value: (i64, i64)) -> Self {
		RequestRange::Span {
			since: Timestamp::from_millisecond(value.0).unwrap(),
			until: Some(Timestamp::from_millisecond(value.1).unwrap()),
		}
	}
}
//,}}}

// Balance {{{
//,}}}

// Exchange Info {{{
//,}}}

// Ticker {{{

impl std::fmt::Display for Ticker {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}:{}", self.exchange_name, self.symbol)
	}
}

impl std::str::FromStr for Ticker {
	type Err = eyre::Report;

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		let (exchange_str, symbol_str) = s.split_once(':').ok_or_else(|| eyre::eyre!("Invalid ticker format"))?;
		let exchange_name = ExchangeName::from_str(exchange_str)?;
		let symbol = Symbol::from_str(symbol_str)?;

		Ok(Ticker { symbol, exchange_name })
	}
}

impl std::fmt::Display for Symbol {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}{}", self.pair, self.instrument)
	}
}

impl std::str::FromStr for Symbol {
	type Err = eyre::Report;

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		let (pair_str, instrument_ticker_str) = s.split_once('.').map(|(p, i)| (p, format!(".{}", i.to_uppercase()))).unwrap_or((s, "".to_owned()));
		let pair = Pair::from_str(pair_str)?;
		let instrument = Instrument::from_str(&instrument_ticker_str)?;

		Ok(Symbol { pair, instrument })
	}
}
impl From<&str> for Symbol {
	fn from(s: &str) -> Self {
		Self::from_str(s).unwrap()
	}
}
//,}}}

// Websocket {{{

//dbg: placeholder, ignore contents
//dbg: placeholder, ignore contents
//,}}}

mod test {
	#[test]
	fn display() {
		let symbol = super::Symbol {
			pair: super::Pair::new("BTC", "USDT"),
			instrument: super::Instrument::Perp,
		};
		let ticker = super::Ticker {
			symbol,
			exchange_name: super::ExchangeName::Bybit,
		};
		assert_eq!(ticker.to_string(), "bybit:BTC-USDT.P");
	}

	#[test]
	fn from_str() {
		let ticker_str = "bybit:BTC-USDT.P";
		let ticker: super::Ticker = ticker_str.parse().unwrap();
		assert_eq!(ticker.symbol.pair, super::Pair::new("BTC", "USDT"));
		assert_eq!(ticker.symbol.instrument, super::Instrument::Perp);
		assert_eq!(ticker.exchange_name, super::ExchangeName::Bybit);
	}

	#[test]
	fn from_str_case_insensitive() {
		// Test lowercase instrument suffix
		let ticker_str = "binance:btc-usdt.p";
		let ticker: super::Ticker = ticker_str.parse().unwrap();
		assert_eq!(ticker.symbol.pair, super::Pair::new("BTC", "USDT"));
		assert_eq!(ticker.symbol.instrument, super::Instrument::Perp);
		assert_eq!(ticker.exchange_name, super::ExchangeName::Binance);

		// Test mixed case
		let ticker_str2 = "bybit:ETH-USDT.pErP_iNvErSe";
		let ticker2: super::Ticker = ticker_str2.parse().unwrap();
		assert_eq!(ticker2.symbol.instrument, super::Instrument::PerpInverse);
	}
}