ccxt-exchanges 0.1.5

Exchange implementations for CCXT Rust
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
#![allow(clippy::disallowed_methods)]
//! Property-based tests for Sandbox/Testnet Support.
//!
//! These tests verify correctness properties for sandbox/testnet URL selection
//! across all supported exchanges using proptest.
//!
//! **Feature: sandbox-testnet-support**

use proptest::prelude::*;

/// For any exchange instance and any sandbox configuration (true/false), the URLs
/// returned by `urls()` SHALL be consistent with the sandbox setting - testnet URLs
/// when sandbox is true, production URLs when sandbox is false.
mod sandbox_url_selection_consistency {
    use super::*;
    use ccxt_exchanges::binance::BinanceBuilder;
    use ccxt_exchanges::bitget::BitgetBuilder;
    use ccxt_exchanges::bybit::BybitBuilder;
    use ccxt_exchanges::hyperliquid::HyperLiquidBuilder;
    use ccxt_exchanges::okx::OkxBuilder;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Test Binance sandbox URL selection consistency
        /// When sandbox=true, URLs should contain testnet domains
        /// When sandbox=false, URLs should contain production domains
        #[test]
        fn prop_binance_sandbox_url_consistency(sandbox in any::<bool>()) {
            let binance = BinanceBuilder::new()
                .sandbox(sandbox)
                .build()
                .expect("Should build Binance instance");

            let urls = binance.urls();

            if sandbox {
                // Testnet URLs should contain testnet domains
                prop_assert!(
                    urls.public.contains("testnet.binance.vision"),
                    "Binance public REST URL should be testnet when sandbox=true, got: {}",
                    urls.public
                );
            } else {
                // Production URLs should contain production domains
                prop_assert!(
                    urls.public.contains("api.binance.com"),
                    "Binance public REST URL should be production when sandbox=false, got: {}",
                    urls.public
                );
            }

            // Verify is_sandbox() matches the configuration
            prop_assert_eq!(
                binance.is_sandbox(),
                sandbox,
                "is_sandbox() should match sandbox configuration"
            );
        }

        /// Test OKX sandbox URL selection consistency
        /// OKX uses same REST domain but different WebSocket URLs for demo mode
        #[test]
        fn prop_okx_sandbox_url_consistency(sandbox in any::<bool>()) {
            let okx = OkxBuilder::new()
                .sandbox(sandbox)
                .build()
                .expect("Should build OKX instance");

            let urls = okx.urls();

            // OKX REST URL should always be production domain (demo uses header)
            prop_assert!(
                urls.rest.contains("www.okx.com"),
                "OKX REST URL should always be production domain, got: {}",
                urls.rest
            );

            if sandbox {
                // Demo WebSocket URLs should contain demo endpoint
                prop_assert!(
                    urls.ws_public.contains("wspap.okx.com") || urls.ws_public.contains("brokerId=9999"),
                    "OKX WS URL should be demo when sandbox=true, got: {}",
                    urls.ws_public
                );
            } else {
                // Production WebSocket URLs
                prop_assert!(
                    urls.ws_public.contains("ws.okx.com"),
                    "OKX WS URL should be production when sandbox=false, got: {}",
                    urls.ws_public
                );
            }

            // Verify is_sandbox() matches the configuration
            prop_assert_eq!(
                okx.is_sandbox(),
                sandbox,
                "is_sandbox() should match sandbox configuration"
            );
        }

        /// Test Bybit sandbox URL selection consistency
        #[test]
        fn prop_bybit_sandbox_url_consistency(sandbox in any::<bool>()) {
            let bybit = BybitBuilder::new()
                .sandbox(sandbox)
                .build()
                .expect("Should build Bybit instance");

            let urls = bybit.urls();

            if sandbox {
                // Testnet URLs
                prop_assert!(
                    urls.rest.contains("api-testnet.bybit.com"),
                    "Bybit REST URL should be testnet when sandbox=true, got: {}",
                    urls.rest
                );
            } else {
                // Production URLs
                prop_assert!(
                    urls.rest.contains("api.bybit.com") && !urls.rest.contains("testnet"),
                    "Bybit REST URL should be production when sandbox=false, got: {}",
                    urls.rest
                );
            }

            // Verify is_sandbox() matches the configuration
            prop_assert_eq!(
                bybit.is_sandbox(),
                sandbox,
                "is_sandbox() should match sandbox configuration"
            );
        }

        /// Test Bitget sandbox URL selection consistency
        #[test]
        fn prop_bitget_sandbox_url_consistency(sandbox in any::<bool>()) {
            let bitget = BitgetBuilder::new()
                .sandbox(sandbox)
                .build()
                .expect("Should build Bitget instance");

            let urls = bitget.urls();

            if sandbox {
                // Testnet URLs
                prop_assert!(
                    urls.rest.contains("api-testnet.bitget.com"),
                    "Bitget REST URL should be testnet when sandbox=true, got: {}",
                    urls.rest
                );
            } else {
                // Production URLs
                prop_assert!(
                    urls.rest.contains("api.bitget.com") && !urls.rest.contains("testnet"),
                    "Bitget REST URL should be production when sandbox=false, got: {}",
                    urls.rest
                );
            }

            // Verify is_sandbox() matches the configuration
            prop_assert_eq!(
                bitget.is_sandbox(),
                sandbox,
                "is_sandbox() should match sandbox configuration"
            );
        }

        /// Test Hyperliquid sandbox URL selection consistency
        #[test]
        fn prop_hyperliquid_sandbox_url_consistency(sandbox in any::<bool>()) {
            let hyperliquid = HyperLiquidBuilder::new()
                .sandbox(sandbox)
                .build()
                .expect("Should build Hyperliquid instance");

            let urls = hyperliquid.urls();

            if sandbox {
                // Testnet URLs
                prop_assert!(
                    urls.rest.contains("hyperliquid-testnet.xyz"),
                    "Hyperliquid REST URL should be testnet when sandbox=true, got: {}",
                    urls.rest
                );
            } else {
                // Production URLs
                prop_assert!(
                    urls.rest.contains("api.hyperliquid.xyz") && !urls.rest.contains("testnet"),
                    "Hyperliquid REST URL should be production when sandbox=false, got: {}",
                    urls.rest
                );
            }

            // Verify is_sandbox() matches the configuration
            prop_assert_eq!(
                hyperliquid.is_sandbox(),
                sandbox,
                "is_sandbox() should match sandbox configuration"
            );
        }

        /// Test that all exchanges have consistent sandbox URL selection
        /// This is a comprehensive test that verifies all exchanges together
        #[test]
        fn prop_all_exchanges_sandbox_consistency(sandbox in any::<bool>()) {
            // Build all exchanges with the same sandbox setting
            let binance = BinanceBuilder::new().sandbox(sandbox).build().expect("Binance");
            let okx = OkxBuilder::new().sandbox(sandbox).build().expect("OKX");
            let bybit = BybitBuilder::new().sandbox(sandbox).build().expect("Bybit");
            let bitget = BitgetBuilder::new().sandbox(sandbox).build().expect("Bitget");
            let hyperliquid = HyperLiquidBuilder::new().sandbox(sandbox).build().expect("Hyperliquid");

            // All is_sandbox() methods should return the same value
            prop_assert_eq!(binance.is_sandbox(), sandbox, "Binance is_sandbox mismatch");
            prop_assert_eq!(okx.is_sandbox(), sandbox, "OKX is_sandbox mismatch");
            prop_assert_eq!(bybit.is_sandbox(), sandbox, "Bybit is_sandbox mismatch");
            prop_assert_eq!(bitget.is_sandbox(), sandbox, "Bitget is_sandbox mismatch");
            prop_assert_eq!(hyperliquid.is_sandbox(), sandbox, "Hyperliquid is_sandbox mismatch");

            // All URLs should be consistent with sandbox setting
            let binance_urls = binance.urls();
            let okx_urls = okx.urls();
            let bybit_urls = bybit.urls();
            let bitget_urls = bitget.urls();
            let hyperliquid_urls = hyperliquid.urls();

            if sandbox {
                // All testnet URLs should contain testnet indicators
                prop_assert!(binance_urls.public.contains("testnet"), "Binance testnet URL");
                // OKX uses same REST domain for demo (header-based)
                prop_assert!(bybit_urls.rest.contains("testnet"), "Bybit testnet URL");
                prop_assert!(bitget_urls.rest.contains("testnet"), "Bitget testnet URL");
                prop_assert!(hyperliquid_urls.rest.contains("testnet"), "Hyperliquid testnet URL");
            } else {
                // All production URLs should NOT contain testnet
                prop_assert!(!binance_urls.public.contains("testnet"), "Binance production URL");
                prop_assert!(!okx_urls.rest.contains("testnet"), "OKX production URL");
                prop_assert!(!bybit_urls.rest.contains("testnet"), "Bybit production URL");
                prop_assert!(!bitget_urls.rest.contains("testnet"), "Bitget production URL");
                prop_assert!(!hyperliquid_urls.rest.contains("testnet"), "Hyperliquid production URL");
            }
        }
    }
}

/// For any exchange instance with sandbox mode enabled, the WebSocket URLs SHALL
/// point to the testnet/demo WebSocket endpoints appropriate for that exchange.
mod websocket_url_selection_consistency {
    use super::*;
    use ccxt_exchanges::binance::BinanceBuilder;
    use ccxt_exchanges::bitget::BitgetBuilder;
    use ccxt_exchanges::bybit::BybitBuilder;
    use ccxt_exchanges::hyperliquid::HyperLiquidBuilder;
    use ccxt_exchanges::okx::OkxBuilder;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Test Binance WebSocket URL selection consistency
        #[test]
        fn prop_binance_ws_url_consistency(sandbox in any::<bool>()) {
            let binance = BinanceBuilder::new()
                .sandbox(sandbox)
                .build()
                .expect("Should build Binance instance");

            let urls = binance.urls();

            if sandbox {
                // Testnet WebSocket URLs
                prop_assert!(
                    urls.ws.contains("testnet.binance.vision"),
                    "Binance WS URL should be testnet when sandbox=true, got: {}",
                    urls.ws
                );
            } else {
                // Production WebSocket URLs
                prop_assert!(
                    urls.ws.contains("stream.binance.com"),
                    "Binance WS URL should be production when sandbox=false, got: {}",
                    urls.ws
                );
            }
        }

        /// Test OKX WebSocket URL selection consistency
        /// OKX uses different WebSocket URLs for demo mode
        #[test]
        fn prop_okx_ws_url_consistency(sandbox in any::<bool>()) {
            let okx = OkxBuilder::new()
                .sandbox(sandbox)
                .build()
                .expect("Should build OKX instance");

            let urls = okx.urls();

            if sandbox {
                // Demo WebSocket URLs should contain demo endpoint
                prop_assert!(
                    urls.ws_public.contains("wspap.okx.com") || urls.ws_public.contains("brokerId=9999"),
                    "OKX public WS URL should be demo when sandbox=true, got: {}",
                    urls.ws_public
                );
                prop_assert!(
                    urls.ws_private.contains("wspap.okx.com") || urls.ws_private.contains("brokerId=9999"),
                    "OKX private WS URL should be demo when sandbox=true, got: {}",
                    urls.ws_private
                );
            } else {
                // Production WebSocket URLs
                prop_assert!(
                    urls.ws_public.contains("ws.okx.com"),
                    "OKX public WS URL should be production when sandbox=false, got: {}",
                    urls.ws_public
                );
                prop_assert!(
                    urls.ws_private.contains("ws.okx.com"),
                    "OKX private WS URL should be production when sandbox=false, got: {}",
                    urls.ws_private
                );
            }
        }

        /// Test Bybit WebSocket URL selection consistency
        #[test]
        fn prop_bybit_ws_url_consistency(sandbox in any::<bool>()) {
            let bybit = BybitBuilder::new()
                .sandbox(sandbox)
                .build()
                .expect("Should build Bybit instance");

            let urls = bybit.urls();

            if sandbox {
                // Testnet WebSocket URLs
                prop_assert!(
                    urls.ws_public.contains("stream-testnet.bybit.com"),
                    "Bybit public WS URL should be testnet when sandbox=true, got: {}",
                    urls.ws_public
                );
                prop_assert!(
                    urls.ws_private.contains("stream-testnet.bybit.com"),
                    "Bybit private WS URL should be testnet when sandbox=true, got: {}",
                    urls.ws_private
                );
            } else {
                // Production WebSocket URLs
                prop_assert!(
                    urls.ws_public.contains("stream.bybit.com") && !urls.ws_public.contains("testnet"),
                    "Bybit public WS URL should be production when sandbox=false, got: {}",
                    urls.ws_public
                );
                prop_assert!(
                    urls.ws_private.contains("stream.bybit.com") && !urls.ws_private.contains("testnet"),
                    "Bybit private WS URL should be production when sandbox=false, got: {}",
                    urls.ws_private
                );
            }
        }

        /// Test Bitget WebSocket URL selection consistency
        #[test]
        fn prop_bitget_ws_url_consistency(sandbox in any::<bool>()) {
            let bitget = BitgetBuilder::new()
                .sandbox(sandbox)
                .build()
                .expect("Should build Bitget instance");

            let urls = bitget.urls();

            if sandbox {
                // Testnet WebSocket URLs
                prop_assert!(
                    urls.ws_public.contains("ws-testnet.bitget.com"),
                    "Bitget public WS URL should be testnet when sandbox=true, got: {}",
                    urls.ws_public
                );
                prop_assert!(
                    urls.ws_private.contains("ws-testnet.bitget.com"),
                    "Bitget private WS URL should be testnet when sandbox=true, got: {}",
                    urls.ws_private
                );
            } else {
                // Production WebSocket URLs
                prop_assert!(
                    urls.ws_public.contains("ws.bitget.com") && !urls.ws_public.contains("testnet"),
                    "Bitget public WS URL should be production when sandbox=false, got: {}",
                    urls.ws_public
                );
                prop_assert!(
                    urls.ws_private.contains("ws.bitget.com") && !urls.ws_private.contains("testnet"),
                    "Bitget private WS URL should be production when sandbox=false, got: {}",
                    urls.ws_private
                );
            }
        }

        /// Test Hyperliquid WebSocket URL selection consistency
        #[test]
        fn prop_hyperliquid_ws_url_consistency(sandbox in any::<bool>()) {
            let hyperliquid = HyperLiquidBuilder::new()
                .sandbox(sandbox)
                .build()
                .expect("Should build Hyperliquid instance");

            let urls = hyperliquid.urls();

            if sandbox {
                // Testnet WebSocket URLs
                prop_assert!(
                    urls.ws.contains("hyperliquid-testnet.xyz"),
                    "Hyperliquid WS URL should be testnet when sandbox=true, got: {}",
                    urls.ws
                );
            } else {
                // Production WebSocket URLs
                prop_assert!(
                    urls.ws.contains("api.hyperliquid.xyz") && !urls.ws.contains("testnet"),
                    "Hyperliquid WS URL should be production when sandbox=false, got: {}",
                    urls.ws
                );
            }
        }

        /// Test that all exchanges have consistent WebSocket URL selection
        #[test]
        fn prop_all_exchanges_ws_consistency(sandbox in any::<bool>()) {
            // Build all exchanges with the same sandbox setting
            let binance = BinanceBuilder::new().sandbox(sandbox).build().expect("Binance");
            let okx = OkxBuilder::new().sandbox(sandbox).build().expect("OKX");
            let bybit = BybitBuilder::new().sandbox(sandbox).build().expect("Bybit");
            let bitget = BitgetBuilder::new().sandbox(sandbox).build().expect("Bitget");
            let hyperliquid = HyperLiquidBuilder::new().sandbox(sandbox).build().expect("Hyperliquid");

            let binance_urls = binance.urls();
            let okx_urls = okx.urls();
            let bybit_urls = bybit.urls();
            let bitget_urls = bitget.urls();
            let hyperliquid_urls = hyperliquid.urls();

            if sandbox {
                // All testnet WebSocket URLs should contain testnet indicators
                prop_assert!(binance_urls.ws.contains("testnet"), "Binance WS testnet");
                // OKX uses different demo WS endpoint
                prop_assert!(
                    okx_urls.ws_public.contains("wspap") || okx_urls.ws_public.contains("brokerId"),
                    "OKX WS demo"
                );
                prop_assert!(bybit_urls.ws_public.contains("testnet"), "Bybit WS testnet");
                prop_assert!(bitget_urls.ws_public.contains("testnet"), "Bitget WS testnet");
                prop_assert!(hyperliquid_urls.ws.contains("testnet"), "Hyperliquid WS testnet");
            } else {
                // All production WebSocket URLs should NOT contain testnet
                prop_assert!(!binance_urls.ws.contains("testnet"), "Binance WS production");
                prop_assert!(!okx_urls.ws_public.contains("testnet"), "OKX WS production");
                prop_assert!(!bybit_urls.ws_public.contains("testnet"), "Bybit WS production");
                prop_assert!(!bitget_urls.ws_public.contains("testnet"), "Bitget WS production");
                prop_assert!(!hyperliquid_urls.ws.contains("testnet"), "Hyperliquid WS production");
            }
        }
    }
}