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
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
//! # MEXC Parser
//!
//! Parsing MEXC Spot API responses to internal types.
//!
//! ## Response Structure
//!
//! Spot API - Direct response (no wrapper):
//! ```json
//! {
//! "symbol": "BTCUSDT",
//! "price": "93200.50"
//! }
//! ```
//!
//! Or array responses:
//! ```json
//! [{ "symbol": "BTCUSDT", "price": "93200.50" }]
//! ```
//!
//! Error response:
//! ```json
//! {
//! "code": 10001,
//! "msg": "Missing required parameter"
//! }
//! ```
//!
//! ## Key Differences from Bybit
//!
//! - No wrapper: Spot responses are direct data (no `retCode`/`result` wrapper)
//! - Error detection: Check for `code` field to detect errors
//! - Kline order: [time, open, high, low, close, volume, close_time, quote_volume]
//! - All timestamps in milliseconds
//! - All numeric values as strings
use serde_json::Value;
use crate::core::types::*;
use crate::core::types::{ExchangeResult, ExchangeError};
pub struct MexcParser;
impl MexcParser {
// ═══════════════════════════════════════════════════════════════════════════════
// RESPONSE WRAPPER
// ═══════════════════════════════════════════════════════════════════════════════
/// Check if response is an error
///
/// MEXC errors have "code" and "msg" fields
pub fn check_error(json: &Value) -> ExchangeResult<()> {
if let Some(code) = json.get("code").and_then(|c| c.as_i64()) {
if code != 0 && code != 200 {
let msg = json.get("msg")
.and_then(|m| m.as_str())
.unwrap_or("Unknown error");
return Err(ExchangeError::Api {
code: code as i32,
message: msg.to_string(),
});
}
}
Ok(())
}
// ═══════════════════════════════════════════════════════════════════════════════
// MARKET DATA PARSERS (REST)
// ═══════════════════════════════════════════════════════════════════════════════
/// Parse ticker from REST response
///
/// Endpoint: GET /api/v3/ticker/24hr
/// Response: { symbol, lastPrice, bidPrice, askPrice, highPrice, lowPrice, volume, ... }
pub fn parse_ticker(json: &Value) -> ExchangeResult<Ticker> {
Self::check_error(json)?;
let symbol = json["symbol"].as_str()
.ok_or_else(|| ExchangeError::Parse("Missing symbol".into()))?;
let last_price = json["lastPrice"].as_str()
.and_then(|s| s.parse::<f64>().ok())
.ok_or_else(|| ExchangeError::Parse("Invalid lastPrice".into()))?;
let bid_price = json["bidPrice"].as_str()
.and_then(|s| s.parse::<f64>().ok());
let ask_price = json["askPrice"].as_str()
.and_then(|s| s.parse::<f64>().ok());
let high_24h = json["highPrice"].as_str()
.and_then(|s| s.parse::<f64>().ok());
let low_24h = json["lowPrice"].as_str()
.and_then(|s| s.parse::<f64>().ok());
let volume_24h = json["volume"].as_str()
.and_then(|s| s.parse::<f64>().ok());
let quote_volume_24h = json["quoteVolume"].as_str()
.and_then(|s| s.parse::<f64>().ok());
let price_change_24h = json["priceChange"].as_str()
.and_then(|s| s.parse::<f64>().ok());
let price_change_percent_24h = json["priceChangePercent"].as_str()
.and_then(|s| s.parse::<f64>().ok());
let timestamp = json["closeTime"].as_i64()
.or_else(|| json["openTime"].as_i64())
.unwrap_or(0);
Ok(Ticker {
symbol: symbol.to_string(),
last_price,
bid_price,
ask_price,
high_24h,
low_24h,
volume_24h,
quote_volume_24h,
price_change_24h,
price_change_percent_24h,
timestamp,
})
}
/// Parse orderbook from REST response (Spot)
///
/// Endpoint: GET /api/v3/depth
/// Response: { lastUpdateId, bids: [[price, qty]], asks: [[price, qty]] }
pub fn parse_orderbook(json: &Value) -> ExchangeResult<OrderBook> {
Self::check_error(json)?;
let bids = json["bids"].as_array()
.ok_or_else(|| ExchangeError::Parse("Missing bids".into()))?
.iter()
.filter_map(|entry| {
let arr = entry.as_array()?;
let price = arr.first()?.as_str()?.parse::<f64>().ok()?;
let size = arr.get(1)?.as_str()?.parse::<f64>().ok()?;
Some(OrderBookLevel::new(price, size))
})
.collect();
let asks = json["asks"].as_array()
.ok_or_else(|| ExchangeError::Parse("Missing asks".into()))?
.iter()
.filter_map(|entry| {
let arr = entry.as_array()?;
let price = arr.first()?.as_str()?.parse::<f64>().ok()?;
let size = arr.get(1)?.as_str()?.parse::<f64>().ok()?;
Some(OrderBookLevel::new(price, size))
})
.collect();
let timestamp = crate::core::timestamp_millis() as i64;
let sequence = json["lastUpdateId"].as_i64().map(|u| u.to_string());
Ok(OrderBook {
bids,
asks,
timestamp,
sequence,
last_update_id: None,
first_update_id: None,
prev_update_id: None,
event_time: None,
transaction_time: None,
checksum: None,
})
}
/// Parse orderbook from futures REST response
///
/// Endpoint: GET /api/v1/contract/depth/{symbol}
/// Response: { "success": true, "code": 0, "data": { "asks": [[price, count, qty]], "bids": [[price, count, qty]], "version": 123, "timestamp": 123 } }
///
/// Futures format: [price, order_count, quantity]
pub fn parse_orderbook_futures(json: &Value) -> ExchangeResult<OrderBook> {
let bids = json["bids"].as_array()
.ok_or_else(|| ExchangeError::Parse("Missing bids in futures orderbook".into()))?
.iter()
.filter_map(|entry| {
let arr = entry.as_array()?;
let price = arr.first()?.as_f64()?;
// arr[1] is order count, arr[2] is quantity
let size = arr.get(2)?.as_f64()?;
Some(OrderBookLevel::new(price, size))
})
.collect();
let asks = json["asks"].as_array()
.ok_or_else(|| ExchangeError::Parse("Missing asks in futures orderbook".into()))?
.iter()
.filter_map(|entry| {
let arr = entry.as_array()?;
let price = arr.first()?.as_f64()?;
// arr[1] is order count, arr[2] is quantity
let size = arr.get(2)?.as_f64()?;
Some(OrderBookLevel::new(price, size))
})
.collect();
let timestamp = json.get("timestamp")
.and_then(|t| t.as_i64())
.unwrap_or_else(|| crate::core::timestamp_millis() as i64);
let sequence = json.get("version")
.and_then(|v| v.as_i64())
.map(|v| v.to_string());
Ok(OrderBook {
bids,
asks,
timestamp,
sequence,
last_update_id: None,
first_update_id: None,
prev_update_id: None,
event_time: None,
transaction_time: None,
checksum: None,
})
}
/// Parse klines from REST response (Spot)
///
/// Endpoint: GET /api/v3/klines
/// Response: [[time, open, high, low, close, volume, close_time, quote_volume], ...]
///
/// Array order: [open_time, open, high, low, close, volume, close_time, quote_volume]
pub fn parse_klines(json: &Value) -> ExchangeResult<Vec<Kline>> {
Self::check_error(json)?;
let list = json.as_array()
.ok_or_else(|| ExchangeError::Parse("Expected klines array".into()))?;
let klines = list.iter()
.filter_map(|entry| {
let arr = entry.as_array()?;
// MEXC order: [open_time, open, high, low, close, volume, close_time, quote_volume]
let open_time = arr.first()?.as_i64()?;
let open = arr.get(1)?.as_str()?.parse::<f64>().ok()?;
let high = arr.get(2)?.as_str()?.parse::<f64>().ok()?;
let low = arr.get(3)?.as_str()?.parse::<f64>().ok()?;
let close = arr.get(4)?.as_str()?.parse::<f64>().ok()?;
let volume = arr.get(5)?.as_str()?.parse::<f64>().ok()?;
let close_time = arr.get(6).and_then(|v| v.as_i64());
let quote_volume = arr.get(7)
.and_then(|v| v.as_str())
.and_then(|s| s.parse::<f64>().ok());
Some(Kline {
open_time,
open,
high,
low,
close,
volume,
quote_volume,
close_time,
trades: None,
})
})
.collect();
Ok(klines)
}
/// Parse klines from futures REST response
///
/// Endpoint: GET /api/v1/contract/kline/{symbol}
/// Response: { "success": true, "code": 0, "data": { "time": [times], "open": [opens], ... } }
///
/// Futures format: separate arrays for each field
pub fn parse_klines_futures(json: &Value) -> ExchangeResult<Vec<Kline>> {
// Futures klines have separate arrays for each field
let time_arr = json.get("time")
.and_then(|t| t.as_array())
.ok_or_else(|| ExchangeError::Parse("Missing time array".into()))?;
let open_arr = json.get("open")
.and_then(|o| o.as_array())
.ok_or_else(|| ExchangeError::Parse("Missing open array".into()))?;
let high_arr = json.get("high")
.and_then(|h| h.as_array())
.ok_or_else(|| ExchangeError::Parse("Missing high array".into()))?;
let low_arr = json.get("low")
.and_then(|l| l.as_array())
.ok_or_else(|| ExchangeError::Parse("Missing low array".into()))?;
let close_arr = json.get("close")
.and_then(|c| c.as_array())
.ok_or_else(|| ExchangeError::Parse("Missing close array".into()))?;
let vol_arr = json.get("vol")
.and_then(|v| v.as_array())
.ok_or_else(|| ExchangeError::Parse("Missing vol array".into()))?;
// Ensure all arrays have the same length
let len = time_arr.len();
if open_arr.len() != len || high_arr.len() != len ||
low_arr.len() != len || close_arr.len() != len || vol_arr.len() != len {
return Err(ExchangeError::Parse("Inconsistent kline array lengths".into()));
}
let mut klines = Vec::with_capacity(len);
for i in 0..len {
let open_time = time_arr[i].as_i64()
.ok_or_else(|| ExchangeError::Parse(format!("Invalid time at index {}", i)))?
* 1000; // Convert to milliseconds
let open = open_arr[i].as_f64()
.ok_or_else(|| ExchangeError::Parse(format!("Invalid open at index {}", i)))?;
let high = high_arr[i].as_f64()
.ok_or_else(|| ExchangeError::Parse(format!("Invalid high at index {}", i)))?;
let low = low_arr[i].as_f64()
.ok_or_else(|| ExchangeError::Parse(format!("Invalid low at index {}", i)))?;
let close = close_arr[i].as_f64()
.ok_or_else(|| ExchangeError::Parse(format!("Invalid close at index {}", i)))?;
let volume = vol_arr[i].as_f64()
.ok_or_else(|| ExchangeError::Parse(format!("Invalid volume at index {}", i)))?;
klines.push(Kline {
open_time,
open,
high,
low,
close,
volume,
quote_volume: None,
close_time: None,
trades: None,
});
}
Ok(klines)
}
/// Parse futures ticker from REST response
///
/// Endpoint: GET /api/v1/contract/ticker
/// Response: { "success": true, "code": 0, "data": [{ symbol, lastPrice, bid1, ask1, ... }] }
pub fn parse_ticker_futures(json: &Value) -> ExchangeResult<Ticker> {
let symbol = json["symbol"].as_str()
.ok_or_else(|| ExchangeError::Parse("Missing symbol".into()))?;
let last_price = json["lastPrice"].as_f64()
.ok_or_else(|| ExchangeError::Parse("Invalid lastPrice".into()))?;
let bid_price = json["bid1"].as_f64();
let ask_price = json["ask1"].as_f64();
let high_24h = json["high24Price"].as_f64();
let low_24h = json["low24Price"].as_f64();
let volume_24h = json["volume24"].as_f64();
let price_change_24h = json["riseFallRate"].as_f64();
Ok(Ticker {
symbol: symbol.to_string(),
last_price,
bid_price,
ask_price,
high_24h,
low_24h,
volume_24h,
quote_volume_24h: None,
price_change_24h: None,
price_change_percent_24h: price_change_24h,
timestamp: crate::core::timestamp_millis() as i64,
})
}
// ═══════════════════════════════════════════════════════════════════════════════
// EXCHANGE INFO PARSERS
// ═══════════════════════════════════════════════════════════════════════════════
/// Parse exchange info (symbol list) from MEXC response
///
/// Endpoint: GET /api/v3/exchangeInfo
/// Response: { symbols: [{ symbol, baseAsset, quoteAsset, status, baseAssetPrecision,
/// quoteAssetPrecision, baseSizePrecision, quoteAmountPrecision, ... }] }
///
/// MEXC uses "1" for active status (not "TRADING" like Binance).
/// Precision values come directly on the symbol object, not in a filters array.
pub fn parse_exchange_info(json: &Value, account_type: AccountType) -> ExchangeResult<Vec<crate::core::types::SymbolInfo>> {
Self::check_error(json)?;
let symbols_arr = json["symbols"].as_array()
.ok_or_else(|| ExchangeError::Parse("Missing symbols array".into()))?;
let symbols = symbols_arr.iter()
.filter_map(|item| {
let symbol = item["symbol"].as_str()?.to_string();
let base_asset = item["baseAsset"].as_str().unwrap_or("").to_string();
let quote_asset = item["quoteAsset"].as_str().unwrap_or("").to_string();
// MEXC uses "1" (string) for active status, not "TRADING".
// Accept both formats for forward-compatibility.
let status_raw = item["status"].as_str()
.or_else(|| item["status"].as_i64().map(|_| ""))
.unwrap_or("");
// status "1" = active trading on MEXC
let is_active = status_raw == "1"
|| status_raw == "TRADING"
|| status_raw == "ENABLED"
|| item["status"].as_i64() == Some(1);
if !is_active {
return None;
}
// Normalize status to a human-readable string
let status = if status_raw == "1" || item["status"].as_i64() == Some(1) {
"TRADING".to_string()
} else {
status_raw.to_string()
};
// MEXC provides precision directly on the symbol object.
// baseAssetPrecision / quoteAssetPrecision are integer decimal places.
let price_precision = item["quoteAssetPrecision"].as_u64()
.or_else(|| item["quotePrecision"].as_u64())
.unwrap_or(8) as u8;
let quantity_precision = item["baseAssetPrecision"].as_u64()
.unwrap_or(8) as u8;
// baseSizePrecision is a string like "0.01" giving the minimum quantity step.
let step_size = item["baseSizePrecision"].as_str()
.and_then(|s| s.parse::<f64>().ok());
// quoteAmountPrecision is the minimum notional in quote currency (string).
let min_notional = item["quoteAmountPrecision"].as_str()
.and_then(|s| s.parse::<f64>().ok());
// min_quantity equals the step size for MEXC (no separate minQty field).
let min_quantity = step_size;
// MEXC has maxQuoteAmount (in quote currency) but no direct maxQty field.
let max_quantity: Option<f64> = None;
// MEXC Spot exchangeInfo has no explicit tickSize field.
// Derive tick_size from quoteAssetPrecision: 10^(-precision).
// Also check pricePrecision (integer) which some MEXC futures endpoints use.
let tick_size = item["quoteAssetPrecision"].as_u64()
.or_else(|| item["quotePrecision"].as_u64())
.or_else(|| item["pricePrecision"].as_u64())
.map(|p| 10f64.powi(-(p as i32)));
Some(crate::core::types::SymbolInfo {
symbol,
base_asset,
quote_asset,
status,
price_precision,
quantity_precision,
min_quantity,
max_quantity,
tick_size,
step_size,
min_notional,
account_type,
})
})
.collect();
Ok(symbols)
}
// ═══════════════════════════════════════════════════════════════════════════════
// ACCOUNT PARSERS (REST)
// ═══════════════════════════════════════════════════════════════════════════════
/// Parse balance from REST response
///
/// Endpoint: GET /api/v3/account
/// Response: { balances: [{ asset, free, locked }] }
pub fn parse_balance(json: &Value) -> ExchangeResult<Vec<Balance>> {
Self::check_error(json)?;
let balances_arr = json["balances"].as_array()
.ok_or_else(|| ExchangeError::Parse("Missing balances array".into()))?;
let balances = balances_arr.iter()
.filter_map(|balance_data| {
let asset = balance_data["asset"].as_str()?.to_string();
let free = balance_data["free"].as_str()?.parse::<f64>().ok()?;
let locked = balance_data["locked"].as_str()?.parse::<f64>().ok().unwrap_or(0.0);
let total = free + locked;
// Skip zero balances
if total == 0.0 {
return None;
}
Some(Balance {
asset,
free,
locked,
total,
})
})
.collect();
Ok(balances)
}
// ═══════════════════════════════════════════════════════════════════════════════
// TRADING PARSERS (REST)
// ═══════════════════════════════════════════════════════════════════════════════
/// Parse order from REST response
///
/// Endpoint: POST /api/v3/order OR GET /api/v3/order
/// Response: { orderId, symbol, side, type, price, origQty, executedQty, status, ... }
pub fn parse_order(json: &Value) -> ExchangeResult<Order> {
Self::check_error(json)?;
let id = json["orderId"].as_str()
.ok_or_else(|| ExchangeError::Parse("Missing orderId".into()))?
.to_string();
let symbol = json["symbol"].as_str()
.ok_or_else(|| ExchangeError::Parse("Missing symbol".into()))?
.to_string();
let side = match json["side"].as_str() {
Some("BUY") => OrderSide::Buy,
Some("SELL") => OrderSide::Sell,
_ => return Err(ExchangeError::Parse("Invalid side".into())),
};
let order_type = match json["type"].as_str() {
Some("MARKET") => OrderType::Market,
Some("LIMIT") | Some("LIMIT_MAKER") => OrderType::Limit { price: 0.0 },
_ => OrderType::Limit { price: 0.0 }, // default
};
let status = Self::parse_order_status(json["status"].as_str().unwrap_or(""));
let price = json["price"].as_str()
.and_then(|s| s.parse::<f64>().ok());
let quantity = json["origQty"].as_str()
.and_then(|s| s.parse::<f64>().ok())
.unwrap_or(0.0);
let filled_quantity = json["executedQty"].as_str()
.and_then(|s| s.parse::<f64>().ok())
.unwrap_or(0.0);
let average_price = if filled_quantity > 0.0 {
json["cummulativeQuoteQty"].as_str()
.and_then(|s| s.parse::<f64>().ok())
.map(|quote_qty| quote_qty / filled_quantity)
} else {
None
};
let created_at = json["time"].as_i64()
.or_else(|| json["transactTime"].as_i64())
.unwrap_or(0);
let updated_at = json["updateTime"].as_i64();
Ok(Order {
id,
client_order_id: json["clientOrderId"].as_str().map(String::from),
symbol,
side,
order_type,
status,
price,
stop_price: json["stopPrice"].as_str().and_then(|s| s.parse::<f64>().ok()),
quantity,
filled_quantity,
average_price,
time_in_force: Self::parse_time_in_force(json["timeInForce"].as_str().unwrap_or("GTC")),
commission: None,
commission_asset: None,
created_at,
updated_at,
})
}
/// Parse order status from string
fn parse_order_status(status: &str) -> OrderStatus {
match status {
"NEW" => OrderStatus::New,
"PARTIALLY_FILLED" => OrderStatus::PartiallyFilled,
"FILLED" => OrderStatus::Filled,
"CANCELED" => OrderStatus::Canceled,
"REJECTED" => OrderStatus::Rejected,
"EXPIRED" => OrderStatus::Expired,
_ => OrderStatus::New,
}
}
/// Parse time in force from string
fn parse_time_in_force(tif: &str) -> TimeInForce {
match tif {
"GTC" => TimeInForce::Gtc,
"IOC" => TimeInForce::Ioc,
"FOK" => TimeInForce::Fok,
_ => TimeInForce::Gtc,
}
}
/// Parse multiple orders from array response
///
/// Used for GET /api/v3/openOrders, GET /api/v3/allOrders
pub fn parse_orders(json: &Value) -> ExchangeResult<Vec<Order>> {
Self::check_error(json)?;
let list = json.as_array()
.ok_or_else(|| ExchangeError::Parse("Expected orders array".into()))?;
let orders: Vec<Order> = list.iter()
.filter_map(|order_json| Self::parse_order(order_json).ok())
.collect();
Ok(orders)
}
// ═══════════════════════════════════════════════════════════════════════════════
// WEBSOCKET PARSERS
// ═══════════════════════════════════════════════════════════════════════════════
/// Parse WebSocket message
///
/// MEXC WebSocket format:
/// { "channel": "spot@public...", "symbol": "BTCUSDT", "sendtime": 123, "publicdeals": {...} }
pub fn parse_ws_message(json: &Value) -> ExchangeResult<(String, &Value)> {
// Check for ping/pong
if let Some(method) = json.get("method").and_then(|m| m.as_str()) {
return Ok((method.to_string(), json));
}
// Check for error/subscription response (has 'msg' but no 'channel')
if json.get("channel").is_none() {
if let Some(msg) = json.get("msg").and_then(|m| m.as_str()) {
return Err(ExchangeError::Parse(format!("Subscription error: {}", msg)));
}
return Err(ExchangeError::Parse("Missing channel field".into()));
}
// Regular data message
let channel = json.get("channel")
.and_then(|c| c.as_str())
.expect("Safe: already checked above");
// Data is in stream-specific fields: publicdeals, publicspotkline, etc.
// For now, just pass the whole message
Ok((channel.to_string(), json))
}
/// Parse WebSocket ticker update (from book ticker stream)
pub fn parse_ws_ticker(data: &Value) -> ExchangeResult<Ticker> {
let symbol = data.get("symbol")
.and_then(|s| s.as_str())
.ok_or_else(|| ExchangeError::Parse("Missing symbol".into()))?;
let timestamp = data.get("sendtime")
.and_then(|t| t.as_i64())
.unwrap_or(0);
// Check for book ticker data
if let Some(book_ticker) = data.get("publicbookticker") {
let bid_price = book_ticker.get("bidprice")
.and_then(|p| p.as_str())
.and_then(|s| s.parse::<f64>().ok());
let ask_price = book_ticker.get("askprice")
.and_then(|p| p.as_str())
.and_then(|s| s.parse::<f64>().ok());
// Use mid-price as last price
let last_price = match (bid_price, ask_price) {
(Some(bid), Some(ask)) => (bid + ask) / 2.0,
(Some(bid), None) => bid,
(None, Some(ask)) => ask,
_ => return Err(ExchangeError::Parse("Missing bid/ask prices".into())),
};
return Ok(Ticker {
symbol: symbol.to_string(),
last_price,
bid_price,
ask_price,
high_24h: None,
low_24h: None,
volume_24h: None,
quote_volume_24h: None,
price_change_24h: None,
price_change_percent_24h: None,
timestamp,
});
}
// Check for trade data
if let Some(deals) = data.get("publicdeals") {
if let Some(deals_list) = deals.get("dealsList").and_then(|d| d.as_array()) {
if let Some(last_deal) = deals_list.first() {
let last_price = last_deal.get("price")
.and_then(|p| p.as_str())
.and_then(|s| s.parse::<f64>().ok())
.ok_or_else(|| ExchangeError::Parse("Invalid price".into()))?;
return Ok(Ticker {
symbol: symbol.to_string(),
last_price,
bid_price: None,
ask_price: None,
high_24h: None,
low_24h: None,
volume_24h: None,
quote_volume_24h: None,
price_change_24h: None,
price_change_percent_24h: None,
timestamp,
});
}
}
}
Err(ExchangeError::Parse("Unknown ticker format".into()))
}
// ═══════════════════════════════════════════════════════════════════════════════
// PROTOBUF DECODERS (WebSocket binary messages)
// ═══════════════════════════════════════════════════════════════════════════════
// MEXC WebSocket uses protobuf encoding since Aug 2025.
// All messages are wrapped in PushDataV3ApiWrapper:
// field 1 (string): channel
// field 3 (string): symbol
// field 6 (varint): sendTime (millis)
// field 309 (bytes): PublicMiniTickerV3Api
// field 314 (bytes): PublicAggreDealsV3Api
// field 306 (bytes): PublicBookTickerV3Api
//
// Instead of pulling in a protobuf crate, we decode manually since
// the schema is small and stable.
/// Decode a varint from protobuf wire format.
/// Returns (value, new_position).
fn decode_varint(data: &[u8], mut pos: usize) -> Option<(u64, usize)> {
let mut result: u64 = 0;
let mut shift = 0;
loop {
if pos >= data.len() { return None; }
let b = data[pos];
pos += 1;
result |= ((b & 0x7f) as u64) << shift;
if b & 0x80 == 0 { break; }
shift += 7;
if shift >= 64 { return None; }
}
Some((result, pos))
}
/// Extract a string field from protobuf bytes by field number.
fn pb_string(data: &[u8], target_field: u32) -> Option<String> {
let mut pos = 0;
while pos < data.len() {
let (tag, new_pos) = Self::decode_varint(data, pos)?;
pos = new_pos;
let field_num = (tag >> 3) as u32;
let wire_type = (tag & 0x07) as u8;
match wire_type {
0 => { // varint - skip
let (_, new_pos) = Self::decode_varint(data, pos)?;
pos = new_pos;
}
2 => { // length-delimited
let (len, new_pos) = Self::decode_varint(data, pos)?;
pos = new_pos;
let end = pos + len as usize;
if end > data.len() { return None; }
if field_num == target_field {
return String::from_utf8(data[pos..end].to_vec()).ok();
}
pos = end;
}
1 => { pos += 8; } // 64-bit fixed
5 => { pos += 4; } // 32-bit fixed
_ => return None,
}
}
None
}
/// Extract a varint field from protobuf bytes by field number.
fn pb_varint(data: &[u8], target_field: u32) -> Option<u64> {
let mut pos = 0;
while pos < data.len() {
let (tag, new_pos) = Self::decode_varint(data, pos)?;
pos = new_pos;
let field_num = (tag >> 3) as u32;
let wire_type = (tag & 0x07) as u8;
match wire_type {
0 => {
let (val, new_pos) = Self::decode_varint(data, pos)?;
pos = new_pos;
if field_num == target_field {
return Some(val);
}
}
2 => {
let (len, new_pos) = Self::decode_varint(data, pos)?;
pos = new_pos;
pos += len as usize;
if pos > data.len() { return None; }
}
1 => { pos += 8; }
5 => { pos += 4; }
_ => return None,
}
}
None
}
/// Extract a bytes (sub-message) field from protobuf by field number.
fn pb_bytes(data: &[u8], target_field: u32) -> Option<&[u8]> {
let mut pos = 0;
while pos < data.len() {
let (tag, new_pos) = Self::decode_varint(data, pos)?;
pos = new_pos;
let field_num = (tag >> 3) as u32;
let wire_type = (tag & 0x07) as u8;
match wire_type {
0 => {
let (_, new_pos) = Self::decode_varint(data, pos)?;
pos = new_pos;
}
2 => {
let (len, new_pos) = Self::decode_varint(data, pos)?;
pos = new_pos;
let end = pos + len as usize;
if end > data.len() { return None; }
if field_num == target_field {
return Some(&data[pos..end]);
}
pos = end;
}
1 => { pos += 8; }
5 => { pos += 4; }
_ => return None,
}
}
None
}
/// Extract ALL repeated bytes fields with a given field number.
fn pb_repeated_bytes(data: &[u8], target_field: u32) -> Vec<&[u8]> {
let mut results = Vec::new();
let mut pos = 0;
while pos < data.len() {
let (tag, new_pos) = match Self::decode_varint(data, pos) {
Some(v) => v,
None => break,
};
pos = new_pos;
let field_num = (tag >> 3) as u32;
let wire_type = (tag & 0x07) as u8;
match wire_type {
0 => {
if let Some((_, new_pos)) = Self::decode_varint(data, pos) {
pos = new_pos;
} else { break; }
}
2 => {
let (len, new_pos) = match Self::decode_varint(data, pos) {
Some(v) => v,
None => break,
};
pos = new_pos;
let end = pos + len as usize;
if end > data.len() { break; }
if field_num == target_field {
results.push(&data[pos..end]);
}
pos = end;
}
1 => { pos += 8; }
5 => { pos += 4; }
_ => break,
}
}
results
}
/// Parse a protobuf binary WebSocket message into a StreamEvent.
///
/// The outer wrapper is PushDataV3ApiWrapper with:
/// field 1: channel, field 3: symbol, field 6: sendTime
/// field 309: MiniTicker, field 314: AggreDeals, field 306: BookTicker
pub fn parse_protobuf_message(data: &[u8]) -> ExchangeResult<(String, StreamEvent)> {
// Extract wrapper fields
let channel = Self::pb_string(data, 1)
.ok_or_else(|| ExchangeError::Parse("Missing channel in protobuf wrapper".into()))?;
let symbol = Self::pb_string(data, 3)
.unwrap_or_default();
let timestamp = Self::pb_varint(data, 6)
.unwrap_or(0) as i64;
// Dispatch based on channel content
if channel.contains("miniTicker") {
// field 309: PublicMiniTickerV3Api
let body = Self::pb_bytes(data, 309)
.ok_or_else(|| ExchangeError::Parse("Missing miniTicker body (field 309)".into()))?;
let ticker = Self::parse_pb_mini_ticker(body, &symbol, timestamp)?;
Ok((channel, StreamEvent::Ticker(ticker)))
} else if channel.contains("aggre.deals") || channel.contains("public.deals") {
// field 314: PublicAggreDealsV3Api (aggre deals)
// field 305: PublicDealsV3Api (non-aggre deals)
let body = Self::pb_bytes(data, 314)
.or_else(|| Self::pb_bytes(data, 305))
.ok_or_else(|| ExchangeError::Parse("Missing deals body".into()))?;
let ticker = Self::parse_pb_aggre_deals(body, &symbol, timestamp)?;
Ok((channel, StreamEvent::Ticker(ticker)))
} else if channel.contains("bookTicker") {
// field 306: PublicBookTickerV3Api
let body = Self::pb_bytes(data, 306)
.ok_or_else(|| ExchangeError::Parse("Missing bookTicker body (field 306)".into()))?;
let ticker = Self::parse_pb_book_ticker(body, &symbol, timestamp)?;
Ok((channel, StreamEvent::Ticker(ticker)))
} else if channel.contains("aggre.depth") || channel.contains("public.depth") {
// field 313: PublicAggreDepthV3Api (aggregated incremental depth updates)
// Try field 313 first (aggre depth), fall back to nearby fields
let body = Self::pb_bytes(data, 313)
.or_else(|| Self::pb_bytes(data, 310))
.or_else(|| Self::pb_bytes(data, 315))
.ok_or_else(|| ExchangeError::Parse("Missing aggre.depth body (field 313)".into()))?;
let orderbook = Self::parse_pb_aggre_depth(body, &symbol, timestamp)?;
let delta = crate::core::types::OrderbookDelta {
bids: orderbook.bids,
asks: orderbook.asks,
timestamp: orderbook.timestamp,
first_update_id: orderbook.first_update_id,
last_update_id: orderbook.last_update_id,
prev_update_id: orderbook.prev_update_id,
event_time: orderbook.event_time,
checksum: orderbook.checksum,
};
Ok((channel, StreamEvent::OrderbookDelta(delta)))
} else {
Err(ExchangeError::Parse(format!("Unsupported protobuf channel: {}", channel)))
}
}
/// Parse PublicMiniTickerV3Api protobuf body.
///
/// Fields: 1=symbol, 2=lastPrice, 3=priceChange%, 4=?, 5=high, 6=low,
/// 7=volume, 8=quoteVolume, 9-12=alternate timezone variants
fn parse_pb_mini_ticker(body: &[u8], symbol: &str, timestamp: i64) -> ExchangeResult<Ticker> {
let last_price_str = Self::pb_string(body, 2)
.ok_or_else(|| ExchangeError::Parse("Missing lastPrice in miniTicker".into()))?;
let last_price: f64 = last_price_str.parse()
.map_err(|_| ExchangeError::Parse(format!("Invalid lastPrice: {}", last_price_str)))?;
let price_change_pct = Self::pb_string(body, 3)
.and_then(|s| s.parse::<f64>().ok());
let high_24h = Self::pb_string(body, 5)
.and_then(|s| s.parse::<f64>().ok());
let low_24h = Self::pb_string(body, 6)
.and_then(|s| s.parse::<f64>().ok());
let volume_24h = Self::pb_string(body, 7)
.and_then(|s| s.parse::<f64>().ok());
let quote_volume_24h = Self::pb_string(body, 8)
.and_then(|s| s.parse::<f64>().ok());
// Use symbol from body if available, otherwise use wrapper symbol
let sym = Self::pb_string(body, 1)
.unwrap_or_else(|| symbol.to_string());
Ok(Ticker {
symbol: sym,
last_price,
bid_price: None,
ask_price: None,
high_24h,
low_24h,
volume_24h,
quote_volume_24h,
price_change_24h: None,
price_change_percent_24h: price_change_pct,
timestamp,
})
}
/// Parse PublicAggreDealsV3Api protobuf body.
///
/// Fields: 1 (repeated)=deal items, 2=eventType
/// Each deal item: 1=price, 2=quantity, 3=tradeType(1=buy,2=sell), 4=time(ms)
fn parse_pb_aggre_deals(body: &[u8], symbol: &str, timestamp: i64) -> ExchangeResult<Ticker> {
let deals = Self::pb_repeated_bytes(body, 1);
if deals.is_empty() {
return Err(ExchangeError::Parse("No deals in aggre deals message".into()));
}
// Use the last (most recent) deal as the price
let last_deal = deals.last().unwrap();
let price_str = Self::pb_string(last_deal, 1)
.ok_or_else(|| ExchangeError::Parse("Missing price in deal".into()))?;
let last_price: f64 = price_str.parse()
.map_err(|_| ExchangeError::Parse(format!("Invalid deal price: {}", price_str)))?;
let deal_time = Self::pb_varint(last_deal, 4)
.unwrap_or(timestamp as u64) as i64;
Ok(Ticker {
symbol: symbol.to_string(),
last_price,
bid_price: None,
ask_price: None,
high_24h: None,
low_24h: None,
volume_24h: None,
quote_volume_24h: None,
price_change_24h: None,
price_change_percent_24h: None,
timestamp: deal_time,
})
}
/// Parse PublicBookTickerV3Api protobuf body.
///
/// Fields: 1=bidPrice, 2=bidQuantity, 3=askPrice, 4=askQuantity
fn parse_pb_book_ticker(body: &[u8], symbol: &str, timestamp: i64) -> ExchangeResult<Ticker> {
let bid_price = Self::pb_string(body, 1)
.and_then(|s| s.parse::<f64>().ok());
let ask_price = Self::pb_string(body, 3)
.and_then(|s| s.parse::<f64>().ok());
let last_price = match (bid_price, ask_price) {
(Some(b), Some(a)) => (b + a) / 2.0,
(Some(b), None) => b,
(None, Some(a)) => a,
_ => return Err(ExchangeError::Parse("Missing bid/ask in bookTicker".into())),
};
Ok(Ticker {
symbol: symbol.to_string(),
last_price,
bid_price,
ask_price,
high_24h: None,
low_24h: None,
volume_24h: None,
quote_volume_24h: None,
price_change_24h: None,
price_change_percent_24h: None,
timestamp,
})
}
/// Parse PublicAggreDepthV3Api protobuf body into an `OrderBook`.
///
/// MEXC aggre.depth sends incremental delta snapshots via protobuf.
/// The body layout (based on MEXC's websocket-proto schema) is:
/// field 1 (repeated bytes): bid price levels — each sub-message:
/// field 1 (string): price, field 2 (string): quantity
/// field 2 (repeated bytes): ask price levels — same sub-message layout
/// field 3 (varint): sequence / update id
///
/// Zero-quantity levels indicate removals (standard L2 delta convention).
fn parse_pb_aggre_depth(body: &[u8], symbol: &str, timestamp: i64) -> ExchangeResult<OrderBook> {
let _ = symbol; // symbol comes from the outer wrapper; not repeated in body
let bid_entries = Self::pb_repeated_bytes(body, 1);
let ask_entries = Self::pb_repeated_bytes(body, 2);
let seq = Self::pb_varint(body, 3);
let parse_levels = |entries: Vec<&[u8]>| -> Vec<OrderBookLevel> {
entries.iter().filter_map(|entry| {
let price_str = Self::pb_string(entry, 1)?;
let qty_str = Self::pb_string(entry, 2)?;
let price: f64 = price_str.parse().ok()?;
let qty: f64 = qty_str.parse().ok()?;
Some(OrderBookLevel::new(price, qty))
}).collect()
};
let bids = parse_levels(bid_entries);
let asks = parse_levels(ask_entries);
Ok(OrderBook {
bids,
asks,
timestamp,
sequence: seq.map(|s| s.to_string()),
last_update_id: seq,
first_update_id: None,
prev_update_id: None,
event_time: Some(timestamp),
transaction_time: None,
checksum: None,
})
}
// ═══════════════════════════════════════════════════════════════════════════
// USER TRADES (FILLS)
// ═══════════════════════════════════════════════════════════════════════════
/// Parse user trade fills from MEXC `/api/v3/myTrades`.
///
/// Response is a direct JSON array (no wrapper):
/// ```json
/// [{"id":"123","orderId":"456","symbol":"BTCUSDT","side":"BUY","price":"50000",
/// "qty":"0.001","commission":"0.01","commissionAsset":"USDT","isMaker":false,"time":1672531200000}]
/// ```
pub fn parse_user_trades(response: &Value) -> ExchangeResult<Vec<UserTrade>> {
Self::check_error(response)?;
let arr = response.as_array()
.ok_or_else(|| ExchangeError::Parse("Expected array of user trades from MEXC".to_string()))?;
arr.iter()
.map(|item| {
let id = item.get("id")
.and_then(|v| v.as_str().map(|s| s.to_string())
.or_else(|| v.as_i64().map(|n| n.to_string())))
.ok_or_else(|| ExchangeError::Parse("Missing 'id' in trade".to_string()))?;
let order_id = item.get("orderId")
.and_then(|v| v.as_str().map(|s| s.to_string())
.or_else(|| v.as_i64().map(|n| n.to_string())))
.unwrap_or_default();
let symbol = item.get("symbol")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let side = match item.get("side")
.and_then(|v| v.as_str())
.unwrap_or("BUY")
.to_uppercase()
.as_str()
{
"SELL" => OrderSide::Sell,
_ => OrderSide::Buy,
};
let price = item.get("price")
.and_then(|v| v.as_str().and_then(|s| s.parse::<f64>().ok())
.or_else(|| v.as_f64()))
.ok_or_else(|| ExchangeError::Parse("Missing 'price' in trade".to_string()))?;
let quantity = item.get("qty")
.and_then(|v| v.as_str().and_then(|s| s.parse::<f64>().ok())
.or_else(|| v.as_f64()))
.ok_or_else(|| ExchangeError::Parse("Missing 'qty' in trade".to_string()))?;
let commission = item.get("commission")
.and_then(|v| v.as_str().and_then(|s| s.parse::<f64>().ok())
.or_else(|| v.as_f64()))
.unwrap_or(0.0)
.abs();
let commission_asset = item.get("commissionAsset")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let is_maker = item.get("isMaker")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let timestamp = item.get("time")
.and_then(|v| v.as_i64())
.unwrap_or(0);
Ok(UserTrade {
id,
order_id,
symbol,
side,
price,
quantity,
commission,
commission_asset,
is_maker,
timestamp,
})
})
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_parse_ticker() {
let json = json!({
"symbol": "BTCUSDT",
"lastPrice": "93200.50",
"bidPrice": "93200.00",
"askPrice": "93210.00",
"highPrice": "93500.00",
"lowPrice": "91800.00",
"volume": "12345.67",
"quoteVolume": "1147893256.23",
"priceChange": "1200.50",
"priceChangePercent": "1.3",
"openTime": 1640080800000_i64,
"closeTime": 1640167200000_i64
});
let ticker = MexcParser::parse_ticker(&json).unwrap();
assert_eq!(ticker.symbol, "BTCUSDT");
assert_eq!(ticker.last_price, 93200.50);
assert_eq!(ticker.bid_price, Some(93200.00));
assert_eq!(ticker.ask_price, Some(93210.00));
}
#[test]
fn test_parse_orderbook() {
let json = json!({
"lastUpdateId": 123456789_i64,
"bids": [
["93220.00", "0.5"],
["93210.00", "1.2"]
],
"asks": [
["93230.00", "0.8"],
["93240.00", "2.1"]
]
});
let orderbook = MexcParser::parse_orderbook(&json).unwrap();
assert_eq!(orderbook.bids.len(), 2);
assert_eq!(orderbook.asks.len(), 2);
assert_eq!(orderbook.bids[0].price, 93220.00);
assert_eq!(orderbook.bids[0].size, 0.5);
}
#[test]
fn test_parse_error() {
let json = json!({
"code": 10001,
"msg": "Missing required parameter"
});
let result = MexcParser::check_error(&json);
assert!(result.is_err());
match result {
Err(ExchangeError::Api { code, message }) => {
assert_eq!(code, 10001);
assert_eq!(message, "Missing required parameter");
},
_ => panic!("Expected API error"),
}
}
}