import json, time, urllib.request, random

MCP = "https://mcp.nonceprotocol.xyz/mcp"


def call():
    body = json.dumps({"jsonrpc": "2.0", "id": 1, "method": "tools/call",
                       "params": {"name": "protocol_info", "arguments": {}}}).encode()
    req = urllib.request.Request(MCP, data=body, headers={
        "Content-Type": "application/json",
        "Accept": "application/json, text/event-stream",
        "User-Agent": "curl/8.7.1",
    })
    with urllib.request.urlopen(req, timeout=25) as r:
        p = json.loads(r.read().decode())
    if "error" in p:
        raise RuntimeError(str(p["error"]))
    return json.loads(p["result"]["content"][0]["text"])


for i in range(6):
    got = None
    for attempt in range(4):
        try:
            got = call()
            break
        except Exception:
            time.sleep(6 + random.uniform(0, 4))
    stamp = time.strftime("%H:%M:%S", time.localtime())
    if got is None:
        print(stamp, "admission=UNREACHABLE", flush=True)
    else:
        print(stamp, "admission=" + str(got.get("admission")),
              "remaining=" + str(got.get("remainingSupply")),
              "signer=" + ("yes" if got.get("signer") else "no"),
              "benchmark=" + str((got.get("benchmark") or {}).get("version")), flush=True)
    time.sleep(20)
