"""totalSupply + decimals for launches missing metadata, via Multicall3 (read-only)."""
import json, sys, time, os
from eth_abi import encode, decode
sys.path.insert(0, '/tmp/run'); from rpc import rpc
MC = '0xcA11bde05977b3631167028862bE2a173976CA11'
toks = open('/tmp/rl/nosup.txt').read().split()
OUT = '/tmp/rl/sup_extra.json'
got = json.load(open(OUT)) if os.path.exists(OUT) else {}
toks = [t for t in toks if t not in got]
print('todo', len(toks), flush=True)
B = 300
for i in range(0, len(toks), B):
    g = toks[i:i + B]
    calls = []
    for t in g:
        calls.append((t, True, bytes.fromhex('18160ddd'))); calls.append((t, True, bytes.fromhex('313ce567')))
    data = '0x82ad56cb' + encode(['(address,bool,bytes)[]'], [calls]).hex()
    r = rpc('eth_call', [{'to': MC, 'data': data}, 'latest'])
    if not isinstance(r, str):
        print('err', r, flush=True); time.sleep(5); continue
    res = decode(['(bool,bytes)[]'], bytes.fromhex(r[2:]))[0]
    for k, t in enumerate(g):
        ok1, s = res[2 * k]; ok2, dd = res[2 * k + 1]
        try:
            sup = int.from_bytes(s[:32], 'big') if ok1 and len(s) >= 32 else None
            dec = int.from_bytes(dd[:32], 'big') if ok2 and len(dd) >= 32 else None
        except Exception: sup = dec = None
        got[t] = [sup, dec]
    if (i // B) % 20 == 0:
        json.dump(got, open(OUT, 'w')); print(i, time.strftime('%H:%M:%S'), flush=True)
    time.sleep(0.3)
json.dump(got, open(OUT, 'w')); print('done', len(got), flush=True)
