"""dripfund.py (A1.28): who first funded a wallet, on chain only (explorer is behind a bot wall).
Binary search eth_getBalance (archive) for the first block with balance > 0, then read that block's txs for a direct
transfer to the wallet; if none, report the block (internal transfer, needs a trace)."""
import sys, json
s = open("/root/fwa1/v3dead.py").read(); k = s.index("def post"); import re; k = k + re.search(r"\n(?=\S)", s[k:]).start()
exec(s[:k])
def call(m, p): return post({"jsonrpc": "2.0", "id": 1, "method": m, "params": p}).get("result")
for w in sys.argv[1:]:
    w = w.lower(); hi = int(call("eth_blockNumber", []), 16); lo = 0
    if int(call("eth_getBalance", [w, hex(hi)]), 16) == 0 and int(call("eth_getTransactionCount", [w, hex(hi)]), 16) == 0:
        print(w, "never funded"); continue
    while hi - lo > 1:
        m = (lo + hi) // 2
        bal = call("eth_getBalance", [w, hex(m)])
        if bal is None: print(w, "no archive at", m); break
        if int(bal, 16) > 0: hi = m
        else: lo = m
    b = call("eth_getBlockByNumber", [hex(hi), True]); f = None
    for t in b["transactions"]:
        if (t.get("to") or "").lower() == w and int(t["value"], 16) > 0: f = (t["from"].lower(), int(t["value"], 16) / 1e18, t["hash"]); break
    print(w, "first funded block", hi, "time", int(b["timestamp"], 16), "funder", f if f else "internal transfer (no direct tx)", flush=True)
