File size: 22,990 Bytes
6993919
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
RugCharts Token Security Matrix
================================
37+ security checks across 3 tiers: Quick Scan, Deep Scan, ML Scan.

Tier 1 (Quick Scan β€” <500ms): GoPlus, honeypot, taxes, basic contract checks
Tier 2 (Deep Scan β€” 2-10s): Bytecode analysis, liquidity analysis, holder distribution
Tier 3 (ML Scan β€” async): XGBoost risk classifier, bytecode anomaly, symbol executor

Wired into DataBus as 'token_security' chain.
Produces the Authentic Score that feeds directly into the scanner.

Scoring bands:
  0-20: SAFE (green)
  21-40: LOW RISK (light green)
  41-60: MEDIUM RISK (yellow)
  61-80: HIGH RISK (orange)
  81-100: DANGER (red) β€” auto-fail
"""

import json
import logging
import os
import time
from collections import defaultdict
from datetime import UTC, datetime
from typing import Any

import httpx
import redis

logger = logging.getLogger("token_security")

REDIS_HOST = os.getenv("REDIS_HOST", "rmi-redis")
REDIS_PORT = int(os.getenv("REDIS_PORT", "6379"))
REDIS_PASSWORD = os.getenv("REDIS_PASSWORD", "")

CACHE_TTL = 300  # 5 min active, 1 hour dormant

# ── Check Definitions ──────────────────────────────────────────────


class SecurityCheck:
    """A single security check with weight, category, and scoring."""

    __slots__ = ("auto_fail", "category", "description", "id", "name", "tier", "weight")

    def __init__(
        self,
        id: str,
        name: str,
        category: str,
        weight: float = 1.0,
        tier: int = 1,
        description: str = "",
        auto_fail: bool = False,
    ):
        self.id = id
        self.name = name
        self.category = category
        self.weight = weight
        self.tier = tier
        self.description = description
        self.auto_fail = auto_fail


# ── 37+ Security Checks ────────────────────────────────────────────

SECURITY_CHECKS = [
    # ── CATEGORY: Contract Risks (CR) ──
    SecurityCheck(
        "CR01",
        "Contract Verified",
        "contract_risks",
        1.5,
        1,
        "Contract source code is verified on explorer",
        auto_fail=True,
    ),
    SecurityCheck(
        "CR02",
        "Proxy Contract",
        "contract_risks",
        2.0,
        1,
        "Upgradeable proxy β€” owner can change logic at any time",
    ),
    SecurityCheck(
        "CR03",
        "Mint Function",
        "contract_risks",
        3.0,
        1,
        "Token has unrestricted mint() β€” infinite supply possible",
        auto_fail=True,
    ),
    SecurityCheck(
        "CR04",
        "Owner Privileges",
        "contract_risks",
        2.5,
        1,
        "Owner can pause trading, blacklist addresses, or adjust fees",
    ),
    SecurityCheck(
        "CR05",
        "Self-Destruct",
        "contract_risks",
        3.0,
        1,
        "Contract has SELFDESTRUCT opcode",
        auto_fail=True,
    ),
    SecurityCheck(
        "CR06",
        "External Call Risk",
        "contract_risks",
        1.5,
        2,
        "Unchecked external calls that could enable reentrancy",
    ),
    SecurityCheck(
        "CR07",
        "Delegatecall Risk",
        "contract_risks",
        2.0,
        2,
        "Use of delegatecall to untrusted contracts",
    ),
    # ── CATEGORY: Honeypot Detection (HP) ──
    SecurityCheck(
        "HP01",
        "Honeypot β€” GoPlus",
        "honeypot",
        5.0,
        1,
        "GoPlus API reports honeypot: buy OK, sell blocked",
        auto_fail=True,
    ),
    SecurityCheck(
        "HP02",
        "Honeypot β€” Honeypot.is",
        "honeypot",
        5.0,
        2,
        "Honeypot.is simulation confirms trapped tokens",
        auto_fail=True,
    ),
    SecurityCheck(
        "HP03",
        "Sell Tax Anomaly",
        "honeypot",
        3.0,
        1,
        "Buy tax normal but sell tax >50% β€” likely honeypot",
    ),
    SecurityCheck(
        "HP04",
        "Transfer Pausability",
        "honeypot",
        2.5,
        1,
        "Owner can pause token transfers entirely",
    ),
    SecurityCheck(
        "HP05",
        "Max Transaction Limit",
        "honeypot",
        1.5,
        1,
        "Extremely small max tx amount blocks legitimate sales",
    ),
    # ── CATEGORY: Liquidity Risks (LR) ──
    SecurityCheck(
        "LR01",
        "Liquidity Locked",
        "liquidity",
        2.0,
        1,
        "LP tokens are time-locked or burned",
        auto_fail=True,
    ),
    SecurityCheck(
        "LR02",
        "Liquidity Amount",
        "liquidity",
        1.5,
        1,
        "Pool liquidity < $1,000 β€” extreme slippage risk",
    ),
    SecurityCheck(
        "LR03",
        "LP Holder Concentration",
        "liquidity",
        2.0,
        1,
        "Single wallet holds >50% of LP tokens",
    ),
    SecurityCheck(
        "LR04",
        "Liquidity/Supply Ratio",
        "liquidity",
        1.0,
        1,
        "Low liquidity relative to total supply",
    ),
    SecurityCheck(
        "LR05",
        "Creator LP Removal",
        "liquidity",
        3.0,
        2,
        "Creator wallet removed significant liquidity",
    ),
    # ── CATEGORY: Holder Distribution (HD) ──
    SecurityCheck("HD01", "Top 10 Concentration", "holders", 2.0, 1, "Top 10 holders control >50% of supply"),
    SecurityCheck("HD02", "Creator Holdings", "holders", 2.5, 1, "Creator/deployer wallet holds >5% supply"),
    SecurityCheck("HD03", "Holder Count", "holders", 1.0, 1, "Very few holders suggests no organic adoption"),
    SecurityCheck("HD04", "Whale Dominance", "holders", 1.5, 2, "Wallets >1% supply own >80% collectively"),
    SecurityCheck(
        "HD05",
        "Fresh Wallet %",
        "holders",
        1.5,
        2,
        "High % of brand-new wallets (<7 days old) = bot risk",
    ),
    # ── CATEGORY: Trading Activity (TA) ──
    SecurityCheck(
        "TA01",
        "Wash Trading %",
        "trading",
        3.0,
        1,
        "Estimated fake volume percentage from authenticity scorer",
    ),
    SecurityCheck(
        "TA02",
        "Volume/Liquidity Ratio",
        "trading",
        1.5,
        1,
        "Suspiciously high volume relative to thin liquidity",
    ),
    SecurityCheck(
        "TA03",
        "Buy/Sell Imbalance",
        "trading",
        1.0,
        1,
        "Extreme buy or sell ratio suggests manipulation",
    ),
    SecurityCheck("TA04", "Trade Count", "trading", 0.5, 1, "Zero or near-zero trading activity"),
    SecurityCheck("TA05", "Price Impact", "trading", 2.0, 2, "Single trade moves price >30%"),
    # ── CATEGORY: Deployer/Team (DT) ──
    SecurityCheck(
        "DT01",
        "Deployer History",
        "deployer",
        3.0,
        2,
        "Deployer wallet previously launched scam tokens",
    ),
    SecurityCheck("DT02", "Deployer Funding", "deployer", 2.0, 2, "Deployer funded from Tornado Cash or mixer"),
    SecurityCheck(
        "DT03",
        "Multi-Token Deployer",
        "deployer",
        2.5,
        1,
        "Deployer launched 10+ tokens β€” factory pattern",
    ),
    SecurityCheck(
        "DT04",
        "Team Wallet Tracing",
        "deployer",
        1.5,
        2,
        "Connected wallets show suspicious activity",
    ),
    SecurityCheck(
        "DT05",
        "Social Presence",
        "deployer",
        0.5,
        1,
        "No website, Twitter, or Telegram β€” likely ghost token",
    ),
    # ── CATEGORY: Token Economics (TE) ──
    SecurityCheck(
        "TE01",
        "Supply Concentration",
        "tokenomics",
        2.0,
        1,
        "Creator/team controls >20% of total supply",
    ),
    SecurityCheck("TE02", "Tax Anomaly", "tokenomics", 2.5, 1, "Buy/sell tax >10% β€” predatory economics"),
    SecurityCheck(
        "TE03",
        "Transfer Fee",
        "tokenomics",
        1.5,
        1,
        "Hidden transfer fees beyond standard DEX swap tax",
    ),
    SecurityCheck(
        "TE04",
        "Blacklist Function",
        "tokenomics",
        2.0,
        1,
        "Owner can blacklist specific addresses from trading",
    ),
    SecurityCheck(
        "TE05",
        "Max Wallet Cap",
        "tokenomics",
        1.0,
        2,
        "Artificially low max wallet cap prevents accumulation",
    ),
    # ── CATEGORY: Rug Pull Indicators (RP) ──
    SecurityCheck(
        "RP01",
        "Rug Pull β€” Known Pattern",
        "rug_pull",
        5.0,
        2,
        "Token matches known rug pull deployment pattern",
        auto_fail=True,
    ),
    SecurityCheck(
        "RP02",
        "Liquidity Removal",
        "rug_pull",
        5.0,
        1,
        "LP was removed or significantly drained",
        auto_fail=True,
    ),
    SecurityCheck("RP03", "Price Crash", "rug_pull", 2.0, 2, "Price dropped >90% within 24h β€” possible rug"),
    SecurityCheck(
        "RP04",
        "Duplicate Token",
        "rug_pull",
        1.5,
        2,
        "Same name/symbol as another token β€” impersonation",
    ),
    SecurityCheck(
        "RP05",
        "Age Risk",
        "rug_pull",
        1.0,
        1,
        "Token deployed within last hour β€” highest risk period",
    ),
]


def get_checks_by_tier(tier: int) -> list[SecurityCheck]:
    return [c for c in SECURITY_CHECKS if c.tier <= tier]


def get_check_matrix() -> dict:
    """Full security check matrix with descriptions."""
    categories = defaultdict(list)
    for check in SECURITY_CHECKS:
        categories[check.category].append(
            {
                "id": check.id,
                "name": check.name,
                "weight": check.weight,
                "tier": check.tier,
                "auto_fail": check.auto_fail,
                "description": check.description,
            }
        )
    return {
        "total_checks": len(SECURITY_CHECKS),
        "categories": dict(categories),
        "tiers": {
            1: "Quick Scan (<500ms) β€” GoPlus, honeypot, basic contract",
            2: "Deep Scan (2-10s) β€” Bytecode, liquidity, deployer tracing",
            3: "ML Scan (async) β€” XGBoost, bytecode anomaly, symbolic executor",
        },
    }


# ── Scoring Engine ─────────────────────────────────────────────────


class TokenSecurityScorer:
    """Weighs and aggregates security check results into a 0-100 risk score."""

    SCORE_BANDS = [
        (0, 20, "SAFE", "#00FF88"),
        (21, 40, "LOW RISK", "#88FF00"),
        (41, 60, "MEDIUM RISK", "#FFD700"),
        (61, 80, "HIGH RISK", "#FF8800"),
        (81, 100, "DANGER", "#FF0044"),
    ]

    @staticmethod
    def band_for_score(score: float) -> tuple[str, str]:
        for lo, hi, band, color in TokenSecurityScorer.SCORE_BANDS:
            if lo <= score <= hi:
                return band, color
        return "DANGER", "#FF0044"

    @staticmethod
    def compute_score(check_results: dict[str, Any]) -> tuple[float, str, str]:
        """Compute weighted risk score from check results.

        Each check result: {"status": "pass"|"fail"|"warning"|"unknown", "details": str}

        Returns (score 0-100, band, color)
        """
        total_weight = 0.0
        weighted_score = 0.0
        auto_fail_triggered = False
        check_details = []

        # Create lookup by ID
        checks_by_id = {c.id: c for c in SECURITY_CHECKS}

        for check_id, result in check_results.items():
            check = checks_by_id.get(check_id)
            if not check:
                continue

            status = result.get("status", "unknown")
            total_weight += check.weight

            if status == "fail":
                weighted_score += check.weight * 100.0
                check_details.append(f"FAIL {check.id} {check.name}: {result.get('details', '')}")
                if check.auto_fail:
                    auto_fail_triggered = True
            elif status == "warning":
                weighted_score += check.weight * 65.0
                check_details.append(f"WARN {check.id} {check.name}: {result.get('details', '')}")
            elif status == "unknown":
                weighted_score += check.weight * 30.0  # uncertainty penalty
            # 'pass' adds 0

        if total_weight == 0:
            return 50.0, "MEDIUM RISK", "#FFD700"

        score = weighted_score / total_weight

        if auto_fail_triggered:
            score = max(score, 85.0)

        band, color = TokenSecurityScorer.band_for_score(score)
        return round(score, 1), band, color


# ── Quick Scan Provider ─────────────────────────────────────────────


async def run_quick_scan(address: str, chain: str = "ethereum", **kw) -> dict[str, Any]:
    """Tier 1 quick scan β€” GoPlus + basic checks. <1s target."""
    results = {}
    api_key = kw.get("api_key", "") or kw.get("goplus_key", "") or os.getenv("GOPLUS_API_KEY", "")

    try:
        # Map chain name to GoPlus chain ID
        chain_map = {
            "ethereum": "1",
            "eth": "1",
            "bsc": "56",
            "polygon": "137",
            "arbitrum": "42161",
            "optimism": "10",
            "base": "8453",
            "avalanche": "43114",
            "fantom": "250",
            "gnosis": "100",
        }
        goplus_chain = chain_map.get(chain.lower(), chain)

        # GoPlus Security API (free, no key needed for basic)
        async with httpx.AsyncClient(timeout=5) as c:
            # Token security detection
            goplus_url = f"https://api.gopluslabs.io/api/v1/token_security/{goplus_chain}"
            addr_lower = address.lower()
            r = await c.get(goplus_url, params={"contract_addresses": addr_lower})
            if r.status_code == 200:
                goplus_data = r.json().get("result", {}).get(address.lower(), {})

                # Parse GoPlus into our check format
                is_honeypot = goplus_data.get("is_honeypot", "0")
                results["HP01"] = {
                    "status": "fail" if is_honeypot in ("1", "1.0") else "pass",
                    "details": f"GoPlus honeypot={'YES' if is_honeypot in ('1', '1.0') else 'no'}",
                }

                # Contract checks
                is_open_source = goplus_data.get("is_open_source", "0")
                results["CR01"] = {
                    "status": "pass" if is_open_source in ("1", "1.0") else "fail",
                    "details": f"Verified={'YES' if is_open_source in ('1', '1.0') else 'NO'}",
                }

                is_proxy = goplus_data.get("is_proxy", "0")
                results["CR02"] = {
                    "status": "warning" if is_proxy in ("1", "1.0") else "pass",
                    "details": f"Proxy={'YES' if is_proxy in ('1', '1.0') else 'no'}",
                }

                is_mintable = goplus_data.get("is_mintable", "0")
                results["CR03"] = {
                    "status": "fail" if is_mintable in ("1", "1.0") else "pass",
                    "details": f"Mintable={'YES' if is_mintable in ('1', '1.0') else 'no'}",
                }

                # Tax checks
                buy_tax = float(goplus_data.get("buy_tax", "0"))
                sell_tax = float(goplus_data.get("sell_tax", "0"))

                if sell_tax > 50:
                    results["HP03"] = {"status": "fail", "details": f"Sell tax={sell_tax}%"}
                elif sell_tax > 10:
                    results["HP03"] = {"status": "warning", "details": f"Sell tax={sell_tax}%"}

                if buy_tax > 10 or sell_tax > 10:
                    results["TE02"] = {
                        "status": "warning" if sell_tax <= 50 else "fail",
                        "details": f"Buy={buy_tax}% Sell={sell_tax}%",
                    }

                # Transfer pausable
                transfer_pausable = goplus_data.get("transfer_pausable", "0")
                results["HP04"] = {
                    "status": "warning" if transfer_pausable in ("1", "1.0") else "pass",
                    "details": f"Pausable={'YES' if transfer_pausable in ('1', '1.0') else 'no'}",
                }

                # Owner privileges
                owner_change_balance = goplus_data.get("owner_change_balance", "0")
                results["CR04"] = {
                    "status": "warning" if owner_change_balance in ("1", "1.0") else "pass",
                    "details": "Owner can change balances" if owner_change_balance in ("1", "1.0") else "",
                }

                # Blacklist
                is_blacklisted = goplus_data.get("is_blacklisted", "0")
                results["TE04"] = {
                    "status": "warning" if is_blacklisted in ("1", "1.0") else "pass",
                    "details": f"Blacklist={'YES' if is_blacklisted in ('1', '1.0') else 'no'}",
                }

                # Holder count
                holder_count = int(goplus_data.get("holder_count", "0"))
                results["HD03"] = {
                    "status": "warning" if holder_count < 10 else "pass",
                    "details": f"Holders={holder_count}",
                }

                # LP holder check
                is_in_anti_whale = goplus_data.get("is_anti_whale", "0")
                results["LR03"] = {
                    "status": "warning" if is_in_anti_whale in ("1", "1.0") else "pass",
                    "details": f"Anti-whale={'YES' if is_in_anti_whale in ('1', '1.0') else 'no'}",
                }

    except Exception as e:
        logger.debug(f"GoPlus scan failed: {e}")

    # ── Our DataBus checks ──
    # Age check
    try:
        api_key = kw.get("api_key", "") or os.getenv("HELIUS_API_KEY", "") or os.getenv("ALCHEMY_API_KEY", "")
        if api_key and chain == "solana":
            async with httpx.AsyncClient(timeout=10) as c:
                r = await c.post(
                    f"https://mainnet.helius-rpc.com/?api-key={api_key}",
                    json={
                        "jsonrpc": "2.0",
                        "id": 1,
                        "method": "getSignaturesForAddress",
                        "params": [address, {"limit": 1}],
                    },
                )
                if r.status_code == 200:
                    sigs = r.json().get("result", [])
                    if sigs:
                        block_time = sigs[0].get("blockTime", 0)
                        age_seconds = int(time.time()) - block_time
                        age_hours = age_seconds / 3600
                        if age_hours < 1:
                            results["RP05"] = {
                                "status": "fail",
                                "details": f"Age={age_hours:.1f}h (<1h)",
                            }
                        elif age_hours < 24:
                            results["RP05"] = {
                                "status": "warning",
                                "details": f"Age={age_hours:.1f}h",
                            }
                        else:
                            results["RP05"] = {"status": "pass", "details": f"Age={age_hours:.0f}h"}
    except Exception:
        pass

    # Volume authenticity
    try:
        volume_24h = float(kw.get("volume_24h", 0))
        liquidity = float(kw.get("liquidity_usd", 0))
        unique_wallets = int(kw.get("unique_wallets", 0))
        tx_count = int(kw.get("tx_count", 0))

        if volume_24h > 0 or liquidity > 0:
            from app.databus.volume_authenticity import quick_authenticity_score

            auth = quick_authenticity_score(volume_24h, liquidity, unique_wallets, tx_count)
            fake_pct = auth.get("fake_volume_pct", 0)

            if fake_pct > 50:
                results["TA01"] = {"status": "fail", "details": f"Fake volume={fake_pct}%"}
            elif fake_pct > 20:
                results["TA01"] = {"status": "warning", "details": f"Fake volume={fake_pct}%"}

            if liquidity > 0:
                vl_ratio = volume_24h / liquidity
                if vl_ratio > 5:
                    results["TA02"] = {"status": "warning", "details": f"V/L={vl_ratio:.1f}x"}
    except Exception:
        pass

    # Compute final score
    score, band, color = TokenSecurityScorer.compute_score(results)

    return {
        "checks": dict(results.items()),
        "total_checks_run": len(results),
        "security_score": score,
        "risk_band": band,
        "risk_color": color,
        "scan_tier": 1,
        "scan_type": "quick",
        "source": "token_security_matrix",
    }


# ── Full Security Scan (Tier 1 + 2 + 3 async) ─────────────────────


async def run_full_scan(address: str = "", chain: str = "ethereum", **kw) -> dict | None:
    """DataBus provider: Full token security scan.

    Returns scored results with breakdown by category.
    """
    if not address:
        return None

    cache_key = f"token_security:{chain}:{address}"
    try:
        r = redis.Redis(
            host=REDIS_HOST,
            port=REDIS_PORT,
            password=REDIS_PASSWORD,
            decode_responses=True,
            socket_connect_timeout=2,
        )
        cached = r.get(cache_key)
        if cached:
            r.close()
            return json.loads(cached)
        r.close()
    except Exception:
        pass

    # Run quick scan
    result = await run_quick_scan(address, chain, **kw)

    # Enrich
    result["token_address"] = address
    result["chain"] = chain
    result["scan_timestamp"] = datetime.now(UTC).isoformat()

    # Category breakdown
    categories = defaultdict(lambda: {"pass": 0, "fail": 0, "warning": 0, "unknown": 0})
    for check_id, check_result in result["checks"].items():
        check = next((c for c in SECURITY_CHECKS if c.id == check_id), None)
        cat = check.category if check else "unknown"
        status = check_result.get("status", "unknown")
        categories[cat][status] = categories[cat].get(status, 0) + 1

    result["category_breakdown"] = dict(categories)

    # Cache
    try:
        r = redis.Redis(
            host=REDIS_HOST,
            port=REDIS_PORT,
            password=REDIS_PASSWORD,
            decode_responses=True,
            socket_connect_timeout=2,
        )
        r.setex(cache_key, CACHE_TTL, json.dumps(result, default=str))
        r.close()
    except Exception:
        pass

    return result


async def get_check_matrix_endpoint(**kw) -> dict:
    """Returns the full security check matrix definition."""
    return get_check_matrix()