riglr-web-tools 0.3.0

Web-based data tools for riglr agents - Twitter, DexScreener, web search, and more
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
//! DexScreener integration for comprehensive token market data and DEX analytics
//!
//! This module provides production-grade tools for accessing DexScreener data,
//! analyzing token metrics, tracking price movements, and identifying trading opportunities.

use crate::{client::WebClient, error::WebToolError};
use chrono::{DateTime, Utc};
use riglr_macros::tool;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::{debug, info};

/// Configuration for DexScreener API access
#[derive(Debug, Clone)]
pub struct DexScreenerConfig {
    /// API base URL (default: https://api.dexscreener.com/latest)
    pub base_url: String,
    /// Rate limit requests per minute (default: 300)
    pub rate_limit_per_minute: u32,
    /// Timeout for API requests in seconds (default: 30)
    pub request_timeout: u64,
}

/// Comprehensive token information including price, volume, and market data
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct TokenInfo {
    /// Token contract address
    pub address: String,
    /// Token name
    pub name: String,
    /// Token symbol
    pub symbol: String,
    /// Token decimals
    pub decimals: u32,
    /// Current price in USD
    pub price_usd: Option<f64>,
    /// Market capitalization in USD
    pub market_cap: Option<f64>,
    /// 24h trading volume in USD
    pub volume_24h: Option<f64>,
    /// Price change percentage (24h)
    pub price_change_24h: Option<f64>,
    /// Price change percentage (1h)
    pub price_change_1h: Option<f64>,
    /// Price change percentage (5m)
    pub price_change_5m: Option<f64>,
    /// Circulating supply
    pub circulating_supply: Option<f64>,
    /// Total supply
    pub total_supply: Option<f64>,
    /// Number of active trading pairs
    pub pair_count: u32,
    /// Top trading pairs
    pub pairs: Vec<TokenPair>,
    /// Blockchain/chain information
    pub chain: ChainInfo,
    /// Verification status and security info
    pub security: SecurityInfo,
    /// Social and community links
    pub socials: Vec<SocialLink>,
    /// Last update timestamp
    pub updated_at: DateTime<Utc>,
}

/// Trading pair information
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct TokenPair {
    /// Unique pair identifier
    pub pair_id: String,
    /// DEX name (e.g., "Uniswap V3", "PancakeSwap")
    pub dex: DexInfo,
    /// Base token information
    pub base_token: PairToken,
    /// Quote token information
    pub quote_token: PairToken,
    /// Current price
    pub price_usd: Option<f64>,
    /// Price in native token units
    pub price_native: Option<f64>,
    /// 24h trading volume in USD
    pub volume_24h: Option<f64>,
    /// 24h price change percentage
    pub price_change_24h: Option<f64>,
    /// Total liquidity in USD
    pub liquidity_usd: Option<f64>,
    /// Fully diluted valuation
    pub fdv: Option<f64>,
    /// Pair creation timestamp
    pub created_at: Option<DateTime<Utc>>,
    /// Latest trade timestamp
    pub last_trade_at: DateTime<Utc>,
    /// Number of transactions (24h)
    pub txns_24h: TransactionStats,
    /// Pair URL on the DEX
    pub url: String,
}

/// DEX platform information
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct DexInfo {
    /// DEX identifier
    pub id: String,
    /// DEX name
    pub name: String,
    /// DEX URL
    pub url: Option<String>,
    /// DEX logo URL
    pub logo: Option<String>,
}

/// Token information within a pair
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct PairToken {
    /// Token contract address
    pub address: String,
    /// Token name
    pub name: String,
    /// Token symbol
    pub symbol: String,
}

/// Transaction statistics for a trading pair
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct TransactionStats {
    /// Number of buy transactions (24h)
    pub buys: Option<u32>,
    /// Number of sell transactions (24h)
    pub sells: Option<u32>,
    /// Total number of transactions (24h)
    pub total: Option<u32>,
    /// Buy volume in USD (24h)
    pub buy_volume_usd: Option<f64>,
    /// Sell volume in USD (24h)
    pub sell_volume_usd: Option<f64>,
}

/// Blockchain/chain information
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct ChainInfo {
    /// Chain identifier (e.g., "ethereum", "bsc", "polygon")
    pub id: String,
    /// Chain name
    pub name: String,
    /// Chain logo URL
    pub logo: Option<String>,
    /// Native token symbol for this chain
    pub native_token: String,
}

/// Token security and verification information
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct SecurityInfo {
    /// Whether the token contract is verified
    pub is_verified: bool,
    /// Whether liquidity is locked
    pub liquidity_locked: Option<bool>,
    /// Contract audit status
    pub audit_status: Option<String>,
    /// Honeypot detection result
    pub honeypot_status: Option<String>,
    /// Contract ownership status
    pub ownership_status: Option<String>,
    /// Risk score (0-100, lower is better)
    pub risk_score: Option<u32>,
}

/// Social media and community links
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct SocialLink {
    /// Platform name (e.g., "twitter", "telegram", "discord")
    pub platform: String,
    /// Profile URL
    pub url: String,
    /// Follower count (if available)
    pub followers: Option<u32>,
}

/// Market analysis result
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct MarketAnalysis {
    /// Token being analyzed
    pub token: TokenInfo,
    /// Market trend analysis
    pub trend_analysis: TrendAnalysis,
    /// Volume analysis
    pub volume_analysis: VolumeAnalysis,
    /// Liquidity analysis
    pub liquidity_analysis: LiquidityAnalysis,
    /// Price level analysis
    pub price_levels: PriceLevelAnalysis,
    /// Risk assessment
    pub risk_assessment: RiskAssessment,
    /// Analysis timestamp
    pub analyzed_at: DateTime<Utc>,
}

/// Market trend analysis including direction, momentum, and key price levels
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct TrendAnalysis {
    /// Overall trend direction (Bullish, Bearish, Neutral)
    pub direction: String,
    /// Trend strength (1-10)
    pub strength: u32,
    /// Momentum score (-100 to 100)
    pub momentum: f64,
    /// Price velocity (rate of change)
    pub velocity: f64,
    /// Support levels
    pub support_levels: Vec<f64>,
    /// Resistance levels
    pub resistance_levels: Vec<f64>,
}

/// Volume analysis including trends, ratios, and trading activity metrics
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct VolumeAnalysis {
    /// Volume rank among all tokens
    pub volume_rank: Option<u32>,
    /// Volume trend (Increasing, Decreasing, Stable)
    pub volume_trend: String,
    /// Volume/Market Cap ratio
    pub volume_mcap_ratio: Option<f64>,
    /// Average volume (7 days)
    pub avg_volume_7d: Option<f64>,
    /// Volume spike factor (current vs average)
    pub spike_factor: Option<f64>,
}

/// Liquidity analysis including depth, distribution, and price impact calculations
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct LiquidityAnalysis {
    /// Total liquidity across all pairs
    pub total_liquidity_usd: f64,
    /// Liquidity distribution across DEXs
    pub dex_distribution: HashMap<String, f64>,
    /// Price impact for different trade sizes
    pub price_impact: HashMap<String, f64>, // "1k", "10k", "100k" -> impact %
    /// Liquidity depth score (1-100)
    pub depth_score: u32,
}

/// Price level analysis
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct PriceLevelAnalysis {
    /// All-time high price
    pub ath: Option<f64>,
    /// All-time low price
    pub atl: Option<f64>,
    /// Distance from ATH (percentage)
    pub ath_distance_pct: Option<f64>,
    /// Distance from ATL (percentage)
    pub atl_distance_pct: Option<f64>,
    /// 24h high
    pub high_24h: Option<f64>,
    /// 24h low
    pub low_24h: Option<f64>,
    /// Current price position in 24h range (0-1)
    pub range_position: Option<f64>,
}

/// Comprehensive risk assessment including liquidity, volatility, and contract risks
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct RiskAssessment {
    /// Overall risk level (Low, Medium, High, Extreme)
    pub risk_level: String,
    /// Detailed risk factors
    pub risk_factors: Vec<RiskFactor>,
    /// Liquidity risk score (1-100)
    pub liquidity_risk: u32,
    /// Volatility risk score (1-100)
    pub volatility_risk: u32,
    /// Smart contract risk score (1-100)
    pub contract_risk: u32,
}

/// Individual risk factor
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct RiskFactor {
    /// Risk category
    pub category: String,
    /// Risk description
    pub description: String,
    /// Severity (Low, Medium, High)
    pub severity: String,
    /// Impact score (1-100)
    pub impact: u32,
}

/// Token search results with metadata and execution information
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct TokenSearchResult {
    /// Search query used
    pub query: String,
    /// List of tokens found in search
    pub tokens: Vec<TokenInfo>,
    /// Search metadata
    pub metadata: SearchMetadata,
    /// Search timestamp
    pub searched_at: DateTime<Utc>,
}

/// Metadata for search results
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct SearchMetadata {
    /// Number of results found
    pub result_count: u32,
    /// Search execution time (ms)
    pub execution_time_ms: u32,
    /// Whether results were limited
    pub limited: bool,
    /// Suggested alternative queries
    pub suggestions: Vec<String>,
}

impl Default for DexScreenerConfig {
    fn default() -> Self {
        Self {
            base_url: "https://api.dexscreener.com".to_string(),
            rate_limit_per_minute: 300,
            request_timeout: 30,
        }
    }
}

/// Get comprehensive token information from DexScreener
///
/// This tool retrieves detailed token information including price, volume,
/// market cap, trading pairs, and security analysis.
#[tool]
pub async fn get_token_info(
    _context: &riglr_core::provider::ApplicationContext,
    token_address: String,
    chain_id: Option<String>,
    include_pairs: Option<bool>,
    include_security: Option<bool>,
) -> crate::error::Result<TokenInfo> {
    debug!(
        "Fetching token info for address: {} on chain: {:?}",
        token_address,
        chain_id.as_deref().unwrap_or("auto-detect")
    );

    let config = DexScreenerConfig::default();
    let client = WebClient::default();

    // Build API endpoint using new v1 endpoint with chainId
    let chain = chain_id.unwrap_or_else(|| "ethereum".to_string());
    let url = if include_pairs.unwrap_or(true) {
        format!("{}/tokens/v1/{}/{}", config.base_url, chain, token_address)
    } else {
        format!(
            "{}/tokens/v1/{}/{}?fields=basic",
            config.base_url, chain, token_address
        )
    };

    // Make API request
    let response = client.get(&url).await.map_err(|e| {
        if e.to_string().contains("timeout") || e.to_string().contains("connection") {
            WebToolError::Network(format!("Failed to fetch token info: {}", e))
        } else {
            WebToolError::Api(format!("Failed to fetch token info: {}", e))
        }
    })?;

    // Parse response (simplified - would parse actual DexScreener JSON)
    let token_info = parse_token_response(&response, &token_address, &chain, include_security)
        .await
        .map_err(|e| WebToolError::Api(format!("Failed to parse token response: {}", e)))?;

    info!(
        "Retrieved token info for {} ({}): ${:.6}",
        token_info.symbol,
        token_info.name,
        token_info.price_usd.unwrap_or(0.0)
    );

    Ok(token_info)
}

/// Search for tokens on DexScreener
///
/// This tool searches for tokens by name, symbol, or address
/// with support for filtering by chain and market cap.
#[tool]
pub async fn search_tokens(
    _context: &riglr_core::provider::ApplicationContext,
    query: String,
    chain_filter: Option<String>,
    min_market_cap: Option<f64>,
    min_liquidity: Option<f64>,
    limit: Option<u32>,
) -> crate::error::Result<TokenSearchResult> {
    debug!("Searching tokens for query: '{}' with filters", query);

    let config = DexScreenerConfig::default();
    let client = WebClient::default();

    // Build search parameters
    let mut params = HashMap::new();
    params.insert("q".to_string(), query.clone());

    if let Some(chain) = chain_filter {
        params.insert("chain".to_string(), chain);
    }

    if let Some(min_mc) = min_market_cap {
        params.insert("min_market_cap".to_string(), min_mc.to_string());
    }

    if let Some(min_liq) = min_liquidity {
        params.insert("min_liquidity".to_string(), min_liq.to_string());
    }

    params.insert("limit".to_string(), limit.unwrap_or(20).to_string());

    // Make search request
    let url = format!("{}/dex/search", config.base_url);
    let response = client
        .get_with_params(&url, &params)
        .await
        .map_err(|e| WebToolError::Network(format!("Search request failed: {}", e)))?;

    // Parse search results
    let tokens = parse_search_results(&response)
        .await
        .map_err(|e| WebToolError::Api(format!("Failed to parse search results: {}", e)))?;

    let result = TokenSearchResult {
        query: query.clone(),
        tokens: tokens.clone(),
        metadata: SearchMetadata {
            result_count: tokens.len() as u32,
            execution_time_ms: 150, // Would measure actual time
            limited: tokens.len() >= limit.unwrap_or(20) as usize,
            suggestions: vec![], // Would provide from API
        },
        searched_at: Utc::now(),
    };

    info!(
        "Token search completed: {} results for '{}'",
        result.tokens.len(),
        query
    );

    Ok(result)
}

/// Get trending tokens from DexScreener
///
/// This tool retrieves trending tokens based on volume,
/// price changes, and social activity.
#[tool]
pub async fn get_trending_tokens(
    _context: &riglr_core::provider::ApplicationContext,
    time_window: Option<String>, // "5m", "1h", "24h"
    chain_filter: Option<String>,
    min_volume: Option<f64>,
    limit: Option<u32>,
) -> crate::error::Result<Vec<TokenInfo>> {
    debug!(
        "Fetching trending tokens for window: {:?}",
        time_window.as_deref().unwrap_or("1h")
    );

    let config = DexScreenerConfig::default();
    let client = WebClient::default();

    // Build trending endpoint
    let window = time_window.unwrap_or_else(|| "1h".to_string());
    let mut params = HashMap::new();
    params.insert("window".to_string(), window);
    params.insert("limit".to_string(), limit.unwrap_or(50).to_string());

    if let Some(chain) = chain_filter {
        params.insert("chain".to_string(), chain);
    }

    if let Some(min_vol) = min_volume {
        params.insert("min_volume".to_string(), min_vol.to_string());
    }

    let url = format!("{}/dex/tokens/trending", config.base_url);
    let response = client
        .get_with_params(&url, &params)
        .await
        .map_err(|e| WebToolError::Network(format!("Failed to fetch trending tokens: {}", e)))?;

    let trending_tokens = parse_trending_response(&response)
        .await
        .map_err(|e| WebToolError::Api(format!("Failed to parse trending response: {}", e)))?;

    info!("Retrieved {} trending tokens", trending_tokens.len());

    Ok(trending_tokens)
}

/// Analyze token market data using heuristic-based calculations
///
/// This tool provides market analysis based on available on-chain data including:
/// - Simple trend analysis based on price changes
/// - Volume pattern calculations from 24h data
/// - Liquidity assessment from DEX pairs
/// - Basic risk evaluation using heuristic scoring
///
/// Note: This is not a substitute for professional financial analysis or
/// machine learning models. All calculations are rule-based heuristics.
#[tool]
pub async fn analyze_token_market(
    _context: &riglr_core::provider::ApplicationContext,
    token_address: String,
    chain_id: Option<String>,
    _include_technical: Option<bool>,
    include_risk: Option<bool>,
) -> crate::error::Result<MarketAnalysis> {
    debug!("Performing market analysis for token: {}", token_address);

    // Get basic token info first
    let token_info = get_token_info(
        _context,
        token_address.clone(),
        chain_id,
        Some(true),
        include_risk,
    )
    .await?;

    // Perform trend analysis
    let trend_analysis = analyze_price_trends(&token_info)
        .await
        .map_err(|e| WebToolError::Api(format!("Trend analysis failed: {}", e)))?;

    // Analyze volume patterns
    let volume_analysis = analyze_volume_patterns(&token_info)
        .await
        .map_err(|e| WebToolError::Api(format!("Volume analysis failed: {}", e)))?;

    // Assess liquidity
    let liquidity_analysis = analyze_liquidity(&token_info)
        .await
        .map_err(|e| WebToolError::Api(format!("Liquidity analysis failed: {}", e)))?;

    // Analyze price levels
    let price_levels = analyze_price_levels(&token_info)
        .await
        .map_err(|e| WebToolError::Api(format!("Price level analysis failed: {}", e)))?;

    // Perform risk assessment
    let risk_assessment = if include_risk.unwrap_or(true) {
        assess_token_risks(&token_info)
            .await
            .map_err(|e| WebToolError::Api(format!("Risk assessment failed: {}", e)))?
    } else {
        RiskAssessment {
            risk_level: "Unknown".to_string(),
            risk_factors: vec![],
            liquidity_risk: 50,
            volatility_risk: 50,
            contract_risk: 50,
        }
    };

    let analysis = MarketAnalysis {
        token: token_info.clone(),
        trend_analysis,
        volume_analysis,
        liquidity_analysis,
        price_levels,
        risk_assessment,
        analyzed_at: Utc::now(),
    };

    info!(
        "Market analysis completed for {} - Risk: {}, Trend: {}",
        token_info.symbol, analysis.risk_assessment.risk_level, analysis.trend_analysis.direction
    );

    Ok(analysis)
}

/// Get top DEX pairs by volume across all chains
///
/// This tool retrieves the highest volume trading pairs,
/// useful for identifying active markets and arbitrage opportunities.
#[tool]
pub async fn get_top_pairs(
    _context: &riglr_core::provider::ApplicationContext,
    time_window: Option<String>, // "5m", "1h", "24h"
    chain_filter: Option<String>,
    dex_filter: Option<String>,
    min_liquidity: Option<f64>,
    limit: Option<u32>,
) -> crate::error::Result<Vec<TokenPair>> {
    debug!(
        "Fetching top pairs for window: {:?}",
        time_window.as_deref().unwrap_or("24h")
    );

    let config = DexScreenerConfig::default();
    let client = WebClient::default();

    let mut params = HashMap::new();
    params.insert("sort".to_string(), "volume".to_string());
    params.insert(
        "window".to_string(),
        time_window.unwrap_or_else(|| "24h".to_string()),
    );
    params.insert("limit".to_string(), limit.unwrap_or(100).to_string());

    if let Some(chain) = chain_filter {
        params.insert("chain".to_string(), chain);
    }

    if let Some(dex) = dex_filter {
        params.insert("dex".to_string(), dex);
    }

    if let Some(min_liq) = min_liquidity {
        params.insert("min_liquidity".to_string(), min_liq.to_string());
    }

    let url = format!("{}/dex/pairs/top", config.base_url);
    let response = client
        .get_with_params(&url, &params)
        .await
        .map_err(|e| WebToolError::Network(format!("Failed to fetch top pairs: {}", e)))?;

    let pairs = parse_pairs_response(&response)
        .await
        .map_err(|e| WebToolError::Api(format!("Failed to parse pairs response: {}", e)))?;

    info!("Retrieved {} top trading pairs", pairs.len());

    Ok(pairs)
}

/// Get latest token profiles from DexScreener
///
/// This tool retrieves the latest token profiles with social links
/// and metadata from the DexScreener platform.
#[tool]
pub async fn get_latest_token_profiles(
    _context: &riglr_core::provider::ApplicationContext,
    limit: Option<u32>,
) -> crate::error::Result<Vec<crate::dexscreener_api::TokenProfile>> {
    debug!("Fetching latest token profiles");

    let profiles = crate::dexscreener_api::get_latest_token_profiles()
        .await
        .map_err(|e| WebToolError::Api(format!("Failed to fetch token profiles: {}", e)))?;

    let limited_profiles = if let Some(limit) = limit {
        profiles.into_iter().take(limit as usize).collect()
    } else {
        profiles
    };

    info!("Retrieved {} token profiles", limited_profiles.len());

    Ok(limited_profiles)
}

/// Get latest boosted tokens from DexScreener
///
/// This tool retrieves tokens that have been recently boosted
/// on the DexScreener platform.
#[tool]
pub async fn get_latest_boosted_tokens(
    _context: &riglr_core::provider::ApplicationContext,
    limit: Option<u32>,
) -> crate::error::Result<Vec<crate::dexscreener_api::BoostsResponse>> {
    debug!("Fetching latest boosted tokens");

    let boosts = crate::dexscreener_api::get_latest_token_boosts()
        .await
        .map_err(|e| WebToolError::Api(format!("Failed to fetch latest boosts: {}", e)))?;

    let limited_boosts = if let Some(limit) = limit {
        boosts.into_iter().take(limit as usize).collect()
    } else {
        boosts
    };

    info!("Retrieved {} boosted tokens", limited_boosts.len());

    Ok(limited_boosts)
}

/// Get top boosted tokens from DexScreener
///
/// This tool retrieves tokens with the most active boosts
/// on the DexScreener platform.
#[tool]
pub async fn get_top_boosted_tokens(
    _context: &riglr_core::provider::ApplicationContext,
    limit: Option<u32>,
) -> crate::error::Result<Vec<crate::dexscreener_api::BoostsResponse>> {
    debug!("Fetching top boosted tokens");

    let boosts = crate::dexscreener_api::get_top_token_boosts()
        .await
        .map_err(|e| WebToolError::Api(format!("Failed to fetch top boosts: {}", e)))?;

    let limited_boosts = if let Some(limit) = limit {
        boosts.into_iter().take(limit as usize).collect()
    } else {
        boosts
    };

    info!("Retrieved {} top boosted tokens", limited_boosts.len());

    Ok(limited_boosts)
}

/// Check orders for a specific token
///
/// This tool retrieves paid orders for a token including
/// token profiles, community takeovers, and ads.
#[tool]
pub async fn check_token_orders(
    _context: &riglr_core::provider::ApplicationContext,
    chain_id: String,
    token_address: String,
) -> crate::error::Result<Vec<crate::dexscreener_api::Order>> {
    debug!(
        "Checking orders for token {} on chain {}",
        token_address, chain_id
    );

    let orders = crate::dexscreener_api::get_token_orders(&chain_id, &token_address)
        .await
        .map_err(|e| WebToolError::Api(format!("Failed to fetch token orders: {}", e)))?;

    info!(
        "Retrieved {} orders for token {} on {}",
        orders.len(),
        token_address,
        chain_id
    );

    Ok(orders)
}

async fn parse_token_response(
    response: &str,
    token_address: &str,
    chain: &str,
    _include_security: Option<bool>,
) -> crate::error::Result<TokenInfo> {
    // Parse actual DexScreener JSON response into raw structs
    let dex_response_raw: crate::dexscreener_api::DexScreenerResponseRaw =
        serde_json::from_str(response).map_err(|e| {
            crate::error::WebToolError::Parsing(format!(
                "Failed to parse DexScreener response: {}",
                e
            ))
        })?;

    // Convert raw to clean types
    let dex_response: crate::dexscreener_api::DexScreenerResponse = dex_response_raw.into();

    debug!("Parsed response with {} pairs", dex_response.pairs.len());

    // Use the aggregate_token_info helper to create TokenInfo from clean pairs
    let token_info_opt =
        crate::dexscreener_api::aggregate_token_info(dex_response.pairs, token_address);

    if let Some(mut token_info) = token_info_opt {
        // Override chain info with the provided chain parameter
        token_info.chain = ChainInfo {
            id: chain.to_string(),
            name: format_chain_name(chain),
            logo: None,
            native_token: get_native_token(chain),
        };
        return Ok(token_info);
    }

    Err(crate::error::WebToolError::Api(format!(
        "No pairs found for token address: {}",
        token_address
    )))
}

/// Format a DEX ID into a human-readable display name
pub fn format_dex_name(dex_id: &str) -> String {
    match dex_id {
        "uniswap" => "Uniswap V2".to_string(),
        "uniswapv3" => "Uniswap V3".to_string(),
        "pancakeswap" => "PancakeSwap".to_string(),
        "sushiswap" => "SushiSwap".to_string(),
        "curve" => "Curve".to_string(),
        "balancer" => "Balancer".to_string(),
        "quickswap" => "QuickSwap".to_string(),
        "raydium" => "Raydium".to_string(),
        "orca" => "Orca".to_string(),
        _ => dex_id.to_string(),
    }
}

/// Format a chain ID into a human-readable display name
pub fn format_chain_name(chain_id: &str) -> String {
    match chain_id {
        "ethereum" => "Ethereum".to_string(),
        "bsc" => "Binance Smart Chain".to_string(),
        "polygon" => "Polygon".to_string(),
        "arbitrum" => "Arbitrum".to_string(),
        "optimism" => "Optimism".to_string(),
        "avalanche" => "Avalanche".to_string(),
        "fantom" => "Fantom".to_string(),
        "solana" => "Solana".to_string(),
        _ => chain_id.to_string(),
    }
}

/// Get the native token symbol for a given blockchain
pub fn get_native_token(chain_id: &str) -> String {
    match chain_id {
        "ethereum" => "ETH".to_string(),
        "bsc" => "BNB".to_string(),
        "polygon" => "MATIC".to_string(),
        "arbitrum" => "ETH".to_string(),
        "optimism" => "ETH".to_string(),
        "avalanche" => "AVAX".to_string(),
        "fantom" => "FTM".to_string(),
        "solana" => "SOL".to_string(),
        _ => "NATIVE".to_string(),
    }
}

/// Parse search results from DexScreener API
async fn parse_search_results(response: &str) -> crate::error::Result<Vec<TokenInfo>> {
    // Parse into raw response and convert to clean types
    let dex_response_raw: crate::dexscreener_api::DexScreenerResponseRaw =
        serde_json::from_str(response)
            .map_err(|e| crate::error::WebToolError::Parsing(e.to_string()))?;
    let dex_response: crate::dexscreener_api::DexScreenerResponse = dex_response_raw.into();

    // Group pairs by token and aggregate data
    let mut tokens_map = std::collections::HashMap::new();

    for pair in dex_response.pairs {
        let token_address = pair.base_token.address.clone();
        let entry = tokens_map
            .entry(token_address.clone())
            .or_insert(TokenInfo {
                address: token_address,
                name: pair.base_token.name.clone(),
                symbol: pair.base_token.symbol.clone(),
                decimals: 18, // Default, as DexScreener doesn't provide this
                price_usd: pair.price_usd,
                market_cap: pair.market_cap,
                volume_24h: pair.volume.as_ref().and_then(|v| v.h24),
                price_change_24h: pair.price_change.as_ref().and_then(|pc| pc.h24),
                price_change_1h: pair.price_change.as_ref().and_then(|pc| pc.h1),
                price_change_5m: pair.price_change.as_ref().and_then(|pc| pc.m5),
                circulating_supply: None,
                total_supply: None,
                pair_count: 0,
                pairs: vec![],
                chain: ChainInfo {
                    id: pair.chain_id.clone(),
                    name: pair.chain_id.clone(), // Using chain_id as name for simplicity
                    logo: None,
                    native_token: "ETH".to_string(), // Default to ETH for simplicity
                },
                security: SecurityInfo {
                    is_verified: false,
                    liquidity_locked: None,
                    audit_status: None,
                    honeypot_status: None,
                    ownership_status: None,
                    risk_score: None,
                },
                socials: vec![],
                updated_at: chrono::Utc::now(),
            });

        // Add this pair to the token's pairs list
        entry.pairs.push(TokenPair {
            pair_id: pair.pair_address.clone(),
            dex: DexInfo {
                id: pair.dex_id.clone(),
                name: pair.dex_id.clone(), // Using dex_id as name for simplicity
                url: Some(pair.url.clone()),
                logo: None,
            },
            base_token: PairToken {
                address: pair.base_token.address.clone(),
                name: pair.base_token.name.clone(),
                symbol: pair.base_token.symbol.clone(),
            },
            quote_token: PairToken {
                address: pair.quote_token.address.clone(),
                name: pair.quote_token.name.clone(),
                symbol: pair.quote_token.symbol.clone(),
            },
            price_usd: pair.price_usd,
            price_native: Some(pair.price_native),
            volume_24h: pair.volume.and_then(|v| v.h24),
            price_change_24h: pair.price_change.and_then(|pc| pc.h24),
            liquidity_usd: pair.liquidity.and_then(|l| l.usd),
            fdv: pair.fdv,
            created_at: None,
            last_trade_at: chrono::Utc::now(),
            txns_24h: TransactionStats {
                buys: pair
                    .txns
                    .as_ref()
                    .and_then(|t| t.h24.as_ref().and_then(|h| h.buys.map(|b| b as u32))),
                sells: pair
                    .txns
                    .as_ref()
                    .and_then(|t| t.h24.as_ref().and_then(|h| h.sells.map(|s| s as u32))),
                total: None,
                buy_volume_usd: None,
                sell_volume_usd: None,
            },
            url: pair.url,
        });
        entry.pair_count += 1;
    }

    Ok(tokens_map.into_values().collect())
}

async fn parse_trending_response(response: &str) -> crate::error::Result<Vec<TokenInfo>> {
    // Same as search results for now
    parse_search_results(response).await
}

/// Parse trading pairs response
async fn parse_pairs_response(response: &str) -> crate::error::Result<Vec<TokenPair>> {
    // Parse into raw response and convert to clean types
    let dex_response_raw: crate::dexscreener_api::DexScreenerResponseRaw =
        serde_json::from_str(response)
            .map_err(|e| crate::error::WebToolError::Parsing(e.to_string()))?;
    let dex_response: crate::dexscreener_api::DexScreenerResponse = dex_response_raw.into();

    let pairs: Vec<TokenPair> = dex_response
        .pairs
        .into_iter()
        .map(|pair| TokenPair {
            pair_id: pair.pair_address.clone(),
            dex: DexInfo {
                id: pair.dex_id.clone(),
                name: pair.dex_id.clone(),
                url: Some(pair.url.clone()),
                logo: None,
            },
            base_token: PairToken {
                address: pair.base_token.address.clone(),
                name: pair.base_token.name.clone(),
                symbol: pair.base_token.symbol.clone(),
            },
            quote_token: PairToken {
                address: pair.quote_token.address.clone(),
                name: pair.quote_token.name.clone(),
                symbol: pair.quote_token.symbol.clone(),
            },
            price_usd: pair.price_usd,
            price_native: Some(pair.price_native),
            volume_24h: pair.volume.and_then(|v| v.h24),
            price_change_24h: pair.price_change.and_then(|pc| pc.h24),
            liquidity_usd: pair.liquidity.and_then(|l| l.usd),
            fdv: pair.fdv,
            created_at: None,
            last_trade_at: chrono::Utc::now(),
            txns_24h: TransactionStats {
                buys: pair
                    .txns
                    .as_ref()
                    .and_then(|t| t.h24.as_ref().and_then(|h| h.buys.map(|b| b as u32))),
                sells: pair
                    .txns
                    .as_ref()
                    .and_then(|t| t.h24.as_ref().and_then(|h| h.sells.map(|s| s as u32))),
                total: None,
                buy_volume_usd: None,
                sell_volume_usd: None,
            },
            url: pair.url,
        })
        .collect();

    Ok(pairs)
}

async fn analyze_price_trends(token: &TokenInfo) -> crate::error::Result<TrendAnalysis> {
    // Calculate trend based on available price change data
    let price_change_24h = token.price_change_24h;
    let price_change_1h = token.price_change_1h;

    let direction = match price_change_24h {
        Some(change) if change > 5.0 => "Bullish",
        Some(change) if change < -5.0 => "Bearish",
        Some(_) => "Neutral",
        None => "Unknown",
    }
    .to_string();

    let strength =
        price_change_24h.map_or(5, |change| ((change.abs() / 10.0).clamp(1.0, 10.0)) as u32); // Default to medium strength if no data

    // Calculate momentum and velocity only if we have data
    let momentum = match (price_change_1h, price_change_24h) {
        (Some(h1), Some(h24)) => h1 * 24.0 - h24, // Acceleration
        _ => 0.0,
    };

    let velocity = price_change_24h.map_or(0.0, |c| c / 24.0);

    // Simple support/resistance based on recent range, if price available
    let (support_levels, resistance_levels) = if let Some(price) = token.price_usd {
        // Basic 5% bands - in production would use actual order book data
        (vec![price * 0.95], vec![price * 1.05])
    } else {
        (vec![], vec![])
    };

    Ok(TrendAnalysis {
        direction,
        strength,
        momentum,
        velocity,
        support_levels,
        resistance_levels,
    })
}

async fn analyze_volume_patterns(token: &TokenInfo) -> crate::error::Result<VolumeAnalysis> {
    // Calculate volume metrics from available data
    let volume_mcap_ratio = match (token.volume_24h, token.market_cap) {
        (Some(vol), Some(mcap)) if mcap > 0.0 => Some(vol / mcap),
        _ => None,
    };

    // We don't have historical data, so we can't determine trend or averages
    // In production, this would query historical data from the API
    Ok(VolumeAnalysis {
        volume_rank: None,                   // Requires comparison with other tokens
        volume_trend: "Unknown".to_string(), // Requires historical data
        volume_mcap_ratio,
        avg_volume_7d: None, // Requires 7-day historical data
        spike_factor: None,  // Requires average to compare against
    })
}

async fn analyze_liquidity(token: &TokenInfo) -> crate::error::Result<LiquidityAnalysis> {
    let total_liquidity = token
        .pairs
        .iter()
        .map(|p| p.liquidity_usd.unwrap_or(0.0))
        .sum();

    let mut dex_distribution = HashMap::new();
    for pair in &token.pairs {
        let current = dex_distribution.get(&pair.dex.name).unwrap_or(&0.0);
        dex_distribution.insert(
            pair.dex.name.clone(),
            current + pair.liquidity_usd.unwrap_or(0.0),
        );
    }

    let mut price_impact = HashMap::new();
    price_impact.insert("1k".to_string(), 0.1);
    price_impact.insert("10k".to_string(), 0.5);
    price_impact.insert("100k".to_string(), 2.0);

    Ok(LiquidityAnalysis {
        total_liquidity_usd: total_liquidity,
        dex_distribution,
        price_impact,
        depth_score: if total_liquidity > 1_000_000.0 {
            85
        } else {
            60
        },
    })
}

async fn analyze_price_levels(token: &TokenInfo) -> crate::error::Result<PriceLevelAnalysis> {
    // We don't have historical ATH/ATL data from the API
    // Only return what we can actually calculate from available data

    // Try to estimate 24h high/low from price and price change
    let (high_24h, low_24h, range_position) = if let Some(price) = token.price_usd {
        // If we have price change %, estimate the range
        match token.price_change_24h {
            Some(change_pct) => {
                // Rough estimate: if price went up X%, low was price/(1+X/100)
                let change_factor = 1.0 + (change_pct / 100.0);
                if change_pct > 0.0 {
                    let low = price / change_factor;
                    (Some(price), Some(low), Some(1.0)) // Currently at high
                } else {
                    let high = price / change_factor;
                    (Some(high), Some(price), Some(0.0)) // Currently at low
                }
            }
            None => (None, None, None),
        }
    } else {
        (None, None, None)
    };

    Ok(PriceLevelAnalysis {
        ath: None, // Requires historical data not available from current API
        atl: None, // Requires historical data not available from current API
        ath_distance_pct: None,
        atl_distance_pct: None,
        high_24h,
        low_24h,
        range_position,
    })
}

async fn assess_token_risks(token: &TokenInfo) -> crate::error::Result<RiskAssessment> {
    let mut risk_factors = vec![];
    let mut total_risk = 0;

    // Check liquidity risk
    let liquidity_score = if token
        .pairs
        .iter()
        .map(|p| p.liquidity_usd.unwrap_or(0.0))
        .sum::<f64>()
        < 100_000.0
    {
        risk_factors.push(RiskFactor {
            category: "Liquidity".to_string(),
            description: "Low liquidity may cause high price impact".to_string(),
            severity: "High".to_string(),
            impact: 75,
        });
        75
    } else {
        25
    };
    total_risk += liquidity_score;

    // Check contract verification
    let contract_score = if !token.security.is_verified {
        risk_factors.push(RiskFactor {
            category: "Contract".to_string(),
            description: "Contract is not verified".to_string(),
            severity: "High".to_string(),
            impact: 80,
        });
        80
    } else {
        20
    };
    total_risk += contract_score;

    // Check volatility
    let volatility_score = match token.price_change_24h {
        Some(change) if change.abs() > 20.0 => {
            risk_factors.push(RiskFactor {
                category: "Volatility".to_string(),
                description: "High price volatility detected".to_string(),
                severity: "Medium".to_string(),
                impact: 60,
            });
            60
        }
        Some(_) => 30, // Normal volatility
        None => {
            risk_factors.push(RiskFactor {
                category: "Data".to_string(),
                description: "Volatility data unavailable".to_string(),
                severity: "Low".to_string(),
                impact: 20,
            });
            20
        }
    };
    total_risk += volatility_score;

    let avg_risk = total_risk / 3;
    let risk_level = match avg_risk {
        0..=25 => "Low",
        26..=50 => "Medium",
        51..=75 => "High",
        _ => "Extreme",
    }
    .to_string();

    Ok(RiskAssessment {
        risk_level,
        risk_factors,
        liquidity_risk: liquidity_score as u32,
        volatility_risk: volatility_score as u32,
        contract_risk: contract_score as u32,
    })
}

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

    #[test]
    fn test_dexscreener_config_default() {
        let config = DexScreenerConfig::default();
        assert_eq!(config.base_url, "https://api.dexscreener.com");
        assert_eq!(config.rate_limit_per_minute, 300);
    }

    #[test]
    fn test_token_info_serialization() {
        let token = TokenInfo {
            address: "0x123".to_string(),
            name: "Test Token".to_string(),
            symbol: "TEST".to_string(),
            decimals: 18,
            price_usd: Some(1.0),
            market_cap: Some(1000000.0),
            volume_24h: Some(50000.0),
            price_change_24h: Some(5.0),
            price_change_1h: Some(-1.0),
            price_change_5m: Some(0.5),
            circulating_supply: Some(1000000.0),
            total_supply: Some(10000000.0),
            pair_count: 1,
            pairs: vec![],
            chain: ChainInfo {
                id: "ethereum".to_string(),
                name: "Ethereum".to_string(),
                logo: None,
                native_token: "ETH".to_string(),
            },
            security: SecurityInfo {
                is_verified: true,
                liquidity_locked: Some(true),
                audit_status: None,
                honeypot_status: None,
                ownership_status: None,
                risk_score: Some(25),
            },
            socials: vec![],
            updated_at: Utc::now(),
        };

        let json = serde_json::to_string(&token).unwrap();
        assert!(json.contains("Test Token"));
    }

    #[test]
    fn test_dexscreener_config_custom_values() {
        let config = DexScreenerConfig {
            base_url: "https://custom.api.com".to_string(),
            rate_limit_per_minute: 100,
            request_timeout: 60,
        };
        assert_eq!(config.base_url, "https://custom.api.com");
        assert_eq!(config.rate_limit_per_minute, 100);
        assert_eq!(config.request_timeout, 60);
    }

    #[test]
    fn test_format_dex_name_when_known_dex_should_return_formatted_name() {
        assert_eq!(format_dex_name("uniswap"), "Uniswap V2");
        assert_eq!(format_dex_name("uniswapv3"), "Uniswap V3");
        assert_eq!(format_dex_name("pancakeswap"), "PancakeSwap");
        assert_eq!(format_dex_name("sushiswap"), "SushiSwap");
        assert_eq!(format_dex_name("curve"), "Curve");
        assert_eq!(format_dex_name("balancer"), "Balancer");
        assert_eq!(format_dex_name("quickswap"), "QuickSwap");
        assert_eq!(format_dex_name("raydium"), "Raydium");
        assert_eq!(format_dex_name("orca"), "Orca");
    }

    #[test]
    fn test_format_dex_name_when_unknown_dex_should_return_original() {
        assert_eq!(format_dex_name("unknown-dex"), "unknown-dex");
        assert_eq!(format_dex_name("custom_dex"), "custom_dex");
        assert_eq!(format_dex_name(""), "");
    }

    #[test]
    fn test_format_chain_name_when_known_chain_should_return_formatted_name() {
        assert_eq!(format_chain_name("ethereum"), "Ethereum");
        assert_eq!(format_chain_name("bsc"), "Binance Smart Chain");
        assert_eq!(format_chain_name("polygon"), "Polygon");
        assert_eq!(format_chain_name("arbitrum"), "Arbitrum");
        assert_eq!(format_chain_name("optimism"), "Optimism");
        assert_eq!(format_chain_name("avalanche"), "Avalanche");
        assert_eq!(format_chain_name("fantom"), "Fantom");
        assert_eq!(format_chain_name("solana"), "Solana");
    }

    #[test]
    fn test_format_chain_name_when_unknown_chain_should_return_original() {
        assert_eq!(format_chain_name("unknown-chain"), "unknown-chain");
        assert_eq!(format_chain_name("custom_chain"), "custom_chain");
        assert_eq!(format_chain_name(""), "");
    }

    #[test]
    fn test_get_native_token_when_known_chain_should_return_correct_token() {
        assert_eq!(get_native_token("ethereum"), "ETH");
        assert_eq!(get_native_token("bsc"), "BNB");
        assert_eq!(get_native_token("polygon"), "MATIC");
        assert_eq!(get_native_token("arbitrum"), "ETH");
        assert_eq!(get_native_token("optimism"), "ETH");
        assert_eq!(get_native_token("avalanche"), "AVAX");
        assert_eq!(get_native_token("fantom"), "FTM");
        assert_eq!(get_native_token("solana"), "SOL");
    }

    #[test]
    fn test_get_native_token_when_unknown_chain_should_return_native() {
        assert_eq!(get_native_token("unknown-chain"), "NATIVE");
        assert_eq!(get_native_token("custom_chain"), "NATIVE");
        assert_eq!(get_native_token(""), "NATIVE");
    }

    #[tokio::test]
    async fn test_analyze_price_trends_when_bullish_data_should_return_bullish_trend() {
        let token = create_test_token_info(Some(10.0), Some(2.0), Some(1.0));
        let result = analyze_price_trends(&token).await.unwrap();

        assert_eq!(result.direction, "Bullish");
        assert_eq!(result.strength, 1); // 10.0 / 10.0 = 1.0, clamped to 1
        assert_eq!(result.momentum, 26.0); // 2.0 * 24.0 - 10.0
        assert_eq!(result.velocity, 10.0 / 24.0);
        assert_eq!(result.support_levels.len(), 1);
        assert_eq!(result.resistance_levels.len(), 1);
    }

    #[tokio::test]
    async fn test_analyze_price_trends_when_bearish_data_should_return_bearish_trend() {
        let token = create_test_token_info(Some(-10.0), Some(-1.0), Some(1.0));
        let result = analyze_price_trends(&token).await.unwrap();

        assert_eq!(result.direction, "Bearish");
        assert_eq!(result.strength, 1); // 10.0 / 10.0 = 1.0, clamped to 1
        assert_eq!(result.momentum, -14.0); // -1.0 * 24.0 - (-10.0)
        assert_eq!(result.velocity, -10.0 / 24.0);
    }

    #[tokio::test]
    async fn test_analyze_price_trends_when_neutral_data_should_return_neutral_trend() {
        let token = create_test_token_info(Some(2.0), Some(0.5), Some(1.0));
        let result = analyze_price_trends(&token).await.unwrap();

        assert_eq!(result.direction, "Neutral");
        assert_eq!(result.strength, 1); // 2.0 / 10.0 = 0.2, clamped to 1.0
        assert_eq!(result.momentum, 10.0); // 0.5 * 24.0 - 2.0
    }

    #[tokio::test]
    async fn test_analyze_price_trends_when_no_data_should_return_unknown_trend() {
        let token = create_test_token_info(None, None, Some(1.0));
        let result = analyze_price_trends(&token).await.unwrap();

        assert_eq!(result.direction, "Unknown");
        assert_eq!(result.strength, 5); // Default when no data
        assert_eq!(result.momentum, 0.0);
        assert_eq!(result.velocity, 0.0);
        assert_eq!(result.support_levels.len(), 0);
        assert_eq!(result.resistance_levels.len(), 0);
    }

    #[tokio::test]
    async fn test_analyze_price_trends_when_no_price_should_return_empty_levels() {
        let token = create_test_token_info(Some(5.0), Some(1.0), None);
        let result = analyze_price_trends(&token).await.unwrap();

        assert_eq!(result.support_levels.len(), 0);
        assert_eq!(result.resistance_levels.len(), 0);
    }

    #[tokio::test]
    async fn test_analyze_volume_patterns_when_valid_data_should_calculate_ratio() {
        let mut token = create_test_token_info(Some(5.0), Some(1.0), Some(1.0));
        token.volume_24h = Some(50000.0);
        token.market_cap = Some(1000000.0);

        let result = analyze_volume_patterns(&token).await.unwrap();

        assert_eq!(result.volume_mcap_ratio, Some(0.05)); // 50000 / 1000000
        assert_eq!(result.volume_trend, "Unknown");
        assert_eq!(result.volume_rank, None);
        assert_eq!(result.avg_volume_7d, None);
        assert_eq!(result.spike_factor, None);
    }

    #[tokio::test]
    async fn test_analyze_volume_patterns_when_zero_market_cap_should_return_none_ratio() {
        let mut token = create_test_token_info(Some(5.0), Some(1.0), Some(1.0));
        token.volume_24h = Some(50000.0);
        token.market_cap = Some(0.0);

        let result = analyze_volume_patterns(&token).await.unwrap();

        assert_eq!(result.volume_mcap_ratio, None);
    }

    #[tokio::test]
    async fn test_analyze_volume_patterns_when_missing_data_should_return_none_ratio() {
        let mut token = create_test_token_info(Some(5.0), Some(1.0), Some(1.0));
        token.volume_24h = None;
        token.market_cap = None;

        let result = analyze_volume_patterns(&token).await.unwrap();

        assert_eq!(result.volume_mcap_ratio, None);
    }

    #[tokio::test]
    async fn test_analyze_liquidity_when_multiple_pairs_should_aggregate_liquidity() {
        let mut token = create_test_token_info(Some(5.0), Some(1.0), Some(1.0));
        token.pairs = vec![
            create_test_token_pair("dex1", 100000.0),
            create_test_token_pair("dex2", 200000.0),
        ];

        let result = analyze_liquidity(&token).await.unwrap();

        assert_eq!(result.total_liquidity_usd, 300000.0);
        assert_eq!(result.dex_distribution.len(), 2);
        assert_eq!(result.dex_distribution.get("dex1"), Some(&100000.0));
        assert_eq!(result.dex_distribution.get("dex2"), Some(&200000.0));
        assert_eq!(result.depth_score, 60); // Less than 1M threshold
    }

    #[tokio::test]
    async fn test_analyze_liquidity_when_high_liquidity_should_return_high_depth_score() {
        let mut token = create_test_token_info(Some(5.0), Some(1.0), Some(1.0));
        token.pairs = vec![create_test_token_pair("dex1", 2000000.0)];

        let result = analyze_liquidity(&token).await.unwrap();

        assert_eq!(result.total_liquidity_usd, 2000000.0);
        assert_eq!(result.depth_score, 85); // Above 1M threshold
    }

    #[tokio::test]
    async fn test_analyze_liquidity_when_no_pairs_should_return_zero_liquidity() {
        let token = create_test_token_info(Some(5.0), Some(1.0), Some(1.0));

        let result = analyze_liquidity(&token).await.unwrap();

        assert_eq!(result.total_liquidity_usd, 0.0);
        assert_eq!(result.dex_distribution.len(), 0);
        assert_eq!(result.depth_score, 60);
    }

    #[tokio::test]
    async fn test_analyze_price_levels_when_positive_change_should_estimate_at_high() {
        let token = create_test_token_info(Some(10.0), Some(1.0), Some(100.0));

        let result = analyze_price_levels(&token).await.unwrap();

        assert_eq!(result.high_24h, Some(100.0));
        assert!(result.low_24h.is_some());
        assert!(result.low_24h.unwrap() < 100.0);
        assert_eq!(result.range_position, Some(1.0)); // At high
        assert_eq!(result.ath, None);
        assert_eq!(result.atl, None);
    }

    #[tokio::test]
    async fn test_analyze_price_levels_when_negative_change_should_estimate_at_low() {
        let token = create_test_token_info(Some(-10.0), Some(1.0), Some(90.0));

        let result = analyze_price_levels(&token).await.unwrap();

        assert!(result.high_24h.is_some());
        assert!(result.high_24h.unwrap() > 90.0);
        assert_eq!(result.low_24h, Some(90.0));
        assert_eq!(result.range_position, Some(0.0)); // At low
    }

    #[tokio::test]
    async fn test_analyze_price_levels_when_no_price_change_should_return_none() {
        let mut token = create_test_token_info(None, Some(1.0), Some(100.0));
        token.price_change_24h = None;

        let result = analyze_price_levels(&token).await.unwrap();

        assert_eq!(result.high_24h, None);
        assert_eq!(result.low_24h, None);
        assert_eq!(result.range_position, None);
    }

    #[tokio::test]
    async fn test_analyze_price_levels_when_no_price_should_return_none() {
        let token = create_test_token_info(Some(10.0), Some(1.0), None);

        let result = analyze_price_levels(&token).await.unwrap();

        assert_eq!(result.high_24h, None);
        assert_eq!(result.low_24h, None);
        assert_eq!(result.range_position, None);
    }

    #[tokio::test]
    async fn test_assess_token_risks_when_low_liquidity_should_add_liquidity_risk() {
        let mut token = create_test_token_info(Some(5.0), Some(1.0), Some(1.0));
        token.pairs = vec![create_test_token_pair("dex1", 50000.0)]; // Low liquidity

        let result = assess_token_risks(&token).await.unwrap();

        assert_eq!(result.liquidity_risk, 75);
        assert!(result
            .risk_factors
            .iter()
            .any(|r| r.category == "Liquidity"));
        assert!(matches!(result.risk_level.as_str(), "High" | "Extreme"));
    }

    #[tokio::test]
    async fn test_assess_token_risks_when_high_liquidity_should_have_low_liquidity_risk() {
        let mut token = create_test_token_info(Some(5.0), Some(1.0), Some(1.0));
        token.pairs = vec![create_test_token_pair("dex1", 500000.0)]; // High liquidity

        let result = assess_token_risks(&token).await.unwrap();

        assert_eq!(result.liquidity_risk, 25);
    }

    #[tokio::test]
    async fn test_assess_token_risks_when_unverified_contract_should_add_contract_risk() {
        let mut token = create_test_token_info(Some(5.0), Some(1.0), Some(1.0));
        token.security.is_verified = false;
        token.pairs = vec![create_test_token_pair("dex1", 500000.0)];

        let result = assess_token_risks(&token).await.unwrap();

        assert_eq!(result.contract_risk, 80);
        assert!(result.risk_factors.iter().any(|r| r.category == "Contract"));
    }

    #[tokio::test]
    async fn test_assess_token_risks_when_verified_contract_should_have_low_contract_risk() {
        let mut token = create_test_token_info(Some(5.0), Some(1.0), Some(1.0));
        token.security.is_verified = true;
        token.pairs = vec![create_test_token_pair("dex1", 500000.0)];

        let result = assess_token_risks(&token).await.unwrap();

        assert_eq!(result.contract_risk, 20);
    }

    #[tokio::test]
    async fn test_assess_token_risks_when_high_volatility_should_add_volatility_risk() {
        let mut token = create_test_token_info(Some(25.0), Some(1.0), Some(1.0)); // High volatility
        token.security.is_verified = true;
        token.pairs = vec![create_test_token_pair("dex1", 500000.0)];

        let result = assess_token_risks(&token).await.unwrap();

        assert_eq!(result.volatility_risk, 60);
        assert!(result
            .risk_factors
            .iter()
            .any(|r| r.category == "Volatility"));
    }

    #[tokio::test]
    async fn test_assess_token_risks_when_normal_volatility_should_have_medium_volatility_risk() {
        let mut token = create_test_token_info(Some(5.0), Some(1.0), Some(1.0)); // Normal volatility
        token.security.is_verified = true;
        token.pairs = vec![create_test_token_pair("dex1", 500000.0)];

        let result = assess_token_risks(&token).await.unwrap();

        assert_eq!(result.volatility_risk, 30);
    }

    #[tokio::test]
    async fn test_assess_token_risks_when_no_volatility_data_should_add_data_risk() {
        let mut token = create_test_token_info(None, Some(1.0), Some(1.0));
        token.security.is_verified = true;
        token.pairs = vec![create_test_token_pair("dex1", 500000.0)];

        let result = assess_token_risks(&token).await.unwrap();

        assert_eq!(result.volatility_risk, 20);
        assert!(result.risk_factors.iter().any(|r| r.category == "Data"));
    }

    #[tokio::test]
    async fn test_assess_token_risks_when_low_risk_should_return_low_risk_level() {
        let mut token = create_test_token_info(Some(5.0), Some(1.0), Some(1.0));
        token.security.is_verified = true;
        token.pairs = vec![create_test_token_pair("dex1", 500000.0)];

        let result = assess_token_risks(&token).await.unwrap();

        assert_eq!(result.risk_level, "Low");
    }

    #[tokio::test]
    async fn test_parse_search_results_when_invalid_json_should_return_error() {
        let invalid_json = "invalid json";
        let result = parse_search_results(invalid_json).await;

        assert!(result.is_err());
        if let Err(crate::error::WebToolError::Parsing(msg)) = result {
            assert!(msg.contains("expected"));
        }
    }

    #[tokio::test]
    async fn test_parse_trending_response_when_invalid_json_should_return_error() {
        let invalid_json = "invalid json";
        let result = parse_trending_response(invalid_json).await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_parse_pairs_response_when_invalid_json_should_return_error() {
        let invalid_json = "invalid json";
        let result = parse_pairs_response(invalid_json).await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_parse_token_response_when_invalid_json_should_return_parsing_error() {
        let invalid_json = "invalid json";
        let result = parse_token_response(invalid_json, "0x123", "ethereum", Some(true)).await;

        assert!(result.is_err());
        if let Err(crate::error::WebToolError::Parsing(msg)) = result {
            assert!(msg.contains("Failed to parse DexScreener response"));
        }
    }

    #[tokio::test]
    async fn test_parse_token_response_when_no_pairs_found_should_return_api_error() {
        let valid_json_no_pairs = r#"{"pairs": []}"#;
        let result =
            parse_token_response(valid_json_no_pairs, "0x123", "ethereum", Some(true)).await;

        assert!(result.is_err());
        if let Err(crate::error::WebToolError::Api(msg)) = result {
            assert!(msg.contains("No pairs found for token address"));
        }
    }

    #[tokio::test]
    async fn test_parse_token_response_when_no_valid_pairs_should_return_api_error() {
        let json_with_unmatched_pairs = r#"{"pairs": [{"base_token": {"address": "0x456", "name": "Other Token", "symbol": "OTHER"}, "quote_token": {"address": "0x789", "name": "USDC", "symbol": "USDC"}, "pair_address": "0xabc", "dex_id": "uniswap", "url": "https://example.com", "price_usd": "1.0", "price_native": "1.0"}]}"#;
        let result =
            parse_token_response(json_with_unmatched_pairs, "0x123", "ethereum", Some(true)).await;

        assert!(result.is_err());
        if let Err(crate::error::WebToolError::Api(msg)) = result {
            assert!(msg.contains("No pairs found for token address"));
        }
    }

    #[tokio::test]
    async fn test_parse_token_response_when_valid_data_should_return_token_info() {
        let valid_json = create_valid_dexscreener_response();
        let result = parse_token_response(&valid_json, "0x123", "ethereum", Some(true)).await;

        assert!(result.is_ok());
        let token = result.unwrap();
        assert_eq!(token.address, "0x123");
        assert_eq!(token.symbol, "TEST");
        assert_eq!(token.name, "Test Token");
        assert_eq!(token.chain.id, "ethereum");
        assert_eq!(token.chain.name, "Ethereum");
        assert_eq!(token.chain.native_token, "ETH");
        assert_eq!(token.pair_count, 1);
        assert!(!token.pairs.is_empty());
    }

    #[tokio::test]
    async fn test_parse_token_response_when_multiple_pairs_should_use_highest_liquidity() {
        let json_with_multiple_pairs = create_dexscreener_response_with_multiple_pairs();
        let result =
            parse_token_response(&json_with_multiple_pairs, "0x123", "ethereum", Some(true)).await;

        assert!(result.is_ok());
        let token = result.unwrap();
        assert_eq!(token.pair_count, 2);
        // Should have aggregated volume from both pairs
        assert!(token.volume_24h.unwrap() > 10000.0);
        // Primary pair should be the one with higher liquidity
        assert_eq!(token.price_usd, Some(2.0)); // From high liquidity pair
    }

    #[tokio::test]
    async fn test_parse_token_response_when_case_insensitive_address_should_match() {
        let valid_json = create_valid_dexscreener_response();
        let result = parse_token_response(&valid_json, "0X123", "ethereum", Some(true)).await; // Uppercase

        assert!(result.is_ok());
        let token = result.unwrap();
        assert_eq!(token.address, "0X123"); // Should preserve input case
    }

    #[tokio::test]
    async fn test_parse_token_response_when_no_liquidity_data_should_handle_gracefully() {
        let json_no_liquidity = create_dexscreener_response_without_liquidity();
        let result =
            parse_token_response(&json_no_liquidity, "0x123", "ethereum", Some(true)).await;

        assert!(result.is_ok());
        let token = result.unwrap();
        assert_eq!(token.pairs[0].liquidity_usd, None);
    }

    #[tokio::test]
    async fn test_parse_token_response_when_transaction_data_available_should_calculate_totals() {
        let json_with_txns = create_dexscreener_response_with_transaction_data();
        let result = parse_token_response(&json_with_txns, "0x123", "ethereum", Some(true)).await;

        assert!(result.is_ok());
        let token = result.unwrap();
        let pair = &token.pairs[0];
        assert_eq!(pair.txns_24h.buys, Some(100));
        assert_eq!(pair.txns_24h.sells, Some(50));
        assert_eq!(pair.txns_24h.total, Some(150)); // Should calculate total
        assert!(pair.txns_24h.buy_volume_usd.is_some());
        assert!(pair.txns_24h.sell_volume_usd.is_some());
    }

    #[tokio::test]
    async fn test_parse_token_response_when_unknown_chain_should_use_default_formatting() {
        let valid_json = create_valid_dexscreener_response();
        let result = parse_token_response(&valid_json, "0x123", "unknown-chain", Some(true)).await;

        assert!(result.is_ok());
        let token = result.unwrap();
        assert_eq!(token.chain.id, "unknown-chain");
        assert_eq!(token.chain.name, "unknown-chain");
        assert_eq!(token.chain.native_token, "NATIVE");
    }

    #[tokio::test]
    async fn test_parse_token_response_when_price_parsing_fails_should_handle_gracefully() {
        let json_invalid_price = create_dexscreener_response_with_invalid_price();
        let result =
            parse_token_response(&json_invalid_price, "0x123", "ethereum", Some(true)).await;

        assert!(result.is_ok());
        let token = result.unwrap();
        assert_eq!(token.price_usd, None); // Should handle parse failure
    }

    // Helper functions for parse_token_response tests
    fn create_valid_dexscreener_response() -> String {
        r#"{
            "schemaVersion": "1.0.0",
            "pairs": [{
                "chainId": "ethereum",
                "baseToken": {
                    "address": "0x123",
                    "name": "Test Token",
                    "symbol": "TEST"
                },
                "quoteToken": {
                    "address": "0x456",
                    "name": "USD Coin",
                    "symbol": "USDC"
                },
                "pairAddress": "0xabc",
                "dexId": "uniswap",
                "url": "https://example.com",
                "priceUsd": "1.5",
                "priceNative": "1.5",
                "marketCap": 1000000.0,
                "liquidity": {"usd": 500000.0},
                "volume": {"h24": 50000.0},
                "priceChange": {"h24": 5.0, "h1": 1.0, "m5": 0.5},
                "fdv": 2000000.0
            }]
        }"#
        .to_string()
    }

    fn create_dexscreener_response_with_multiple_pairs() -> String {
        r#"{
            "schemaVersion": "1.0.0",
            "pairs": [{
                "chainId": "ethereum",
                "baseToken": {
                    "address": "0x123",
                    "name": "Test Token",
                    "symbol": "TEST"
                },
                "quoteToken": {
                    "address": "0x456",
                    "name": "USD Coin",
                    "symbol": "USDC"
                },
                "pairAddress": "0xabc",
                "dexId": "uniswap",
                "url": "https://example.com",
                "priceUsd": "1.0",
                "priceNative": "1.0",
                "marketCap": 1000000.0,
                "liquidity": {"usd": 300000.0},
                "volume": {"h24": 30000.0},
                "priceChange": {"h24": 3.0}
            }, {
                "chainId": "ethereum",
                "baseToken": {
                    "address": "0x123",
                    "name": "Test Token",
                    "symbol": "TEST"
                },
                "quoteToken": {
                    "address": "0x789",
                    "name": "Ethereum",
                    "symbol": "ETH"
                },
                "pairAddress": "0xdef",
                "dexId": "sushiswap",
                "url": "https://sushi.example.com",
                "priceUsd": "2.0",
                "priceNative": "2.0",
                "marketCap": 1500000.0,
                "liquidity": {"usd": 800000.0},
                "volume": {"h24": 80000.0},
                "priceChange": {"h24": 8.0}
            }]
        }"#
        .to_string()
    }

    fn create_dexscreener_response_without_liquidity() -> String {
        r#"{
            "schemaVersion": "1.0.0",
            "pairs": [{
                "chainId": "ethereum",
                "baseToken": {
                    "address": "0x123",
                    "name": "Test Token",
                    "symbol": "TEST"
                },
                "quoteToken": {
                    "address": "0x456",
                    "name": "USD Coin",
                    "symbol": "USDC"
                },
                "pairAddress": "0xabc",
                "dexId": "uniswap",
                "url": "https://example.com",
                "priceUsd": "1.5",
                "priceNative": "1.5"
            }]
        }"#
        .to_string()
    }

    fn create_dexscreener_response_with_transaction_data() -> String {
        r#"{
            "schemaVersion": "1.0.0",
            "pairs": [{
                "chainId": "ethereum",
                "baseToken": {
                    "address": "0x123",
                    "name": "Test Token",
                    "symbol": "TEST"
                },
                "quoteToken": {
                    "address": "0x456",
                    "name": "USD Coin",
                    "symbol": "USDC"
                },
                "pairAddress": "0xabc",
                "dexId": "uniswap",
                "url": "https://example.com",
                "priceUsd": "1.5",
                "priceNative": "1.5",
                "volume": {"h24": 60000.0},
                "txns": {
                    "h24": {
                        "buys": 100,
                        "sells": 50
                    }
                }
            }]
        }"#
        .to_string()
    }

    fn create_dexscreener_response_with_invalid_price() -> String {
        r#"{
            "schemaVersion": "1.0.0",
            "pairs": [{
                "chainId": "ethereum",
                "baseToken": {
                    "address": "0x123",
                    "name": "Test Token",
                    "symbol": "TEST"
                },
                "quoteToken": {
                    "address": "0x456",
                    "name": "USD Coin",
                    "symbol": "USDC"
                },
                "pairAddress": "0xabc",
                "dexId": "uniswap",
                "url": "https://example.com",
                "priceUsd": "invalid_price",
                "priceNative": "invalid_price"
            }]
        }"#
        .to_string()
    }

    // Helper functions for tests
    fn create_test_token_info(
        price_change_24h: Option<f64>,
        price_change_1h: Option<f64>,
        price_usd: Option<f64>,
    ) -> TokenInfo {
        TokenInfo {
            address: "0x123".to_string(),
            name: "Test Token".to_string(),
            symbol: "TEST".to_string(),
            decimals: 18,
            price_usd,
            market_cap: Some(1000000.0),
            volume_24h: Some(50000.0),
            price_change_24h,
            price_change_1h,
            price_change_5m: Some(0.5),
            circulating_supply: Some(1000000.0),
            total_supply: Some(10000000.0),
            pair_count: 0,
            pairs: vec![],
            chain: ChainInfo {
                id: "ethereum".to_string(),
                name: "Ethereum".to_string(),
                logo: None,
                native_token: "ETH".to_string(),
            },
            security: SecurityInfo {
                is_verified: true,
                liquidity_locked: Some(true),
                audit_status: None,
                honeypot_status: None,
                ownership_status: None,
                risk_score: Some(25),
            },
            socials: vec![],
            updated_at: Utc::now(),
        }
    }

    fn create_test_token_pair(dex_name: &str, liquidity: f64) -> TokenPair {
        TokenPair {
            pair_id: "pair123".to_string(),
            dex: DexInfo {
                id: dex_name.to_string(),
                name: dex_name.to_string(),
                url: Some("https://dex.com".to_string()),
                logo: None,
            },
            base_token: PairToken {
                address: "0x123".to_string(),
                name: "Test Token".to_string(),
                symbol: "TEST".to_string(),
            },
            quote_token: PairToken {
                address: "0x456".to_string(),
                name: "USD Coin".to_string(),
                symbol: "USDC".to_string(),
            },
            price_usd: Some(1.0),
            price_native: Some(1.0),
            volume_24h: Some(10000.0),
            price_change_24h: Some(5.0),
            liquidity_usd: Some(liquidity),
            fdv: Some(2000000.0),
            created_at: None,
            last_trade_at: Utc::now(),
            txns_24h: TransactionStats {
                buys: Some(100),
                sells: Some(80),
                total: Some(180),
                buy_volume_usd: Some(6000.0),
                sell_volume_usd: Some(4000.0),
            },
            url: "https://dex.com/pair/123".to_string(),
        }
    }
}