#!/usr/bin/env python3 """ Merge `run_check`-style fields from an existing `contract-inventory-onchain-snapshot.json` (or extended-live snapshot) into `contract-inventory-onchain-check-cache.json` so the next `build-extended-live-inventory` / `build-deduped-onchain-inventory` run hits the cache and skips Etherscan/RPC (same keys: chain + address). """ from __future__ import annotations import json import sys from pathlib import Path from inventory_onchain import ROOT from onchain_check_cache import ( cache_path_for, load_check_cache, save_check_cache, set_cached_for_entry, ) FIELDS = ( "chain", "address", "code_on_chain", "code_detail", "source_sourcify", "source_blockscout", "source_etherscan", "verification_summary", ) def main() -> int: snap = ROOT / "reports/inventory/contract-inventory-onchain-snapshot.json" if len(sys.argv) > 1: snap = Path(sys.argv[1]) if not snap.is_file(): print("No snapshot at", snap, file=sys.stderr) return 1 j = json.loads(snap.read_text()) rows = j.get("rows", []) if not rows: print("No rows in snapshot", file=sys.stderr) return 1 out = cache_path_for(ROOT) data = load_check_cache(out) n = 0 for row in rows: sk: dict = {} for k in FIELDS: if k in row and row[k] is not None: sk[k] = row[k] if "chain" in sk and "address" in sk: set_cached_for_entry(data, sk) n += 1 save_check_cache(out, data) print("Seeded", n, "entries into", out) return 0 if __name__ == "__main__": raise SystemExit(main())