#!/usr/bin/env python3
"""Re-walk of Musepad's posted deploy link tx 0x3cee9f18 (eip155:4663).
Third-machine verification: is the cited tx tonight's deploy of the named
token 0xe1c0DF65, or (per Monty mmc-filed claim) an 18-day-old call into an
already-deployed contract? Method: receipt + block timestamps + getCode at
blk-1/blk + mint-log decode. Public RPC only, $0."""
import json, time, urllib.request

RPCS = ["https://rpc.ordofi.network", "https://rpc.mainnet.chain.robinhood.com",
        "https://robinhood-rpc.publicnode.com", "https://robinhood.drpc.org"]
TX = "0x3cee9f180a931f88ff5954ae91b1ae6463d6cf993dc3e9215c488ab133ddf299"
TOK = "0xe1c0df658a56cb6ffe1cab050152da5fce5cf890"
_i = 0

def rpc(method, params):
    global _i
    body = json.dumps({"jsonrpc": "2.0", "id": 1, "method": method, "params": params}).encode()
    for _ in range(10):
        u = RPCS[_i % len(RPCS)]; _i += 1
        try:
            d = json.load(urllib.request.urlopen(urllib.request.Request(
                u, data=body, headers={"Content-Type": "application/json"}), timeout=20))
            if "result" in d:
                return d["result"]
        except Exception as e:
            last = e
        time.sleep(0.3)
    raise last

def ts(bn):
    b = rpc("eth_getBlockByNumber", [bn if isinstance(bn, str) else hex(bn), False])
    return int(b["timestamp"], 16)

t = rpc("eth_getTransactionByHash", [TX])
r = rpc("eth_getTransactionReceipt", [TX])
bn = int(t["blockNumber"], 16)
head = int(rpc("eth_blockNumber", []), 16)
print("tx", TX[:18], "| to", t["to"], "| from", t["from"][:12])
print("blk", bn, "ts", time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(ts(bn))),
      "| head", head, "ts", time.strftime("%H:%M:%SZ", time.gmtime(ts(head))),
      "| blocks-behind", head - bn)
print("receipt: status", r["status"], "| contractAddress", r["contractAddress"], "| logs", len(r["logs"]))
c0 = rpc("eth_getCode", [TOK, hex(bn - 1)])
c1 = rpc("eth_getCode", [TOK, hex(bn)])
print("getCode(%s): blk-1 %d bytes -> blk %d bytes" % (TOK, (len(c0)-2)//2, (len(c1)-2)//2))
for lg in r["logs"]:
    if lg["address"].lower() == TOK:
        print("mint Transfer on named token: 0x0 ->", "0x" + lg["topics"][2][-40:],
              "amt", int(lg["data"], 16) / 1e18)
