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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
//! Bybit exchange implementation.
//!
//! Supports spot trading and futures trading (USDT-M and Coin-M) with REST API and WebSocket support.
//! Bybit uses V5 unified account API with HMAC-SHA256 authentication.
use ccxt_core::types::default_type::{DefaultSubType, DefaultType};
use ccxt_core::{BaseExchange, ExchangeConfig, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub mod auth;
pub mod builder;
pub mod endpoint_router;
pub mod error;
pub mod parser;
pub mod rest;
pub mod signed_request;
pub mod symbol;
pub mod ws;
mod ws_exchange_impl;
pub use auth::BybitAuth;
pub use builder::BybitBuilder;
pub use endpoint_router::BybitEndpointRouter;
pub use error::{BybitErrorCode, is_error_response, parse_error};
/// Bybit exchange structure.
#[derive(Debug)]
pub struct Bybit {
/// Base exchange instance.
base: BaseExchange,
/// Bybit-specific options.
options: BybitOptions,
}
/// Bybit-specific options.
///
/// # Example
///
/// ```rust
/// use ccxt_exchanges::bybit::BybitOptions;
/// use ccxt_core::types::default_type::{DefaultType, DefaultSubType};
///
/// let options = BybitOptions {
/// default_type: DefaultType::Swap,
/// default_sub_type: Some(DefaultSubType::Linear),
/// ..Default::default()
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BybitOptions {
/// Account type: UNIFIED, CONTRACT, SPOT.
///
/// This is kept for backward compatibility with existing configurations.
pub account_type: String,
/// Default trading type (spot/swap/futures/option).
///
/// This determines which category to use for API calls.
/// Bybit uses a unified V5 API with category-based filtering:
/// - `Spot` -> category=spot
/// - `Swap` + Linear -> category=linear
/// - `Swap` + Inverse -> category=inverse
/// - `Option` -> category=option
#[serde(default)]
pub default_type: DefaultType,
/// Default sub-type for contract settlement (linear/inverse).
///
/// - `Linear`: USDT-margined contracts (category=linear)
/// - `Inverse`: Coin-margined contracts (category=inverse)
///
/// Only applicable when `default_type` is `Swap` or `Futures`.
/// Ignored for `Spot` and `Option` types.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub default_sub_type: Option<DefaultSubType>,
/// Enables testnet environment.
pub testnet: bool,
/// Receive window in milliseconds.
pub recv_window: u64,
}
impl Default for BybitOptions {
fn default() -> Self {
Self {
account_type: "UNIFIED".to_string(),
default_type: DefaultType::default(), // Defaults to Spot
default_sub_type: None,
testnet: false,
recv_window: 5000,
}
}
}
impl Bybit {
/// Creates a new Bybit instance using the builder pattern.
///
/// This is the recommended way to create a Bybit instance.
///
/// # Example
///
/// ```no_run
/// use ccxt_exchanges::bybit::Bybit;
///
/// let bybit = Bybit::builder()
/// .api_key("your-api-key")
/// .secret("your-secret")
/// .testnet(true)
/// .build()
/// .unwrap();
/// ```
pub fn builder() -> BybitBuilder {
BybitBuilder::new()
}
/// Creates a new Bybit instance.
///
/// # Arguments
///
/// * `config` - Exchange configuration.
pub fn new(config: ExchangeConfig) -> Result<Self> {
let base = BaseExchange::new(config)?;
let options = BybitOptions::default();
Ok(Self { base, options })
}
/// Creates a new Bybit instance with custom options.
///
/// This is used internally by the builder pattern.
///
/// # Arguments
///
/// * `config` - Exchange configuration.
/// * `options` - Bybit-specific options.
pub fn new_with_options(config: ExchangeConfig, options: BybitOptions) -> Result<Self> {
let base = BaseExchange::new(config)?;
Ok(Self { base, options })
}
/// Returns a reference to the base exchange.
pub fn base(&self) -> &BaseExchange {
&self.base
}
/// Returns a mutable reference to the base exchange.
pub fn base_mut(&mut self) -> &mut BaseExchange {
&mut self.base
}
/// Returns the Bybit options.
pub fn options(&self) -> &BybitOptions {
&self.options
}
/// Sets the Bybit options.
pub fn set_options(&mut self, options: BybitOptions) {
self.options = options;
}
/// Returns the exchange ID.
pub fn id(&self) -> &'static str {
"bybit"
}
/// Returns the exchange name.
pub fn name(&self) -> &'static str {
"Bybit"
}
/// Returns the API version.
pub fn version(&self) -> &'static str {
"v5"
}
/// Returns `true` if the exchange is CCXT-certified.
pub fn certified(&self) -> bool {
false
}
/// Returns `true` if Pro version (WebSocket) is supported.
pub fn pro(&self) -> bool {
true
}
/// Returns the rate limit in requests per second.
pub fn rate_limit(&self) -> u32 {
20
}
/// Returns `true` if sandbox/testnet mode is enabled.
///
/// Sandbox mode is enabled when either:
/// - `config.sandbox` is set to `true`
/// - `options.testnet` is set to `true`
///
/// # Returns
///
/// `true` if sandbox mode is enabled, `false` otherwise.
///
/// # Example
///
/// ```no_run
/// use ccxt_exchanges::bybit::Bybit;
/// use ccxt_core::ExchangeConfig;
///
/// let config = ExchangeConfig {
/// sandbox: true,
/// ..Default::default()
/// };
/// let bybit = Bybit::new(config).unwrap();
/// assert!(bybit.is_sandbox());
/// ```
pub fn is_sandbox(&self) -> bool {
self.base().config.sandbox || self.options.testnet
}
/// Returns the supported timeframes.
pub fn timeframes(&self) -> HashMap<String, String> {
let mut timeframes = HashMap::new();
timeframes.insert("1m".to_string(), "1".to_string());
timeframes.insert("3m".to_string(), "3".to_string());
timeframes.insert("5m".to_string(), "5".to_string());
timeframes.insert("15m".to_string(), "15".to_string());
timeframes.insert("30m".to_string(), "30".to_string());
timeframes.insert("1h".to_string(), "60".to_string());
timeframes.insert("2h".to_string(), "120".to_string());
timeframes.insert("4h".to_string(), "240".to_string());
timeframes.insert("6h".to_string(), "360".to_string());
timeframes.insert("12h".to_string(), "720".to_string());
timeframes.insert("1d".to_string(), "D".to_string());
timeframes.insert("1w".to_string(), "W".to_string());
timeframes.insert("1M".to_string(), "M".to_string());
timeframes
}
/// Returns the API URLs.
pub fn urls(&self) -> BybitUrls {
if self.base().config.sandbox || self.options.testnet {
BybitUrls::testnet()
} else {
BybitUrls::production()
}
}
/// Returns the default type configuration.
pub fn default_type(&self) -> DefaultType {
self.options.default_type
}
/// Returns the default sub-type configuration.
pub fn default_sub_type(&self) -> Option<DefaultSubType> {
self.options.default_sub_type
}
/// Checks if the current default_type is a contract type (Swap, Futures, or Option).
///
/// This is useful for determining whether contract-specific API parameters should be used.
///
/// # Returns
///
/// `true` if the default_type is Swap, Futures, or Option; `false` otherwise.
pub fn is_contract_type(&self) -> bool {
self.options.default_type.is_contract()
}
/// Checks if the current configuration uses inverse (coin-margined) contracts.
///
/// # Returns
///
/// `true` if default_sub_type is Inverse; `false` otherwise.
pub fn is_inverse(&self) -> bool {
matches!(self.options.default_sub_type, Some(DefaultSubType::Inverse))
}
/// Checks if the current configuration uses linear (USDT-margined) contracts.
///
/// # Returns
///
/// `true` if default_sub_type is Linear or not specified (defaults to Linear); `false` otherwise.
pub fn is_linear(&self) -> bool {
!self.is_inverse()
}
/// Returns the Bybit category string based on the current default_type and default_sub_type.
///
/// Bybit V5 API uses category parameter for filtering:
/// - `Spot` -> "spot"
/// - `Swap` + Linear -> "linear"
/// - `Swap` + Inverse -> "inverse"
/// - `Futures` + Linear -> "linear"
/// - `Futures` + Inverse -> "inverse"
/// - `Option` -> "option"
/// - `Margin` -> "spot" (margin trading uses spot category)
///
/// # Returns
///
/// The category string to use for Bybit API calls.
pub fn category(&self) -> &'static str {
match self.options.default_type {
DefaultType::Spot | DefaultType::Margin => "spot",
DefaultType::Swap | DefaultType::Futures => {
if self.is_inverse() {
"inverse"
} else {
"linear"
}
}
DefaultType::Option => "option",
}
}
/// Creates a public WebSocket client.
///
/// The WebSocket URL is selected based on the configured `default_type`:
/// - `Spot` -> spot public stream
/// - `Swap`/`Futures` + Linear -> linear public stream
/// - `Swap`/`Futures` + Inverse -> inverse public stream
/// - `Option` -> option public stream
///
/// # Returns
///
/// Returns a `BybitWs` instance for public data streams.
///
/// # Example
/// ```no_run
/// use ccxt_exchanges::bybit::Bybit;
/// use ccxt_core::ExchangeConfig;
///
/// # async fn example() -> ccxt_core::error::Result<()> {
/// let bybit = Bybit::new(ExchangeConfig::default())?;
/// let ws = bybit.create_ws();
/// ws.connect().await?;
/// # Ok(())
/// # }
/// ```
pub fn create_ws(&self) -> ws::BybitWs {
let urls = self.urls();
let category = self.category();
let ws_url = urls.ws_public_for_category(category);
ws::BybitWs::new(ws_url)
}
/// Creates a signed request builder for authenticated API calls.
///
/// This method provides a fluent API for constructing authenticated requests
/// to Bybit's private endpoints. The builder handles:
/// - Credential validation
/// - Millisecond timestamp generation
/// - HMAC-SHA256 signature generation (hex encoded)
/// - Authentication header injection (X-BAPI-* headers)
///
/// # Arguments
///
/// * `endpoint` - API endpoint path (e.g., "/v5/account/wallet-balance")
///
/// # Returns
///
/// Returns a `BybitSignedRequestBuilder` for method chaining.
///
/// # Example
///
/// ```no_run
/// use ccxt_exchanges::bybit::Bybit;
/// use ccxt_exchanges::bybit::signed_request::HttpMethod;
/// use ccxt_core::ExchangeConfig;
///
/// # async fn example() -> ccxt_core::Result<()> {
/// let bybit = Bybit::builder()
/// .api_key("your-api-key")
/// .secret("your-secret")
/// .build()?;
///
/// // GET request
/// let balance = bybit.signed_request("/v5/account/wallet-balance")
/// .param("accountType", "UNIFIED")
/// .execute()
/// .await?;
///
/// // POST request
/// let order = bybit.signed_request("/v5/order/create")
/// .method(HttpMethod::Post)
/// .param("category", "spot")
/// .param("symbol", "BTCUSDT")
/// .param("side", "Buy")
/// .param("orderType", "Limit")
/// .param("qty", "0.001")
/// .param("price", "50000")
/// .execute()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn signed_request(
&self,
endpoint: impl Into<String>,
) -> signed_request::BybitSignedRequestBuilder<'_> {
signed_request::BybitSignedRequestBuilder::new(self, endpoint)
}
}
/// Bybit API URLs.
#[derive(Debug, Clone)]
pub struct BybitUrls {
/// REST API base URL.
pub rest: String,
/// Public WebSocket base URL (without category suffix).
pub ws_public_base: String,
/// Public WebSocket URL (default: spot).
pub ws_public: String,
/// Private WebSocket URL.
pub ws_private: String,
}
impl BybitUrls {
/// Returns production environment URLs.
pub fn production() -> Self {
Self {
rest: "https://api.bybit.com".to_string(),
ws_public_base: "wss://stream.bybit.com/v5/public".to_string(),
ws_public: "wss://stream.bybit.com/v5/public/spot".to_string(),
ws_private: "wss://stream.bybit.com/v5/private".to_string(),
}
}
/// Returns testnet environment URLs.
pub fn testnet() -> Self {
Self {
rest: "https://api-testnet.bybit.com".to_string(),
ws_public_base: "wss://stream-testnet.bybit.com/v5/public".to_string(),
ws_public: "wss://stream-testnet.bybit.com/v5/public/spot".to_string(),
ws_private: "wss://stream-testnet.bybit.com/v5/private".to_string(),
}
}
/// Returns the public WebSocket URL for a specific category.
///
/// Bybit V5 API uses different WebSocket endpoints for different categories:
/// - spot: /v5/public/spot
/// - linear: /v5/public/linear
/// - inverse: /v5/public/inverse
/// - option: /v5/public/option
///
/// # Arguments
///
/// * `category` - The category string (spot, linear, inverse, option)
///
/// # Returns
///
/// The full WebSocket URL for the specified category.
pub fn ws_public_for_category(&self, category: &str) -> String {
format!("{}/{}", self.ws_public_base, category)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bybit_creation() {
let config = ExchangeConfig {
id: "bybit".to_string(),
name: "Bybit".to_string(),
..Default::default()
};
let bybit = Bybit::new(config);
assert!(bybit.is_ok());
let bybit = bybit.unwrap();
assert_eq!(bybit.id(), "bybit");
assert_eq!(bybit.name(), "Bybit");
assert_eq!(bybit.version(), "v5");
assert!(!bybit.certified());
assert!(bybit.pro());
}
#[test]
fn test_timeframes() {
let config = ExchangeConfig::default();
let bybit = Bybit::new(config).unwrap();
let timeframes = bybit.timeframes();
assert!(timeframes.contains_key("1m"));
assert!(timeframes.contains_key("1h"));
assert!(timeframes.contains_key("1d"));
assert_eq!(timeframes.len(), 13);
}
#[test]
fn test_urls() {
let config = ExchangeConfig::default();
let bybit = Bybit::new(config).unwrap();
let urls = bybit.urls();
assert!(urls.rest.contains("api.bybit.com"));
assert!(urls.ws_public.contains("stream.bybit.com"));
assert!(urls.ws_public_base.contains("stream.bybit.com"));
}
#[test]
fn test_testnet_urls() {
let config = ExchangeConfig {
sandbox: true,
..Default::default()
};
let bybit = Bybit::new(config).unwrap();
let urls = bybit.urls();
assert!(urls.rest.contains("api-testnet.bybit.com"));
assert!(urls.ws_public.contains("stream-testnet.bybit.com"));
assert!(urls.ws_public_base.contains("stream-testnet.bybit.com"));
}
#[test]
fn test_is_sandbox_with_config_sandbox() {
let config = ExchangeConfig {
sandbox: true,
..Default::default()
};
let bybit = Bybit::new(config).unwrap();
assert!(bybit.is_sandbox());
}
#[test]
fn test_is_sandbox_with_options_testnet() {
let config = ExchangeConfig::default();
let options = BybitOptions {
testnet: true,
..Default::default()
};
let bybit = Bybit::new_with_options(config, options).unwrap();
assert!(bybit.is_sandbox());
}
#[test]
fn test_is_sandbox_false_by_default() {
let config = ExchangeConfig::default();
let bybit = Bybit::new(config).unwrap();
assert!(!bybit.is_sandbox());
}
#[test]
fn test_ws_public_for_category() {
let urls = BybitUrls::production();
assert_eq!(
urls.ws_public_for_category("spot"),
"wss://stream.bybit.com/v5/public/spot"
);
assert_eq!(
urls.ws_public_for_category("linear"),
"wss://stream.bybit.com/v5/public/linear"
);
assert_eq!(
urls.ws_public_for_category("inverse"),
"wss://stream.bybit.com/v5/public/inverse"
);
assert_eq!(
urls.ws_public_for_category("option"),
"wss://stream.bybit.com/v5/public/option"
);
}
#[test]
fn test_ws_public_for_category_testnet() {
let urls = BybitUrls::testnet();
assert_eq!(
urls.ws_public_for_category("spot"),
"wss://stream-testnet.bybit.com/v5/public/spot"
);
assert_eq!(
urls.ws_public_for_category("linear"),
"wss://stream-testnet.bybit.com/v5/public/linear"
);
}
#[test]
fn test_default_options() {
let options = BybitOptions::default();
assert_eq!(options.account_type, "UNIFIED");
assert_eq!(options.default_type, DefaultType::Spot);
assert_eq!(options.default_sub_type, None);
assert!(!options.testnet);
assert_eq!(options.recv_window, 5000);
}
#[test]
fn test_bybit_options_with_default_type() {
let options = BybitOptions {
default_type: DefaultType::Swap,
default_sub_type: Some(DefaultSubType::Linear),
..Default::default()
};
assert_eq!(options.default_type, DefaultType::Swap);
assert_eq!(options.default_sub_type, Some(DefaultSubType::Linear));
}
#[test]
fn test_bybit_options_serialization() {
let options = BybitOptions {
default_type: DefaultType::Swap,
default_sub_type: Some(DefaultSubType::Linear),
..Default::default()
};
let json = serde_json::to_string(&options).unwrap();
assert!(json.contains("\"default_type\":\"swap\""));
assert!(json.contains("\"default_sub_type\":\"linear\""));
}
#[test]
fn test_bybit_options_deserialization() {
let json = r#"{
"account_type": "CONTRACT",
"default_type": "swap",
"default_sub_type": "inverse",
"testnet": true,
"recv_window": 10000
}"#;
let options: BybitOptions = serde_json::from_str(json).unwrap();
assert_eq!(options.account_type, "CONTRACT");
assert_eq!(options.default_type, DefaultType::Swap);
assert_eq!(options.default_sub_type, Some(DefaultSubType::Inverse));
assert!(options.testnet);
assert_eq!(options.recv_window, 10000);
}
#[test]
fn test_bybit_options_deserialization_without_default_type() {
// Test backward compatibility - default_type should default to Spot
let json = r#"{
"account_type": "UNIFIED",
"testnet": false,
"recv_window": 5000
}"#;
let options: BybitOptions = serde_json::from_str(json).unwrap();
assert_eq!(options.default_type, DefaultType::Spot);
assert_eq!(options.default_sub_type, None);
}
#[test]
fn test_bybit_category_spot() {
let config = ExchangeConfig::default();
let options = BybitOptions {
default_type: DefaultType::Spot,
..Default::default()
};
let bybit = Bybit::new_with_options(config, options).unwrap();
assert_eq!(bybit.category(), "spot");
}
#[test]
fn test_bybit_category_linear() {
let config = ExchangeConfig::default();
let options = BybitOptions {
default_type: DefaultType::Swap,
default_sub_type: Some(DefaultSubType::Linear),
..Default::default()
};
let bybit = Bybit::new_with_options(config, options).unwrap();
assert_eq!(bybit.category(), "linear");
}
#[test]
fn test_bybit_category_inverse() {
let config = ExchangeConfig::default();
let options = BybitOptions {
default_type: DefaultType::Swap,
default_sub_type: Some(DefaultSubType::Inverse),
..Default::default()
};
let bybit = Bybit::new_with_options(config, options).unwrap();
assert_eq!(bybit.category(), "inverse");
}
#[test]
fn test_bybit_category_option() {
let config = ExchangeConfig::default();
let options = BybitOptions {
default_type: DefaultType::Option,
..Default::default()
};
let bybit = Bybit::new_with_options(config, options).unwrap();
assert_eq!(bybit.category(), "option");
}
#[test]
fn test_bybit_is_contract_type() {
let config = ExchangeConfig::default();
// Spot is not a contract type
let options = BybitOptions {
default_type: DefaultType::Spot,
..Default::default()
};
let bybit = Bybit::new_with_options(config.clone(), options).unwrap();
assert!(!bybit.is_contract_type());
// Swap is a contract type
let options = BybitOptions {
default_type: DefaultType::Swap,
..Default::default()
};
let bybit = Bybit::new_with_options(config.clone(), options).unwrap();
assert!(bybit.is_contract_type());
// Futures is a contract type
let options = BybitOptions {
default_type: DefaultType::Futures,
..Default::default()
};
let bybit = Bybit::new_with_options(config.clone(), options).unwrap();
assert!(bybit.is_contract_type());
// Option is a contract type
let options = BybitOptions {
default_type: DefaultType::Option,
..Default::default()
};
let bybit = Bybit::new_with_options(config, options).unwrap();
assert!(bybit.is_contract_type());
}
// ============================================================
// Sandbox Mode Market Type URL Selection Tests
// ============================================================
#[test]
fn test_sandbox_market_type_spot() {
// Sandbox mode with Spot type should use spot testnet endpoints
let config = ExchangeConfig {
sandbox: true,
..Default::default()
};
let options = BybitOptions {
default_type: DefaultType::Spot,
..Default::default()
};
let bybit = Bybit::new_with_options(config, options).unwrap();
assert!(bybit.is_sandbox());
assert_eq!(bybit.category(), "spot");
let urls = bybit.urls();
assert!(
urls.rest.contains("api-testnet.bybit.com"),
"Spot sandbox REST URL should contain api-testnet.bybit.com, got: {}",
urls.rest
);
let ws_url = urls.ws_public_for_category("spot");
assert!(
ws_url.contains("stream-testnet.bybit.com"),
"Spot sandbox WS URL should contain stream-testnet.bybit.com, got: {}",
ws_url
);
assert!(
ws_url.contains("/v5/public/spot"),
"Spot sandbox WS URL should contain /v5/public/spot, got: {}",
ws_url
);
}
#[test]
fn test_sandbox_market_type_linear() {
// Sandbox mode with Swap/Linear should use linear testnet endpoints
let config = ExchangeConfig {
sandbox: true,
..Default::default()
};
let options = BybitOptions {
default_type: DefaultType::Swap,
default_sub_type: Some(DefaultSubType::Linear),
..Default::default()
};
let bybit = Bybit::new_with_options(config, options).unwrap();
assert!(bybit.is_sandbox());
assert_eq!(bybit.category(), "linear");
let urls = bybit.urls();
assert!(
urls.rest.contains("api-testnet.bybit.com"),
"Linear sandbox REST URL should contain api-testnet.bybit.com, got: {}",
urls.rest
);
let ws_url = urls.ws_public_for_category("linear");
assert!(
ws_url.contains("stream-testnet.bybit.com"),
"Linear sandbox WS URL should contain stream-testnet.bybit.com, got: {}",
ws_url
);
assert!(
ws_url.contains("/v5/public/linear"),
"Linear sandbox WS URL should contain /v5/public/linear, got: {}",
ws_url
);
}
#[test]
fn test_sandbox_market_type_inverse() {
// Sandbox mode with Swap/Inverse should use inverse testnet endpoints
let config = ExchangeConfig {
sandbox: true,
..Default::default()
};
let options = BybitOptions {
default_type: DefaultType::Swap,
default_sub_type: Some(DefaultSubType::Inverse),
..Default::default()
};
let bybit = Bybit::new_with_options(config, options).unwrap();
assert!(bybit.is_sandbox());
assert_eq!(bybit.category(), "inverse");
let urls = bybit.urls();
assert!(
urls.rest.contains("api-testnet.bybit.com"),
"Inverse sandbox REST URL should contain api-testnet.bybit.com, got: {}",
urls.rest
);
let ws_url = urls.ws_public_for_category("inverse");
assert!(
ws_url.contains("stream-testnet.bybit.com"),
"Inverse sandbox WS URL should contain stream-testnet.bybit.com, got: {}",
ws_url
);
assert!(
ws_url.contains("/v5/public/inverse"),
"Inverse sandbox WS URL should contain /v5/public/inverse, got: {}",
ws_url
);
}
#[test]
fn test_sandbox_market_type_option() {
// Sandbox mode with Option type should use option testnet endpoints
let config = ExchangeConfig {
sandbox: true,
..Default::default()
};
let options = BybitOptions {
default_type: DefaultType::Option,
..Default::default()
};
let bybit = Bybit::new_with_options(config, options).unwrap();
assert!(bybit.is_sandbox());
assert_eq!(bybit.category(), "option");
let urls = bybit.urls();
assert!(
urls.rest.contains("api-testnet.bybit.com"),
"Option sandbox REST URL should contain api-testnet.bybit.com, got: {}",
urls.rest
);
let ws_url = urls.ws_public_for_category("option");
assert!(
ws_url.contains("stream-testnet.bybit.com"),
"Option sandbox WS URL should contain stream-testnet.bybit.com, got: {}",
ws_url
);
assert!(
ws_url.contains("/v5/public/option"),
"Option sandbox WS URL should contain /v5/public/option, got: {}",
ws_url
);
}
#[test]
fn test_sandbox_private_websocket_url() {
// Verify private WebSocket URL in sandbox mode
let config = ExchangeConfig {
sandbox: true,
..Default::default()
};
let bybit = Bybit::new(config).unwrap();
assert!(bybit.is_sandbox());
let urls = bybit.urls();
assert!(
urls.ws_private.contains("stream-testnet.bybit.com"),
"Private WS sandbox URL should contain stream-testnet.bybit.com, got: {}",
urls.ws_private
);
assert!(
urls.ws_private.contains("/v5/private"),
"Private WS sandbox URL should contain /v5/private, got: {}",
urls.ws_private
);
}
#[test]
fn test_sandbox_futures_linear() {
// Sandbox mode with Futures/Linear should use linear testnet endpoints
let config = ExchangeConfig {
sandbox: true,
..Default::default()
};
let options = BybitOptions {
default_type: DefaultType::Futures,
default_sub_type: Some(DefaultSubType::Linear),
..Default::default()
};
let bybit = Bybit::new_with_options(config, options).unwrap();
assert!(bybit.is_sandbox());
assert_eq!(bybit.category(), "linear");
let urls = bybit.urls();
assert!(
urls.rest.contains("api-testnet.bybit.com"),
"Futures Linear sandbox REST URL should contain api-testnet.bybit.com, got: {}",
urls.rest
);
}
#[test]
fn test_sandbox_futures_inverse() {
let config = ExchangeConfig {
sandbox: true,
..Default::default()
};
let options = BybitOptions {
default_type: DefaultType::Futures,
default_sub_type: Some(DefaultSubType::Inverse),
..Default::default()
};
let bybit = Bybit::new_with_options(config, options).unwrap();
assert!(bybit.is_sandbox());
assert_eq!(bybit.category(), "inverse");
let urls = bybit.urls();
assert!(
urls.rest.contains("api-testnet.bybit.com"),
"Futures Inverse sandbox REST URL should contain api-testnet.bybit.com, got: {}",
urls.rest
);
}
}