"""Diagnostic script: list all active BTC binary markets on Polymarket Gamma API. Usage: python debug_list_markets.py """ import os import asyncio import json import aiohttp from dotenv import load_dotenv load_dotenv() def _normalize_proxy(raw: str) -> str: raw = (raw or "").strip() if not raw: return "" if raw.endswith("/"): raw = raw[:-1] if raw.startswith(("http://", "https://", "socks://", "socks4://", "socks5://")): return raw return "http://" + raw _PROXY_URL = _normalize_proxy( os.getenv("HTTPS_PROXY") or os.getenv("HTTP_PROXY") or os.getenv("http_proxy") or os.getenv("https_proxy") or "" ) GAMMA = "https://gamma-api.polymarket.com" async def main(): print(f"[proxy] Using: {_PROXY_URL or 'NONE'}") timeout = aiohttp.ClientTimeout(total=45, connect=15) async with aiohttp.ClientSession(timeout=timeout, trust_env=True) as session: # Try 3 common queries to find BTC markets queries = [ # Query 1: active markets with "btc" in slug {"slug": "btc", "active": "true", "closed": "false", "limit": 50}, # Query 2: active markets with "Bitcoin" in question/title {"title": "Bitcoin", "active": "true", "closed": "false", "limit": 50}, # Query 3: closed=false, order by newest, with tags {"closed": "false", "limit": 100, "order": "end_date", "ascending": "false"}, ] seen = set() all_markets = [] for qi, params in enumerate(queries, 1): print(f"\n--- Query {qi}: {list(params.keys())} ---") try: async with session.get( f"{GAMMA}/markets", params=params, proxy=_PROXY_URL or None, ) as resp: print(f"HTTP {resp.status}") if resp.status != 200: print(await resp.text()) continue markets = await resp.json() print(f"Got {len(markets)} markets in response") except Exception as e: print(f"Request FAILED: {type(e).__name__}: {e}") continue for m in markets: condition_id = m.get("conditionId") or m.get("id") if not condition_id or condition_id in seen: continue seen.add(condition_id) title = (m.get("question") or m.get("title") or "").strip() slug = (m.get("slug") or "").strip() # Only keep markets mentioning BTC/bitcoin text = (title + " " + slug).lower() if "btc" not in text and "bitcoin" not in text: continue end = m.get("endDate") or m.get("end_date_iso") or "" start = m.get("startDate") or m.get("start_date_iso") or "" active = m.get("active") closed = m.get("closed") neg_risk = m.get("negativeRisk") tokens = m.get("tokens") or [] outcomes = [ (t.get("outcome") or t.get("name") or "?")[:6] for t in tokens ] all_markets.append({ "id": condition_id[:12], "slug": slug[:80], "title": title[:120], "active": active, "closed": closed, "neg_risk": neg_risk, "start": str(start)[:25], "end": str(end)[:25], "outcomes": outcomes, }) print("\n" + "=" * 120) print(f"Found {len(all_markets)} BTC-related markets") print("=" * 120) for i, m in enumerate(all_markets, 1): print(f"\n#{i} id={m['id']} active={m['active']} closed={m['closed']} negRisk={m['neg_risk']}") print(f" slug: {m['slug']}") print(f" title: {m['title']}") print(f" outcomes: {m['outcomes']}") print(f" start: {m['start']}") print(f" end: {m['end']}") # Save for later with open("_debug_btc_markets.json", "w", encoding="utf-8") as f: json.dump(all_markets, f, ensure_ascii=False, indent=2) print(f"\nSaved full list to _debug_btc_markets.json") # Also dump slugs for regex analysis print("\n--- All slugs (for regex tuning) ---") for m in all_markets: print(m["slug"]) if __name__ == "__main__": asyncio.run(main())