import sys; sys.path.insert(0, "/root/fwa1")
exec(open("/root/fwa1/t4b.py").read().split("ev = {}")[0])
p10 = {t: p for t, p in ws.execute("select token, p10 from coin where crew in (?,?)", (A, BC))}
allsw = ws.execute("select crew, token, b, side, eth, px, wallet from sw where wallet is not null and crew in (?,?) and day>='2026-09-21' order by b", (A, BC)).fetchall()
pos = {}
for cr, tok, b, side, eth, px, w in allsw:
    w = w.lower(); k = (tok, w)
    p = pos.setdefault(k, dict(crew=cr, fb=None, fe=0, px=None, fs=None))
    if side == 1 and p["fb"] is None: p["fb"] = b; p["fe"] = eth; p["px"] = px
    if side == -1 and p["fb"] is not None and p["fs"] is None: p["fs"] = b
X90 = "0x90e2c652d8fe296dcb7fa38dff038c781fda2e23"
rows_ = []
for (tok, w), p in pos.items():
    if p["fb"] is None or tok not in coin or not p10.get(tok) or not p["px"]: continue
    f = ff.get(w)
    if f is None: continue
    fund = (f[0] or "").lower(); via = (f[1] or "").lower()
    if w in crewset or fund in dist or via in dist: cls = "crew"
    elif w in rep or fund in crewset: continue
    else: cls = "outside"
    b0, pull = coin[tok]; age = (p["fb"] - b0) / 10
    if age < 0 or age > 300 or (pull is not None and pull <= p["fb"]): continue
    mult = p["px"] / p10[tok]
    held = not p["fs"] or (p["fs"] - p["fb"]) >= 100
    pulled = pull is not None and pull - p["fb"] <= 600
    ab = 0 if age < 20 else 1 if age < 60 else 2 if age < 120 else 3
    mb = 0 if mult < 1 else 1 if mult < 2 else 2 if mult < 3 else 3 if mult < 5 else 4
    rows_.append(dict(cls=cls, w=w, eth=p["fe"], held=held, pulled=pulled, cell=(p["crew"], ab, mb)))
def test(name, big, ctrl):
    cc = collections.defaultdict(lambda: [0, 0])
    for r in ctrl: cc[r["cell"]][0] += 1; cc[r["cell"]][1] += r["pulled"]
    n = obs = 0; exp = 0.0; miss = 0
    for r in big:
        c = cc.get(r["cell"])
        if not c or c[0] < 5: miss += 1; continue
        n += 1; obs += r["pulled"]; exp += c[1] / c[0]
    print("%-58s n %4d pulled %3d (%3.0f%%)  matched-control %5.1f (%3.0f%%)  lift %.1fx  unmatched %d" % (name, n, obs, 100 * obs / max(n, 1), exp, 100 * exp / max(n, 1), obs / exp if exp else 0, miss))
O = [r for r in rows_ if r["cls"] == "outside"]; C = [r for r in rows_ if r["cls"] == "crew"]
dust = [r for r in O if r["eth"] < 0.002]
for lab, keep in (("all wallets", lambda r: True), ("without 0x90e2", lambda r: r["w"] != X90)):
    for hl, hf in (("held", lambda r: r["held"]), ("any", lambda r: True)):
        big = [r for r in O if r["eth"] >= 0.0033 and keep(r) and hf(r)]
        test("$9+ outside, %s, %s vs outside dust" % (hl, lab), big, [r for r in dust if hf(r)])
        test("$9+ outside, %s, %s vs crew buys same size" % (hl, lab), big, [r for r in C if r["eth"] >= 0.0033])
        for lo, hi, nm in ((0.0033, 0.0075, "$9-20"), (0.0075, 0.035, "$20-95"), (0.035, 9, "$95+")):
            test("   %s outside, %s, %s vs dust" % (nm, hl, lab), [r for r in big if lo <= r["eth"] < hi], [r for r in dust if hf(r)])
