digdigdig3 0.1.30

Unified async Rust API for 44 exchange connectors — crypto, stocks, forex. REST + WebSocket.
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
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
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
//! # Bitfinex Connector
//!
//! Implementation of all core traits for Bitfinex API v2.
//!
//! ## Core Traits
//! - `ExchangeIdentity` - Exchange identification
//! - `MarketData` - Market data endpoints
//! - `Trading` - Trading operations
//! - `Account` - Account information
//! - `Positions` - Margin/futures positions

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use async_trait::async_trait;
use serde_json::{json, Value};

use crate::core::{
    HttpClient, Credentials,
    ExchangeId, ExchangeType, AccountType, Symbol,
    ExchangeError, ExchangeResult,
    Price, Kline, Ticker, OrderBook,
    Order, OrderSide, OrderType, Balance, AccountInfo,
    Position,
    OrderRequest, CancelRequest, CancelScope,
    BalanceQuery, PositionQuery, PositionModification,
    OrderHistoryFilter, PlaceOrderResponse, FeeInfo,
    UserTrade, UserTradeFilter,
};
use crate::core::types::SymbolInfo;
use crate::core::traits::{
    ExchangeIdentity, MarketData, Trading, Account, Positions,
};
use crate::core::types::{MarketDataCapabilities, TradingCapabilities, AccountCapabilities};
use crate::core::{CancelAll, AmendOrder, BatchOrders, AccountTransfers, CustodialFunds, SubAccounts};
use crate::core::traits::{FundingHistory, AccountLedger};
use crate::core::types::{
    ConnectorStats, CancelAllResponse, OrderResult, AmendRequest,
    TransferRequest, TransferHistoryFilter, TransferResponse,
    DepositAddress, WithdrawResponse, FundsRecord,
};
use crate::core::types::{
    WithdrawRequest, FundsHistoryFilter, FundsRecordType,
    SubAccountOperation, SubAccountResult, SubAccount,
};
use crate::core::types::{
    FundingPayment, FundingFilter,
    LedgerEntry, LedgerEntryType, LedgerFilter,
    RateLimitCapabilities, LimitModel, RestLimitPool, WsLimits,
};
use crate::core::utils::{RuntimeLimiter, RateLimitMonitor, RateLimitPressure};
use crate::core::utils::PrecisionCache;

use super::endpoints::{BitfinexUrls, BitfinexEndpoint, format_symbol, build_candle_key};
use super::auth::BitfinexAuth;
use super::parser::BitfinexParser;

// ═══════════════════════════════════════════════════════════════════════════════
// RATE LIMIT CAPABILITIES (static — embedded in binary, no allocation)
// ═══════════════════════════════════════════════════════════════════════════════

static BITFINEX_RATE_POOLS: &[RestLimitPool] = &[RestLimitPool {
    name: "default",
    max_budget: 90,
    window_seconds: 60,
    is_weight: false,
    has_server_headers: false,
    server_header: None,
    header_reports_used: false,
}];

static BITFINEX_RATE_CAPS: RateLimitCapabilities = RateLimitCapabilities {
    model: LimitModel::Simple,
    rest_pools: BITFINEX_RATE_POOLS,
    decaying: None,
    endpoint_weights: &[],
    ws: WsLimits {
        max_connections: None,
        max_subs_per_conn: Some(30),
        max_msg_per_sec: None,
        max_streams_per_conn: None,
    },
};

// ═══════════════════════════════════════════════════════════════════════════════
// CONNECTOR
// ═══════════════════════════════════════════════════════════════════════════════

/// Bitfinex connector
pub struct BitfinexConnector {
    /// HTTP client
    http: HttpClient,
    /// Authentication (None for public methods only)
    auth: Option<BitfinexAuth>,
    /// URLs (mainnet — Bitfinex has no separate testnet URLs)
    urls: BitfinexUrls,
    /// Paper-trading mode flag.
    /// Bitfinex has no dedicated testnet; paper trading uses prefixed symbols
    /// (e.g., tTESTBTC:TESTUSD) on the same mainnet endpoints.
    /// Stored here for future paper trading symbol routing support.
    testnet: bool,
    /// Runtime rate limiter (Simple model: 90 req/60s)
    limiter: Arc<Mutex<RuntimeLimiter>>,
    /// Pressure monitor — logs transitions, gates non-essential requests at >= 90%
    monitor: Arc<Mutex<RateLimitMonitor>>,
    /// Per-symbol precision cache for safe price/qty formatting
    precision: PrecisionCache,
}

impl BitfinexConnector {
    /// Create new connector
    ///
    /// Note: Bitfinex has no separate testnet URLs. When `testnet` is `true`
    /// the connector still connects to the same mainnet endpoints; paper trading
    /// requires using prefixed symbols like `tTESTBTC:TESTUSD`.
    pub async fn new(credentials: Option<Credentials>, testnet: bool) -> ExchangeResult<Self> {
        let urls = BitfinexUrls::MAINNET;
        let http = HttpClient::new(30_000)?; // 30 sec timeout

        let auth = credentials
            .as_ref()
            .map(BitfinexAuth::new)
            .transpose()?;

        let limiter = Arc::new(Mutex::new(RuntimeLimiter::from_caps(&BITFINEX_RATE_CAPS)));
        let monitor = Arc::new(Mutex::new(RateLimitMonitor::new("Bitfinex")));

        Ok(Self {
            http,
            auth,
            urls,
            testnet,
            limiter,
            monitor,
            precision: PrecisionCache::new(),
        })
    }

    /// Create connector for public methods only
    pub async fn public(testnet: bool) -> ExchangeResult<Self> {
        Self::new(None, testnet).await
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // HTTP HELPERS
    // ═══════════════════════════════════════════════════════════════════════════

    /// Wait for rate limit budget. Non-essential requests are dropped at >= 90% utilization.
    ///
    /// Returns `true` if acquired, `false` if dropped due to cutoff pressure.
    /// Trading endpoints should pass `essential: true` to always wait through.
    async fn rate_limit_wait(&self, weight: u32, essential: bool) -> bool {
        loop {
            let wait_time = {
                let mut limiter = self.limiter.lock()
                    .expect("rate limiter mutex poisoned");

                let pressure = self.monitor.lock()
                    .expect("rate monitor mutex poisoned")
                    .check(&mut limiter);
                if pressure >= RateLimitPressure::Cutoff && !essential {
                    return false;
                }

                if limiter.try_acquire("default", weight) {
                    return true;
                }
                limiter.time_until_ready("default", weight)
            };
            if wait_time > Duration::ZERO {
                tokio::time::sleep(wait_time).await;
            }
        }
    }

    /// GET request
    async fn get(
        &self,
        endpoint: BitfinexEndpoint,
        path_params: &[(&str, &str)],
        query_params: HashMap<String, String>,
    ) -> ExchangeResult<Value> {
        if !self.rate_limit_wait(1, false).await {
            return Err(ExchangeError::RateLimitExceeded {
                retry_after: None,
                message: "Rate limit budget >= 90% used; non-essential market data request dropped".to_string(),
            });
        }

        let base_url = self.urls.rest_url(endpoint.requires_auth());
        let mut path = endpoint.path().to_string();

        // Replace path parameters
        for (key, value) in path_params {
            path = path.replace(&format!("{{{}}}", key), value);
        }

        // Build query string
        let query = if query_params.is_empty() {
            String::new()
        } else {
            let qs: Vec<String> = query_params.iter()
                .map(|(k, v)| format!("{}={}", k, v))
                .collect();
            format!("?{}", qs.join("&"))
        };

        let url = format!("{}{}{}", base_url, path, query);

        let response = self.http.get(&url, &HashMap::new()).await?;
        BitfinexParser::check_error(&response)?;
        Ok(response)
    }

    /// POST request (authenticated)
    async fn post(
        &self,
        endpoint: BitfinexEndpoint,
        path_params: &[(&str, &str)],
        body: Value,
    ) -> ExchangeResult<Value> {
        self.rate_limit_wait(1, true).await;

        let base_url = self.urls.rest_url(true); // Always use auth URL for POST
        let mut path = endpoint.path().to_string();

        // Replace path parameters
        for (key, value) in path_params {
            path = path.replace(&format!("{{{}}}", key), value);
        }

        let url = format!("{}{}", base_url, path);

        // Get auth headers
        let auth = self.auth.as_ref()
            .ok_or_else(|| ExchangeError::Auth("Authentication required".to_string()))?;

        // API path without /v2 prefix (auth expects "v2/auth/r/wallets" not "/v2/auth/r/wallets")
        let api_path = path.trim_start_matches('/');
        let body_str = body.to_string();
        let headers = auth.sign_request(api_path, &body_str);

        let response = self.http.post(&url, &body, &headers).await?;
        BitfinexParser::check_error(&response)?;
        Ok(response)
    }

    /// Format symbol helper
    fn fmt_symbol(symbol: &Symbol, account_type: AccountType) -> String {
        if let Some(raw) = symbol.raw() {
            raw.to_string()
        } else {
            format_symbol(&symbol.base, &symbol.quote, account_type)
        }
    }

    /// Determine if account type is derivatives (margin/futures)
    fn is_derivatives(account_type: AccountType) -> bool {
        matches!(account_type, AccountType::Margin | AccountType::FuturesCross | AccountType::FuturesIsolated)
    }

    /// Get order type string prefix ("EXCHANGE " for spot, "" for margin/futures)
    fn order_type_prefix(account_type: AccountType) -> &'static str {
        if Self::is_derivatives(account_type) {
            ""
        } else {
            "EXCHANGE "
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// EXCHANGE IDENTITY
// ═══════════════════════════════════════════════════════════════════════════════

impl ExchangeIdentity for BitfinexConnector {
    fn exchange_id(&self) -> ExchangeId {
        ExchangeId::Bitfinex
    }

    fn metrics(&self) -> ConnectorStats {
        let (http_requests, http_errors, last_latency_ms) = self.http.stats();
        let (rate_used, rate_max) = if let Ok(mut limiter) = self.limiter.lock() {
            limiter.primary_stats()
        } else {
            (0, 0)
        };
        ConnectorStats {
            http_requests,
            http_errors,
            last_latency_ms,
            rate_used,
            rate_max,
            rate_groups: Vec::new(),
            ws_ping_rtt_ms: 0,
        }
    }

    fn is_testnet(&self) -> bool {
        // Bitfinex has no separate testnet URLs; this flag enables paper trading symbol routing
        self.testnet
    }

    fn supported_account_types(&self) -> Vec<AccountType> {
        vec![
            AccountType::Spot,
            AccountType::Margin,
            AccountType::FuturesCross,
        ]
    }

    fn exchange_type(&self) -> ExchangeType {
        ExchangeType::Cex
    }

    fn rate_limit_capabilities(&self) -> RateLimitCapabilities {
        BITFINEX_RATE_CAPS
    }
}

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

#[async_trait]
impl MarketData for BitfinexConnector {
    async fn get_price(
        &self,
        symbol: Symbol,
        account_type: AccountType,
    ) -> ExchangeResult<Price> {
        let formatted_symbol = Self::fmt_symbol(&symbol, account_type);

        let response = self.get(
            BitfinexEndpoint::Ticker,
            &[("symbol", &formatted_symbol)],
            HashMap::new(),
        ).await?;

        let ticker = BitfinexParser::parse_ticker(&response, &formatted_symbol)?;
        Ok(ticker.last_price)
    }

    async fn get_orderbook(
        &self,
        symbol: Symbol,
        _depth: Option<u16>,
        account_type: AccountType,
    ) -> ExchangeResult<OrderBook> {
        let formatted_symbol = Self::fmt_symbol(&symbol, account_type);

        // Use P0 precision (highest aggregation) for best performance
        let response = self.get(
            BitfinexEndpoint::Orderbook,
            &[("symbol", &formatted_symbol), ("precision", "P0")],
            HashMap::new(),
        ).await?;

        BitfinexParser::parse_orderbook(&response)
    }

    async fn get_klines(
        &self,
        symbol: Symbol,
        interval: &str,
        limit: Option<u16>,
        account_type: AccountType,
        end_time: Option<i64>,
    ) -> ExchangeResult<Vec<Kline>> {
        let formatted_symbol = Self::fmt_symbol(&symbol, account_type);
        let candle_key = build_candle_key(&formatted_symbol, interval);

        let mut params = HashMap::new();
        if let Some(lim) = limit {
            params.insert("limit".to_string(), lim.min(10000).to_string());
        }
        // Don't use sort=1 — it returns data from 2013. Default (newest-first) + parser.reverse() is correct.

        if let Some(et) = end_time {
            params.insert("end".to_string(), et.to_string());
        }

        let response = self.get(
            BitfinexEndpoint::Candles,
            &[("candle", &candle_key)],
            params,
        ).await?;

        BitfinexParser::parse_klines(&response)
    }

    async fn get_ticker(
        &self,
        symbol: Symbol,
        account_type: AccountType,
    ) -> ExchangeResult<Ticker> {
        let formatted_symbol = Self::fmt_symbol(&symbol, account_type);

        let response = self.get(
            BitfinexEndpoint::Ticker,
            &[("symbol", &formatted_symbol)],
            HashMap::new(),
        ).await?;

        BitfinexParser::parse_ticker(&response, &formatted_symbol)
    }

    async fn ping(&self) -> ExchangeResult<()> {
        let response = self.get(
            BitfinexEndpoint::PlatformStatus,
            &[],
            HashMap::new(),
        ).await?;

        // Platform status returns [1] for operative, [0] for maintenance
        if let Some(arr) = response.as_array() {
            if !arr.is_empty() {
                if let Some(status) = arr[0].as_i64() {
                    if status == 1 {
                        return Ok(());
                    }
                }
            }
        }

        Err(ExchangeError::Network("Platform in maintenance".to_string()))
    }

    async fn get_exchange_info(&self, account_type: AccountType) -> ExchangeResult<Vec<SymbolInfo>> {
        // Use Bitfinex v1 symbols_details endpoint (returns array with pair info)
        // Note: v1 is still supported and returns more detail than v2 conf endpoints
        if !self.rate_limit_wait(1, false).await {
            return Err(ExchangeError::RateLimitExceeded {
                retry_after: None,
                message: "Rate limit budget >= 90% used; non-essential market data request dropped".to_string(),
            });
        }
        let url = "https://api.bitfinex.com/v1/symbols_details";
        let response = self.http.get(url, &HashMap::new()).await?;
        let info = BitfinexParser::parse_exchange_info(&response, account_type)?;
        self.precision.load_from_symbols(&info);
        Ok(info)
    }

    fn market_data_capabilities(&self, _account_type: AccountType) -> MarketDataCapabilities {
        MarketDataCapabilities {
            has_ping: true,
            has_price: true,
            has_ticker: true,
            has_orderbook: true,
            has_klines: true,
            has_exchange_info: true,
            // No public recent-trades endpoint implemented in this connector.
            has_recent_trades: false,
            // Bitfinex candle timeframes: 1m 3m 5m 15m 30m 1h 2h 3h 4h 6h 8h 12h 1D 1W 14D 1M
            supported_intervals: &[
                "1m", "3m", "5m", "15m", "30m",
                "1h", "2h", "3h", "4h", "6h", "8h", "12h",
                "1d", "1w", "2w", "1M",
            ],
            // Bitfinex accepts up to 10 000 candles per request (capped in get_klines with .min(10000)).
            max_kline_limit: Some(10000),
            // ticker, trades, book, candles channels all available in Bitfinex WS v2.
            has_ws_ticker: true,
            has_ws_trades: true,
            has_ws_orderbook: true,
            has_ws_klines: true,
        }
    }
}

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

#[async_trait]
impl Trading for BitfinexConnector {
    async fn place_order(&self, req: OrderRequest) -> ExchangeResult<PlaceOrderResponse> {
        let symbol = req.symbol.clone();
        let side = req.side;
        let quantity = req.quantity;
        let account_type = req.account_type;
        let formatted_symbol = Self::fmt_symbol(&symbol, account_type);
        let prefix = Self::order_type_prefix(account_type);

        // Amount: positive=buy, negative=sell (apply qty precision to absolute value then re-sign)
        let qty_str = self.precision.qty(&formatted_symbol, quantity);
        let amount_str = match side {
            OrderSide::Buy => qty_str,
            OrderSide::Sell => format!("-{}", qty_str),
        };

        match req.order_type {
            OrderType::Market => {
                let body = json!({
                    "type": format!("{}MARKET", prefix),
                    "symbol": formatted_symbol,
                    "amount": amount_str,
                });
                let response = self.post(BitfinexEndpoint::SubmitOrder, &[], body).await?;
                BitfinexParser::parse_submit_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::Limit { price } => {
                let body = json!({
                    "type": format!("{}LIMIT", prefix),
                    "symbol": formatted_symbol,
                    "amount": amount_str,
                    "price": self.precision.price(&formatted_symbol, price),
                });
                let response = self.post(BitfinexEndpoint::SubmitOrder, &[], body).await?;
                BitfinexParser::parse_submit_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::StopMarket { stop_price } => {
                // Bitfinex: EXCHANGE STOP (triggers market at stop_price)
                let body = json!({
                    "type": format!("{}STOP", prefix),
                    "symbol": formatted_symbol,
                    "amount": amount_str,
                    "price": self.precision.price(&formatted_symbol, stop_price),
                });
                let response = self.post(BitfinexEndpoint::SubmitOrder, &[], body).await?;
                BitfinexParser::parse_submit_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::StopLimit { stop_price, limit_price } => {
                // Bitfinex: EXCHANGE STOP LIMIT
                let body = json!({
                    "type": format!("{}STOP LIMIT", prefix),
                    "symbol": formatted_symbol,
                    "amount": amount_str,
                    "price": self.precision.price(&formatted_symbol, limit_price),
                    "price_aux_limit": self.precision.price(&formatted_symbol, stop_price),
                });
                let response = self.post(BitfinexEndpoint::SubmitOrder, &[], body).await?;
                BitfinexParser::parse_submit_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::TrailingStop { callback_rate, activation_price: _ } => {
                // Bitfinex: EXCHANGE TRAILING STOP
                // trail_pct is callback_rate in percent
                let body = json!({
                    "type": format!("{}TRAILING STOP", prefix),
                    "symbol": formatted_symbol,
                    "amount": amount_str,
                    "price": callback_rate.to_string(), // trail distance as % string
                });
                let response = self.post(BitfinexEndpoint::SubmitOrder, &[], body).await?;
                BitfinexParser::parse_submit_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::PostOnly { price } => {
                // Bitfinex: EXCHANGE LIMIT with flags = 4096 (POST_ONLY)
                let body = json!({
                    "type": format!("{}LIMIT", prefix),
                    "symbol": formatted_symbol,
                    "amount": amount_str,
                    "price": self.precision.price(&formatted_symbol, price),
                    "flags": 4096,
                });
                let response = self.post(BitfinexEndpoint::SubmitOrder, &[], body).await?;
                BitfinexParser::parse_submit_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::Ioc { price } => {
                // Bitfinex: EXCHANGE IOC
                let price_val = price.unwrap_or(0.0);
                let body = json!({
                    "type": format!("{}IOC", prefix),
                    "symbol": formatted_symbol,
                    "amount": amount_str,
                    "price": self.precision.price(&formatted_symbol, price_val),
                });
                let response = self.post(BitfinexEndpoint::SubmitOrder, &[], body).await?;
                BitfinexParser::parse_submit_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::Fok { price } => {
                // Bitfinex: EXCHANGE FOK
                let body = json!({
                    "type": format!("{}FOK", prefix),
                    "symbol": formatted_symbol,
                    "amount": amount_str,
                    "price": self.precision.price(&formatted_symbol, price),
                });
                let response = self.post(BitfinexEndpoint::SubmitOrder, &[], body).await?;
                BitfinexParser::parse_submit_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::Iceberg { price, display_quantity } => {
                // Bitfinex: EXCHANGE LIMIT with max_show parameter
                let body = json!({
                    "type": format!("{}LIMIT", prefix),
                    "symbol": formatted_symbol,
                    "amount": amount_str,
                    "price": self.precision.price(&formatted_symbol, price),
                    "meta": {
                        "max_show": self.precision.qty(&formatted_symbol, display_quantity),
                    },
                    "flags": 64, // Hidden flag
                });
                let response = self.post(BitfinexEndpoint::SubmitOrder, &[], body).await?;
                BitfinexParser::parse_submit_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::ReduceOnly { price } => {
                // Bitfinex: LIMIT with reduce-only flag (only valid for margin/futures)
                if !Self::is_derivatives(account_type) {
                    return Err(ExchangeError::UnsupportedOperation(
                        "ReduceOnly not supported for Spot".to_string()
                    ));
                }
                let order_type_str = if price.is_some() { "LIMIT" } else { "MARKET" };
                let mut body = json!({
                    "type": order_type_str,
                    "symbol": formatted_symbol,
                    "amount": amount_str,
                    "flags": 1024, // REDUCE_ONLY flag
                });
                if let Some(p) = price {
                    body["price"] = json!(self.precision.price(&formatted_symbol, p));
                }
                let response = self.post(BitfinexEndpoint::SubmitOrder, &[], body).await?;
                BitfinexParser::parse_submit_order(&response).map(PlaceOrderResponse::Simple)
            }

            OrderType::Oto { .. } => Err(ExchangeError::UnsupportedOperation(
                "Oto orders not supported on Bitfinex".into()
            )),
            OrderType::ConditionalPlan { .. } => Err(ExchangeError::UnsupportedOperation(
                "ConditionalPlan orders not supported on Bitfinex".into()
            )),
            OrderType::DcaRecurring { .. } => Err(ExchangeError::UnsupportedOperation(
                "DcaRecurring orders not supported on Bitfinex".into()
            )),
            _ => Err(ExchangeError::UnsupportedOperation(
                format!("{:?} order type not supported on {:?}", req.order_type, self.exchange_id())
            )),
        }
    }

    async fn cancel_order(&self, req: CancelRequest) -> ExchangeResult<Order> {
        match req.scope {
            CancelScope::Single { ref order_id } => {
                let id = order_id.parse::<i64>()
                    .map_err(|_| ExchangeError::InvalidRequest("Invalid order ID".to_string()))?;

                let body = json!({ "id": id });
                let response = self.post(BitfinexEndpoint::CancelOrder, &[], body).await?;
                BitfinexParser::parse_submit_order(&response)
            }

            CancelScope::Batch { ref order_ids } => {
                // Bitfinex: POST /auth/w/order/cancel/multi with {"id": [...]}
                let ids: Vec<i64> = order_ids.iter()
                    .filter_map(|id| id.parse::<i64>().ok())
                    .collect();

                if ids.is_empty() {
                    return Err(ExchangeError::InvalidRequest("No valid order IDs".to_string()));
                }

                let body = json!({ "id": ids });
                let _response = self.post(BitfinexEndpoint::CancelMultipleOrders, &[], body).await?;

                // Return a placeholder — Bitfinex multi-cancel returns notifications, not a single order
                Ok(Order {
                    id: ids[0].to_string(),
                    client_order_id: None,
                    symbol: req.symbol.as_ref().map(|s| s.to_string()).unwrap_or_default(),
                    side: OrderSide::Buy,
                    order_type: OrderType::Market,
                    status: crate::core::OrderStatus::Canceled,
                    price: None,
                    stop_price: None,
                    quantity: 0.0,
                    filled_quantity: 0.0,
                    average_price: None,
                    commission: None,
                    commission_asset: None,
                    created_at: 0,
                    updated_at: Some(crate::core::timestamp_millis() as i64),
                    time_in_force: crate::core::TimeInForce::Gtc,
                })
            }

            _ => Err(ExchangeError::UnsupportedOperation(
                format!("{:?} cancel scope not supported — use CancelAll trait for All/BySymbol", req.scope)
            )),
        }
    }

    async fn get_order_history(
        &self,
        filter: OrderHistoryFilter,
        _account_type: AccountType,
    ) -> ExchangeResult<Vec<Order>> {
        let mut body = json!({});

        if let Some(sym) = &filter.symbol {
            // sym is already a Symbol struct
            let formatted_symbol = Self::fmt_symbol(sym, _account_type);
            body["symbol"] = json!(formatted_symbol);
        }

        if let Some(start) = filter.start_time {
            body["start"] = json!(start);
        }
        if let Some(end) = filter.end_time {
            body["end"] = json!(end);
        }
        if let Some(limit) = filter.limit {
            body["limit"] = json!(limit.min(2500));
        }

        let response = self.post(BitfinexEndpoint::OrderHistory, &[], body).await?;
        BitfinexParser::parse_orders(&response)
    }

    async fn get_order(
        &self,
        symbol: &str,
        order_id: &str,
        account_type: AccountType,
    ) -> ExchangeResult<Order> {
        let symbol_parts: Vec<&str> = symbol.split('/').collect();
        let symbol = if symbol_parts.len() == 2 {
            crate::core::Symbol::new(symbol_parts[0], symbol_parts[1])
        } else {
            crate::core::Symbol { base: symbol.to_string(), quote: String::new(), raw: Some(symbol.to_string()) }
        };

        let formatted_symbol = Self::fmt_symbol(&symbol, account_type);

        let body = json!({ "symbol": formatted_symbol });

        let response = self.post(
            BitfinexEndpoint::ActiveOrdersBySymbol,
            &[("symbol", &formatted_symbol)],
            body,
        ).await?;

        let orders = BitfinexParser::parse_orders(&response)?;

        orders.into_iter()
            .find(|o| o.id == order_id)
            .ok_or_else(|| ExchangeError::Parse(format!("Order {} not found", order_id)))
    }

    async fn get_open_orders(
        &self,
        symbol: Option<&str>,
        account_type: AccountType,
    ) -> ExchangeResult<Vec<Order>> {
        let symbol: Option<crate::core::Symbol> = symbol.map(|s| {
            let parts: Vec<&str> = s.split('/').collect();
            if parts.len() == 2 {
                crate::core::Symbol::new(parts[0], parts[1])
            } else {
                crate::core::Symbol { base: s.to_string(), quote: String::new(), raw: Some(s.to_string()) }
            }
        });

        let response = if let Some(s) = symbol {
            let formatted_symbol = Self::fmt_symbol(&s, account_type);
            self.post(
                BitfinexEndpoint::ActiveOrdersBySymbol,
                &[("symbol", &formatted_symbol)],
                json!({}),
            ).await?
        } else {
            self.post(
                BitfinexEndpoint::ActiveOrders,
                &[],
                json!({}),
            ).await?
        };

        BitfinexParser::parse_orders(&response)
    }

    async fn get_user_trades(
        &self,
        filter: UserTradeFilter,
        account_type: AccountType,
    ) -> ExchangeResult<Vec<UserTrade>> {
        // Build request body — Bitfinex uses POST with JSON body for authenticated endpoints
        let mut body = json!({ "sort": -1 });

        if let Some(start) = filter.start_time {
            body["start"] = json!(start);
        }
        if let Some(end) = filter.end_time {
            body["end"] = json!(end);
        }
        if let Some(lim) = filter.limit {
            body["limit"] = json!(lim.min(250));
        }

        // Use symbol-scoped endpoint when symbol is provided
        let response = if let Some(sym_raw) = &filter.symbol {
            // Parse symbol string "BTC/USDT" or raw "tBTCUSD"
            let formatted = if sym_raw.starts_with('t') || sym_raw.starts_with('f') {
                // Already in Bitfinex format
                sym_raw.clone()
            } else {
                let parts: Vec<&str> = sym_raw.split('/').collect();
                if parts.len() == 2 {
                    let s = Symbol::new(parts[0], parts[1]);
                    Self::fmt_symbol(&s, account_type)
                } else {
                    format!("t{}", sym_raw.to_uppercase())
                }
            };
            self.post(
                BitfinexEndpoint::TradeHistoryBySymbol,
                &[("symbol", &formatted)],
                body,
            ).await?
        } else {
            self.post(BitfinexEndpoint::TradeHistory, &[], body).await?
        };

        BitfinexParser::parse_user_trades(&response)
    }

    fn trading_capabilities(&self, _account_type: AccountType) -> TradingCapabilities {
        // All order types (Market, Limit, Stop, StopLimit, TrailingStop, PostOnly, IOC, FOK,
        // Iceberg) work for both Spot and Derivatives. The only Derivatives-only order type is
        // ReduceOnly, but TradingCapabilities has no has_reduce_only field — the gate is
        // enforced at runtime in place_order(). Wire format differences (the "EXCHANGE " prefix
        // for Spot) are handled transparently by order_type_prefix(). No per-account branching.
        TradingCapabilities {
            has_market_order: true,
            has_limit_order: true,
            has_stop_market: true,  // "EXCHANGE STOP" (Spot) / "STOP" (Derivatives)
            has_stop_limit: true,   // "EXCHANGE STOP LIMIT" / "STOP LIMIT"
            has_trailing_stop: true, // "EXCHANGE TRAILING STOP" / "TRAILING STOP"
            // Bitfinex has no bracket (TP+SL combo) order type.
            has_bracket: false,
            // Bitfinex has no OCO order type.
            has_oco: false,
            has_amend: true,        // AmendOrder trait implemented via UpdateOrder endpoint
            has_batch: true,        // BatchOrders trait implemented via OrderMulti endpoint
            // Bitfinex OrderMulti accepts up to 75 operations per call.
            max_batch_size: Some(75),
            has_cancel_all: true,   // CancelAll trait implemented via CancelMultipleOrders
            has_user_trades: true,
            has_order_history: true,
        }
    }
}

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

#[async_trait]
impl Account for BitfinexConnector {
    async fn get_balance(&self, _query: BalanceQuery) -> ExchangeResult<Vec<Balance>> {
        let response = self.post(
            BitfinexEndpoint::Wallets,
            &[],
            json!({}),
        ).await?;

        BitfinexParser::parse_balances(&response)
    }

    async fn get_account_info(&self, account_type: AccountType) -> ExchangeResult<AccountInfo> {
        let balances = self.get_balance(BalanceQuery { asset: None, account_type }).await?;

        Ok(AccountInfo {
            account_type,
            can_trade: true,
            can_withdraw: true,
            can_deposit: true,
            maker_commission: 0.1,  // Default Bitfinex fees
            taker_commission: 0.2,
            balances,
        })
    }

    async fn get_fees(&self, symbol: Option<&str>) -> ExchangeResult<FeeInfo> {
        // Bitfinex: POST /auth/r/trades/hist returns trades with fee info
        // Use a recent trade to get actual fee rates, or return defaults from account summary
        let body = json!({ "limit": 1 });
        let response = self.post(BitfinexEndpoint::TradeHistory, &[], body).await?;

        // Try to extract fee from most recent trade
        if let Some(arr) = response.as_array() {
            if let Some(trade) = arr.first() {
                if let Some(trade_arr) = trade.as_array() {
                    // Trade array format: [ID, PAIR, MTS_CREATE, ORDER_ID, EXEC_AMOUNT, EXEC_PRICE, ORDER_TYPE, ORDER_PRICE, MAKER, FEE, FEE_CURRENCY]
                    if trade_arr.len() > 9 {
                        let fee = trade_arr[9].as_f64().unwrap_or(0.0).abs();
                        let amount = trade_arr[4].as_f64().unwrap_or(1.0).abs();
                        let price = trade_arr[5].as_f64().unwrap_or(1.0);
                        let rate = if amount * price > 0.0 { fee / (amount * price) } else { 0.002 };

                        return Ok(FeeInfo {
                            maker_rate: rate,
                            taker_rate: rate,
                            symbol: symbol.map(|s| s.to_string()),
                            tier: None,
                        });
                    }
                }
            }
        }

        // Fallback: default Bitfinex fees
        Ok(FeeInfo {
            maker_rate: 0.001,  // 0.1%
            taker_rate: 0.002,  // 0.2%
            symbol: symbol.map(|s| s.to_string()),
            tier: None,
        })
    }

    fn account_capabilities(&self, account_type: AccountType) -> AccountCapabilities {
        let is_futures = !matches!(account_type, AccountType::Spot | AccountType::Margin);

        AccountCapabilities {
            has_balances: true,
            has_account_info: true,
            has_fees: true,
            has_transfers: true,        // AccountTransfers trait implemented (wallet-to-wallet)
            has_sub_accounts: true,     // SubAccounts trait implemented (list + transfer)
            has_deposit_withdraw: true, // CustodialFunds trait implemented (deposit address + withdraw + movements)
            // Bitfinex margin borrowing is order-flag-based, no dedicated borrow/repay endpoint.
            has_margin: false,
            // No earn or staking product endpoints in this connector.
            has_earn_staking: false,
            // Funding payments (ledger category 28) are perpetual interest charges on open
            // derivative positions — not applicable to Spot accounts.
            has_funding_history: is_futures,
            has_ledger: true,           // AccountLedger trait works for all account types
            // No coin-to-coin conversion endpoint implemented.
            has_convert: false,
            // Positions trait implemented; applies to margin/futures account types.
            has_positions: is_futures,
        }
    }
}

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

#[async_trait]
impl Positions for BitfinexConnector {
    async fn get_positions(&self, query: PositionQuery) -> ExchangeResult<Vec<Position>> {
        let account_type = query.account_type;

        if account_type == AccountType::Spot {
            return Err(ExchangeError::UnsupportedOperation(
                "Positions not supported for Spot".to_string()
            ));
        }

        let response = self.post(
            BitfinexEndpoint::Positions,
            &[],
            json!({}),
        ).await?;

        BitfinexParser::parse_positions(&response)
    }

    async fn get_funding_rate(
        &self,
        _symbol: &str,
        account_type: AccountType,
    ) -> ExchangeResult<crate::core::FundingRate> {
        match account_type {
            AccountType::Spot | AccountType::Margin
            | AccountType::Earn | AccountType::Lending
            | AccountType::Options | AccountType::Convert => {
                return Err(ExchangeError::UnsupportedOperation(
                    "Funding rate not supported for Spot/Margin".to_string()
                ));
            }
            _ => {}
        }

        // Bitfinex doesn't have a direct funding rate endpoint for perpetuals
        // Would need to implement via derivatives API or funding book
        Err(ExchangeError::UnsupportedOperation(
            "Funding rate endpoint not implemented for Bitfinex".to_string()
        ))
    }

    async fn modify_position(&self, req: PositionModification) -> ExchangeResult<()> {
        match req {
            PositionModification::SetLeverage { account_type, .. } => {
                if account_type == AccountType::Spot {
                    return Err(ExchangeError::UnsupportedOperation(
                        "Leverage not supported for Spot".to_string()
                    ));
                }
                // Bitfinex handles leverage via order flags, not a separate endpoint
                Err(ExchangeError::UnsupportedOperation(
                    "Set leverage not available - use order flags instead".to_string()
                ))
            }
            _ => Err(ExchangeError::UnsupportedOperation(
                format!("{:?} not supported on {:?}", req, self.exchange_id())
            )),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// CANCEL ALL (optional trait)
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl CancelAll for BitfinexConnector {
    async fn cancel_all_orders(
        &self,
        scope: CancelScope,
        _account_type: AccountType,
    ) -> ExchangeResult<CancelAllResponse> {
        match scope {
            CancelScope::All { symbol: None } => {
                // Cancel all orders across all symbols
                let body = json!({ "all": 1 });
                let _response = self.post(BitfinexEndpoint::CancelMultipleOrders, &[], body).await?;

                Ok(CancelAllResponse {
                    cancelled_count: 0, // Bitfinex doesn't return count
                    failed_count: 0,
                    details: vec![],
                })
            }

            CancelScope::All { symbol: Some(sym) } | CancelScope::BySymbol { symbol: sym } => {
                // Cancel all orders for a specific symbol
                let formatted_symbol = Self::fmt_symbol(&sym, _account_type);
                let body = json!({ "symbol": formatted_symbol });
                let _response = self.post(BitfinexEndpoint::CancelMultipleOrders, &[], body).await?;

                Ok(CancelAllResponse {
                    cancelled_count: 0,
                    failed_count: 0,
                    details: vec![],
                })
            }

            _ => Err(ExchangeError::UnsupportedOperation(
                format!("{:?} not supported in cancel_all_orders", scope)
            )),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// AMEND ORDER (optional trait)
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl AmendOrder for BitfinexConnector {
    async fn amend_order(&self, req: AmendRequest) -> ExchangeResult<Order> {
        let id = req.order_id.parse::<i64>()
            .map_err(|_| ExchangeError::InvalidRequest("Invalid order ID".to_string()))?;

        let symbol = &req.symbol;
        let formatted_symbol = Self::fmt_symbol(symbol, req.account_type);

        let mut body = json!({ "id": id });

        if let Some(price) = req.fields.price {
            body["price"] = json!(self.precision.price(&formatted_symbol, price));
        }
        if let Some(qty) = req.fields.quantity {
            // For Bitfinex, amount sign determines buy/sell — preserve original sign
            body["amount"] = json!(self.precision.qty(&formatted_symbol, qty));
        }
        if let Some(stop_price) = req.fields.trigger_price {
            body["price_aux_limit"] = json!(self.precision.price(&formatted_symbol, stop_price));
        }

        let response = self.post(BitfinexEndpoint::UpdateOrder, &[], body).await?;
        BitfinexParser::parse_submit_order(&response)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// BATCH ORDERS (optional trait)
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl BatchOrders for BitfinexConnector {
    /// Place multiple orders in a single batch request.
    ///
    /// Bitfinex endpoint: POST /v2/auth/w/order/multi
    /// Body: `{"ops": [["on", {...order_params}], ...]}`
    /// Max 75 operations per request.
    async fn place_orders_batch(
        &self,
        orders: Vec<OrderRequest>,
    ) -> ExchangeResult<Vec<OrderResult>> {
        if orders.is_empty() {
            return Ok(vec![]);
        }

        let ops: Vec<Value> = orders.iter().map(|req| {
            let account_type = req.account_type;
            let formatted_symbol = Self::fmt_symbol(&req.symbol, account_type);
            let prefix = Self::order_type_prefix(account_type);

            let qty_str = self.precision.qty(&formatted_symbol, req.quantity);
            let amount_str = match req.side {
                OrderSide::Buy => qty_str,
                OrderSide::Sell => format!("-{}", qty_str),
            };

            let (order_type_str, price, price_aux) = match &req.order_type {
                OrderType::Market => (format!("{}MARKET", prefix), None, None),
                OrderType::Limit { price } => (format!("{}LIMIT", prefix), Some(*price), None),
                OrderType::StopMarket { stop_price } => (format!("{}STOP", prefix), Some(*stop_price), None),
                OrderType::StopLimit { stop_price, limit_price } => (
                    format!("{}STOP LIMIT", prefix),
                    Some(*limit_price),
                    Some(*stop_price),
                ),
                OrderType::PostOnly { price } => (format!("{}LIMIT", prefix), Some(*price), None),
                OrderType::Ioc { price } => (format!("{}IOC", prefix), price.map(|p| p), None),
                OrderType::Fok { price } => (format!("{}FOK", prefix), Some(*price), None),
                _ => (format!("{}MARKET", prefix), None, None),
            };

            let mut order_obj = json!({
                "type": order_type_str,
                "symbol": formatted_symbol,
                "amount": amount_str,
            });

            if let Some(p) = price {
                order_obj["price"] = json!(self.precision.price(&formatted_symbol, p));
            }
            if let Some(aux) = price_aux {
                order_obj["price_aux_limit"] = json!(self.precision.price(&formatted_symbol, aux));
            }

            // PostOnly flag
            if matches!(req.order_type, OrderType::PostOnly { .. }) {
                order_obj["flags"] = json!(4096);
            }

            json!(["on", order_obj])
        }).collect();

        let body = json!({ "ops": ops });
        let response = self.post(BitfinexEndpoint::OrderMulti, &[], body).await?;

        // Bitfinex returns array of notification arrays
        // Each notification: [0, "on-req", null, null, [order_array], ...]
        // We parse what we can from the response
        let results = if let Some(arr) = response.as_array() {
            arr.iter().enumerate().map(|(i, item)| {
                // Try to extract order from notification
                let order_arr = item.as_array()
                    .and_then(|a| a.get(4))
                    .and_then(|v| v.as_array());

                if let Some(order_data) = order_arr {
                    if let Some(id_val) = order_data.first() {
                        if let Some(id) = id_val.as_i64() {
                            let order = orders.get(i);
                            return OrderResult {
                                order: Some(Order {
                                    id: id.to_string(),
                                    client_order_id: None,
                                    symbol: order.map(|o| o.symbol.to_string()).unwrap_or_default(),
                                    side: order.map(|o| o.side).unwrap_or(OrderSide::Buy),
                                    order_type: order.map(|o| o.order_type.clone()).unwrap_or(OrderType::Market),
                                    status: crate::core::OrderStatus::New,
                                    price: None,
                                    stop_price: None,
                                    quantity: order.map(|o| o.quantity).unwrap_or(0.0),
                                    filled_quantity: 0.0,
                                    average_price: None,
                                    commission: None,
                                    commission_asset: None,
                                    created_at: crate::core::timestamp_millis() as i64,
                                    updated_at: None,
                                    time_in_force: crate::core::TimeInForce::Gtc,
                                }),
                                client_order_id: None,
                                success: true,
                                error: None,
                                error_code: None,
                            };
                        }
                    }
                }

                OrderResult {
                    order: None,
                    client_order_id: None,
                    success: false,
                    error: Some("Failed to parse batch order response".to_string()),
                    error_code: None,
                }
            }).collect()
        } else {
            orders.iter().map(|_| OrderResult {
                order: None,
                client_order_id: None,
                success: false,
                error: Some("Unexpected response format".to_string()),
                error_code: None,
            }).collect()
        };

        Ok(results)
    }

    /// Cancel multiple orders in a single batch request.
    ///
    /// Bitfinex endpoint: POST /v2/auth/w/order/multi
    /// Body: `{"ops": [["oc", {"id": N}], ...]}`
    async fn cancel_orders_batch(
        &self,
        order_ids: Vec<String>,
        _symbol: Option<&str>,
        _account_type: AccountType,
    ) -> ExchangeResult<Vec<OrderResult>> {
        if order_ids.is_empty() {
            return Ok(vec![]);
        }

        let ops: Vec<Value> = order_ids.iter()
            .filter_map(|id| id.parse::<i64>().ok())
            .map(|id| json!(["oc", { "id": id }]))
            .collect();

        if ops.is_empty() {
            return Err(ExchangeError::InvalidRequest("No valid order IDs".to_string()));
        }

        let body = json!({ "ops": ops });
        let _response = self.post(BitfinexEndpoint::OrderMulti, &[], body).await?;

        // Return success results — Bitfinex returns notifications, not per-order status
        let results = order_ids.iter().map(|_id| OrderResult {
            order: None,
            client_order_id: None,
            success: true,
            error: None,
            error_code: None,
        }).collect();

        let _ = order_ids; // silence unused after move
        Ok(results)
    }

    /// Maximum batch place size (Bitfinex limit: 75 operations per request).
    fn max_batch_place_size(&self) -> usize {
        75
    }

    /// Maximum batch cancel size (Bitfinex limit: 75 operations per request).
    fn max_batch_cancel_size(&self) -> usize {
        75
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// ACCOUNT TRANSFERS (optional trait)
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl AccountTransfers for BitfinexConnector {
    /// Transfer between Bitfinex wallets (exchange/margin/funding).
    ///
    /// Endpoint: POST /v2/auth/w/transfer
    /// Body: from, to, currency, currency_to, amount
    async fn transfer(&self, req: TransferRequest) -> ExchangeResult<TransferResponse> {
        fn account_type_to_wallet(account_type: AccountType) -> &'static str {
            match account_type {
                AccountType::Spot => "exchange",
                AccountType::Margin => "margin",
                AccountType::FuturesCross | AccountType::FuturesIsolated => "funding",
                AccountType::Lending => "funding",
                AccountType::Earn | AccountType::Options | AccountType::Convert => "exchange",
            }
        }

        let from_wallet = account_type_to_wallet(req.from_account);
        let to_wallet = account_type_to_wallet(req.to_account);

        let body = json!({
            "from": from_wallet,
            "to": to_wallet,
            "currency": req.asset.to_uppercase(),
            "currency_to": req.asset.to_uppercase(),
            "amount": req.amount.to_string(),
        });

        let response = self.post(BitfinexEndpoint::Transfer, &[], body).await?;

        // Response: [1, "SUCCESS", null, "transfer", [...transfer_data]]
        // Or: [0, "ERROR", null, "transfer", "error message"]
        if let Some(arr) = response.as_array() {
            let mts_created = arr.get(4)
                .and_then(|v| v.as_array())
                .and_then(|inner| inner.first())
                .and_then(|v| v.as_i64());

            return Ok(TransferResponse {
                transfer_id: format!("{}", mts_created.unwrap_or(0)),
                status: "Successful".to_string(),
                asset: req.asset,
                amount: req.amount,
                timestamp: mts_created,
            });
        }

        Err(ExchangeError::Parse("Unexpected transfer response format".to_string()))
    }

    /// Transfer history is not available via a standard endpoint on Bitfinex.
    ///
    /// Returns an empty vec — use movements endpoint for deposit/withdrawal history instead.
    async fn get_transfer_history(
        &self,
        _filter: TransferHistoryFilter,
    ) -> ExchangeResult<Vec<TransferResponse>> {
        Ok(vec![])
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// CUSTODIAL FUNDS (optional trait)
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl CustodialFunds for BitfinexConnector {
    /// Get deposit address for an asset on a given network/method.
    ///
    /// Endpoint: POST /v2/auth/w/deposit/address
    /// Body: wallet (exchange), method (network/coin), op_renew (0/1)
    async fn get_deposit_address(
        &self,
        asset: &str,
        network: Option<&str>,
    ) -> ExchangeResult<DepositAddress> {
        let method = network.unwrap_or(asset).to_lowercase();

        let body = json!({
            "wallet": "exchange",
            "method": method,
            "op_renew": 0,
        });

        let response = self.post(BitfinexEndpoint::DepositAddress, &[], body).await?;

        // Response: [MTS, TYPE, MESSAGE_ID, null, [nil, METHOD, CURRENCY_CODE, nil, nil, ADDRESS, ...]]
        if let Some(arr) = response.as_array() {
            if let Some(inner) = arr.get(4).and_then(|v| v.as_array()) {
                let address = inner.get(5)
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();

                let tag = inner.get(6)
                    .and_then(|v| v.as_str())
                    .filter(|s| !s.is_empty())
                    .map(|s| s.to_string());

                return Ok(DepositAddress {
                    address,
                    tag,
                    network: Some(method),
                    asset: asset.to_string(),
                    created_at: None,
                });
            }
        }

        Err(ExchangeError::Parse("Unexpected deposit address response format".to_string()))
    }

    /// Submit a withdrawal request.
    ///
    /// Endpoint: POST /v2/auth/w/withdraw
    /// Body: wallet, method, amount, address, payment_id (tag)
    async fn withdraw(&self, req: WithdrawRequest) -> ExchangeResult<WithdrawResponse> {
        let method = req.network
            .as_deref()
            .unwrap_or(&req.asset)
            .to_lowercase();

        let mut body = json!({
            "wallet": "exchange",
            "method": method,
            "amount": req.amount.to_string(),
            "address": req.address,
        });

        if let Some(tag) = &req.tag {
            body["payment_id"] = json!(tag);
        }

        let response = self.post(BitfinexEndpoint::Withdraw, &[], body).await?;

        // Response: [MTS, TYPE, MESSAGE_ID, null, [WITHDRAWAL_ID, ...]]
        if let Some(arr) = response.as_array() {
            let withdraw_id = arr.get(4)
                .and_then(|v| v.as_array())
                .and_then(|inner| inner.first())
                .and_then(|v| v.as_i64())
                .map(|id| id.to_string())
                .unwrap_or_else(|| "0".to_string());

            return Ok(WithdrawResponse {
                withdraw_id,
                status: "Pending".to_string(),
                tx_hash: None,
            });
        }

        Err(ExchangeError::Parse("Unexpected withdraw response format".to_string()))
    }

    /// Get deposit and/or withdrawal history via movements endpoint.
    ///
    /// Endpoint: POST /v2/auth/r/movements/{Symbol}/hist
    /// Movements cover both deposits (positive amount) and withdrawals (negative amount).
    async fn get_funds_history(
        &self,
        filter: FundsHistoryFilter,
    ) -> ExchangeResult<Vec<FundsRecord>> {
        let symbol = filter.asset.as_deref().unwrap_or("").to_uppercase();
        let symbol_path = if symbol.is_empty() { "".to_string() } else { symbol.clone() };

        let mut body = json!({});
        if let Some(start) = filter.start_time {
            body["start"] = json!(start);
        }
        if let Some(end) = filter.end_time {
            body["end"] = json!(end);
        }
        if let Some(limit) = filter.limit {
            body["limit"] = json!(limit.min(1000));
        }

        let response = self.post(
            BitfinexEndpoint::Movements,
            &[("symbol", &symbol_path)],
            body,
        ).await?;

        // Movements array: each element is:
        // [ID, CURRENCY, CURRENCY_NAME, nil, nil, MTS_STARTED, MTS_UPDATED, nil, nil,
        //  STATUS, nil, nil, AMOUNT, FEES, nil, DESTINATION_ADDRESS, nil, nil, nil, TRANSACTION_ID, ...]
        let records = if let Some(arr) = response.as_array() {
            arr.iter().filter_map(|item| {
                let m = item.as_array()?;
                let id = m.first()?.as_i64()?.to_string();
                let currency = m.get(1)?.as_str().unwrap_or("").to_string();
                let timestamp = m.get(5)?.as_i64().unwrap_or(0);
                let status = m.get(9)?.as_str().unwrap_or("Unknown").to_string();
                let amount = m.get(12)?.as_f64().unwrap_or(0.0);
                let _fee = m.get(13).and_then(|v| v.as_f64());
                let address = m.get(15).and_then(|v| v.as_str()).unwrap_or("").to_string();
                let tx_hash = m.get(20).and_then(|v| v.as_str())
                    .filter(|s| !s.is_empty())
                    .map(|s| s.to_string());

                // Filter by asset if specified
                if let Some(ref asset_filter) = filter.asset {
                    if !currency.eq_ignore_ascii_case(asset_filter) {
                        return None;
                    }
                }

                if amount >= 0.0 {
                    // Deposit
                    if matches!(filter.record_type, FundsRecordType::Deposit | FundsRecordType::Both) {
                        Some(FundsRecord::Deposit {
                            id,
                            asset: currency,
                            amount,
                            tx_hash,
                            network: None,
                            status,
                            timestamp,
                        })
                    } else {
                        None
                    }
                } else {
                    // Withdrawal (negative amount)
                    if matches!(filter.record_type, FundsRecordType::Withdrawal | FundsRecordType::Both) {
                        Some(FundsRecord::Withdrawal {
                            id,
                            asset: currency,
                            amount: amount.abs(),
                            fee: _fee.map(|f| f.abs()),
                            address,
                            tag: None,
                            tx_hash,
                            network: None,
                            status,
                            timestamp,
                        })
                    } else {
                        None
                    }
                }
            }).collect()
        } else {
            vec![]
        };

        Ok(records)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// SUB ACCOUNTS (optional trait)
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl SubAccounts for BitfinexConnector {
    /// Perform sub-account operations (list, transfer; create/get_balance not supported).
    async fn sub_account_operation(
        &self,
        op: SubAccountOperation,
    ) -> ExchangeResult<SubAccountResult> {
        match op {
            SubAccountOperation::List => {
                let response = self.post(BitfinexEndpoint::SubAccountList, &[], json!({})).await?;

                // Response is an array of sub-account objects
                let accounts = if let Some(arr) = response.as_array() {
                    arr.iter().filter_map(|item| {
                        let obj = item.as_object()?;
                        let id = obj.get("id")?.as_i64()?.to_string();
                        let name = obj.get("email")
                            .or_else(|| obj.get("name"))
                            .and_then(|v| v.as_str())
                            .unwrap_or("")
                            .to_string();
                        let status = obj.get("status")
                            .and_then(|v| v.as_str())
                            .unwrap_or("Normal")
                            .to_string();
                        Some(SubAccount { id, name, status })
                    }).collect()
                } else {
                    vec![]
                };

                Ok(SubAccountResult {
                    id: None,
                    name: None,
                    accounts,
                    transaction_id: None,
                })
            }

            SubAccountOperation::Transfer { sub_account_id, asset, amount, to_sub } => {
                // Bitfinex sub-account transfer: POST /v2/auth/w/sub_account/transfer
                // Body: sub_account_id, wallet_from, wallet_to, amount, currency
                // Bitfinex uses "exchange" wallet for both master→sub and sub→master transfers
                let (wallet_from, wallet_to) = ("exchange", "exchange");

                let body = json!({
                    "sub_account_id": sub_account_id.parse::<i64>().unwrap_or(0),
                    "wallet_from": wallet_from,
                    "wallet_to": wallet_to,
                    "amount": amount.to_string(),
                    "currency": asset.to_uppercase(),
                    "to_sub": to_sub,
                });

                let response = self.post(BitfinexEndpoint::SubAccountTransfer, &[], body).await?;

                let transaction_id = response.as_array()
                    .and_then(|arr| arr.get(4))
                    .and_then(|v| v.as_array())
                    .and_then(|inner| inner.first())
                    .and_then(|v| v.as_i64())
                    .map(|id| id.to_string());

                Ok(SubAccountResult {
                    id: Some(sub_account_id),
                    name: None,
                    accounts: vec![],
                    transaction_id,
                })
            }

            SubAccountOperation::Create { .. } => {
                Err(ExchangeError::UnsupportedOperation(
                    "Create sub-account not supported via Bitfinex REST API".to_string()
                ))
            }

            SubAccountOperation::GetBalance { .. } => {
                Err(ExchangeError::UnsupportedOperation(
                    "Get sub-account balance not supported via standard Bitfinex REST API".to_string()
                ))
            }
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// FUNDING HISTORY (optional trait)
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl FundingHistory for BitfinexConnector {
    /// Get historical funding payments for the account.
    ///
    /// Uses `POST /v2/auth/r/ledgers/{currency}/hist` with `{"category": 28}`.
    /// Category 28 = funding charges/payments on perpetual positions.
    ///
    /// `filter.symbol` is treated as the settlement currency (e.g. "UST", "BTC").
    /// When `None`, defaults to "UST" (Bitfinex perpetual settlement asset).
    async fn get_funding_payments(
        &self,
        filter: FundingFilter,
        _account_type: AccountType,
    ) -> ExchangeResult<Vec<FundingPayment>> {
        // Bitfinex uses currency-level ledger queries; derive currency from symbol or use UST
        let currency = filter.symbol
            .as_deref()
            .map(|s| {
                // Symbol may be like "tBTCF0:USTF0" — extract the settlement currency
                if let Some(idx) = s.rfind(':') {
                    // e.g. "USTF0" → strip "F0"
                    s[idx + 1..].trim_end_matches("F0").to_uppercase()
                } else {
                    s.to_uppercase()
                }
            })
            .unwrap_or_else(|| "UST".to_string());

        // Build POST body: category 28 = funding charges
        let mut body = json!({"category": 28});

        if let Some(start) = filter.start_time {
            body["start"] = json!(start);
        }
        if let Some(end) = filter.end_time {
            body["end"] = json!(end);
        }
        if let Some(limit) = filter.limit {
            body["limit"] = json!(limit.min(500));
        }

        let response = self.post(
            BitfinexEndpoint::LedgerHist,
            &[("currency", &currency)],
            body,
        ).await?;

        // Response: array of arrays [[ID, CURRENCY, null, MTS, null, AMOUNT, BALANCE, null, DESCRIPTION], ...]
        let entries = response.as_array()
            .ok_or_else(|| ExchangeError::Parse("Expected array for ledger response".to_string()))?;

        let payments = entries.iter().filter_map(|row| {
            let arr = row.as_array()?;

            let id = arr.first()?.as_i64()?.to_string();
            let asset = arr.get(1)?.as_str()?.to_string();
            let timestamp = arr.get(3)?.as_i64()?;
            let amount = arr.get(5)?.as_f64()?;
            let balance = arr.get(6).and_then(|v| v.as_f64());
            let description = arr.get(8)
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();

            // Derive symbol from the description when possible (Bitfinex includes it)
            // Description format: "Margin funding payment on wallet margin for BTC/USD @ 0.001"
            // Use id as ref; payment amount is the actual funding payment
            let _ = (id, balance, description);

            Some(FundingPayment {
                // Bitfinex ledger doesn't carry per-entry symbol/instrument info directly
                symbol: currency.clone(),
                // Funding rate is not returned in ledger entries (only in separate funding data)
                funding_rate: 0.0,
                // Position size not available in ledger entries
                position_size: 0.0,
                payment: amount,
                asset,
                timestamp,
            })
        }).collect();

        Ok(payments)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// ACCOUNT LEDGER (optional trait)
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl AccountLedger for BitfinexConnector {
    /// Get account ledger entries.
    ///
    /// Uses `POST /v2/auth/r/ledgers/{currency}/hist`.
    /// When `filter.asset` is `None`, queries with currency "USD" as default.
    /// To fetch all currencies, callers should query per currency.
    ///
    /// Bitfinex ledger categories:
    /// - 1  = deposit
    /// - 2  = withdrawal
    /// - 4  = exchange
    /// - 5  = margin (trade)
    /// - 28 = funding (perpetual interest)
    /// - 68 = affiliates earning
    async fn get_ledger(
        &self,
        filter: LedgerFilter,
        _account_type: AccountType,
    ) -> ExchangeResult<Vec<LedgerEntry>> {
        let currency = filter.asset
            .as_deref()
            .unwrap_or("USD")
            .to_uppercase();

        // Map entry_type filter to Bitfinex category
        let category: Option<i32> = filter.entry_type.as_ref().map(|t| match t {
            LedgerEntryType::Deposit   => 1,
            LedgerEntryType::Withdrawal => 2,
            LedgerEntryType::Trade     => 5,
            LedgerEntryType::Funding   => 28,
            LedgerEntryType::Fee       => 4,
            LedgerEntryType::Rebate    => 4,
            LedgerEntryType::Transfer  => 2,
            LedgerEntryType::Liquidation => 5,
            LedgerEntryType::Settlement  => 5,
            LedgerEntryType::Other(_)    => -1, // -1 = no category filter
        });

        let mut body = json!({});
        if let Some(cat) = category {
            if cat >= 0 {
                body["category"] = json!(cat);
            }
        }
        if let Some(start) = filter.start_time {
            body["start"] = json!(start);
        }
        if let Some(end) = filter.end_time {
            body["end"] = json!(end);
        }
        if let Some(limit) = filter.limit {
            body["limit"] = json!(limit.min(500));
        }

        let response = self.post(
            BitfinexEndpoint::LedgerHist,
            &[("currency", &currency)],
            body,
        ).await?;

        // Response: [[ID, CURRENCY, null, MTS, null, AMOUNT, BALANCE, null, DESCRIPTION], ...]
        let entries = response.as_array()
            .ok_or_else(|| ExchangeError::Parse("Expected array for ledger response".to_string()))?;

        let ledger: Vec<LedgerEntry> = entries.iter().filter_map(|row| {
            let arr = row.as_array()?;

            let id = arr.first()?.as_i64()?.to_string();
            let asset = arr.get(1)?.as_str().unwrap_or(&currency).to_string();
            let timestamp = arr.get(3)?.as_i64()?;
            let amount = arr.get(5)?.as_f64()?;
            let balance = arr.get(6).and_then(|v| v.as_f64());
            let description = arr.get(8)
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();

            // Classify the entry type from description keywords
            let entry_type = classify_bitfinex_ledger_entry(&description);

            Some(LedgerEntry {
                id,
                asset,
                amount,
                balance,
                entry_type,
                description,
                ref_id: None,
                timestamp,
            })
        }).collect();

        Ok(ledger)
    }
}

/// Classify a Bitfinex ledger entry type from its description string.
fn classify_bitfinex_ledger_entry(description: &str) -> LedgerEntryType {
    let lower = description.to_lowercase();
    if lower.contains("deposit") || lower.contains("crypto deposit") {
        LedgerEntryType::Deposit
    } else if lower.contains("withdraw") {
        LedgerEntryType::Withdrawal
    } else if lower.contains("transfer") {
        LedgerEntryType::Transfer
    } else if lower.contains("margin funding") || lower.contains("funding payment") {
        LedgerEntryType::Funding
    } else if lower.contains("trading fee") || lower.contains("taker fee") || lower.contains("maker fee") {
        LedgerEntryType::Fee
    } else if lower.contains("rebate") {
        LedgerEntryType::Rebate
    } else if lower.contains("liquidat") {
        LedgerEntryType::Liquidation
    } else if lower.contains("settlement") || lower.contains("settle") {
        LedgerEntryType::Settlement
    } else if lower.contains("trade") || lower.contains("exchange") || lower.contains("order") {
        LedgerEntryType::Trade
    } else {
        LedgerEntryType::Other(description.to_string())
    }
}