WSOL="So11111111111111111111111111111111111111112"
STABLE={"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v":"USDC","Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB":"USDT",
 "USD1ttGY1N17NEEHLmELoaybftRBUSErhqYiQzvEmuB":"USD1","2b1kV6DkPAnxd5ixfnxCpjxmKwqjjaYmCZfHsFu24GXo":"PYUSD"}
def parse(t,mint,pools):
    if not t or t.get("meta") is None or t["meta"].get("err"):return None
    m=t["meta"];msg=t["transaction"]["message"]
    keys=[k["pubkey"] if isinstance(k,dict) else k for k in msg["accountKeys"]]
    signers=[k["pubkey"] for k in msg["accountKeys"] if isinstance(k,dict) and k.get("signer")]
    payer=keys[0]
    pre,post=m["preBalances"],m["postBalances"]
    def tb(lst):
        d={}
        for b in lst or []:
            a=b["uiTokenAmount"].get("uiAmount") or 0
            d[(b.get("owner"),b["mint"],b["accountIndex"])]=a
        return d
    P,Q=tb(m.get("preTokenBalances")),tb(m.get("postTokenBalances"))
    allk=set(P)|set(Q)
    delta={}   # (owner,mint)->delta
    preamt={}
    for k in allk:
        o,mi,_=k;delta[(o,mi)]=delta.get((o,mi),0)+Q.get(k,0)-P.get(k,0);preamt[(o,mi)]=preamt.get((o,mi),0)+P.get(k,0)
    md={o:v for (o,mi),v in delta.items() if mi==mint}
    pos={o:v for o,v in md.items() if v>0 and o not in pools}
    neg={o:v for o,v in md.items() if v<0}
    if not pos or not neg:return None           # not a swap buy (transfer, mint, sell)
    buyer=max(pos,key=pos.get);multi=len(pos)>1
    def sol_dec(a):
        if a in keys:i=keys.index(a);return (pre[i]-post[i])/1e9
        return 0
    def tok_dec(a,mi):return -delta.get((a,mi),0)
    cands=[buyer]+([payer] if payer!=buyer else [])
    spent_sol=max(sol_dec(a)+tok_dec(a,WSOL) for a in cands)
    spent_stab=max(sum(tok_dec(a,s) for s in STABLE) for a in cands)
    if spent_sol<=0.001 and spent_stab<=0.5:return None   # tokens received without paying -> not a buy by this wallet
    cash_sol=None
    if buyer in keys:cash_sol=pre[keys.index(buyer)]/1e9
    cash_wsol=preamt.get((buyer,WSOL),0)
    cash_stab={STABLE[s]:preamt.get((buyer,s),0) for s in STABLE if (buyer,s) in preamt}
    return {"sig":t["transaction"]["signatures"][0],"ts":t["blockTime"],"buyer":buyer,"payer":payer,
      "buyer_is_signer":buyer in signers,"buyer_is_payer":buyer==payer,"multi":multi,"tokens":pos[buyer],
      "spent_sol":spent_sol,"spent_stab":spent_stab,"cash_sol":cash_sol,"cash_wsol":cash_wsol,"cash_stab":cash_stab,"nsig":len(signers)}

import requests,time,random,json,sys,threading,bisect,concurrent.futures as cf
SOLPX=[[1785542400, 72.795], [1785546000, 72.942], [1785549600, 72.968], [1785553200, 73.009], [1785556800, 72.955], [1785560400, 73.091], [1785564000, 72.869], [1785567600, 72.927], [1785571200, 72.909], [1785574800, 72.974], [1785578400, 72.808], [1785582000, 72.772], [1785585600, 72.828], [1785589200, 72.87], [1785592800, 72.917], [1785596400, 72.945], [1785600000, 72.746], [1785603600, 72.623], [1785607200, 71.847], [1785610800, 71.019], [1785614400, 71.195], [1785618000, 71.496], [1785621600, 71.792], [1785625200, 71.86], [1785628800, 71.894], [1785632400, 72.035], [1785636000, 72.594], [1785639600, 73.072], [1785643200, 73.145], [1785646800, 73.469], [1785650400, 73.544], [1785654000, 73.387], [1785657600, 73.426], [1785661200, 73.123], [1785664800, 73.319], [1785668400, 73.206], [1785672000, 72.931], [1785675600, 72.874], [1785679200, 72.894], [1785682800, 73.029], [1785686400, 72.96], [1785690000, 73.075], [1785693600, 73.252], [1785697200, 73.499], [1785700800, 73.65], [1785704400, 73.624], [1785708000, 73.939], [1785711600, 73.583], [1785715200, 73.547], [1785718800, 73.143], [1785722400, 72.935], [1785726000, 72.953], [1785729600, 72.848], [1785733200, 72.795], [1785736800, 72.847], [1785740400, 72.544], [1785744000, 72.448], [1785747600, 72.293], [1785751200, 72.676], [1785754800, 72.545], [1785758400, 72.374], [1785762000, 72.345], [1785765600, 73.178], [1785769200, 73.705], [1785772800, 73.53], [1785776400, 73.336], [1785780000, 73.619], [1785783600, 73.913], [1785787200, 73.966], [1785790800, 73.787], [1785794400, 73.653], [1785798000, 73.466], [1785801600, 73.489], [1785805200, 73.009], [1785808800, 73.733], [1785812400, 73.808], [1785816000, 73.629], [1785819600, 73.795], [1785823200, 73.68], [1785826800, 73.352], [1785830400, 73.619], [1785834000, 73.268], [1785837600, 73.208], [1785841200, 73.538], [1785844800, 73.874], [1785848400, 73.758], [1785852000, 73.575], [1785855600, 73.77], [1785859200, 73.931], [1785862800, 73.647], [1785866400, 73.722], [1785870000, 74.1], [1785873600, 74.189], [1785877200, 74.217], [1785880800, 74.077], [1785884400, 74.046], [1785888000, 73.713], [1785891600, 73.451], [1785895200, 74.042], [1785898800, 74.152], [1785902400, 73.727], [1785906000, 73.717], [1785909600, 74.092], [1785913200, 73.957], [1785916800, 73.928], [1785920400, 74.007], [1785924000, 73.944], [1785927600, 73.789], [1785931200, 73.715], [1785934800, 74.026], [1785938400, 73.862], [1785942000, 73.907], [1785945600, 73.911], [1785949200, 74.234], [1785952800, 74.496], [1785956400, 74.468], [1785960000, 74.425], [1785963600, 74.326], [1785967200, 74.089], [1785970800, 73.95], [1785974400, 73.986], [1785978000, 74.018], [1785981600, 73.702], [1785985200, 73.475], [1785988800, 73.421], [1785992400, 73.785], [1785996000, 74.154], [1785999600, 73.969], [1786003200, 73.807], [1786006800, 73.928], [1786010400, 73.229], [1786014000, 73.237], [1786017600, 73.393], [1786021200, 73.089], [1786024800, 73.178], [1786028400, 73.41], [1786032000, 73.298], [1786035600, 73.387], [1786039200, 73.128], [1786042800, 72.757], [1786046400, 72.763], [1786050000, 72.809], [1786053600, 72.811], [1786057200, 72.63], [1786060800, 72.631], [1786064400, 72.601], [1786068000, 72.67], [1786071600, 72.657], [1786075200, 72.548], [1786078800, 72.611], [1786082400, 72.818], [1786086000, 72.762], [1786089600, 72.868], [1786093200, 73.139], [1786096800, 73.573], [1786100400, 73.474], [1786104000, 73.562], [1786107600, 73.851], [1786111200, 74.106], [1786114800, 73.674], [1786118400, 73.87], [1786122000, 73.544], [1786125600, 73.364], [1786129200, 73.347], [1786132800, 73.963], [1786136400, 73.79], [1786140000, 73.605], [1786143600, 73.599], [1786147200, 73.617], [1786150800, 73.668], [1786154400, 73.846], [1786158000, 73.826], [1786161600, 74.826], [1786165200, 74.595], [1786168800, 74.598], [1786172400, 74.668], [1786176000, 74.772], [1786179600, 74.759], [1786183200, 74.833], [1786186800, 75.032], [1786190400, 75.529], [1786194000, 75.387], [1786197600, 75.484], [1786201200, 76.455], [1786204800, 76.328], [1786208400, 76.331], [1786212000, 76.336], [1786215600, 76.081], [1786219200, 76.105], [1786222800, 76.201], [1786226400, 76.164], [1786230000, 75.857], [1786233600, 75.96], [1786237200, 75.957], [1786240800, 75.834], [1786244400, 75.813], [1786248000, 75.933], [1786251600, 75.916], [1786255200, 75.953], [1786258800, 76.354], [1786262400, 76.296], [1786266000, 76.263], [1786269600, 76.381], [1786273200, 76.326], [1786276800, 76.447], [1786280400, 76.31], [1786284000, 76.523], [1786287600, 76.908], [1786291200, 77.336], [1786294800, 77.263], [1786298400, 77.115], [1786302000, 77.149], [1786305600, 77.178], [1786309200, 77.21], [1786312800, 77.316], [1786316400, 76.759], [1786320000, 76.214], [1786323600, 76.494], [1786327200, 76.839], [1786330800, 76.751], [1786334400, 76.567], [1786338000, 76.512], [1786341600, 76.831], [1786345200, 76.931], [1786348800, 76.937], [1786352400, 76.883], [1786356000, 76.512], [1786359600, 76.77], [1786363200, 76.981], [1786366800, 76.579], [1786370400, 76.243], [1786374000, 76.21], [1786377600, 75.91], [1786381200, 75.623], [1786384800, 75.905], [1786388400, 75.763], [1786392000, 76.103], [1786395600, 76.374], [1786399200, 76.247], [1786402800, 76.149], [1786406400, 75.925], [1786410000, 75.815], [1786413600, 76.02], [1786417200, 75.978], [1786420800, 76.008], [1786424400, 76.018], [1786428000, 75.745], [1786431600, 75.738], [1786435200, 75.619], [1786438800, 75.869], [1786442400, 75.778], [1786446000, 75.889], [1786449600, 76.166], [1786453200, 75.886], [1786456800, 75.976], [1786460400, 75.147], [1786464000, 74.88], [1786467600, 74.879], [1786471200, 75.109], [1786474800, 75.192], [1786478400, 75.607], [1786482000, 75.998], [1786485600, 76.356], [1786489200, 76.343], [1786492800, 76.216], [1786496400, 76.306], [1786500000, 76.369], [1786503600, 76.366], [1786507200, 76.349], [1786510800, 76.231], [1786514400, 76.257], [1786518000, 75.977], [1786521600, 76.187], [1786525200, 76.112], [1786528800, 76.701], [1786532400, 76.715], [1786536000, 76.773], [1786539600, 76.586], [1786543200, 76.166], [1786546800, 75.399], [1786550400, 75.684], [1786554000, 75.628], [1786557600, 75.854], [1786561200, 75.837], [1786564800, 75.69], [1786568400, 75.863], [1786572000, 75.447], [1786575600, 75.459], [1786579200, 75.559], [1786582800, 75.566], [1786586400, 75.579], [1786590000, 75.898], [1786593600, 76.181], [1786597200, 76.061], [1786600800, 76.458], [1786604400, 76.448], [1786608000, 76.384], [1786611600, 76.143], [1786615200, 76.093], [1786618800, 75.687], [1786622400, 75.582], [1786626000, 76.05], [1786629600, 76.115], [1786633200, 76.408], [1786636800, 75.63], [1786640400, 75.499], [1786644000, 75.709], [1786647600, 75.948], [1786651200, 76.124], [1786654800, 76.154], [1786658400, 76.148], [1786662000, 76.189], [1786665600, 76.202], [1786669200, 76.005], [1786672800, 76.027], [1786676400, 75.987], [1786680000, 75.743], [1786683600, 75.766], [1786687200, 75.892], [1786690800, 75.482], [1786694400, 75.532], [1786698000, 75.783], [1786701600, 75.411], [1786705200, 75.327], [1786708800, 75.465], [1786712400, 75.39], [1786716000, 75.27], [1786719600, 75.205], [1786723200, 75.608], [1786726800, 75.41], [1786730400, 75.215], [1786734000, 74.925], [1786737600, 74.958], [1786741200, 75.078], [1786744800, 75.116], [1786748400, 75.123], [1786752000, 75.335], [1786755600, 75.296], [1786759200, 75.349], [1786762800, 75.595], [1786766400, 75.361], [1786770000, 75.466], [1786773600, 75.29], [1786777200, 75.366], [1786780800, 75.168], [1786784400, 75.02], [1786788000, 75.173], [1786791600, 75.223], [1786795200, 75.182], [1786798800, 75.345], [1786802400, 75.501], [1786806000, 75.393], [1786809600, 75.462], [1786813200, 75.425], [1786816800, 75.598], [1786820400, 75.495], [1786824000, 75.493], [1786827600, 75.465], [1786831200, 75.571], [1786834800, 75.524], [1786838400, 75.28], [1786842000, 75.401], [1786845600, 75.348], [1786849200, 75.517], [1786852800, 75.526], [1786856400, 75.4], [1786860000, 75.3], [1786863600, 75.318], [1786867200, 75.394], [1786870800, 75.408], [1786874400, 75.256], [1786878000, 75.205], [1786881600, 75.22], [1786885200, 75.163], [1786888800, 75.249], [1786892400, 75.334], [1786896000, 75.509], [1786899600, 75.498], [1786903200, 75.216], [1786906800, 75.129], [1786910400, 75.167], [1786914000, 74.955], [1786917600, 74.298], [1786921200, 74.44], [1786924800, 74.548], [1786928400, 74.561], [1786932000, 75.063], [1786935600, 75.425], [1786939200, 75.463], [1786942800, 75.503], [1786946400, 75.352], [1786950000, 75.829], [1786953600, 75.737], [1786957200, 75.179], [1786960800, 75.28], [1786964400, 75.701], [1786968000, 75.631], [1786971600, 75.361], [1786975200, 75.553], [1786978800, 75.908], [1786982400, 76.039], [1786986000, 75.953], [1786989600, 75.868], [1786993200, 75.837], [1786996800, 75.731], [1787000400, 75.933], [1787004000, 75.907], [1787007600, 75.667], [1787011200, 75.973], [1787014800, 75.977], [1787018400, 75.535], [1787022000, 75.202], [1787025600, 75.43], [1787029200, 75.766], [1787032800, 75.669], [1787036400, 75.94], [1787040000, 76.026], [1787043600, 76.091], [1787047200, 75.877], [1787050800, 75.77], [1787054400, 76.289], [1787058000, 76.165], [1787061600, 76.134], [1787065200, 76.772], [1787068800, 77.142], [1787072400, 77.028], [1787076000, 76.848], [1787079600, 77.202], [1787083200, 77.114], [1787086800, 76.845], [1787090400, 76.938], [1787094000, 76.905], [1787097600, 77.002], [1787101200, 76.876], [1787104800, 76.747], [1787108400, 76.76], [1787112000, 76.933], [1787115600, 76.808], [1787119200, 76.759], [1787122800, 77.023], [1787126400, 76.899], [1787130000, 77.347], [1787133600, 77.257], [1787137200, 77.318], [1787140800, 77.483], [1787144400, 78.329], [1787148000, 78.365], [1787151600, 80.851], [1787155200, 81.838], [1787158800, 81.565], [1787162400, 81.632], [1787166000, 82.186], [1787169600, 82.328], [1787173200, 83.753], [1787176800, 86.644], [1787180400, 85.438], [1787184000, 85.333], [1787187600, 84.91], [1787191200, 85.272], [1787194800, 84.67], [1787198400, 84.432], [1787202000, 84.79], [1787205600, 84.889], [1787209200, 85.845], [1787212800, 86.076], [1787216400, 87.476], [1787220000, 87.27], [1787223600, 87.353], [1787227200, 87.219], [1787230800, 87.014], [1787234400, 86.491], [1787238000, 86.511], [1787241600, 87.314], [1787245200, 87.681], [1787248800, 87.55], [1787252400, 86.868], [1787256000, 87.252], [1787259600, 87.545], [1787263200, 87.472], [1787266800, 87.2], [1787270400, 87.652], [1787274000, 88.186], [1787277600, 88.979], [1787281200, 89.225], [1787284800, 89.118], [1787288400, 89.381], [1787292000, 90.333], [1787295600, 90.158], [1787299200, 90.364], [1787302800, 93.191], [1787306400, 91.638], [1787310000, 91.449], [1787313600, 90.293], [1787317200, 90.82], [1787320800, 91.014], [1787324400, 91.838], [1787328000, 91.309], [1787331600, 91.594], [1787335200, 91.766], [1787338800, 91.528], [1787342400, 90.918], [1787346000, 91.799], [1787349600, 93.711], [1787353200, 94.288], [1787356800, 93.694], [1787360400, 93.854], [1787364000, 93.765], [1787367600, 95.541], [1787371200, 96.817], [1787374800, 99.215], [1787378400, 94.193], [1787382000, 94.289], [1787385600, 94.441], [1787389200, 94.061], [1787392800, 93.899], [1787396400, 92.851], [1787400000, 93.22], [1787403600, 93.86], [1787407200, 94.1], [1787410800, 93.18], [1787414400, 93.063], [1787418000, 94.31], [1787421600, 94.232], [1787425200, 94.399], [1787428800, 94.516], [1787432400, 94.391], [1787436000, 92.9], [1787439600, 93.597], [1787443200, 93.885], [1787446800, 96.322], [1787450400, 95.995], [1787454000, 94.939], [1787457600, 93.706], [1787461200, 92.605], [1787464800, 92.335], [1787468400, 92.186], [1787472000, 92.416], [1787475600, 93.165], [1787479200, 93.318], [1787482800, 93.632], [1787486400, 94.563], [1787490000, 94.569], [1787493600, 95.37], [1787497200, 95.024], [1787500800, 95.387], [1787504400, 95.415], [1787508000, 95.118], [1787511600, 95.269], [1787515200, 95.054], [1787518800, 95.054], [1787522400, 95.369], [1787526000, 95.56], [1787529600, 95.408], [1787533200, 95.455], [1787536800, 93.922], [1787540400, 94.539], [1787544000, 93.721], [1787547600, 94.142], [1787551200, 94.267], [1787554800, 94.518], [1787558400, 94.081], [1787562000, 93.905], [1787565600, 94.643], [1787569200, 94.671], [1787572800, 95.723], [1787576400, 96.225], [1787580000, 95.006], [1787583600, 96.051], [1787587200, 96.397], [1787590800, 96.054], [1787594400, 95.365], [1787598000, 95.921], [1787601600, 96.563], [1787605200, 96.515], [1787608800, 97.477], [1787612400, 97.43], [1787616000, 98.867], [1787619600, 101.752], [1787623200, 100.442], [1787626800, 100.9], [1787630400, 101.512], [1787634000, 101.264], [1787637600, 101.888], [1787641200, 100.664], [1787644800, 100.298], [1787648400, 100.374], [1787652000, 99.744], [1787655600, 99.63], [1787659200, 98.586], [1787662800, 98.594], [1787666400, 97.064], [1787670000, 98.154], [1787673600, 98.331], [1787677200, 98.169], [1787680800, 98.179], [1787684400, 98.533], [1787688000, 97.925], [1787691600, 96.695], [1787695200, 97.178], [1787698800, 97.225], [1787702400, 96.576], [1787706000, 96.671], [1787709600, 97.004], [1787713200, 97.263], [1787716800, 97.039], [1787720400, 96.612], [1787724000, 97.059], [1787727600, 96.776], [1787731200, 96.921], [1787734800, 96.793], [1787738400, 96.145], [1787742000, 97.488], [1787745600, 97.02], [1787749200, 97.052], [1787752800, 96.544], [1787756400, 95.46], [1787760000, 95.953], [1787763600, 95.964], [1787767200, 96.955], [1787770800, 96.823], [1787774400, 96.773], [1787778000, 96.917], [1787781600, 98.158], [1787785200, 99.746], [1787788800, 102.049], [1787792400, 100.744], [1787796000, 101.47], [1787799600, 101.157], [1787803200, 101.054], [1787806800, 101.055], [1787810400, 101.9], [1787814000, 101.262], [1787817600, 101.817], [1787821200, 104.497], [1787824800, 104.36], [1787828400, 104.066], [1787832000, 103.992], [1787835600, 104.499], [1787839200, 106.036], [1787842800, 106.739], [1787846400, 107.255], [1787850000, 108.952], [1787853600, 108.217], [1787857200, 108.323], [1787860800, 109.266], [1787864400, 109.02], [1787868000, 108.745], [1787871600, 109.183], [1787875200, 109.177], [1787878800, 109.16], [1787882400, 107.868], [1787886000, 106.536], [1787889600, 106.854], [1787893200, 106.753], [1787896800, 107.21], [1787900400, 107.536], [1787904000, 106.439], [1787907600, 106.056], [1787911200, 105.94], [1787914800, 106.176], [1787918400, 105.821], [1787922000, 104.882], [1787925600, 105.403], [1787929200, 106.365], [1787932800, 105.34], [1787936400, 105.177], [1787940000, 104.703], [1787943600, 103.038], [1787947200, 103.364], [1787950800, 103.847], [1787954400, 103.421], [1787958000, 104.1], [1787961600, 104.114], [1787965200, 103.902], [1787968800, 104.167], [1787972400, 103.939], [1787976000, 103.889], [1787979600, 103.876], [1787983200, 103.998], [1787986800, 103.588], [1787990400, 103.726], [1787994000, 103.449], [1787997600, 103.331], [1788001200, 103.449], [1788004800, 103.505], [1788008400, 103.611], [1788012000, 103.82], [1788015600, 105.362], [1788019200, 105.017], [1788022800, 104.886], [1788026400, 104.836], [1788030000, 105.123], [1788033600, 105.364], [1788037200, 105.232], [1788040800, 105.042], [1788044400, 105.504], [1788048000, 105.594], [1788051600, 105.276], [1788055200, 105.23], [1788058800, 105.228], [1788062400, 105.061], [1788066000, 105.05], [1788069600, 105.046], [1788073200, 105.374], [1788076800, 104.841], [1788080400, 104.733], [1788084000, 104.599], [1788087600, 105.102], [1788091200, 105.071], [1788094800, 105.992], [1788098400, 107.122], [1788102000, 106.743], [1788105600, 106.247], [1788109200, 106.827], [1788112800, 106.374], [1788116400, 106.685], [1788120000, 105.79], [1788123600, 104.769], [1788127200, 104.434], [1788130800, 103.765], [1788134400, 101.683], [1788138000, 102.107], [1788141600, 101.466], [1788145200, 101.417], [1788148800, 101.931], [1788152400, 101.436], [1788156000, 102.628], [1788159600, 102.709], [1788163200, 102.934], [1788166800, 103.223], [1788170400, 103.072], [1788174000, 103.622], [1788177600, 103.345], [1788181200, 102.728], [1788184800, 102.356], [1788188400, 103.495], [1788192000, 102.809], [1788195600, 102.829], [1788199200, 103.617], [1788202800, 104.483], [1788206400, 103.619], [1788210000, 103.721], [1788213600, 103.675], [1788217200, 103.161], [1788220800, 103.011], [1788224400, 103.148], [1788228000, 103.161], [1788231600, 103.35], [1788235200, 103.621], [1788238800, 103.573], [1788242400, 104.256], [1788246000, 103.347], [1788249600, 103.118], [1788253200, 102.073], [1788256800, 101.958], [1788260400, 102.352], [1788264000, 102.237], [1788267600, 101.894], [1788271200, 101.911], [1788274800, 101.97], [1788278400, 102.012], [1788282000, 100.928], [1788285600, 100.563], [1788289200, 99.528], [1788292800, 100.039], [1788296400, 100.004], [1788300000, 99.548], [1788303600, 99.611], [1788307200, 99.965], [1788310800, 99.618], [1788314400, 99.06], [1788318000, 99.863], [1788321600, 100.227], [1788325200, 100.06], [1788328800, 100.242], [1788332400, 100.026], [1788336000, 99.815], [1788339600, 98.986], [1788343200, 98.588], [1788346800, 98.162], [1788350400, 98.037], [1788354000, 97.653], [1788357600, 99.114], [1788361200, 98.877], [1788364800, 99.244], [1788368400, 98.801], [1788372000, 99.266], [1788375600, 99.446], [1788379200, 99.367], [1788382800, 99.686], [1788386400, 99.609], [1788390000, 99.518], [1788393600, 100.391], [1788397200, 99.509], [1788400800, 100.139], [1788404400, 101.029], [1788408000, 100.499], [1788411600, 100.872], [1788415200, 100.834], [1788418800, 100.837], [1788422400, 100.367], [1788426000, 100.593], [1788429600, 100.132], [1788433200, 100.384], [1788436800, 100.779], [1788440400, 101.653], [1788444000, 101.328], [1788447600, 104.955], [1788451200, 105.126], [1788454800, 104.548], [1788458400, 105.07], [1788462000, 105.198], [1788465600, 105.193], [1788469200, 104.931], [1788472800, 105.206], [1788476400, 104.163], [1788480000, 103.961], [1788483600, 103.54], [1788487200, 103.795], [1788490800, 103.737], [1788494400, 103.719], [1788498000, 103.868], [1788501600, 103.692], [1788505200, 103.592], [1788508800, 103.574], [1788512400, 104.338], [1788516000, 104.057], [1788519600, 104.098], [1788523200, 104.091], [1788526800, 101.559], [1788530400, 101.641], [1788534000, 100.8], [1788537600, 101.438], [1788541200, 101.845], [1788544800, 101.255], [1788548400, 101.659], [1788552000, 101.709], [1788555600, 101.775], [1788559200, 101.599], [1788562800, 101.748], [1788566400, 101.929], [1788570000, 101.862], [1788573600, 101.879], [1788577200, 101.977], [1788580800, 101.897], [1788584400, 101.777], [1788588000, 101.881], [1788591600, 102.299], [1788595200, 102.226], [1788598800, 102.574], [1788602400, 102.548], [1788606000, 102.533], [1788609600, 102.35], [1788613200, 103.251], [1788616800, 102.658], [1788620400, 102.878], [1788624000, 102.939], [1788627600, 103.401], [1788631200, 104.251], [1788634800, 103.88], [1788638400, 103.438], [1788642000, 103.245], [1788645600, 103.727], [1788649200, 103.378], [1788652800, 103.166], [1788656400, 103.322], [1788660000, 104.073], [1788663600, 104.384], [1788667200, 105.988], [1788670800, 105.747], [1788674400, 106.364], [1788678000, 105.371], [1788681600, 105.263], [1788685200, 105.198], [1788688800, 106.492], [1788692400, 106.61], [1788696000, 106.446], [1788699600, 106.82], [1788703200, 107.079], [1788706800, 105.666], [1788710400, 105.759], [1788714000, 106.08], [1788717600, 106.187], [1788721200, 105.983], [1788724800, 105.469], [1788728400, 105.704], [1788732000, 105.922], [1788735600, 105.847], [1788739200, 106.49], [1788742800, 106.037], [1788746400, 105.312], [1788750000, 105.731], [1788753600, 104.936], [1788757200, 105.062], [1788760800, 105.107], [1788764400, 105.069], [1788768000, 104.443], [1788771600, 104.69], [1788775200, 104.648], [1788778800, 104.998], [1788782400, 104.92], [1788786000, 105.713], [1788789600, 105.041], [1788793200, 104.649], [1788796800, 103.227], [1788800400, 103.836], [1788804000, 104.014], [1788807600, 103.904], [1788811200, 104.329], [1788814800, 104.08], [1788818400, 103.855], [1788822000, 103.508], [1788825600, 103.772], [1788829200, 104.065], [1788832800, 104.357], [1788836400, 103.277], [1788840000, 103.306], [1788843600, 103.007], [1788847200, 102.943], [1788850800, 102.666], [1788854400, 102.841], [1788858000, 103.002], [1788861600, 103.624], [1788865200, 103.371], [1788868800, 102.785], [1788872400, 103.066], [1788876000, 102.67], [1788879600, 103.297], [1788883200, 104.185], [1788886800, 104.202], [1788890400, 103.701], [1788894000, 103.401], [1788897600, 103.176], [1788901200, 103.1], [1788904800, 102.984], [1788908400, 103.395], [1788912000, 103.36], [1788915600, 103.769], [1788919200, 103.771], [1788922800, 103.485], [1788926400, 103.305], [1788930000, 104.378], [1788933600, 104.057], [1788937200, 104.511], [1788940800, 104.562], [1788944400, 104.768], [1788948000, 103.491], [1788951600, 103.779], [1788955200, 104.238], [1788958800, 104.649], [1788962400, 103.787], [1788966000, 103.472], [1788969600, 103.059], [1788973200, 103.365], [1788976800, 103.527], [1788980400, 103.257], [1788984000, 102.3], [1788987600, 102.554], [1788991200, 102.124], [1788994800, 100.774], [1788998400, 101.568], [1789002000, 100.989], [1789005600, 101.026], [1789009200, 101.851], [1789012800, 101.791], [1789016400, 101.949], [1789020000, 102.06], [1789023600, 101.902], [1789027200, 101.266], [1789030800, 101.134], [1789034400, 101.061], [1789038000, 101.023], [1789041600, 100.986], [1789045200, 98.949], [1789048800, 99.724], [1789052400, 100.137], [1789056000, 99.662], [1789059600, 99.617], [1789063200, 99.95], [1789066800, 99.855], [1789070400, 99.67], [1789074000, 100.036], [1789077600, 99.753], [1789081200, 99.249], [1789084800, 98.631], [1789088400, 99.128], [1789092000, 99.41], [1789095600, 99.314], [1789099200, 99.302], [1789102800, 99.662], [1789106400, 99.853], [1789110000, 99.773], [1789113600, 99.706], [1789117200, 99.892], [1789120800, 99.389], [1789124400, 99.165], [1789128000, 99.434], [1789131600, 101.588], [1789135200, 104.776], [1789138800, 103.256], [1789142400, 101.599], [1789146000, 102.354], [1789149600, 101.938], [1789153200, 101.039], [1789156800, 102.007], [1789160400, 102.484], [1789164000, 102.09], [1789167600, 101.828], [1789171200, 102.394], [1789174800, 101.898], [1789178400, 101.995], [1789182000, 101.813], [1789185600, 101.611], [1789189200, 101.593], [1789192800, 101.632], [1789196400, 101.674], [1789200000, 101.641], [1789203600, 101.802], [1789207200, 101.989], [1789210800, 102.119], [1789214400, 102.109], [1789218000, 102.03], [1789221600, 101.952], [1789225200, 101.983], [1789228800, 102.04], [1789232400, 102.049], [1789236000, 101.956], [1789239600, 101.619], [1789243200, 101.261], [1789246800, 101.34], [1789250400, 101.45], [1789254000, 101.665], [1789257600, 101.752], [1789261200, 101.714], [1789264800, 102.272], [1789268400, 101.885], [1789272000, 101.594], [1789275600, 101.696], [1789279200, 101.791], [1789282800, 101.199], [1789286400, 100.688], [1789290000, 100.003], [1789293600, 99.841], [1789297200, 99.526], [1789300800, 99.784], [1789304400, 99.663], [1789308000, 100.071], [1789311600, 100.653], [1789315200, 100.224], [1789318800, 100.787], [1789322400, 100.97], [1789326000, 101.168], [1789329600, 100.939], [1789333200, 101.454], [1789336800, 101.184], [1789340400, 99.7], [1789344000, 99.326], [1789347600, 99.633], [1789351200, 100.076], [1789354800, 100.956], [1789358400, 100.939], [1789362000, 101.141], [1789365600, 101.091], [1789369200, 101.498], [1789372800, 101.581], [1789376400, 101.451], [1789380000, 101.921], [1789383600, 101.746], [1789387200, 101.329], [1789390800, 101.32], [1789394400, 101.352], [1789398000, 101.578], [1789401600, 101.945], [1789405200, 102.563], [1789408800, 103.044], [1789412400, 103.6], [1789416000, 103.241], [1789419600, 103.985], [1789423200, 103.214], [1789426800, 102.849], [1789430400, 102.514], [1789434000, 102.457], [1789437600, 102.248], [1789441200, 101.957], [1789444800, 101.498], [1789448400, 101.254], [1789452000, 101.323], [1789455600, 100.867], [1789459200, 100.407], [1789462800, 100.571], [1789466400, 100.822], [1789470000, 100.935], [1789473600, 100.802], [1789477200, 100.967], [1789480800, 100.455], [1789484400, 98.839], [1789488000, 99.326], [1789491600, 99.059], [1789495200, 100.163], [1789498800, 98.136], [1789502400, 97.849], [1789506000, 97.355], [1789509600, 96.494], [1789513200, 97.106], [1789516800, 96.873], [1789520400, 97.028], [1789524000, 96.429], [1789527600, 97.174], [1789531200, 97.188], [1789534800, 97.212], [1789538400, 97.167], [1789542000, 97.291], [1789545600, 96.882], [1789549200, 96.953], [1789552800, 97.237], [1789556400, 97.175], [1789560000, 97.947], [1789563600, 97.494], [1789567200, 97.12], [1789570800, 96.942], [1789574400, 97.063], [1789578000, 97.168], [1789581600, 97.022], [1789585200, 98.101], [1789588800, 98.039], [1789592400, 98.625], [1789596000, 98.357], [1789599600, 97.426], [1789603200, 98.542], [1789606800, 98.644], [1789610400, 99.19], [1789614000, 99.31], [1789617600, 99.873], [1789621200, 99.819], [1789624800, 99.462], [1789628400, 99.663], [1789632000, 99.945], [1789635600, 100.23], [1789639200, 100.159], [1789642800, 99.743], [1789646400, 100.004], [1789650000, 100.67], [1789653600, 100.616], [1789657200, 101.029], [1789660800, 101.187], [1789664400, 101.285], [1789668000, 101.448], [1789671600, 100.775], [1789675200, 100.849], [1789678800, 101.214], [1789682400, 101.026], [1789686000, 101.326], [1789689600, 101.569], [1789693200, 101.32], [1789696800, 102.378], [1789700400, 102.666], [1789704000, 104.582], [1789707600, 105.681], [1789711200, 105.313], [1789714800, 105.449], [1789718400, 105.737], [1789722000, 106.125], [1789725600, 106.335], [1789729200, 106.186], [1789732800, 105.629], [1789736400, 105.69], [1789740000, 108.445], [1789743600, 110.664], [1789747200, 111.236], [1789750800, 111.668], [1789754400, 112.415], [1789758000, 112.175], [1789761600, 114.021], [1789765200, 113.532], [1789768800, 113.242], [1789772400, 113.16], [1789776000, 112.702], [1789779600, 113.589], [1789783200, 113.381], [1789786800, 113.548], [1789790400, 113.233], [1789794000, 112.331], [1789797600, 111.997], [1789801200, 111.81], [1789804800, 111.602], [1789808400, 112.234], [1789812000, 111.816], [1789815600, 111.643], [1789819200, 111.852], [1789822800, 111.748], [1789826400, 111.607], [1789830000, 111.787], [1789833600, 111.619], [1789837200, 111.933], [1789840800, 111.418], [1789844400, 111.073], [1789848000, 111.145], [1789851600, 111.057], [1789855200, 110.548], [1789858800, 110.622], [1789862400, 111.015], [1789866000, 110.906], [1789869600, 110.097], [1789873200, 107.529], [1789876800, 108.877], [1789880400, 108.978], [1789884000, 108.732], [1789887600, 108.413], [1789891200, 108.44], [1789894800, 108.037], [1789898400, 108.263], [1789902000, 108.008], [1789905600, 108.436], [1789909200, 108.345], [1789912800, 108.168], [1789916400, 108.116], [1789920000, 108.707], [1789923600, 110.226], [1789927200, 109.875], [1789930800, 109.404], [1789934400, 110.455], [1789938000, 110.113], [1789941600, 109.839], [1789945200, 110.956], [1789948800, 111.154], [1789952400, 112.294], [1789956000, 110.981], [1789959600, 111.189], [1789963200, 111.536], [1789966800, 111.449], [1789970400, 111.761], [1789974000, 112.136], [1789977600, 112.083], [1789981200, 115.292], [1789984800, 115.834], [1789988400, 115.901], [1789992000, 116.716], [1789995600, 116.804], [1789999200, 118.188], [1790002800, 117.999], [1790006400, 117.992], [1790010000, 117.572], [1790013600, 117.134], [1790017200, 117.714], [1790020800, 118.307], [1790024400, 119.11], [1790028000, 119.134], [1790031600, 119.404], [1790035200, 118.872], [1790038800, 118.285], [1790042400, 117.538], [1790046000, 116.84], [1790049600, 116.798], [1790053200, 116.342], [1790056800, 116.875], [1790060400, 116.533], [1790064000, 116.258], [1790067600, 117.488], [1790071200, 117.404], [1790074800, 116.821], [1790078400, 117.275], [1790082000, 117.419], [1790085600, 117.886], [1790089200, 116.846], [1790092800, 117.367], [1790096400, 117.457], [1790100000, 117.975], [1790103600, 118.139], [1790107200, 118.196], [1790110800, 117.958], [1790114400, 118.095], [1790118000, 118.477], [1790121600, 118.507], [1790125200, 118.807], [1790128800, 118.26], [1790132400, 118.441], [1790136000, 119.1], [1790139600, 119.511], [1790143200, 118.399], [1790146800, 118.892], [1790150400, 117.918], [1790154000, 117.373], [1790157600, 117.383], [1790161200, 117.579], [1790164800, 117.112], [1790168400, 116.878], [1790172000, 116.802], [1790175600, 115.062], [1790179200, 113.604], [1790182800, 114.393], [1790186400, 114.507], [1790190000, 114.383], [1790193600, 114.584], [1790197200, 114.01], [1790200800, 114.691], [1790204400, 115.099], [1790208000, 114.985], [1790211600, 115.114], [1790215200, 114.698], [1790218800, 115.26], [1790222400, 114.653], [1790226000, 115.383], [1790229600, 114.871], [1790233200, 114.824], [1790236800, 115.503], [1790240400, 114.053], [1790244000, 113.102], [1790247600, 113.387], [1790251200, 113.26], [1790254800, 113.837], [1790258400, 114.858], [1790262000, 114.468], [1790265600, 116.267], [1790269200, 116.918], [1790272800, 116.689], [1790276400, 117.349], [1790280000, 117.246], [1790283600, 116.792], [1790287200, 116.807], [1790290800, 116.554], [1790294400, 117.009], [1790298000, 117.379], [1790301600, 117.431], [1790305200, 116.849], [1790308800, 116.834], [1790312400, 116.392], [1790316000, 116.423], [1790319600, 116.238], [1790323200, 116.367], [1790326800, 117.264], [1790330400, 118.327], [1790334000, 119.154], [1790337600, 120.849], [1790341200, 120.766], [1790344800, 119.542], [1790348400, 120.294], [1790352000, 120.084], [1790355600, 120.965], [1790359200, 120.929], [1790362800, 121.936], [1790366400, 121.926], [1790370000, 121.56], [1790373600, 120.964], [1790377200, 122.209], [1790380800, 122.081], [1790384400, 121.515], [1790388000, 122.028], [1790391600, 121.576], [1790395200, 120.616], [1790398800, 120.657], [1790402400, 120.5], [1790406000, 120.216], [1790409600, 120.739], [1790413200, 120.678], [1790416800, 119.941], [1790420400, 120.593], [1790424000, 121.27], [1790427600, 121.053], [1790431200, 120.731], [1790434800, 120.919], [1790438400, 121.861], [1790442000, 121.57], [1790445600, 121.46], [1790449200, 121.035], [1790452800, 121.115], [1790456400, 120.319], [1790460000, 121.287], [1790463600, 121.122], [1790467200, 121.379], [1790470800, 120.799], [1790474400, 121.405], [1790478000, 121.105], [1790481600, 120.583], [1790485200, 120.124], [1790488800, 121.093], [1790492400, 121.295], [1790496000, 124.161], [1790499600, 124.113], [1790503200, 124.42], [1790506800, 123.967], [1790510400, 123.927], [1790514000, 123.317], [1790517600, 123.141], [1790521200, 121.694], [1790524800, 121.737], [1790528400, 121.78], [1790532000, 122.038], [1790535600, 123.258], [1790539200, 123.034], [1790542800, 122.687], [1790546400, 122.84], [1790550000, 121.383], [1790553600, 121.956], [1790557200, 121.689], [1790560800, 120.702], [1790564400, 120.021], [1790568000, 119.839], [1790571600, 119.967], [1790575200, 119.106], [1790578800, 118.686], [1790582400, 118.048], [1790586000, 118.361], [1790589600, 117.695], [1790593200, 118.538], [1790596800, 118.668], [1790600400, 119.991], [1790604000, 119.766], [1790607600, 118.025], [1790611200, 118.698], [1790614800, 119.198], [1790618400, 119.689], [1790622000, 119.94], [1790625600, 118.378], [1790629200, 118.785], [1790632800, 117.691], [1790636400, 118.557], [1790640000, 118.82], [1790643600, 118.082], [1790647200, 117.083], [1790650800, 116.905], [1790654400, 117.791], [1790658000, 117.652], [1790661600, 118.205], [1790665200, 119.202], [1790668800, 119.525], [1790672400, 119.115], [1790676000, 119.863], [1790679600, 119.401], [1790683200, 120.09], [1790686800, 120.829], [1790690400, 121.01], [1790694000, 120.149], [1790697600, 118.254], [1790701200, 118.046], [1790704800, 117.77], [1790708400, 119.141], [1790712000, 118.76], [1790715600, 119.028], [1790719200, 118.864], [1790722800, 119.078], [1790726400, 119.093], [1790730000, 118.833], [1790733600, 119.424], [1790737200, 119.654], [1790740800, 119.102], [1790744400, 118.752], [1790748000, 119.008], [1790751600, 118.113], [1790755200, 118.43], [1790758800, 118.164], [1790762400, 119.317], [1790766000, 119.783], [1790769600, 119.42]]
_st=[x[0] for x in SOLPX]
def solpx(t):return SOLPX[min(len(_st)-1,bisect.bisect_left(_st,t))][1]
UA={"User-Agent":"Mozilla/5.0 (Macintosh) Chrome/120"}
RPC="https://api.mainnet-beta.solana.com";S=requests.Session();PL=threading.Lock();_last=[0]
def rpc(method,params,tries=10):
    for i in range(tries):
        try:
            r=S.post(RPC,json={"jsonrpc":"2.0","id":1,"method":method,"params":params},timeout=40)
            if r.status_code==429:time.sleep(0.5+i*0.7);continue
            j=r.json()
            if "error" in j:time.sleep(0.5+i);continue
            return j["result"]
        except Exception:time.sleep(1+i)
    return None
NOW_SLOT=rpc("getSlot",[]);NOW=time.time()
def cursor(t):return "%012d0000000000-%d"%(int(NOW_SLOT-(NOW-t)/0.4),int(t*1000))
def pget(u):
    for i in range(8):
        with PL:
            w=0.8-(time.time()-_last[0])
            if w>0:time.sleep(w)
            _last[0]=time.time()
        try:
            r=requests.get(u,headers=UA,timeout=30)
            if r.status_code==200:return r.json()
            time.sleep(5+5*i)
        except Exception:time.sleep(3+i)
    return None
def gettx(s):return rpc("getTransaction",[s,{"encoding":"jsonParsed","maxSupportedTransactionVersion":0,"commitment":"confirmed"}])
def ts_of(x):return int(time.mktime(time.strptime(x["timestamp"][:19],"%Y-%m-%dT%H:%M:%S")))-time.timezone
def run(tok,CAP=80,W=48*3600):
    c=tok["created_timestamp"]/1000;mint=tok["mint"]
    pools={tok.get("bonding_curve"),tok.get("pump_swap_pool")}-{None}
    ms=round(450/solpx(c),2);cur=cursor(c+W);allt=[];pages=0;complete=False
    while pages<40:
        r=pget(f"https://swap-api.pump.fun/v2/coins/{mint}/trades?limit=100&cursor={cur}&minSolAmount={ms}")
        if r is None:break
        pages+=1;t=r.get("trades",[])
        for x in t:
            x["ts"]=ts_of(x)
            if c<=x["ts"]<c+W:allt.append(x)
        pg=r.get("pagination",{})
        if not pg.get("hasMore") or (t and ts_of(t[-1])<c):complete=True;break
        cur=pg["nextCursor"]
    big=[x for x in allt if x["type"]=="buy" and float(x["amountUsd"] or 0)>=500]
    random.seed(mint);samp=random.sample(big,min(CAP,len(big)))
    with cf.ThreadPoolExecutor(6) as ex:txs=list(ex.map(lambda x:gettx(x["tx"]),samp))
    ev=[]
    for x,t in zip(samp,txs):
        r=None
        if t is not None:
            try:r=parse(t,mint,pools)
            except Exception:r=None
        ev.append({"mint":mint,"sig":x["tx"],"ts":x["ts"],"api_user":x["userAddress"],"usd":float(x["amountUsd"]),"sol":float(x["amountSol"] or 0),"px":float(x["priceUsd"] or 0),"program":x["program"],"tx_ok":t is not None,"parsed":r})
    return {"mint":mint,"created":c,"n_big_trades":len(allt),"n_big_buys":len(big),"n_sampled":len(samp),"pages":pages,"complete":complete},ev
if __name__=="__main__":
    arg=sys.argv[1];out=sys.argv[2]
    if arg.endswith(".json"):toks=json.load(open(arg))
    else:
        toks=[]
        for m in open(arg).read().split():
            c=pget(f"https://frontend-api-v3.pump.fun/coins-v2/{m}")
            if c:toks.append({k:c.get(k) for k in("mint","symbol","created_timestamp","bonding_curve","pump_swap_pool")})
        print("meta",len(toks),flush=True)
    done=set()
    try:done={json.loads(l)["meta"]["mint"] for l in open(out) if '"meta"' in l}
    except FileNotFoundError:pass
    L=threading.Lock()
    def one(tok):
        if tok["mint"] in done:return
        t0=time.time()
        try:m,ev=run(tok)
        except Exception as e:m,ev={"mint":tok["mint"],"error":repr(e)},[]
        with L,open(out,"a") as f:
            f.write(json.dumps({"meta":m})+"\n")
            for e in ev:f.write(json.dumps(e)+"\n")
            print(tok.get("symbol"),m.get("n_big_buys"),m.get("n_sampled"),m.get("complete"),round(time.time()-t0),flush=True)
    with cf.ThreadPoolExecutor(2) as ex:list(ex.map(one,toks))
