import sys,json
sys.argv=["x","h3k","0"]
exec(open("/opt/crew/work/auditor2/v4sim.py").read().split("test,N=")[0])
PM="0x8366a39cc670b4001a1121b8f6a443a643e40951"
TSW="0x40e9cecb9f5f1f1c5b9c97dec2917b7ee92e57ba5563708daca94dd84ad7112f"
def sg(x):
    x=int(x,16); return x-(1<<256) if x>>255 else x
def cmp(pid,nmax=6):
    pl=pools[pid]; key=pool_key(pid,pl["block"]);tok=pl["token"]
    bb=probe_balance(tok);a=probe_allow(tok,W,P2)
    slot2=h(k(pad(UR)+k(pad(tok)+k(pad(W)+pad(1)))))
    n=0
    for b0 in range(pl["block"],pl["block"]+1500,100):
      for g in rpc("eth_getLogs",[{"address":PM,"topics":[TSW,pid],"fromBlock":hex(b0),"toBlock":hex(b0+99)}]).get("result",[]):
        d=g["data"][2:]; a0=sg(d[:64]); a1=sg(d[64:128])
        if a0>0 and a1<0 and n<nmax:
            n+=1; blk=int(g["blockNumber"],16); amt=-a1
            tx=rpc("eth_getTransactionByHash",[g["transactionHash"]])["result"]
            BLK[0]=hex(blk-1)
            ov={tok:{"stateDiff":{bb[1]:h(pad(amt)),a[1]:h(pad(BIG))}},P2:{"stateDiff":{slot2:h(pad(((1<<48)-1)<<160|(1<<160)-1))}},W:{"balance":"0x"+"f"*20}}
            r2=call_at(UR,build(key,tok,amt,1<<120),ov); dd=r2.get("error",{}).get("data","")
            sim=int(dd[-64:],16)/1e18 if len(dd)>=138 else None
            print(pid[:8],"blk",blk,"from",tx["from"][:10],"real_out",round(a0/1e18,6),"fresh_sim",sim,"ratio",(sim/(a0/1e18) if sim else None))
if __name__=="__main__":
    for p in sys.argv_pids if False else []: pass
