digdigdig3 0.3.16

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

use serde_json::Value;

use crate::core::types::{
    ExchangeError, ExchangeResult, AccountType,
    Kline, OrderBook, OrderBookLevel, Ticker, Order, Balance, Position,
    OrderSide, OrderType, OrderStatus, PositionSide,
    FundingRate, PublicTrade, TradeSide, SymbolInfo,
    UserTrade,
    LedgerEntry, LedgerEntryType,
};

/// Parser for Crypto.com API responses
pub struct CryptoComParser;

impl CryptoComParser {
    // ═══════════════════════════════════════════════════════════════════════════
    // HELPERS
    // ═══════════════════════════════════════════════════════════════════════════

    /// Extract result from response
    pub fn extract_result(response: &Value) -> ExchangeResult<&Value> {
        response.get("result")
            .ok_or_else(|| ExchangeError::Parse("Missing 'result' field".to_string()))
    }

    /// Check response code (0 = success)
    pub fn check_response(response: &Value) -> ExchangeResult<()> {
        let code = response.get("code")
            .and_then(|c| c.as_i64())
            .unwrap_or(0);

        if code != 0 {
            let message = response.get("message")
                .and_then(|m| m.as_str())
                .unwrap_or("Unknown error");
            return Err(ExchangeError::Api {
                code: code as i32,
                message: message.to_string(),
            });
        }

        Ok(())
    }

    /// Parse f64 from string or number
    fn parse_f64(value: &Value) -> Option<f64> {
        value.as_str()
            .and_then(|s| s.parse().ok())
            .or_else(|| value.as_f64())
    }

    /// Get f64 from field
    fn get_f64(data: &Value, key: &str) -> Option<f64> {
        data.get(key).and_then(Self::parse_f64)
    }

    /// Get required f64
    fn require_f64(data: &Value, key: &str) -> ExchangeResult<f64> {
        Self::get_f64(data, key)
            .ok_or_else(|| ExchangeError::Parse(format!("Missing or invalid '{}'", key)))
    }

    /// Get string from field
    fn get_str<'a>(data: &'a Value, key: &str) -> Option<&'a str> {
        data.get(key).and_then(|v| v.as_str())
    }

    /// Get required string
    fn require_str<'a>(data: &'a Value, key: &str) -> ExchangeResult<&'a str> {
        Self::get_str(data, key)
            .ok_or_else(|| ExchangeError::Parse(format!("Missing '{}'", key)))
    }

    /// Get i64 from field
    fn get_i64(data: &Value, key: &str) -> Option<i64> {
        data.get(key)
            .and_then(|v| v.as_str().and_then(|s| s.parse().ok()))
            .or_else(|| data.get(key).and_then(|v| v.as_i64()))
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // MARKET DATA
    // ═══════════════════════════════════════════════════════════════════════════

    /// Parse price (ticker response)
    pub fn parse_price(response: &Value) -> ExchangeResult<f64> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        let data = result.get("data")
            .and_then(|d| d.as_array())
            .and_then(|arr| arr.first())
            .ok_or_else(|| ExchangeError::Parse("No ticker data".to_string()))?;

        Self::get_f64(data, "a") // "a" = last price
            .ok_or_else(|| ExchangeError::Parse("Missing last price".to_string()))
    }

    /// Parse klines
    pub fn parse_klines(response: &Value) -> ExchangeResult<Vec<Kline>> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        let data = result.get("data")
            .and_then(|d| d.as_array())
            .ok_or_else(|| ExchangeError::Parse("Expected array of candlesticks".to_string()))?;

        let mut klines = Vec::with_capacity(data.len());

        for candle in data {
            let open_time = Self::get_i64(candle, "t").unwrap_or(0);
            let open = Self::get_f64(candle, "o").unwrap_or(0.0);
            let high = Self::get_f64(candle, "h").unwrap_or(0.0);
            let low = Self::get_f64(candle, "l").unwrap_or(0.0);
            let close = Self::get_f64(candle, "c").unwrap_or(0.0);
            let volume = Self::get_f64(candle, "v").unwrap_or(0.0);

            klines.push(Kline {
                open_time,
                open,
                high,
                low,
                close,
                volume,
                quote_volume: None,
                close_time: None,
                trades: None,
            });
        }

        Ok(klines)
    }

    /// Parse orderbook
    pub fn parse_orderbook(response: &Value) -> ExchangeResult<OrderBook> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        let data = result.get("data")
            .and_then(|d| d.as_array())
            .and_then(|arr| arr.first())
            .ok_or_else(|| ExchangeError::Parse("No orderbook data".to_string()))?;

        let parse_levels = |key: &str| -> Vec<OrderBookLevel> {
            data.get(key)
                .and_then(|v| v.as_array())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|level| {
                            let pair = level.as_array()?;
                            if pair.len() < 2 { return None; }
                            let price = Self::parse_f64(&pair[0])?;
                            let size = Self::parse_f64(&pair[1])?;
                            Some(OrderBookLevel::new(price, size))
                        })
                        .collect()
                })
                .unwrap_or_default()
        };

        let timestamp = Self::get_i64(data, "t").unwrap_or(0);

        Ok(OrderBook {
            timestamp,
            bids: parse_levels("bids"),
            asks: parse_levels("asks"),
            sequence: None,
            last_update_id: None,
            first_update_id: None,
            prev_update_id: None,
            event_time: None,
            transaction_time: None,
            checksum: None,
        })
    }

    /// Parse ticker
    pub fn parse_ticker(response: &Value) -> ExchangeResult<Ticker> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        let data = result.get("data")
            .and_then(|d| d.as_array())
            .and_then(|arr| arr.first())
            .ok_or_else(|| ExchangeError::Parse("No ticker data".to_string()))?;

        Ok(Ticker {
            last_price: Self::get_f64(data, "a").unwrap_or(0.0),
            bid_price: Self::get_f64(data, "b"),
            ask_price: Self::get_f64(data, "k"),
            high_24h: Self::get_f64(data, "h"),
            low_24h: Self::get_f64(data, "l"),
            volume_24h: Self::get_f64(data, "v"),
            quote_volume_24h: Self::get_f64(data, "vv"),
            price_change_24h: None,
            price_change_percent_24h: Self::get_f64(data, "c").map(|r| r * 100.0),
            timestamp: Self::get_i64(data, "t").unwrap_or(0),
        })
    }

    /// Parse funding rate
    pub fn parse_funding_rate(response: &Value) -> ExchangeResult<FundingRate> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        let data = result.get("data")
            .and_then(|d| d.as_array())
            .and_then(|arr| arr.first())
            .ok_or_else(|| ExchangeError::Parse("No valuation data".to_string()))?;

        Ok(FundingRate {
            rate: Self::require_f64(data, "funding_rate")?,
            next_funding_time: Self::get_i64(data, "next_funding_time"),
            timestamp: 0,
        })
    }

    /// Parse insurance fund balance from `public/get-insurance` response.
    ///
    /// Returns `(instrument_type, balance_usd)` from the first data entry.
    pub fn parse_insurance(response: &Value) -> ExchangeResult<(String, f64)> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        let data = result.get("data")
            .and_then(|d| d.as_array())
            .and_then(|arr| arr.first())
            .ok_or_else(|| ExchangeError::Parse("No insurance data".to_string()))?;

        let instrument_type = Self::get_str(data, "instrument_type")
            .unwrap_or("")
            .to_string();
        let balance = Self::require_f64(data, "balance")?;
        Ok((instrument_type, balance))
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // TRADING
    // ═══════════════════════════════════════════════════════════════════════════

    /// Parse order from create order response
    pub fn parse_order_id(response: &Value) -> ExchangeResult<String> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        Self::require_str(result, "order_id").map(String::from)
    }

    /// Parse order details
    pub fn parse_order(response: &Value) -> ExchangeResult<Order> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        Self::parse_order_data(result)
    }

    /// Parse order from data object
    pub fn parse_order_data(data: &Value) -> ExchangeResult<Order> {
        let side = match Self::get_str(data, "side").unwrap_or("BUY") {
            "SELL" => OrderSide::Sell,
            _ => OrderSide::Buy,
        };

        let order_type = match Self::get_str(data, "type").unwrap_or("LIMIT") {
            "MARKET" => OrderType::Market,
            _ => OrderType::Limit { price: 0.0 },
        };

        let status = Self::parse_order_status(data);

        Ok(Order {
            id: Self::get_str(data, "order_id").unwrap_or("").to_string(),
            client_order_id: Self::get_str(data, "client_oid").map(String::from),
            symbol: Self::get_str(data, "instrument_name").map(String::from),
            side,
            order_type,
            status,
            price: Self::get_f64(data, "price"),
            stop_price: Self::get_f64(data, "trigger_price"),
            quantity: Self::get_f64(data, "quantity").unwrap_or(0.0),
            filled_quantity: Self::get_f64(data, "cumulative_quantity").unwrap_or(0.0),
            average_price: Self::get_f64(data, "avg_price"),
            commission: None,
            commission_asset: Self::get_str(data, "fee_currency").map(String::from),
            created_at: Self::get_i64(data, "create_time").unwrap_or(0),
            updated_at: Self::get_i64(data, "update_time"),
            time_in_force: crate::core::TimeInForce::Gtc,
        })
    }

    /// Parse order status
    fn parse_order_status(data: &Value) -> OrderStatus {
        match Self::get_str(data, "status").unwrap_or("ACTIVE") {
            "ACTIVE" => OrderStatus::New,
            "FILLED" => OrderStatus::Filled,
            "CANCELED" => OrderStatus::Canceled,
            "REJECTED" => OrderStatus::Rejected,
            "EXPIRED" => OrderStatus::Expired,
            "PENDING" => OrderStatus::New,
            _ => OrderStatus::New,
        }
    }

    /// Parse list of orders
    pub fn parse_orders(response: &Value) -> ExchangeResult<Vec<Order>> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        let order_list = result.get("order_list")
            .and_then(|v| v.as_array())
            .ok_or_else(|| ExchangeError::Parse("Expected order_list array".to_string()))?;

        order_list.iter()
            .map(Self::parse_order_data)
            .collect()
    }

    /// Parse user trades (fills) from `private/get-trades` response.
    ///
    /// Response format:
    /// ```json
    /// {"result":{"data":[{"trade_id":"123","order_id":"456","instrument_name":"BTC_USDT",
    ///   "side":"BUY","price":"50000","quantity":"0.001","fee":"0.01",
    ///   "fee_currency":"USDT","liquidity_indicator":"MAKER","create_time":1672531200000}]}}
    /// ```
    pub fn parse_user_trades(response: &Value) -> ExchangeResult<Vec<UserTrade>> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;

        let data = result.get("data")
            .and_then(|v| v.as_array())
            .ok_or_else(|| ExchangeError::Parse("Expected 'data' array in get-trades response".to_string()))?;

        let mut trades = Vec::with_capacity(data.len());

        for item in data {
            let side = match Self::get_str(item, "side").unwrap_or("BUY") {
                "SELL" => OrderSide::Sell,
                _ => OrderSide::Buy,
            };

            let is_maker = matches!(
                Self::get_str(item, "liquidity_indicator"),
                Some("MAKER")
            );

            trades.push(UserTrade {
                id: Self::get_str(item, "trade_id").unwrap_or("").to_string(),
                order_id: Self::get_str(item, "order_id").unwrap_or("").to_string(),
                symbol: Self::get_str(item, "instrument_name").unwrap_or("").to_string(),
                side,
                price: Self::get_f64(item, "price").unwrap_or(0.0),
                quantity: Self::get_f64(item, "quantity").unwrap_or(0.0),
                commission: Self::get_f64(item, "fee").unwrap_or(0.0),
                commission_asset: Self::get_str(item, "fee_currency").unwrap_or("").to_string(),
                is_maker,
                timestamp: Self::get_i64(item, "create_time").unwrap_or(0),
            });
        }

        Ok(trades)
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // ACCOUNT
    // ═══════════════════════════════════════════════════════════════════════════

    /// Parse balances from user-balance response
    pub fn parse_balances(response: &Value) -> ExchangeResult<Vec<Balance>> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        let instruments = result.get("instrument_collateral_list")
            .and_then(|v| v.as_array())
            .ok_or_else(|| ExchangeError::Parse("Expected instrument_collateral_list".to_string()))?;

        let mut balances = Vec::new();

        for item in instruments {
            let asset = Self::get_str(item, "instrument_name").unwrap_or("").to_string();
            if asset.is_empty() { continue; }

            let free = Self::get_f64(item, "quantity").unwrap_or(0.0);
            let locked = Self::get_f64(item, "reserved_qty").unwrap_or(0.0);

            balances.push(Balance {
                asset,
                free,
                locked,
                total: free + locked,
            });
        }

        Ok(balances)
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // POSITIONS
    // ═══════════════════════════════════════════════════════════════════════════

    /// Parse positions
    pub fn parse_positions(response: &Value) -> ExchangeResult<Vec<Position>> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        let data = result.get("data")
            .and_then(|d| d.as_array())
            .ok_or_else(|| ExchangeError::Parse("Expected positions array".to_string()))?;

        let mut positions = Vec::new();

        for item in data {
            if let Some(pos) = Self::parse_position_data(item) {
                positions.push(pos);
            }
        }

        Ok(positions)
    }

    /// Parse single position
    fn parse_position_data(data: &Value) -> Option<Position> {
        let symbol = Self::get_str(data, "instrument_name")?.to_string();
        let quantity = Self::get_f64(data, "quantity").unwrap_or(0.0);

        // Skip empty positions
        if quantity.abs() < f64::EPSILON {
            return None;
        }

        let side = if quantity > 0.0 {
            PositionSide::Long
        } else {
            PositionSide::Short
        };

        Some(Position {
            symbol,
            side,
            quantity: quantity.abs(),
            entry_price: Self::get_f64(data, "entry_price").unwrap_or(0.0),
            mark_price: Self::get_f64(data, "mark_price"),
            unrealized_pnl: Self::get_f64(data, "open_position_pnl").unwrap_or(0.0),
            realized_pnl: Self::get_f64(data, "session_pnl"),
            leverage: Self::get_f64(data, "leverage").map(|l| l as u32).unwrap_or(1),
            liquidation_price: None,
            margin: Self::get_f64(data, "initial_margin"),
            margin_type: if Self::get_str(data, "type") == Some("ISOLATED") {
                crate::core::MarginType::Isolated
            } else {
                crate::core::MarginType::Cross
            },
            take_profit: None,
            stop_loss: None,
        })
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // WEBSOCKET PARSING
    // ═══════════════════════════════════════════════════════════════════════════

    /// Parse WebSocket ticker message
    pub fn parse_ws_ticker(data: &Value) -> ExchangeResult<Ticker> {
        Ok(Ticker {
            last_price: Self::get_f64(data, "a").unwrap_or(0.0),
            bid_price: Self::get_f64(data, "b"),
            ask_price: Self::get_f64(data, "k"),
            high_24h: Self::get_f64(data, "h"),
            low_24h: Self::get_f64(data, "l"),
            volume_24h: Self::get_f64(data, "v"),
            quote_volume_24h: Self::get_f64(data, "vv"),
            price_change_24h: None,
            price_change_percent_24h: Self::get_f64(data, "c").map(|r| r * 100.0),
            timestamp: Self::get_i64(data, "t").unwrap_or(0),
        })
    }

    /// Parse WebSocket trade message
    pub fn parse_ws_trade(data: &Value) -> ExchangeResult<PublicTrade> {
        let side = match Self::get_str(data, "s").unwrap_or("BUY") {
            "SELL" => TradeSide::Sell,
            _ => TradeSide::Buy,
        };

        Ok(PublicTrade {
            id: Self::get_str(data, "d").unwrap_or("").to_string(),
            price: Self::require_f64(data, "p")?,
            quantity: Self::get_f64(data, "q").unwrap_or(0.0),
            side,
            timestamp: Self::get_i64(data, "t").unwrap_or(0),
        })
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // EXCHANGE INFO
    // ═══════════════════════════════════════════════════════════════════════════

    /// Parse exchange info from Crypto.com get-instruments response.
    ///
    /// Response format:
    /// ```json
    /// {"code":0,"result":{"data":[{"symbol":"BTC_USDT","inst_type":"CCY_PAIR","display_name":"BTC/USDT","base_ccy":"BTC","quote_ccy":"USDT","quote_decimals":2,"quantity_decimals":4,"price_tick_size":"0.01","qty_tick_size":"0.0001","max_leverage":"50","tradable":true,"expiry_timestamp_ms":0,"put_call":"NONE","strike_price":"0","underlying_symbol":""},...]}}
    /// ```
    /// Parse fee rate from private/get-fee-rate or private/get-instrument-fee-rate response
    pub fn parse_fee_rate(response: &Value) -> ExchangeResult<crate::core::FeeInfo> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;

        // get-fee-rate: result.maker_rate, result.taker_rate (strings, already in decimal form e.g. "0.001")
        // get-instrument-fee-rate: result.maker_rate, result.taker_rate per instrument
        let maker = result.get("maker_rate")
            .and_then(|v| v.as_str().and_then(|s| s.parse::<f64>().ok())

                .or_else(|| v.as_f64()))
            .unwrap_or(0.001); // 0.1% default maker

        let taker = result.get("taker_rate")
            .and_then(|v| v.as_str().and_then(|s| s.parse::<f64>().ok())

                .or_else(|| v.as_f64()))
            .unwrap_or(0.00075); // 0.075% default taker

        let symbol = result.get("instrument_name")
            .and_then(|v| v.as_str())
            .map(String::from);

        Ok(crate::core::FeeInfo {
            maker_rate: maker,
            taker_rate: taker,
            symbol,
            tier: None,
        })
    }

    pub fn parse_exchange_info(response: &Value, account_type: AccountType) -> ExchangeResult<Vec<SymbolInfo>> {
        let result = response.get("result")
            .ok_or_else(|| ExchangeError::Parse("Missing 'result' field".to_string()))?;

        let data = result.get("data")
            .and_then(|d| d.as_array())
            .ok_or_else(|| ExchangeError::Parse("Missing 'data' array in result".to_string()))?;

        let mut symbols = Vec::with_capacity(data.len());

        for item in data {
            // RAW: no tradable filter — return every instrument the exchange lists.
            // Station owns normalization / filtering.

            let symbol = match item.get("symbol").and_then(|v| v.as_str()) {
                Some(s) => s.to_string(),
                None => continue,
            };

            let base_asset = item.get("base_ccy")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();

            let quote_asset = item.get("quote_ccy")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();

            if base_asset.is_empty() || quote_asset.is_empty() {
                continue;
            }

            // RAW: Crypto.com get-instruments has no dedicated status field.
            // The `tradable` bool is the closest proxy; carry it in `extra`.
            // Use empty string rather than faking "TRADING".
            let status = String::new();

            let price_precision = item.get("quote_decimals")
                .and_then(|v| v.as_u64())
                .unwrap_or(2) as u8;

            let quantity_precision = item.get("quantity_decimals")
                .and_then(|v| v.as_u64())
                .unwrap_or(4) as u8;

            let step_size = item.get("qty_tick_size")
                .and_then(|v| v.as_str())
                .and_then(|s| s.parse::<f64>().ok());

            let min_quantity = step_size; // Minimum tradeable is typically 1 step

            let tick_size = item.get("price_tick_size")
                .and_then(|v| v.as_str())
                .and_then(|s| s.parse::<f64>().ok());

            // RAW native instrument type: "CCY_PAIR", "PERPETUAL_SWAP", "FUTURE", etc.
            let instrument_type = item.get("inst_type")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            symbols.push(SymbolInfo {
                symbol,
                base_asset,
                quote_asset,
                status,
                price_precision,
                quantity_precision,
                min_quantity,
                max_quantity: None,
                tick_size,
                step_size,
                min_notional: None,
                account_type,
                instrument_type,
                // RAW passthrough — full native instrument record.
                extra: item.clone(),
            });
        }

        Ok(symbols)
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // ACCOUNT LEDGER
    // ═══════════════════════════════════════════════════════════════════════════

    /// Parse `private/get-transactions` response into ledger entries.
    ///
    /// Response shape:
    /// `{ "code": 0, "result": { "data": [ { "journal_id": "...",
    ///   "journal_type": "TRADING", "instrument_name": "BTC_USDT",
    ///   "event_type": "trade", "amount": "0.001", "fee": "0.01",
    ///   "currency": "BTC", "create_time": 1672531200000 } ] } }`
    pub fn parse_ledger(response: &Value) -> ExchangeResult<Vec<LedgerEntry>> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;

        let data = result
            .get("data")
            .and_then(|d| d.as_array())
            .cloned()
            .unwrap_or_default();

        let mut entries = Vec::with_capacity(data.len());

        for item in &data {
            let id = item
                .get("journal_id")
                .and_then(|v| {
                    v.as_str()
                        .map(String::from)
                        .or_else(|| v.as_i64().map(|n| n.to_string()))
                })
                .unwrap_or_default();

            let asset = item
                .get("currency")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();

            let amount = Self::get_f64(item, "amount").unwrap_or(0.0);
            let fee = Self::get_f64(item, "fee").unwrap_or(0.0);

            let journal_type = item
                .get("journal_type")
                .and_then(|v| v.as_str())
                .unwrap_or("");

            let event_type = item
                .get("event_type")
                .and_then(|v| v.as_str())
                .unwrap_or("");

            let (entry_type, net_amount, description) =
                Self::classify_ledger_entry(journal_type, event_type, amount, fee);

            let timestamp = item
                .get("create_time")
                .and_then(|v| v.as_i64())
                .unwrap_or(0);

            let instrument = item
                .get("instrument_name")
                .and_then(|v| v.as_str())
                .map(String::from);

            let desc = if description.is_empty() {
                instrument
                    .clone()
                    .unwrap_or_else(|| journal_type.to_string())
            } else {
                description
            };

            entries.push(LedgerEntry {
                id,
                asset,
                amount: net_amount,
                balance: None,
                entry_type,
                description: desc,
                ref_id: instrument,
                timestamp,
            });
        }

        Ok(entries)
    }

    /// Map Crypto.com `journal_type` + `event_type` to `LedgerEntryType` and compute net amount.
    ///
    /// Returns `(entry_type, signed_amount, description)`.
    fn classify_ledger_entry(
        journal_type: &str,
        event_type: &str,
        amount: f64,
        fee: f64,
    ) -> (LedgerEntryType, f64, String) {
        match journal_type {
            "TRADING" => {
                let desc = format!("Trade ({})", event_type);
                (LedgerEntryType::Trade, amount, desc)
            }
            "FUNDING" => (
                LedgerEntryType::Funding,
                amount,
                "Funding payment".to_string(),
            ),
            "FEE_AND_REBATE" => {
                if amount >= 0.0 {
                    (LedgerEntryType::Rebate, amount, "Fee rebate".to_string())
                } else {
                    (LedgerEntryType::Fee, amount, "Trading fee".to_string())
                }
            }
            "WITHDRAW" => {
                let net = if amount > 0.0 { -amount } else { amount };
                let net = net - fee.abs();
                (LedgerEntryType::Withdrawal, net, "Withdrawal".to_string())
            }
            "DEPOSIT" => {
                let net = if amount < 0.0 { -amount } else { amount };
                (LedgerEntryType::Deposit, net, "Deposit".to_string())
            }
            "TRANSFER" => (
                LedgerEntryType::Transfer,
                amount,
                "Internal transfer".to_string(),
            ),
            "LIQUIDATION" => (
                LedgerEntryType::Liquidation,
                amount,
                "Liquidation".to_string(),
            ),
            "SETTLEMENT" => (
                LedgerEntryType::Settlement,
                amount,
                "Settlement".to_string(),
            ),
            other => {
                let desc = format!("{} ({})", other, event_type);
                (LedgerEntryType::Other(other.to_string()), amount, desc)
            }
        }
    }
    // ═══════════════════════════════════════════════════════════════════════════
    // VALUATIONS PARSERS (mark price klines, index price klines, funding history)
    // ═══════════════════════════════════════════════════════════════════════════

    /// Map a canonical interval string to milliseconds.
    ///
    /// Supported: `"1m"`, `"5m"`, `"15m"`, `"30m"`, `"1h"`, `"2h"`, `"4h"`,
    /// `"6h"`, `"12h"`, `"1d"`.
    ///
    /// Returns `None` for unrecognised strings (caller should fall back to
    /// raw tick output or return an error).
    pub fn interval_to_ms(interval: &str) -> Option<i64> {
        match interval {
            "1m"  => Some(60_000),
            "5m"  => Some(300_000),
            "15m" => Some(900_000),
            "30m" => Some(1_800_000),
            "1h"  => Some(3_600_000),
            "2h"  => Some(7_200_000),
            "4h"  => Some(14_400_000),
            "6h"  => Some(21_600_000),
            "12h" => Some(43_200_000),
            "1d"  => Some(86_400_000),
            _ => None,
        }
    }

    /// Parse `public/get-valuations` response as a `Vec<Kline>`.
    ///
    /// Used for `valuation_type=mark_price` and `valuation_type=index_price`.
    /// Each data point is `{"v": "<price>", "t": <unix_ms>}` — no OHLC spread,
    /// so open/high/low/close are all set to the single value `v`.
    ///
    /// Response structure:
    /// ```json
    /// {
    ///   "code": 0,
    ///   "result": {
    ///     "data": [{"v": "45000.00", "t": 1700000000000}, ...]
    ///   }
    /// }
    /// ```
    pub fn parse_valuations_as_klines(response: &Value) -> ExchangeResult<Vec<Kline>> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        let data = result.get("data")
            .and_then(|d| d.as_array())
            .ok_or_else(|| ExchangeError::Parse("Crypto.com valuations: missing 'result.data'".into()))?;

        let mut klines = Vec::with_capacity(data.len());
        for item in data {
            let ts = Self::get_i64(item, "t").unwrap_or(0);
            let price = Self::get_f64(item, "v").unwrap_or(0.0);
            klines.push(Kline {
                open_time: ts,
                open:   price,
                high:   price,
                low:    price,
                close:  price,
                volume: 0.0,
                quote_volume: None,
                close_time:   None,
                trades:       None,
            });
        }
        Ok(klines)
    }

    /// Parse `public/get-valuations` response as OHLC klines bucketed to `interval_ms`.
    ///
    /// Crypto.com returns per-minute tick points `{"v": "<price>", "t": <unix_ms>}`.
    /// This function buckets those ticks into proper OHLC candles of the requested
    /// `interval_ms` width so that `open_time % interval_ms == 0` for every output bar.
    ///
    /// Bucketing rule:
    /// - `bar_open = floor(t / interval_ms) * interval_ms`
    /// - `open`  = `v` of the earliest tick in the bucket
    /// - `high`  = max `v` across all ticks in the bucket
    /// - `low`   = min `v` across all ticks in the bucket
    /// - `close` = `v` of the latest tick in the bucket
    /// - `volume` = 0 (valuations carry no volume)
    ///
    /// Output is sorted oldest-first. Empty buckets are omitted.
    pub fn parse_valuations_as_klines_bucketed(response: &Value, interval_ms: i64) -> ExchangeResult<Vec<Kline>> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        let data = result.get("data")
            .and_then(|d| d.as_array())
            .ok_or_else(|| ExchangeError::Parse("Crypto.com valuations: missing 'result.data'".into()))?;

        // Collect (timestamp_ms, price) pairs, skipping malformed entries.
        let ticks: Vec<(i64, f64)> = data.iter()
            .filter_map(|item| {
                let ts = Self::get_i64(item, "t")?;
                let v  = Self::get_f64(item, "v")?;
                Some((ts, v))
            })
            .collect();

        if ticks.is_empty() {
            return Ok(Vec::new());
        }

        // Use a BTreeMap keyed by bar_open so buckets are auto-sorted oldest-first.
        use std::collections::BTreeMap;

        struct BucketState {
            high:  f64,
            low:   f64,
            // Track earliest/latest tick timestamp to resolve open/close correctly
            // when the API does not guarantee ordering within a bucket.
            first_ts: i64,
            last_ts:  i64,
            first_v:  f64,
            last_v:   f64,
        }

        let mut buckets: BTreeMap<i64, BucketState> = BTreeMap::new();

        for (ts, v) in &ticks {
            let bar_open = (ts / interval_ms) * interval_ms;
            let entry = buckets.entry(bar_open).or_insert(BucketState {
                high:     *v,
                low:      *v,
                first_ts: *ts,
                last_ts:  *ts,
                first_v:  *v,
                last_v:   *v,
            });

            if *v > entry.high { entry.high = *v; }
            if *v < entry.low  { entry.low  = *v; }

            if *ts < entry.first_ts {
                entry.first_ts = *ts;
                entry.first_v  = *v;
            }
            if *ts > entry.last_ts {
                entry.last_ts = *ts;
                entry.last_v  = *v;
            }
        }

        let klines = buckets
            .into_iter()
            .map(|(bar_open, b)| Kline {
                open_time:    bar_open,
                open:         b.first_v,
                high:         b.high,
                low:          b.low,
                close:        b.last_v,
                volume:       0.0,
                quote_volume: None,
                close_time:   None,
                trades:       None,
            })
            .collect();

        Ok(klines)
    }

    /// Parse `public/get-valuations` response as a `Vec<FundingRate>`.
    ///
    /// Used for `valuation_type=funding_hist`.
    /// Each data point is `{"v": "<rate>", "t": <unix_ms>}` where `v` is the
    /// settled funding rate (hourly).
    ///
    /// Response structure mirrors `parse_valuations_as_klines`.
    pub fn parse_valuations_as_funding_rates(response: &Value) -> ExchangeResult<Vec<FundingRate>> {
        Self::check_response(response)?;
        let result = Self::extract_result(response)?;
        let data = result.get("data")
            .and_then(|d| d.as_array())
            .ok_or_else(|| ExchangeError::Parse("Crypto.com funding hist: missing 'result.data'".into()))?;

        let mut rates = Vec::with_capacity(data.len());
        for item in data {
            let ts = Self::get_i64(item, "t").unwrap_or(0);
            let rate = Self::get_f64(item, "v").unwrap_or(0.0);
            rates.push(FundingRate {
                rate,
                next_funding_time: None,
                timestamp: ts,
            });
        }
        Ok(rates)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_check_response_success() {
        let response = json!({
            "code": 0,
            "result": {}
        });
        assert!(CryptoComParser::check_response(&response).is_ok());
    }

    #[test]
    fn test_check_response_error() {
        let response = json!({
            "code": 10003,
            "message": "INVALID_SIGNATURE"
        });
        assert!(CryptoComParser::check_response(&response).is_err());
    }

    #[test]
    fn test_parse_price() {
        let response = json!({
            "code": 0,
            "result": {
                "data": [{
                    "i": "BTCUSD-PERP",
                    "a": "50000.00"
                }]
            }
        });

        let price = CryptoComParser::parse_price(&response).unwrap();
        assert!((price - 50000.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_parse_orderbook() {
        let response = json!({
            "code": 0,
            "result": {
                "data": [{
                    "bids": [["50000.00", "1.5"], ["49999.00", "2.0"]],
                    "asks": [["50001.00", "1.0"], ["50002.00", "0.5"]],
                    "t": 1234567890
                }]
            }
        });

        let orderbook = CryptoComParser::parse_orderbook(&response).unwrap();
        assert_eq!(orderbook.bids.len(), 2);
        assert_eq!(orderbook.asks.len(), 2);
        assert!((orderbook.bids[0].price - 50000.0).abs() < f64::EPSILON);
        assert_eq!(orderbook.timestamp, 1234567890);
    }

    #[test]
    fn test_parse_ticker() {
        let response = json!({
            "code": 0,
            "result": {
                "data": [{
                    "i": "BTCUSD-PERP",
                    "b": "50000.00",
                    "k": "50001.00",
                    "a": "50000.50",
                    "h": "51000.00",
                    "l": "49000.00",
                    "v": "1000.5",
                    "vv": "50000000",
                    "c": "0.02",
                    "t": 1234567890
                }]
            }
        });

        let ticker = CryptoComParser::parse_ticker(&response).unwrap();
        assert!((ticker.last_price - 50000.50).abs() < f64::EPSILON);
        assert_eq!(ticker.timestamp, 1234567890);
    }

    #[test]
    fn test_parse_order_status() {
        let data = json!({"status": "FILLED"});
        assert_eq!(CryptoComParser::parse_order_status(&data), OrderStatus::Filled);

        let data = json!({"status": "ACTIVE"});
        assert_eq!(CryptoComParser::parse_order_status(&data), OrderStatus::New);

        let data = json!({"status": "CANCELED"});
        assert_eq!(CryptoComParser::parse_order_status(&data), OrderStatus::Canceled);
    }
}