File size: 2,045 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
"""
Multi-chain portfolio scanner.
Scans the same wallet address across all supported chains.
"""

import time

from app.adapters.binance_web3 import CHAIN_IDS, CHAIN_NAMES, get_wallet_holdings
from app.analyzer.portfolio import build_portfolio

SCAN_DELAY = 0.3  # seconds between chain requests


def scan_all_chains(address: str) -> dict:
    """
    Scan a wallet across all supported chains.

    Returns:
        {
          "chains": {
            "BSC": {"total_value": float, "token_count": int, "change_24h_pct": float},
            ...
          },
          "grand_total": float,
          "grand_change_24h_usd": float,
          "grand_change_24h_pct": float,
          "errors": [...],
        }
    """
    result = {
        "chains": {},
        "grand_total": 0.0,
        "grand_change_24h_usd": 0.0,
        "grand_change_24h_pct": 0.0,
        "errors": [],
    }

    grand_yesterday = 0.0

    for chain_key, chain_id in CHAIN_IDS.items():
        try:
            holdings = get_wallet_holdings(address, chain_id)
            portfolio = build_portfolio(holdings)

            if portfolio["total_value"] > 0:
                chain_name = CHAIN_NAMES.get(chain_id, chain_key.upper())
                result["chains"][chain_name] = {
                    "total_value": portfolio["total_value"],
                    "token_count": portfolio["token_count"],
                    "change_24h_usd": portfolio["change_24h_usd"],
                    "change_24h_pct": portfolio["change_24h_pct"],
                }
                result["grand_total"] += portfolio["total_value"]
                result["grand_change_24h_usd"] += portfolio["change_24h_usd"]
                grand_yesterday += portfolio["total_value"] - portfolio["change_24h_usd"]

            time.sleep(SCAN_DELAY)

        except Exception as e:
            result["errors"].append(f"{chain_key.upper()}: {e}")

    if grand_yesterday > 0:
        result["grand_change_24h_pct"] = (result["grand_change_24h_usd"] / grand_yesterday) * 100

    return result