# Author: Simon-Pierre Boucher # Mail: contact@spboucher.ai """Central config loading — YAML files + environment overrides. Env vars (see .env.example): EXPLORER_DB path to the SQLite DB (default ./explorer.db) (PostgreSQL via DATABASE_URL lands in Deliverable 5) EXPLORER_CHAINS comma-separated subset of chains to index (default all) EXPLORER_CONFIG directory holding chains.yaml / tokens.yaml """ import os import pathlib import yaml ROOT = pathlib.Path(__file__).parent.parent CONFIG_DIR = pathlib.Path(os.environ.get("EXPLORER_CONFIG", ROOT / "config")) def db_path(): return os.environ.get("EXPLORER_DB", str(ROOT / "explorer.db")) def load_chains(): return yaml.safe_load((CONFIG_DIR / "chains.yaml").read_text())["chains"] def load_tokens(): return yaml.safe_load((CONFIG_DIR / "tokens.yaml").read_text())["tokens"] def backfill_blocks(chain_cfg): """How many blocks of history to grow backwards. Per-chain `backfill_days` wins; else EXPLORER_BACKFILL_DAYS env; else 0 (off).""" days = chain_cfg.get("backfill_days") if days is None: days = float(os.environ.get("EXPLORER_BACKFILL_DAYS", 0) or 0) if not days: return 0 return int(days * 86400 / float(chain_cfg.get("block_time", 12))) def selected_chains(chains, tokens, cli_arg=None, families=("evm",)): """Chains to run: CLI arg > EXPLORER_CHAINS env > every chain that has both a chain config and a token list. Chains whose family has no adapter yet are skipped with a warning.""" env = os.environ.get("EXPLORER_CHAINS", "").strip() if cli_arg: wanted = list(cli_arg) elif env: wanted = [c.strip() for c in env.split(",") if c.strip()] else: wanted = [c for c in chains if c in tokens] unknown = [c for c in wanted if c not in chains or c not in tokens] if unknown: raise SystemExit(f"unknown chains (need entries in both YAML files): {unknown}") skipped = [c for c in wanted if chains[c].get("family", "evm") not in families] if skipped: import logging logging.warning("skipping chains with no adapter yet: %s", skipped) return [c for c in wanted if chains[c].get("family", "evm") in families]