scope-bca-core 0.8.2

Scope Blockchain Analysis - Core library for blockchain data analysis
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
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
//! # Blockchain Client Module
//!
//! This module provides abstractions and implementations for interacting
//! with various blockchain networks. It defines a common `ChainClient` trait
//! that all chain-specific implementations must satisfy.
//!
//! ## Capabilities
//!
//! All chain clients support:
//! - **Balance queries** with optional USD valuation via DexScreener
//! - **Transaction lookup** by hash/signature with full details
//! - **Transaction history** for addresses with pagination
//! - **Token balances** (ERC-20, SPL, TRC-20) for address book tracking
//!
//! ## Supported Chains
//!
//! ### EVM-Compatible Chains
//!
//! - **Ethereum** - Ethereum Mainnet (via Etherscan V2 API)
//! - **Polygon** - Polygon PoS
//! - **Arbitrum** - Arbitrum One
//! - **Optimism** - Optimism Mainnet
//! - **Base** - Base (Coinbase L2)
//! - **BSC** - BNB Smart Chain (Binance)
//!
//! ### Non-EVM Chains
//!
//! - **Solana** - Solana Mainnet (JSON-RPC with `jsonParsed` encoding)
//! - **Tron** - Tron Mainnet (TronGrid API, base58check address validation)
//!
//! ### DEX Data
//!
//! - **DexScreener** - Token prices, volume, liquidity, and trading data across all DEX pairs
//!
//! ## Usage
//!
//! ### Ethereum/EVM Client
//!
//! ```rust,no_run
//! use scope::chains::{ChainClient, EthereumClient};
//! use scope::Config;
//!
//! #[tokio::main]
//! async fn main() -> scope::Result<()> {
//!     let config = Config::load(None)?;
//!     let http = scope::http::build(&config)?.client;
//!     let client = EthereumClient::new_with_http(&config.chains, http)?;
//!     
//!     let balance = client.get_balance("0x742d35Cc6634C0532925a3b844Bc9e7595f1b3c2").await?;
//!     println!("Balance: {} ETH", balance.formatted);
//!     Ok(())
//! }
//! ```
//!
//! ### Solana Client
//!
//! ```rust,no_run
//! use scope::chains::SolanaClient;
//! use scope::Config;
//!
//! #[tokio::main]
//! async fn main() -> scope::Result<()> {
//!     let config = Config::load(None)?;
//!     let http = scope::http::build(&config)?.client;
//!     let client = SolanaClient::new_with_http(&config.chains, http)?;
//!     
//!     let balance = client.get_balance("DRpbCBMxVnDK7maPM5tGv6MvB3v1sRMC86PZ8okm21hy").await?;
//!     println!("Balance: {} SOL", balance.formatted);
//!     Ok(())
//! }
//! ```
//!
//! ### Tron Client
//!
//! ```rust,no_run
//! use scope::chains::TronClient;
//! use scope::Config;
//!
//! #[tokio::main]
//! async fn main() -> scope::Result<()> {
//!     let config = Config::load(None)?;
//!     let http = scope::http::build(&config)?.client;
//!     let client = TronClient::new_with_http(&config.chains, http)?;
//!     
//!     let balance = client.get_balance("TDqSquXBgUCLYvYC4XZgrprLK589dkhSCf").await?;
//!     println!("Balance: {} TRX", balance.formatted);
//!     Ok(())
//! }
//! ```

pub mod dex;
pub mod ethereum;
pub mod registry;
pub mod solana;
pub mod tron;

pub use dex::{DexClient, DexDataSource, DiscoverToken, TokenSearchResult};
pub use ethereum::{ApiType, EthereumClient};
pub use solana::{SolanaClient, validate_solana_address, validate_solana_signature};
pub use tron::{TronClient, validate_tron_address, validate_tron_tx_hash};

use crate::error::Result;
use crate::http::HttpClient;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Trait defining common blockchain client operations.
///
/// All chain-specific clients must implement this trait to provide
/// a consistent interface for blockchain interactions.
///
/// ## Core Methods
///
/// Every implementation must provide: `chain_name`, `native_token_symbol`,
/// `get_balance`, `get_transaction`, `get_transactions`, `get_block_number`,
/// `enrich_balance_usd`, and `get_token_balances`.
///
/// ## Token Explorer Methods
///
/// The token-explorer methods (`get_token_info`, `get_token_holders`,
/// `get_token_holder_count`) have default implementations that return
/// "not supported" errors or empty results. Only chains with block-explorer
/// support for these endpoints (currently EVM chains) need to override them.
#[async_trait]
pub trait ChainClient: Send + Sync {
    /// Returns the name of the blockchain network.
    fn chain_name(&self) -> &str;

    /// Returns the native token symbol (e.g., "ETH", "MATIC").
    fn native_token_symbol(&self) -> &str;

    /// Fetches the native token balance for an address.
    ///
    /// # Arguments
    ///
    /// * `address` - The blockchain address to query
    ///
    /// # Returns
    ///
    /// Returns a [`Balance`] containing the balance in multiple formats.
    async fn get_balance(&self, address: &str) -> Result<Balance>;

    /// Enriches a balance with USD valuation via DexScreener.
    ///
    /// # Arguments
    ///
    /// * `balance` - The balance to enrich with a USD value
    async fn enrich_balance_usd(&self, balance: &mut Balance);

    /// Fetches transaction details by hash.
    ///
    /// # Arguments
    ///
    /// * `hash` - The transaction hash to query
    ///
    /// # Returns
    ///
    /// Returns [`Transaction`] details or an error if not found.
    async fn get_transaction(&self, hash: &str) -> Result<Transaction>;

    /// Fetches recent transactions for an address.
    ///
    /// # Arguments
    ///
    /// * `address` - The address to query
    /// * `limit` - Maximum number of transactions to return
    ///
    /// # Returns
    ///
    /// Returns a vector of [`Transaction`] objects.
    async fn get_transactions(&self, address: &str, limit: u32) -> Result<Vec<Transaction>>;

    /// Fetches the current block number.
    async fn get_block_number(&self) -> Result<u64>;

    /// Fetches token balances for an address.
    ///
    /// Returns a unified [`TokenBalance`] list regardless of chain
    /// (ERC-20, SPL, TRC-20 all map to the same type).
    async fn get_token_balances(&self, address: &str) -> Result<Vec<TokenBalance>>;

    /// Fetches token information for a contract address.
    ///
    /// Default implementation returns "not supported" error.
    /// Override in chain clients that support token info lookups.
    async fn get_token_info(&self, _address: &str) -> Result<Token> {
        Err(crate::error::ScopeError::Chain(
            "Token info lookup not supported on this chain".to_string(),
        ))
    }

    /// Fetches top token holders for a contract address.
    ///
    /// Default implementation returns an empty vector.
    /// Override in chain clients that support holder lookups.
    async fn get_token_holders(&self, _address: &str, _limit: u32) -> Result<Vec<TokenHolder>> {
        Ok(Vec::new())
    }

    /// Fetches total token holder count for a contract address.
    ///
    /// Default implementation returns 0.
    /// Override in chain clients that support holder count lookups.
    async fn get_token_holder_count(&self, _address: &str) -> Result<u64> {
        Ok(0)
    }

    /// Fetches bytecode at address (EVM: eth_getCode).
    /// Returns "0x" for EOA, non-empty hex for contracts.
    /// Default: not supported.
    async fn get_code(&self, _address: &str) -> Result<String> {
        Err(crate::error::ScopeError::Chain(
            "Code lookup not supported on this chain".to_string(),
        ))
    }

    /// Reads a storage slot value at a contract address (EVM: eth_getStorageAt).
    /// Returns the 32-byte hex value at the given slot position.
    /// Default: not supported.
    async fn get_storage_at(&self, _address: &str, _slot: &str) -> Result<String> {
        Err(crate::error::ScopeError::Chain(
            "Storage lookup not supported on this chain".to_string(),
        ))
    }
}

/// Factory trait for creating chain clients and DEX data sources.
///
/// Bundles both chain and DEX client creation so CLI functions
/// only need one injected dependency instead of two.
///
/// # Example
///
/// ```rust,no_run
/// use scope::chains::{ChainClientFactory, DefaultClientFactory};
/// use scope::http::NativeHttpClient;
/// use scope::Config;
/// use std::sync::Arc;
///
/// let config = Config::default();
/// let http = Arc::new(NativeHttpClient::new().unwrap());
/// let factory = DefaultClientFactory { chains_config: config.chains.clone(), http };
/// let client = factory.create_chain_client("ethereum").unwrap();
/// ```
pub trait ChainClientFactory: Send + Sync {
    /// Creates a chain client for the given blockchain network.
    ///
    /// # Arguments
    ///
    /// * `chain` - The chain name (e.g., "ethereum", "solana", "tron")
    fn create_chain_client(&self, chain: &str) -> Result<Box<dyn ChainClient>>;

    /// Creates a DEX data source client.
    fn create_dex_client(&self) -> Box<dyn DexDataSource>;

    /// The HTTP transport for clients this factory does not create itself
    /// (exchange venues, compliance and contract lookups), so they use the
    /// same transport as the chain clients.
    fn http(&self) -> Arc<dyn HttpClient>;
}

/// Default factory that creates real chain clients from configuration.
///
/// Holds a shared [`HttpClient`] transport so all chain clients route
/// through the same backend (native `reqwest` or Ghola sidecar).
pub struct DefaultClientFactory {
    /// Chain configuration containing API keys and endpoints.
    pub chains_config: crate::config::ChainsConfig,
    /// Shared HTTP transport injected into every client.
    pub http: Arc<dyn HttpClient>,
}

impl ChainClientFactory for DefaultClientFactory {
    fn create_chain_client(&self, chain: &str) -> Result<Box<dyn ChainClient>> {
        match chain.to_lowercase().as_str() {
            "solana" | "sol" => Ok(Box::new(SolanaClient::new_with_http(
                &self.chains_config,
                self.http.clone(),
            )?)),
            "tron" | "trx" => Ok(Box::new(TronClient::new_with_http(
                &self.chains_config,
                self.http.clone(),
            )?)),
            _ => Ok(Box::new(EthereumClient::for_chain_with_http(
                chain,
                &self.chains_config,
                self.http.clone(),
            )?)),
        }
    }

    fn create_dex_client(&self) -> Box<dyn DexDataSource> {
        Box::new(DexClient::new_with_http(self.http.clone()))
    }

    fn http(&self) -> Arc<dyn HttpClient> {
        self.http.clone()
    }
}

/// Balance representation with multiple formats.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Balance {
    /// Raw balance in smallest unit (e.g., wei).
    pub raw: String,

    /// Human-readable formatted balance.
    pub formatted: String,

    /// Number of decimals for the token.
    pub decimals: u8,

    /// Token symbol.
    pub symbol: String,

    /// USD value (if available).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usd_value: Option<f64>,
}

/// Transaction information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transaction {
    /// Transaction hash.
    pub hash: String,

    /// Block number (None if pending).
    pub block_number: Option<u64>,

    /// Block timestamp (None if pending).
    pub timestamp: Option<u64>,

    /// Sender address.
    pub from: String,

    /// Recipient address (None for contract creation).
    pub to: Option<String>,

    /// Value transferred in native token.
    pub value: String,

    /// Gas limit.
    pub gas_limit: u64,

    /// Gas used (None if pending).
    pub gas_used: Option<u64>,

    /// Gas price in wei.
    pub gas_price: String,

    /// Transaction nonce.
    pub nonce: u64,

    /// Input data.
    pub input: String,

    /// Transaction status (None if pending, Some(true) for success).
    pub status: Option<bool>,
}

/// Token information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Token {
    /// Contract address.
    pub contract_address: String,

    /// Token symbol.
    pub symbol: String,

    /// Token name.
    pub name: String,

    /// Decimal places.
    pub decimals: u8,
}

/// Token balance for an address.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenBalance {
    /// Token information.
    pub token: Token,

    /// Raw balance.
    pub balance: String,

    /// Formatted balance.
    pub formatted_balance: String,

    /// USD value (if available).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usd_value: Option<f64>,
}

// ============================================================================
// Token Analytics Types
// ============================================================================

/// A token holder with their balance and percentage of supply.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenHolder {
    /// Holder's address.
    pub address: String,

    /// Raw balance amount.
    pub balance: String,

    /// Formatted balance with proper decimals.
    pub formatted_balance: String,

    /// Percentage of total supply held.
    pub percentage: f64,

    /// Rank among all holders (1 = largest).
    pub rank: u32,
}

/// A price data point for historical charting.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PricePoint {
    /// Unix timestamp in seconds.
    pub timestamp: i64,

    /// Price in USD.
    pub price: f64,
}

/// A volume data point for historical charting.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VolumePoint {
    /// Unix timestamp in seconds.
    pub timestamp: i64,

    /// Volume in USD.
    pub volume: f64,
}

/// A holder count data point for historical charting.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HolderCountPoint {
    /// Unix timestamp in seconds.
    pub timestamp: i64,

    /// Number of holders.
    pub count: u64,
}

/// DEX trading pair information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DexPair {
    /// DEX name (e.g., "Uniswap V3", "SushiSwap").
    pub dex_name: String,

    /// Pair address on the DEX.
    pub pair_address: String,

    /// Base token symbol.
    pub base_token: String,

    /// Quote token symbol.
    pub quote_token: String,

    /// Current price in USD.
    pub price_usd: f64,

    /// 24h trading volume in USD.
    pub volume_24h: f64,

    /// Liquidity in USD.
    pub liquidity_usd: f64,

    /// Base-token reserve of the pool (DexScreener `liquidity.base`), when
    /// reported. The synthetic AMM order book is built from it.
    #[serde(default)]
    pub liquidity_base: Option<f64>,

    /// Price change percentage in 24h.
    pub price_change_24h: f64,

    /// Buy transactions in 24h.
    pub buys_24h: u64,

    /// Sell transactions in 24h.
    pub sells_24h: u64,

    /// Buy transactions in 6h.
    pub buys_6h: u64,

    /// Sell transactions in 6h.
    pub sells_6h: u64,

    /// Buy transactions in 1h.
    pub buys_1h: u64,

    /// Sell transactions in 1h.
    pub sells_1h: u64,

    /// Pair creation timestamp.
    pub pair_created_at: Option<i64>,

    /// Direct URL to this pair on DexScreener.
    pub url: Option<String>,
}

/// Comprehensive token analytics data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenAnalytics {
    /// Token information.
    pub token: Token,

    /// Blockchain network name.
    pub chain: String,

    /// Top token holders.
    pub holders: Vec<TokenHolder>,

    /// Total number of holders.
    pub total_holders: u64,

    /// 24-hour trading volume in USD.
    pub volume_24h: f64,

    /// 7-day trading volume in USD.
    pub volume_7d: f64,

    /// Current price in USD.
    pub price_usd: f64,

    /// 24-hour price change percentage.
    pub price_change_24h: f64,

    /// 7-day price change percentage.
    pub price_change_7d: f64,

    /// Total liquidity across DEXs in USD.
    pub liquidity_usd: f64,

    /// Market capitalization (if available).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub market_cap: Option<f64>,

    /// Fully diluted valuation (if available).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fdv: Option<f64>,

    /// Total supply.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub total_supply: Option<String>,

    /// Circulating supply.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub circulating_supply: Option<String>,

    /// Historical price data for charting.
    pub price_history: Vec<PricePoint>,

    /// Historical volume data for charting.
    pub volume_history: Vec<VolumePoint>,

    /// Historical holder count data for charting.
    pub holder_history: Vec<HolderCountPoint>,

    /// DEX trading pairs.
    pub dex_pairs: Vec<DexPair>,

    /// Timestamp when this data was fetched.
    pub fetched_at: i64,

    /// Percentage of supply held by top 10 holders.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_10_concentration: Option<f64>,

    /// Percentage of supply held by top 50 holders.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_50_concentration: Option<f64>,

    /// Percentage of supply held by top 100 holders.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_100_concentration: Option<f64>,

    /// 6-hour price change percentage.
    pub price_change_6h: f64,

    /// 1-hour price change percentage.
    pub price_change_1h: f64,

    /// Total buy transactions in 24 hours.
    pub total_buys_24h: u64,

    /// Total sell transactions in 24 hours.
    pub total_sells_24h: u64,

    /// Total buy transactions in 6 hours.
    pub total_buys_6h: u64,

    /// Total sell transactions in 6 hours.
    pub total_sells_6h: u64,

    /// Total buy transactions in 1 hour.
    pub total_buys_1h: u64,

    /// Total sell transactions in 1 hour.
    pub total_sells_1h: u64,

    /// Token age in hours (since earliest pair creation).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token_age_hours: Option<f64>,

    /// Token image URL.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image_url: Option<String>,

    /// Token website URLs.
    pub websites: Vec<String>,

    /// Token social media links.
    pub socials: Vec<TokenSocial>,

    /// DexScreener URL for the primary pair.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dexscreener_url: Option<String>,
}

/// Social media link for a token.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TokenSocial {
    /// Platform name (twitter, telegram, discord, etc.)
    pub platform: String,
    /// URL or handle for the social account.
    pub url: String,
}

// ============================================================================
// NFT Types
// ============================================================================

/// NFT token metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NftMetadata {
    /// Token ID.
    pub token_id: String,
    /// NFT name.
    pub name: Option<String>,
    /// NFT description.
    pub description: Option<String>,
    /// Image URL.
    pub image_url: Option<String>,
    /// Token URI (metadata JSON location).
    pub token_uri: Option<String>,
    /// Token standard (ERC-721 or ERC-1155).
    pub standard: String,
    /// Collection/contract name.
    pub collection_name: Option<String>,
    /// Additional attributes.
    pub attributes: Vec<NftAttribute>,
}

/// An NFT attribute (trait).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NftAttribute {
    /// Trait type/name.
    pub trait_type: String,
    /// Trait value.
    pub value: String,
}

// ============================================================================
// Gas Analysis Types
// ============================================================================

/// Gas usage analysis for a contract or address.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GasAnalysis {
    /// Average gas used per transaction.
    pub avg_gas_used: u64,
    /// Maximum gas used in a single transaction.
    pub max_gas_used: u64,
    /// Minimum gas used in a single transaction.
    pub min_gas_used: u64,
    /// Total gas spent (sum of gas_used * gas_price).
    pub total_gas_cost_wei: String,
    /// Total gas cost in ETH/native token.
    pub total_gas_cost_formatted: String,
    /// Number of transactions analyzed.
    pub tx_count: u64,
    /// Gas usage by function selector (top callers).
    pub gas_by_function: Vec<GasByFunction>,
    /// Failed transaction count and wasted gas.
    pub failed_tx_count: u64,
    /// Gas wasted on failed transactions.
    pub wasted_gas: u64,
}

/// Gas usage breakdown by function.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GasByFunction {
    /// Function selector or name.
    pub function: String,
    /// Number of calls.
    pub call_count: u64,
    /// Average gas per call.
    pub avg_gas: u64,
    /// Total gas for this function.
    pub total_gas: u64,
}

// ============================================================================
// Chain Metadata
// ============================================================================

/// The chain a request falls back to when none is given.
pub const DEFAULT_CHAIN: &str = "ethereum";

/// Metadata for a blockchain network (symbol, decimals, explorer URLs).
///
/// Used for normalized presentation across all chains.
#[derive(Debug, Clone)]
pub struct ChainMetadata {
    /// Canonical chain identifier.
    pub chain_id: &'static str,
    /// Native token symbol (e.g., ETH, SOL, TRX).
    pub native_symbol: &'static str,
    /// Native token decimals.
    pub native_decimals: u8,
    /// Block explorer base URL for token pages.
    pub explorer_token_base: &'static str,
}

/// Returns chain metadata for display and formatting.
///
/// Returns `None` for unknown chains.
pub fn chain_metadata(chain: &str) -> Option<ChainMetadata> {
    let c = registry::chain(chain)?;
    Some(ChainMetadata {
        chain_id: c.id,
        native_symbol: c.native_symbol,
        native_decimals: c.native_decimals,
        explorer_token_base: c.explorer_token_base?,
    })
}

/// Returns the native token symbol for a chain, or "???" if unknown.
pub fn native_symbol(chain: &str) -> &'static str {
    registry::chain(chain).map_or("???", |c| c.native_symbol)
}

// ============================================================================
// Chain Inference
// ============================================================================

/// Infers the blockchain from an address format.
///
/// Returns `Some(chain_name)` if the address format is unambiguous,
/// or `None` if the format is not recognized.
///
/// # Supported Formats
///
/// - **EVM** (ethereum): `0x` prefix + 40 hex chars (42 total)
/// - **Tron**: Starts with `T` + 34 chars (Base58Check)
/// - **Solana**: Base58, 32-44 chars, decodes to 32 bytes
///
/// # Examples
///
/// ```
/// use scope::chains::infer_chain_from_address;
///
/// assert_eq!(infer_chain_from_address("0x742d35Cc6634C0532925a3b844Bc9e7595f1b3c2"), Some("ethereum"));
/// assert_eq!(infer_chain_from_address("TDqSquXBgUCLYvYC4XZgrprLK589dkhSCf"), Some("tron"));
/// assert_eq!(infer_chain_from_address("DRpbCBMxVnDK7maPM5tGv6MvB3v1sRMC86PZ8okm21hy"), Some("solana"));
/// assert_eq!(infer_chain_from_address("invalid"), None);
/// ```
pub fn infer_chain_from_address(address: &str) -> Option<&'static str> {
    // Tron: starts with 'T', 34 chars, valid base58
    if address.starts_with('T') && address.len() == 34 && bs58::decode(address).into_vec().is_ok() {
        return Some("tron");
    }

    // EVM: 0x prefix, 42 chars total (40 hex + "0x")
    if address.starts_with("0x")
        && address.len() == 42
        && address[2..].chars().all(|c| c.is_ascii_hexdigit())
    {
        return Some("ethereum");
    }

    // Solana: base58, 32-44 chars, decodes to 32 bytes
    if address.len() >= 32
        && address.len() <= 44
        && let Ok(decoded) = bs58::decode(address).into_vec()
        && decoded.len() == 32
    {
        return Some("solana");
    }

    None
}

/// Infers the blockchain from a transaction hash format.
///
/// Returns `Some(chain_name)` if the hash format is unambiguous,
/// or `None` if the format is not recognized.
///
/// # Supported Formats
///
/// - **EVM** (ethereum): `0x` prefix + 64 hex chars (66 total)
/// - **Tron**: 64 hex chars (no prefix)
/// - **Solana**: Base58, 80-90 chars, decodes to 64 bytes
///
/// # Examples
///
/// ```
/// use scope::chains::infer_chain_from_hash;
///
/// // EVM hash
/// let evm_hash = "0xabc123def456789012345678901234567890123456789012345678901234abcd";
/// assert_eq!(infer_chain_from_hash(evm_hash), Some("ethereum"));
///
/// // Tron hash (64 hex chars, no 0x prefix)
/// let tron_hash = "abc123def456789012345678901234567890123456789012345678901234abcd";
/// assert_eq!(infer_chain_from_hash(tron_hash), Some("tron"));
/// ```
pub fn infer_chain_from_hash(hash: &str) -> Option<&'static str> {
    // EVM: 0x prefix, 66 chars total (64 hex + "0x")
    if hash.starts_with("0x")
        && hash.len() == 66
        && hash[2..].chars().all(|c| c.is_ascii_hexdigit())
    {
        return Some("ethereum");
    }

    // Tron: 64 hex chars, no prefix
    if hash.len() == 64 && hash.chars().all(|c| c.is_ascii_hexdigit()) {
        return Some("tron");
    }

    // Solana: base58, 80-90 chars, decodes to 64 bytes
    if hash.len() >= 80
        && hash.len() <= 90
        && let Ok(decoded) = bs58::decode(hash).into_vec()
        && decoded.len() == 64
    {
        return Some("solana");
    }

    None
}

/// Analyze gas usage from a set of transactions.
///
/// Computes statistics on gas consumption, identifies gas-heavy functions,
/// and flags failed transactions.
pub fn analyze_gas_usage(transactions: &[Transaction]) -> GasAnalysis {
    if transactions.is_empty() {
        return GasAnalysis {
            avg_gas_used: 0,
            max_gas_used: 0,
            min_gas_used: 0,
            total_gas_cost_wei: "0".to_string(),
            total_gas_cost_formatted: "0".to_string(),
            tx_count: 0,
            gas_by_function: vec![],
            failed_tx_count: 0,
            wasted_gas: 0,
        };
    }

    let mut total_gas: u64 = 0;
    let mut max_gas: u64 = 0;
    let mut min_gas: u64 = u64::MAX;
    let mut failed_count: u64 = 0;
    let mut wasted_gas: u64 = 0;
    let mut function_gas: std::collections::HashMap<String, (u64, u64)> =
        std::collections::HashMap::new();

    for tx in transactions {
        let gas_used = tx.gas_used.unwrap_or(0);
        total_gas += gas_used;
        if gas_used > max_gas {
            max_gas = gas_used;
        }
        if gas_used < min_gas {
            min_gas = gas_used;
        }

        // Track failed transactions
        if tx.status == Some(false) {
            failed_count += 1;
            wasted_gas += gas_used;
        }

        // Group by function selector
        let selector = if tx.input.len() >= 10 {
            tx.input[..10].to_string()
        } else if tx.input.is_empty() || tx.input == "0x" {
            "transfer()".to_string()
        } else {
            tx.input.clone()
        };

        let entry = function_gas.entry(selector).or_insert((0, 0));
        entry.0 += 1; // call count
        entry.1 += gas_used; // total gas
    }

    let tx_count = transactions.len() as u64;
    let avg_gas = total_gas.checked_div(tx_count).unwrap_or(0);

    if min_gas == u64::MAX {
        min_gas = 0;
    }

    // Sort functions by total gas usage
    let mut gas_by_function: Vec<GasByFunction> = function_gas
        .into_iter()
        .map(|(function, (call_count, total_gas_fn))| GasByFunction {
            function,
            call_count,
            avg_gas: total_gas_fn.checked_div(call_count).unwrap_or(0),
            total_gas: total_gas_fn,
        })
        .collect();
    gas_by_function.sort_by_key(|g| std::cmp::Reverse(g.total_gas));

    // Format total gas cost (rough estimate using average gas price)
    let total_gas_cost_formatted = format!("{} gas units", total_gas);

    GasAnalysis {
        avg_gas_used: avg_gas,
        max_gas_used: max_gas,
        min_gas_used: min_gas,
        total_gas_cost_wei: total_gas.to_string(),
        total_gas_cost_formatted,
        tx_count,
        gas_by_function,
        failed_tx_count: failed_count,
        wasted_gas,
    }
}

// ============================================================================
// Unit Tests
// ============================================================================

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

    #[test]
    fn test_balance_serialization() {
        let balance = Balance {
            raw: "1000000000000000000".to_string(),
            formatted: "1.0".to_string(),
            decimals: 18,
            symbol: "ETH".to_string(),
            usd_value: Some(3500.0),
        };

        let json = serde_json::to_string(&balance).unwrap();
        assert!(json.contains("1000000000000000000"));
        assert!(json.contains("1.0"));
        assert!(json.contains("ETH"));
        assert!(json.contains("3500"));

        let deserialized: Balance = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.raw, balance.raw);
        assert_eq!(deserialized.decimals, 18);
    }

    #[test]
    fn test_balance_without_usd() {
        let balance = Balance {
            raw: "1000000000000000000".to_string(),
            formatted: "1.0".to_string(),
            decimals: 18,
            symbol: "ETH".to_string(),
            usd_value: None,
        };

        let json = serde_json::to_string(&balance).unwrap();
        assert!(!json.contains("usd_value"));
    }

    #[test]
    fn test_transaction_serialization() {
        let tx = Transaction {
            hash: "0xabc123".to_string(),
            block_number: Some(12345678),
            timestamp: Some(1700000000),
            from: "0xfrom".to_string(),
            to: Some("0xto".to_string()),
            value: "1.0".to_string(),
            gas_limit: 21000,
            gas_used: Some(21000),
            gas_price: "20000000000".to_string(),
            nonce: 42,
            input: "0x".to_string(),
            status: Some(true),
        };

        let json = serde_json::to_string(&tx).unwrap();
        assert!(json.contains("0xabc123"));
        assert!(json.contains("12345678"));
        assert!(json.contains("0xfrom"));
        assert!(json.contains("0xto"));

        let deserialized: Transaction = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.hash, tx.hash);
        assert_eq!(deserialized.nonce, 42);
    }

    #[test]
    fn test_pending_transaction_serialization() {
        let tx = Transaction {
            hash: "0xpending".to_string(),
            block_number: None,
            timestamp: None,
            from: "0xfrom".to_string(),
            to: Some("0xto".to_string()),
            value: "1.0".to_string(),
            gas_limit: 21000,
            gas_used: None,
            gas_price: "20000000000".to_string(),
            nonce: 0,
            input: "0x".to_string(),
            status: None,
        };

        let json = serde_json::to_string(&tx).unwrap();
        assert!(json.contains("0xpending"));
        assert!(json.contains("null")); // None values serialize as null

        let deserialized: Transaction = serde_json::from_str(&json).unwrap();
        assert!(deserialized.block_number.is_none());
        assert!(deserialized.status.is_none());
    }

    #[test]
    fn test_contract_creation_transaction() {
        let tx = Transaction {
            hash: "0xcreate".to_string(),
            block_number: Some(100),
            timestamp: Some(1700000000),
            from: "0xdeployer".to_string(),
            to: None, // Contract creation
            value: "0".to_string(),
            gas_limit: 1000000,
            gas_used: Some(500000),
            gas_price: "20000000000".to_string(),
            nonce: 0,
            input: "0x608060...".to_string(),
            status: Some(true),
        };

        let json = serde_json::to_string(&tx).unwrap();
        assert!(json.contains("\"to\":null"));
    }

    #[test]
    fn test_token_serialization() {
        let token = Token {
            contract_address: "0xtoken".to_string(),
            symbol: "USDC".to_string(),
            name: "USD Coin".to_string(),
            decimals: 6,
        };

        let json = serde_json::to_string(&token).unwrap();
        assert!(json.contains("USDC"));
        assert!(json.contains("USD Coin"));
        assert!(json.contains("\"decimals\":6"));

        let deserialized: Token = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.decimals, 6);
    }

    #[test]
    fn test_token_balance_serialization() {
        let token_balance = TokenBalance {
            token: Token {
                contract_address: "0xtoken".to_string(),
                symbol: "USDC".to_string(),
                name: "USD Coin".to_string(),
                decimals: 6,
            },
            balance: "1000000".to_string(),
            formatted_balance: "1.0".to_string(),
            usd_value: Some(1.0),
        };

        let json = serde_json::to_string(&token_balance).unwrap();
        assert!(json.contains("USDC"));
        assert!(json.contains("1000000"));
        assert!(json.contains("1.0"));
    }

    #[test]
    fn test_balance_debug() {
        let balance = Balance {
            raw: "1000".to_string(),
            formatted: "0.001".to_string(),
            decimals: 18,
            symbol: "ETH".to_string(),
            usd_value: None,
        };

        let debug_str = format!("{:?}", balance);
        assert!(debug_str.contains("Balance"));
        assert!(debug_str.contains("1000"));
    }

    #[test]
    fn test_transaction_debug() {
        let tx = Transaction {
            hash: "0xtest".to_string(),
            block_number: Some(1),
            timestamp: Some(0),
            from: "0x1".to_string(),
            to: Some("0x2".to_string()),
            value: "0".to_string(),
            gas_limit: 21000,
            gas_used: Some(21000),
            gas_price: "0".to_string(),
            nonce: 0,
            input: "0x".to_string(),
            status: Some(true),
        };

        let debug_str = format!("{:?}", tx);
        assert!(debug_str.contains("Transaction"));
        assert!(debug_str.contains("0xtest"));
    }

    // ============================================================================
    // Chain Inference Tests
    // ============================================================================

    #[test]
    fn test_infer_chain_from_address_evm() {
        // Valid EVM addresses (0x + 40 hex chars)
        assert_eq!(
            super::infer_chain_from_address("0x742d35Cc6634C0532925a3b844Bc9e7595f1b3c2"),
            Some("ethereum")
        );
        assert_eq!(
            super::infer_chain_from_address("0x0000000000000000000000000000000000000000"),
            Some("ethereum")
        );
        assert_eq!(
            super::infer_chain_from_address("0xABCDEF1234567890abcdef1234567890ABCDEF12"),
            Some("ethereum")
        );
    }

    #[test]
    fn test_infer_chain_from_address_tron() {
        // Valid Tron addresses (T + 33 chars = 34 total, base58)
        assert_eq!(
            super::infer_chain_from_address("TDqSquXBgUCLYvYC4XZgrprLK589dkhSCf"),
            Some("tron")
        );
        assert_eq!(
            super::infer_chain_from_address("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"),
            Some("tron")
        );
    }

    #[test]
    fn test_infer_chain_from_address_solana() {
        // Valid Solana addresses (base58, 32-44 chars, decodes to 32 bytes)
        assert_eq!(
            super::infer_chain_from_address("DRpbCBMxVnDK7maPM5tGv6MvB3v1sRMC86PZ8okm21hy"),
            Some("solana")
        );
        // System program address
        assert_eq!(
            super::infer_chain_from_address("11111111111111111111111111111111"),
            Some("solana")
        );
    }

    #[test]
    fn test_infer_chain_from_address_invalid() {
        // Too short
        assert_eq!(super::infer_chain_from_address("0x123"), None);
        // Invalid characters
        assert_eq!(super::infer_chain_from_address("not_an_address"), None);
        // Empty
        assert_eq!(super::infer_chain_from_address(""), None);
        // EVM-like but wrong length
        assert_eq!(super::infer_chain_from_address("0x123456"), None);
        // Tron-like but not starting with T
        assert_eq!(
            super::infer_chain_from_address("ADqSquXBgUCLYvYC4XZgrprLK589dkhSCf"),
            None
        );
    }

    #[test]
    fn test_infer_chain_from_hash_evm() {
        // Valid EVM transaction hash (0x + 64 hex chars)
        assert_eq!(
            super::infer_chain_from_hash(
                "0xabc123def456789012345678901234567890123456789012345678901234abcd"
            ),
            Some("ethereum")
        );
        assert_eq!(
            super::infer_chain_from_hash(
                "0x0000000000000000000000000000000000000000000000000000000000000000"
            ),
            Some("ethereum")
        );
    }

    #[test]
    fn test_infer_chain_from_hash_tron() {
        // Valid Tron transaction hash (64 hex chars, no 0x prefix)
        assert_eq!(
            super::infer_chain_from_hash(
                "abc123def456789012345678901234567890123456789012345678901234abcd"
            ),
            Some("tron")
        );
        assert_eq!(
            super::infer_chain_from_hash(
                "0000000000000000000000000000000000000000000000000000000000000000"
            ),
            Some("tron")
        );
    }

    #[test]
    fn test_infer_chain_from_hash_solana() {
        // Valid Solana signature (base58, 80-90 chars, decodes to 64 bytes)
        // This is a made-up example that fits the pattern
        let solana_sig = "5VERv8NMvzbJMEkV8xnrLkEaWRtSz9CosKDYjCJjBRnbJLgp8uirBgmQpjKhoR4tjF3ZpRzrFmBV6UjKdiSZkQUW";
        assert_eq!(super::infer_chain_from_hash(solana_sig), Some("solana"));
    }

    #[test]
    fn test_infer_chain_from_hash_invalid() {
        // Too short
        assert_eq!(super::infer_chain_from_hash("0x123"), None);
        // Invalid
        assert_eq!(super::infer_chain_from_hash("not_a_hash"), None);
        // Empty
        assert_eq!(super::infer_chain_from_hash(""), None);
        // 64 chars but with invalid hex (contains 'g')
        assert_eq!(
            super::infer_chain_from_hash(
                "abc123gef456789012345678901234567890123456789012345678901234abcd"
            ),
            None
        );
    }

    // ============================================================================
    // DefaultClientFactory Tests
    // ============================================================================

    #[test]
    fn test_default_client_factory_create_dex_client() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let factory = DefaultClientFactory {
            chains_config: config,
            http,
        };
        let dex = factory.create_dex_client();
        // Just verify it returns without panicking - the client is a Box<dyn DexDataSource>
        let _ = format!("{:?}", std::mem::size_of_val(&dex));
    }

    #[test]
    fn test_default_client_factory_create_ethereum_client() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let factory = DefaultClientFactory {
            chains_config: config,
            http,
        };
        // ethereum, polygon, etc use EthereumClient::for_chain
        let client = factory.create_chain_client("ethereum");
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "ethereum");
    }

    #[test]
    fn test_default_client_factory_create_polygon_client() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let factory = DefaultClientFactory {
            chains_config: config,
            http,
        };
        let client = factory.create_chain_client("polygon");
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "polygon");
    }

    #[test]
    fn test_default_client_factory_create_solana_client() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let factory = DefaultClientFactory {
            chains_config: config,
            http,
        };
        let client = factory.create_chain_client("solana");
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "solana");
    }

    #[test]
    fn test_default_client_factory_create_sol_alias() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let factory = DefaultClientFactory {
            chains_config: config,
            http,
        };
        let client = factory.create_chain_client("sol");
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "solana");
    }

    #[test]
    fn test_default_client_factory_create_tron_client() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let factory = DefaultClientFactory {
            chains_config: config,
            http,
        };
        let client = factory.create_chain_client("tron");
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "tron");
    }

    #[test]
    fn test_default_client_factory_create_trx_alias() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let factory = DefaultClientFactory {
            chains_config: config,
            http,
        };
        let client = factory.create_chain_client("trx");
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "tron");
    }

    #[test]
    fn test_default_client_factory_create_arbitrum_client() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let factory = DefaultClientFactory {
            chains_config: config,
            http,
        };
        let client = factory.create_chain_client("arbitrum");
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "arbitrum");
    }

    #[test]
    fn test_default_client_factory_create_optimism_client() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let factory = DefaultClientFactory {
            chains_config: config,
            http,
        };
        let client = factory.create_chain_client("optimism");
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "optimism");
    }

    #[test]
    fn test_default_client_factory_create_base_client() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let factory = DefaultClientFactory {
            chains_config: config,
            http,
        };
        let client = factory.create_chain_client("base");
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "base");
    }

    #[test]
    fn test_default_client_factory_create_unsupported_chain_returns_err() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let factory = DefaultClientFactory {
            chains_config: config,
            http,
        };
        let client = factory.create_chain_client("avalanche");
        match &client {
            Err(e) => assert!(e.to_string().contains("Unsupported")),
            Ok(_) => panic!("expected Err for unsupported chain"),
        }
    }

    // ============================================================================
    // HttpClient injection tests
    // ============================================================================

    #[test]
    fn test_solana_client_new_with_http() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let client = SolanaClient::new_with_http(&config, http);
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "solana");
    }

    #[test]
    fn test_tron_client_new_with_http() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let client = TronClient::new_with_http(&config, http);
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "tron");
    }

    #[test]
    fn test_dex_client_new_with_http() {
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let client = DexClient::new_with_http(http);
        let _ = format!("{}", std::mem::size_of_val(&client));
    }

    #[test]
    fn test_factory_shares_http_transport() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let weak = Arc::downgrade(&http);
        let factory = DefaultClientFactory {
            chains_config: config,
            http,
        };
        // Factory holds a clone of the Arc; original strong count was 1,
        // factory clone makes 2 minus the 1 we moved = factory has it.
        // Creating clients bumps the count via Arc::clone inside each
        // constructor.
        let _eth = factory.create_chain_client("ethereum").unwrap();
        let _sol = factory.create_chain_client("solana").unwrap();
        let _trx = factory.create_chain_client("tron").unwrap();
        let _dex = factory.create_dex_client();
        // The weak ref should still be valid (Arc not fully dropped)
        assert!(weak.upgrade().is_some());
    }

    #[test]
    fn test_ethereum_client_new_with_http() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let client = EthereumClient::new_with_http(&config, http);
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "ethereum");
    }

    #[test]
    fn test_ethereum_client_for_chain_with_http() {
        let config = crate::config::ChainsConfig::default();
        let http: Arc<dyn HttpClient> = Arc::new(crate::http::NativeHttpClient::new().unwrap());
        let client = EthereumClient::for_chain_with_http("polygon", &config, http);
        assert!(client.is_ok());
        assert_eq!(client.unwrap().chain_name(), "polygon");
    }

    // ============================================================================
    // ChainClient trait default method tests
    // ============================================================================

    #[tokio::test]
    async fn test_chain_client_default_get_token_info() {
        use super::mocks::MockChainClient;
        // Create a client without token_info set (None)
        let client = MockChainClient::new("ethereum", "ETH");
        let result = client.get_token_info("0xsometoken").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_chain_client_default_get_token_holders() {
        use super::mocks::MockChainClient;
        let client = MockChainClient::new("ethereum", "ETH");
        let holders = client.get_token_holders("0xsometoken", 10).await.unwrap();
        assert!(holders.is_empty());
    }

    #[tokio::test]
    async fn test_chain_client_default_get_token_holder_count() {
        use super::mocks::MockChainClient;
        let client = MockChainClient::new("ethereum", "ETH");
        let count = client.get_token_holder_count("0xsometoken").await.unwrap();
        assert_eq!(count, 0);
    }

    #[tokio::test]
    async fn test_mock_client_factory_creates_chain_client() {
        use super::mocks::MockClientFactory;
        let factory = MockClientFactory::new();
        let client = factory.create_chain_client("anything").unwrap();
        assert_eq!(client.chain_name(), "ethereum"); // defaults to ethereum mock
    }

    #[tokio::test]
    async fn test_mock_client_factory_creates_dex_client() {
        use super::mocks::MockClientFactory;
        let factory = MockClientFactory::new();
        let dex = factory.create_dex_client();
        let price = dex.get_token_price("ethereum", "0xtest").await;
        assert_eq!(price, Some(1.0));
    }

    #[tokio::test]
    async fn test_mock_chain_client_balance() {
        use super::mocks::MockChainClient;
        let client = MockChainClient::new("ethereum", "ETH");
        let balance = client.get_balance("0xtest").await.unwrap();
        assert_eq!(balance.formatted, "1.0");
        assert_eq!(balance.symbol, "ETH");
        assert_eq!(balance.usd_value, Some(2500.0));
    }

    #[tokio::test]
    async fn test_mock_chain_client_transaction() {
        use super::mocks::MockChainClient;
        let client = MockChainClient::new("ethereum", "ETH");
        let tx = client.get_transaction("0xanyhash").await.unwrap();
        assert_eq!(tx.hash, "0xmocktx");
        assert_eq!(tx.nonce, 42);
    }

    #[tokio::test]
    async fn test_mock_chain_client_block_number() {
        use super::mocks::MockChainClient;
        let client = MockChainClient::new("ethereum", "ETH");
        let block = client.get_block_number().await.unwrap();
        assert_eq!(block, 12345678);
    }

    #[tokio::test]
    async fn test_mock_dex_source_data() {
        use super::mocks::MockDexSource;
        let dex = MockDexSource::new();
        let data = dex.get_token_data("ethereum", "0xtest").await.unwrap();
        assert_eq!(data.symbol, "MOCK");
        assert_eq!(data.price_usd, 1.0);
    }

    #[tokio::test]
    async fn test_mock_dex_source_search() {
        use super::mocks::MockDexSource;
        let dex = MockDexSource::new();
        let results = dex.search_tokens("test", None).await.unwrap();
        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn test_mock_dex_source_native_price() {
        use super::mocks::MockDexSource;
        let dex = MockDexSource::new();
        let price = dex.get_native_token_price("ethereum").await;
        assert_eq!(price, Some(2500.0));
    }

    // ========================================================================
    // Default ChainClient trait method tests
    // ========================================================================

    /// Minimal ChainClient impl that uses all default methods.
    struct MinimalChainClient;

    #[async_trait::async_trait]
    impl ChainClient for MinimalChainClient {
        fn chain_name(&self) -> &str {
            "test"
        }

        fn native_token_symbol(&self) -> &str {
            "TEST"
        }

        async fn get_balance(&self, _address: &str) -> Result<Balance> {
            Ok(Balance {
                raw: "0".to_string(),
                formatted: "0".to_string(),
                decimals: 18,
                symbol: "TEST".to_string(),
                usd_value: None,
            })
        }

        async fn get_transaction(&self, _hash: &str) -> Result<Transaction> {
            unimplemented!()
        }

        async fn get_transactions(&self, _address: &str, _limit: u32) -> Result<Vec<Transaction>> {
            Ok(Vec::new())
        }

        async fn get_block_number(&self) -> Result<u64> {
            Ok(0)
        }

        async fn get_token_balances(&self, _address: &str) -> Result<Vec<TokenBalance>> {
            Ok(Vec::new())
        }

        async fn enrich_balance_usd(&self, _balance: &mut Balance) {}
    }

    #[tokio::test]
    async fn test_default_get_token_info() {
        let client = MinimalChainClient;
        let result = client.get_token_info("0xtest").await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not supported"));
    }

    #[tokio::test]
    async fn test_default_get_token_holders() {
        let client = MinimalChainClient;
        let holders = client.get_token_holders("0xtest", 10).await.unwrap();
        assert!(holders.is_empty());
    }

    #[tokio::test]
    async fn test_default_get_token_holder_count() {
        let client = MinimalChainClient;
        let count = client.get_token_holder_count("0xtest").await.unwrap();
        assert_eq!(count, 0);
    }

    // ============================================================================
    // Chain Metadata Tests
    // ============================================================================

    #[test]
    fn test_chain_metadata_ethereum() {
        let meta = chain_metadata("ethereum").unwrap();
        assert_eq!(meta.chain_id, "ethereum");
        assert_eq!(meta.native_symbol, "ETH");
        assert_eq!(meta.native_decimals, 18);
        assert_eq!(meta.explorer_token_base, "https://etherscan.io/token");
    }

    #[test]
    fn test_chain_metadata_ethereum_alias() {
        let meta = chain_metadata("eth").unwrap();
        assert_eq!(meta.chain_id, "ethereum");
        assert_eq!(meta.native_symbol, "ETH");
    }

    #[test]
    fn test_chain_metadata_polygon() {
        let meta = chain_metadata("polygon").unwrap();
        assert_eq!(meta.chain_id, "polygon");
        assert_eq!(meta.native_symbol, "MATIC");
        assert_eq!(meta.native_decimals, 18);
        assert_eq!(meta.explorer_token_base, "https://polygonscan.com/token");
    }

    #[test]
    fn test_chain_metadata_bsc() {
        let meta = chain_metadata("bsc").unwrap();
        assert_eq!(meta.chain_id, "bsc");
        assert_eq!(meta.native_symbol, "BNB");
        assert_eq!(meta.native_decimals, 18);
        assert_eq!(meta.explorer_token_base, "https://bscscan.com/token");
    }

    #[test]
    fn test_chain_metadata_solana() {
        let meta = chain_metadata("solana").unwrap();
        assert_eq!(meta.chain_id, "solana");
        assert_eq!(meta.native_symbol, "SOL");
        assert_eq!(meta.native_decimals, 9);
        assert_eq!(meta.explorer_token_base, "https://solscan.io/token");
    }

    #[test]
    fn test_chain_metadata_solana_alias() {
        let meta = chain_metadata("sol").unwrap();
        assert_eq!(meta.chain_id, "solana");
        assert_eq!(meta.native_symbol, "SOL");
    }

    #[test]
    fn test_chain_metadata_tron() {
        let meta = chain_metadata("tron").unwrap();
        assert_eq!(meta.chain_id, "tron");
        assert_eq!(meta.native_symbol, "TRX");
        assert_eq!(meta.native_decimals, 6);
        assert_eq!(meta.explorer_token_base, "https://tronscan.org/#/token20");
    }

    #[test]
    fn test_chain_metadata_tron_alias() {
        let meta = chain_metadata("trx").unwrap();
        assert_eq!(meta.chain_id, "tron");
        assert_eq!(meta.native_symbol, "TRX");
    }

    #[test]
    fn test_chain_metadata_arbitrum() {
        let meta = chain_metadata("arbitrum").unwrap();
        assert_eq!(meta.chain_id, "arbitrum");
        assert_eq!(meta.native_symbol, "ETH");
        assert_eq!(meta.native_decimals, 18);
        assert_eq!(meta.explorer_token_base, "https://arbiscan.io/token");
    }

    #[test]
    fn test_chain_metadata_optimism() {
        let meta = chain_metadata("optimism").unwrap();
        assert_eq!(meta.chain_id, "optimism");
        assert_eq!(meta.native_symbol, "ETH");
        assert_eq!(meta.native_decimals, 18);
        assert_eq!(
            meta.explorer_token_base,
            "https://optimistic.etherscan.io/token"
        );
    }

    #[test]
    fn test_chain_metadata_base() {
        let meta = chain_metadata("base").unwrap();
        assert_eq!(meta.chain_id, "base");
        assert_eq!(meta.native_symbol, "ETH");
        assert_eq!(meta.native_decimals, 18);
        assert_eq!(meta.explorer_token_base, "https://basescan.org/token");
    }

    #[test]
    fn test_chain_metadata_case_insensitive() {
        let meta1 = chain_metadata("ETHEREUM").unwrap();
        let meta2 = chain_metadata("Ethereum").unwrap();
        let meta3 = chain_metadata("ethereum").unwrap();
        assert_eq!(meta1.chain_id, meta2.chain_id);
        assert_eq!(meta2.chain_id, meta3.chain_id);
    }

    #[test]
    fn test_chain_metadata_unknown() {
        assert!(chain_metadata("bitcoin").is_none());
        assert!(chain_metadata("litecoin").is_none());
        assert!(chain_metadata("unknown").is_none());
        assert!(chain_metadata("").is_none());
    }

    #[test]
    fn test_native_symbol_ethereum() {
        assert_eq!(native_symbol("ethereum"), "ETH");
        assert_eq!(native_symbol("eth"), "ETH");
    }

    #[test]
    fn test_native_symbol_polygon() {
        assert_eq!(native_symbol("polygon"), "MATIC");
    }

    #[test]
    fn test_native_symbol_bsc() {
        assert_eq!(native_symbol("bsc"), "BNB");
    }

    #[test]
    fn test_native_symbol_solana() {
        assert_eq!(native_symbol("solana"), "SOL");
        assert_eq!(native_symbol("sol"), "SOL");
    }

    #[test]
    fn test_native_symbol_tron() {
        assert_eq!(native_symbol("tron"), "TRX");
        assert_eq!(native_symbol("trx"), "TRX");
    }

    #[test]
    fn test_native_symbol_arbitrum() {
        assert_eq!(native_symbol("arbitrum"), "ETH");
    }

    #[test]
    fn test_native_symbol_optimism() {
        assert_eq!(native_symbol("optimism"), "ETH");
    }

    #[test]
    fn test_native_symbol_base() {
        assert_eq!(native_symbol("base"), "ETH");
    }

    #[test]
    fn test_native_symbol_unknown() {
        assert_eq!(native_symbol("unknown"), "???");
        assert_eq!(native_symbol("bitcoin"), "???");
        assert_eq!(native_symbol(""), "???");
    }

    #[test]
    fn test_native_symbol_case_insensitive() {
        assert_eq!(native_symbol("ETHEREUM"), "ETH");
        assert_eq!(native_symbol("Ethereum"), "ETH");
        assert_eq!(native_symbol("ethereum"), "ETH");
    }

    #[tokio::test]
    async fn test_chain_client_default_get_code() {
        let client = MinimalChainClient;
        let result = client.get_code("0x1234").await;
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("not supported"));
    }

    // ============================================================================
    // analyze_gas_usage Tests
    // ============================================================================

    fn tx(hash: &str, gas_used: Option<u64>, input: &str, status: Option<bool>) -> Transaction {
        Transaction {
            hash: hash.to_string(),
            block_number: Some(1),
            timestamp: Some(1700000000),
            from: "0xfrom".to_string(),
            to: Some("0xto".to_string()),
            value: "0".to_string(),
            gas_limit: 21000,
            gas_used,
            gas_price: "20000000000".to_string(),
            nonce: 0,
            input: input.to_string(),
            status,
        }
    }

    #[test]
    fn test_analyze_gas_usage_empty_transactions() {
        let txs: Vec<Transaction> = vec![];
        let result = super::analyze_gas_usage(&txs);
        assert_eq!(result.avg_gas_used, 0);
        assert_eq!(result.max_gas_used, 0);
        assert_eq!(result.min_gas_used, 0);
        assert_eq!(result.tx_count, 0);
        assert_eq!(result.failed_tx_count, 0);
        assert_eq!(result.wasted_gas, 0);
        assert!(result.gas_by_function.is_empty());
    }

    #[test]
    fn test_analyze_gas_usage_single_tx() {
        let txs = vec![tx("0x1", Some(100_000), "0x", Some(true))];
        let result = super::analyze_gas_usage(&txs);
        assert_eq!(result.avg_gas_used, 100_000);
        assert_eq!(result.max_gas_used, 100_000);
        assert_eq!(result.min_gas_used, 100_000);
        assert_eq!(result.tx_count, 1);
    }

    #[test]
    fn test_analyze_gas_usage_multiple_txs() {
        let txs = vec![
            tx("0x1", Some(50_000), "0xa9059cbb", Some(true)),
            tx("0x2", Some(150_000), "0xa9059cbb", Some(true)),
            tx("0x3", Some(100_000), "0xa9059cbb", Some(true)),
        ];
        let result = super::analyze_gas_usage(&txs);
        assert_eq!(result.avg_gas_used, 100_000); // (50+150+100)/3
        assert_eq!(result.max_gas_used, 150_000);
        assert_eq!(result.min_gas_used, 50_000);
        assert_eq!(result.tx_count, 3);
    }

    #[test]
    fn test_analyze_gas_usage_failed_tx() {
        let txs = vec![
            tx("0x1", Some(80_000), "0x", Some(true)),
            tx("0x2", Some(120_000), "0x", Some(false)),
        ];
        let result = super::analyze_gas_usage(&txs);
        assert_eq!(result.failed_tx_count, 1);
        assert_eq!(result.wasted_gas, 120_000);
    }

    #[test]
    fn test_analyze_gas_usage_gas_by_function() {
        // Selector is first 10 chars (0x + 8 hex) of input
        let txs = vec![
            tx("0x1", Some(100_000), "0xa9059cbb0000", Some(true)),
            tx("0x2", Some(200_000), "0xa9059cbb0000", Some(true)),
            tx("0x3", Some(50_000), "0x095ea7b30000", Some(true)),
        ];
        let result = super::analyze_gas_usage(&txs);
        assert_eq!(result.gas_by_function.len(), 2);
        let by_sel: std::collections::HashMap<_, _> = result
            .gas_by_function
            .iter()
            .map(|g| (g.function.as_str(), g))
            .collect();
        let transfer = by_sel.get("0xa9059cbb").unwrap();
        assert_eq!(transfer.call_count, 2);
        assert_eq!(transfer.total_gas, 300_000);
        assert_eq!(transfer.avg_gas, 150_000);
        let approve = by_sel.get("0x095ea7b3").unwrap();
        assert_eq!(approve.call_count, 1);
        assert_eq!(approve.total_gas, 50_000);
    }

    #[test]
    fn test_analyze_gas_usage_input_0x_transfer() {
        let txs = vec![tx("0x1", Some(21_000), "0x", Some(true))];
        let result = super::analyze_gas_usage(&txs);
        assert_eq!(result.gas_by_function.len(), 1);
        assert_eq!(result.gas_by_function[0].function, "transfer()");
    }

    #[test]
    fn test_analyze_gas_usage_input_empty_transfer() {
        let txs = vec![tx("0x1", Some(21_000), "", Some(true))];
        let result = super::analyze_gas_usage(&txs);
        assert_eq!(result.gas_by_function.len(), 1);
        assert_eq!(result.gas_by_function[0].function, "transfer()");
    }

    #[test]
    fn test_analyze_gas_usage_gas_used_none() {
        let txs = vec![tx("0x1", None, "0x", Some(true))];
        let result = super::analyze_gas_usage(&txs);
        assert_eq!(result.avg_gas_used, 0);
        assert_eq!(result.max_gas_used, 0);
        assert_eq!(result.min_gas_used, 0);
    }

    #[test]
    fn test_analyze_gas_usage_short_input_uses_full_input_as_selector() {
        let txs = vec![tx("0x1", Some(50_000), "0x1234567", Some(true))];
        let result = super::analyze_gas_usage(&txs);
        assert_eq!(result.gas_by_function.len(), 1);
        assert_eq!(result.gas_by_function[0].function, "0x1234567");
    }
}

// ============================================================================
// Mock Test Utilities
// ============================================================================

/// Test helper module providing mock implementations of chain client traits.
///
/// These mocks are available across all test modules in the crate for
/// end-to-end testing of CLI `run()` functions without network calls.
#[cfg(any(test, feature = "test-support"))]
pub mod mocks {
    use super::*;
    use crate::chains::dex::{DexDataSource, DexTokenData, TokenSearchResult};
    use async_trait::async_trait;

    /// Mock chain client with configurable responses.
    #[derive(Debug, Clone)]
    pub struct MockChainClient {
        pub chain: String,
        pub symbol: String,
        pub balance: Balance,
        pub transaction: Transaction,
        pub transactions: Vec<Transaction>,
        pub token_balances: Vec<TokenBalance>,
        pub block_number: u64,
        pub token_info: Option<Token>,
        pub token_holders: Vec<TokenHolder>,
        pub token_holder_count: u64,
    }

    impl MockChainClient {
        /// Creates a mock client with sensible default test data.
        pub fn new(chain: &str, symbol: &str) -> Self {
            Self {
                chain: chain.to_string(),
                symbol: symbol.to_string(),
                balance: Balance {
                    raw: "1000000000000000000".to_string(),
                    formatted: "1.0".to_string(),
                    decimals: 18,
                    symbol: symbol.to_string(),
                    usd_value: Some(2500.0),
                },
                transaction: Transaction {
                    hash: "0xmocktx".to_string(),
                    block_number: Some(12345678),
                    timestamp: Some(1700000000),
                    from: "0xfrom".to_string(),
                    to: Some("0xto".to_string()),
                    value: "1.0".to_string(),
                    gas_limit: 21000,
                    gas_used: Some(21000),
                    gas_price: "20000000000".to_string(),
                    nonce: 42,
                    input: "0x".to_string(),
                    status: Some(true),
                },
                transactions: vec![],
                token_balances: vec![],
                block_number: 12345678,
                token_info: None,
                token_holders: vec![],
                token_holder_count: 0,
            }
        }
    }

    #[async_trait]
    impl ChainClient for MockChainClient {
        fn chain_name(&self) -> &str {
            &self.chain
        }

        fn native_token_symbol(&self) -> &str {
            &self.symbol
        }

        async fn get_balance(&self, _address: &str) -> Result<Balance> {
            Ok(self.balance.clone())
        }

        async fn enrich_balance_usd(&self, _balance: &mut Balance) {
            // Mock: no-op, balance already has usd_value set
        }

        async fn get_transaction(&self, _hash: &str) -> Result<Transaction> {
            Ok(self.transaction.clone())
        }

        async fn get_transactions(&self, _address: &str, _limit: u32) -> Result<Vec<Transaction>> {
            Ok(self.transactions.clone())
        }

        async fn get_block_number(&self) -> Result<u64> {
            Ok(self.block_number)
        }

        async fn get_token_balances(&self, _address: &str) -> Result<Vec<TokenBalance>> {
            Ok(self.token_balances.clone())
        }

        async fn get_token_info(&self, _address: &str) -> Result<Token> {
            match &self.token_info {
                Some(t) => Ok(t.clone()),
                None => Err(crate::error::ScopeError::Chain(
                    "Token info not available".to_string(),
                )),
            }
        }

        async fn get_token_holders(&self, _address: &str, _limit: u32) -> Result<Vec<TokenHolder>> {
            Ok(self.token_holders.clone())
        }

        async fn get_token_holder_count(&self, _address: &str) -> Result<u64> {
            Ok(self.token_holder_count)
        }
    }

    /// Mock DEX data source with configurable responses.
    #[derive(Debug, Clone)]
    pub struct MockDexSource {
        pub token_price: Option<f64>,
        pub native_price: Option<f64>,
        pub token_data: Option<DexTokenData>,
        pub search_results: Vec<TokenSearchResult>,
    }

    impl Default for MockDexSource {
        fn default() -> Self {
            Self::new()
        }
    }

    impl MockDexSource {
        /// Creates a mock DEX source with default test data.
        pub fn new() -> Self {
            Self {
                token_price: Some(1.0),
                native_price: Some(2500.0),
                token_data: Some(DexTokenData {
                    address: "0xmocktoken".to_string(),
                    symbol: "MOCK".to_string(),
                    name: "Mock Token".to_string(),
                    price_usd: 1.0,
                    price_change_24h: 5.0,
                    price_change_6h: 2.0,
                    price_change_1h: 0.5,
                    price_change_5m: 0.1,
                    volume_24h: 1_000_000.0,
                    volume_6h: 250_000.0,
                    volume_1h: 50_000.0,
                    liquidity_usd: 5_000_000.0,
                    market_cap: Some(100_000_000.0),
                    fdv: Some(200_000_000.0),
                    pairs: vec![],
                    price_history: vec![],
                    volume_history: vec![],
                    total_buys_24h: 500,
                    total_sells_24h: 450,
                    total_buys_6h: 120,
                    total_sells_6h: 110,
                    total_buys_1h: 20,
                    total_sells_1h: 18,
                    earliest_pair_created_at: Some(1690000000),
                    image_url: None,
                    websites: vec![],
                    socials: vec![],
                    dexscreener_url: None,
                }),
                search_results: vec![],
            }
        }
    }

    #[async_trait]
    impl DexDataSource for MockDexSource {
        async fn get_token_price(&self, _chain: &str, _address: &str) -> Option<f64> {
            self.token_price
        }

        async fn get_native_token_price(&self, _chain: &str) -> Option<f64> {
            self.native_price
        }

        async fn get_token_data(&self, _chain: &str, _address: &str) -> Result<DexTokenData> {
            match &self.token_data {
                Some(data) => Ok(data.clone()),
                None => Err(crate::error::ScopeError::NotFound(
                    "No DEX data found".to_string(),
                )),
            }
        }

        async fn search_tokens(
            &self,
            _query: &str,
            _chain: Option<&str>,
        ) -> Result<Vec<TokenSearchResult>> {
            Ok(self.search_results.clone())
        }
    }

    /// Mock client factory that returns pre-configured mock clients.
    pub struct MockClientFactory {
        pub mock_client: MockChainClient,
        pub mock_dex: MockDexSource,
    }

    impl Default for MockClientFactory {
        fn default() -> Self {
            Self::new()
        }
    }

    impl MockClientFactory {
        /// Creates a factory with default mock data for Ethereum.
        pub fn new() -> Self {
            Self {
                mock_client: MockChainClient::new("ethereum", "ETH"),
                mock_dex: MockDexSource::new(),
            }
        }
    }

    impl ChainClientFactory for MockClientFactory {
        fn create_chain_client(&self, _chain: &str) -> Result<Box<dyn ChainClient>> {
            Ok(Box::new(self.mock_client.clone()))
        }

        fn create_dex_client(&self) -> Box<dyn DexDataSource> {
            Box::new(self.mock_dex.clone())
        }

        /// A native transport: the mocks serve chain and DEX data, and
        /// anything else (venues, Etherscan) goes to the given URL.
        fn http(&self) -> Arc<dyn HttpClient> {
            crate::http::shared::fallback()
        }
    }
}