ccxt-rust 0.1.5

Cryptocurrency exchange trading library in 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
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
//! Binance期货转账功能集成测试
//!
//! 测试futures_transfer()和fetch_futures_transfers()方法

use ccxt_core::ExchangeConfig;
use ccxt_exchanges::binance::Binance;
use std::collections::HashMap;

/// 测试辅助函数:创建测试用的Binance实例
fn create_test_binance() -> Binance {
    let config = ExchangeConfig {
        api_key: std::env::var("BINANCE_API_KEY").ok(),
        secret: std::env::var("BINANCE_API_SECRET").ok(),
        testnet: true, // 使用测试网
        ..Default::default()
    };
    
    Binance::new(config).expect("创建Binance实例失败")
}

/// 检查是否配置了API密钥
fn has_credentials() -> bool {
    std::env::var("BINANCE_API_KEY").is_ok() && std::env::var("BINANCE_API_SECRET").is_ok()
}

// ============================================================================
// futures_transfer() 方法测试
// ============================================================================

#[tokio::test]
#[ignore] // 需要有效的API密钥才能运行
async fn test_futures_transfer_spot_to_usdtm() {
    if !has_credentials() {
        println!("跳过测试:需要设置 BINANCE_API_KEY 和 BINANCE_API_SECRET");
        return;
    }

    let binance = create_test_binance();
    
    // Type 1: 现货 → USDT-M期货
    let result = binance
        .futures_transfer(
            "USDT",
            10.0,
            1,      // type=1: 现货 → USDT-M期货
            None,
        )
        .await;
    
    match result {
        Ok(transfer) => {
            assert!(transfer.id.is_some(), "转账应该有ID");
            assert_eq!(transfer.currency, "USDT");
            assert_eq!(transfer.amount, 10.0);
            assert_eq!(transfer.from_account.as_deref(), Some("spot"));
            assert_eq!(transfer.to_account.as_deref(), Some("future"));
            assert!(!transfer.status.is_empty());
            println!("✅ 转账成功: {:?}", transfer.id);
        }
        Err(e) => {
            println!("⚠️ 转账失败(预期): {}", e);
        }
    }
}

#[tokio::test]
#[ignore]
async fn test_futures_transfer_usdtm_to_spot() {
    if !has_credentials() {
        return;
    }

    let binance = create_test_binance();
    
    // Type 2: USDT-M期货 → 现货
    let result = binance
        .futures_transfer("USDT", 5.0, 2, None)
        .await;
    
    match result {
        Ok(transfer) => {
            assert_eq!(transfer.currency, "USDT");
            assert_eq!(transfer.amount, 5.0);
            assert_eq!(transfer.from_account.as_deref(), Some("future"));
            assert_eq!(transfer.to_account.as_deref(), Some("spot"));
            println!("✅ 转账成功");
        }
        Err(e) => {
            println!("⚠️ 转账失败: {}", e);
        }
    }
}

#[tokio::test]
#[ignore]
async fn test_futures_transfer_spot_to_coinm() {
    if !has_credentials() {
        return;
    }

    let binance = create_test_binance();
    
    // Type 3: 现货 → COIN-M期货
    let result = binance
        .futures_transfer("BTC", 0.001, 3, None)
        .await;
    
    match result {
        Ok(transfer) => {
            assert_eq!(transfer.currency, "BTC");
            assert_eq!(transfer.amount, 0.001);
            assert_eq!(transfer.from_account.as_deref(), Some("spot"));
            assert_eq!(transfer.to_account.as_deref(), Some("delivery"));
            println!("✅ 转账成功");
        }
        Err(e) => {
            println!("⚠️ 转账失败: {}", e);
        }
    }
}

#[tokio::test]
#[ignore]
async fn test_futures_transfer_coinm_to_spot() {
    if !has_credentials() {
        return;
    }

    let binance = create_test_binance();
    
    // Type 4: COIN-M期货 → 现货
    let result = binance
        .futures_transfer("BTC", 0.001, 4, None)
        .await;
    
    match result {
        Ok(transfer) => {
            assert_eq!(transfer.currency, "BTC");
            assert_eq!(transfer.amount, 0.001);
            assert_eq!(transfer.from_account.as_deref(), Some("delivery"));
            assert_eq!(transfer.to_account.as_deref(), Some("spot"));
            println!("✅ 转账成功");
        }
        Err(e) => {
            println!("⚠️ 转账失败: {}", e);
        }
    }
}

// ============================================================================
// fetch_futures_transfers() 方法测试
// ============================================================================

#[tokio::test]
#[ignore]
async fn test_fetch_futures_transfers_all() {
    if !has_credentials() {
        return;
    }

    let binance = create_test_binance();
    
    let result = binance
        .fetch_futures_transfers("USDT", None, Some(20), None)
        .await;
    
    match result {
        Ok(transfers) => {
            assert!(transfers.len() <= 20, "应该不超过限制数量");
            
            for transfer in &transfers {
                assert_eq!(transfer.currency, "USDT");
                assert!(transfer.amount > 0.0);
                assert!(!transfer.status.is_empty());
                assert!(transfer.from_account.is_some());
                assert!(transfer.to_account.is_some());
            }
            
            println!("✅ 查询到 {} 条转账记录", transfers.len());
        }
        Err(e) => {
            println!("⚠️ 查询失败: {}", e);
        }
    }
}

#[tokio::test]
#[ignore]
async fn test_fetch_futures_transfers_with_time_range() {
    if !has_credentials() {
        return;
    }

    let binance = create_test_binance();
    
    // 查询最近7天的转账
    let seven_days_ago = chrono::Utc::now().timestamp_millis() as u64 - 7 * 86400_000;
    
    let result = binance
        .fetch_futures_transfers("USDT", Some(seven_days_ago), Some(50), None)
        .await;
    
    match result {
        Ok(transfers) => {
            for transfer in &transfers {
                assert_eq!(transfer.currency, "USDT");
                assert!(transfer.timestamp >= seven_days_ago);
            }
            println!("✅ 查询到 {} 条最近7天的转账记录", transfers.len());
        }
        Err(e) => {
            println!("⚠️ 查询失败: {}", e);
        }
    }
}

#[tokio::test]
#[ignore]
async fn test_fetch_futures_transfers_with_pagination() {
    if !has_credentials() {
        return;
    }

    let binance = create_test_binance();
    
    // 使用分页参数
    let mut params = HashMap::new();
    params.insert("current".to_string(), "1".to_string());
    params.insert("size".to_string(), "10".to_string());
    
    let result = binance
        .fetch_futures_transfers("USDT", None, None, Some(params))
        .await;
    
    match result {
        Ok(transfers) => {
            assert!(transfers.len() <= 10);
            println!("✅ 分页查询到 {} 条记录", transfers.len());
        }
        Err(e) => {
            println!("⚠️ 查询失败: {}", e);
        }
    }
}

#[tokio::test]
#[ignore]
async fn test_fetch_futures_transfers_btc() {
    if !has_credentials() {
        return;
    }

    let binance = create_test_binance();
    
    let result = binance
        .fetch_futures_transfers("BTC", None, Some(10), None)
        .await;
    
    match result {
        Ok(transfers) => {
            for transfer in &transfers {
                assert_eq!(transfer.currency, "BTC");
            }
            println!("✅ 查询到 {} 条BTC转账记录", transfers.len());
        }
        Err(e) => {
            println!("⚠️ 查询失败: {}", e);
        }
    }
}

// ============================================================================
// 转账类型验证测试
// ============================================================================

#[tokio::test]
#[ignore]
async fn test_futures_transfer_type_validation() {
    if !has_credentials() {
        return;
    }

    let binance = create_test_binance();
    
    // 测试所有有效的转账类型
    let valid_types = vec![1, 2, 3, 4];
    
    for transfer_type in valid_types {
        let asset = if transfer_type <= 2 { "USDT" } else { "BTC" };
        let amount = if transfer_type <= 2 { 1.0 } else { 0.0001 };
        
        let result = binance
            .futures_transfer(asset, amount, transfer_type, None)
            .await;
        
        match result {
            Ok(transfer) => {
                // 验证转账方向
                let (expected_from, expected_to) = match transfer_type {
                    1 => ("spot", "future"),
                    2 => ("future", "spot"),
                    3 => ("spot", "delivery"),
                    4 => ("delivery", "spot"),
                    _ => unreachable!(),
                };
                
                assert_eq!(transfer.from_account.as_deref(), Some(expected_from));
                assert_eq!(transfer.to_account.as_deref(), Some(expected_to));
                println!("✅ Type {} 验证通过", transfer_type);
            }
            Err(e) => {
                println!("⚠️ Type {} 失败: {}", transfer_type, e);
            }
        }
    }
}

// ============================================================================
// 错误处理测试
// ============================================================================

#[tokio::test]
async fn test_futures_transfer_invalid_type() {
    let binance = create_test_binance();
    
    // 测试无效的转账类型(type必须是1-4)
    let result = binance
        .futures_transfer("USDT", 10.0, 5, None)
        .await;
    
    assert!(result.is_err(), "无效的type应该失败");
    
    if let Err(e) = result {
        let error_msg = e.to_string();
        assert!(
            error_msg.contains("type must be") || error_msg.contains("between 1 and 4"),
            "错误信息应该提示type范围"
        );
        println!("✅ 正确处理了无效的type参数");
    }
}

#[tokio::test]
async fn test_futures_transfer_without_credentials() {
    let config = ExchangeConfig {
        api_key: None,
        secret: None,
        ..Default::default()
    };
    
    let binance = Binance::new(config).expect("创建实例失败");
    
    let result = binance
        .futures_transfer("USDT", 1.0, 1, None)
        .await;
    
    assert!(result.is_err(), "没有凭证应该失败");
    println!("✅ 正确处理了缺少凭证的情况");
}

#[tokio::test]
async fn test_fetch_futures_transfers_without_credentials() {
    let config = ExchangeConfig {
        api_key: None,
        secret: None,
        ..Default::default()
    };
    
    let binance = Binance::new(config).expect("创建实例失败");
    
    let result = binance
        .fetch_futures_transfers("USDT", None, Some(10), None)
        .await;
    
    assert!(result.is_err(), "没有凭证应该失败");
    println!("✅ 正确处理了缺少凭证的情况");
}

// ============================================================================
// 数据完整性测试
// ============================================================================

#[tokio::test]
#[ignore]
async fn test_futures_transfer_response_structure() {
    if !has_credentials() {
        return;
    }

    let binance = create_test_binance();
    
    let result = binance
        .futures_transfer("USDT", 1.0, 1, None)
        .await;
    
    match result {
        Ok(transfer) => {
            // 验证Transfer结构的所有必需字段
            assert!(transfer.id.is_some(), "应该有转账ID");
            assert!(!transfer.currency.is_empty(), "币种不能为空");
            assert!(transfer.amount > 0.0, "数量必须大于0");
            assert!(transfer.from_account.is_some(), "应该有源账户");
            assert!(transfer.to_account.is_some(), "应该有目标账户");
            assert!(!transfer.status.is_empty(), "状态不能为空");
            assert!(transfer.timestamp > 0, "时间戳必须有效");
            assert!(!transfer.datetime.is_empty(), "日期时间不能为空");
            
            println!("✅ 响应结构验证通过");
        }
        Err(e) => {
            println!("⚠️ 测试失败: {}", e);
        }
    }
}

#[tokio::test]
#[ignore]
async fn test_fetch_futures_transfers_response_structure() {
    if !has_credentials() {
        return;
    }

    let binance = create_test_binance();
    
    let result = binance
        .fetch_futures_transfers("USDT", None, Some(5), None)
        .await;
    
    match result {
        Ok(transfers) => {
            if !transfers.is_empty() {
                let transfer = &transfers[0];
                
                // 验证每条记录的结构
                assert!(transfer.id.is_some());
                assert!(!transfer.currency.is_empty());
                assert!(transfer.amount > 0.0);
                assert!(transfer.from_account.is_some());
                assert!(transfer.to_account.is_some());
                assert!(!transfer.status.is_empty());
                assert!(transfer.timestamp > 0);
                
                println!("✅ 历史记录结构验证通过");
            } else {
                println!("⚠️ 没有历史记录可验证");
            }
        }
        Err(e) => {
            println!("⚠️ 查询失败: {}", e);
        }
    }
}

// ============================================================================
// 边界条件测试
// ============================================================================

#[tokio::test]
#[ignore]
async fn test_futures_transfer_minimum_amount() {
    if !has_credentials() {
        return;
    }

    let binance = create_test_binance();
    
    // 测试最小转账金额
    let result = binance
        .futures_transfer("USDT", 0.01, 1, None)
        .await;
    
    match result {
        Ok(transfer) => {
            assert_eq!(transfer.amount, 0.01);
            println!("✅ 最小金额转账成功");
        }
        Err(e) => {
            println!("⚠️ 最小金额转账失败: {}", e);
        }
    }
}

#[tokio::test]
#[ignore]
async fn test_fetch_futures_transfers_max_limit() {
    if !has_credentials() {
        return;
    }

    let binance = create_test_binance();
    
    // 测试最大限制(Binance限制是100)
    let result = binance
        .fetch_futures_transfers("USDT", None, Some(100), None)
        .await;
    
    match result {
        Ok(transfers) => {
            assert!(transfers.len() <= 100);
            println!("✅ 最大限制测试通过,返回 {} 条记录", transfers.len());
        }
        Err(e) => {
            println!("⚠️ 查询失败: {}", e);
        }
    }
}