File size: 5,012 Bytes
d4c51f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""Turn a digest run into ready-to-post distribution assets.

The product's bottleneck is distribution, not code. This module converts the
same digest data into:
  - an X / Twitter thread (list of <=280-char tweets), and
  - a dark-theme PNG card of the top moves (for the first tweet).

Posting itself needs a human-owned account (or API keys) β€” this just produces
the assets so posting is one copy-paste / one API call away.
"""
from __future__ import annotations

from pathlib import Path

import pandas as pd

from .digest import Move

TWEET_LIMIT = 280


def _short(w: str) -> str:
    return f"{w[:6]}…{w[-4:]}"


def x_thread(selected: pd.DataFrame, moves: list[Move], hours: int,
             site_url: str = "") -> list[str]:
    """Compose the thread. Tweet 1 = hook, then top moves, then CTA."""
    n_traders = len(selected)
    hook = (
        f"πŸ“ˆ Consistent Polymarket smart-money β€” last {hours}h\n\n"
        f"The {n_traders} mid-size traders whale trackers ignore "
        f"(too small to flag) β€” and their highest-conviction moves right now.\n\n"
        f"A thread 🧡"
    )
    tweets = [hook]

    # diversify: best move per wallet first, so the thread isn't 5 near-dupes
    seen: set[str] = set()
    diverse = [m for m in moves if not (m.wallet in seen or seen.add(m.wallet))]
    if len(diverse) < 5:
        diverse += [m for m in moves if m not in diverse]

    for i, m in enumerate(diverse[:5], 1):
        stars = "β˜…" * max(1, int(round(m.signal_quality * 5)))
        title = (m.title[:70] + "…") if len(m.title) > 71 else m.title
        t = (
            f"{i}/ {stars}  signal {m.signal_quality:.2f}\n"
            f"{_short(m.wallet)} β†’ {title} ({m.outcome})\n"
            f"@ {m.price:.2f} Β· ${m.usdc:,.0f} Β· conviction {m.conviction:.0%}"
        )
        tweets.append(t[:TWEET_LIMIT])

    best = selected.head(3)
    lead = "Why these traders: picked for consistency (green most months) + a "
    lead += "repeatable style, NOT size. Sample of who we track:\n\n"
    lead += "\n".join(
        f"β€’ {_short(r['wallet'])} β€” score {r['trader_score']:.2f}, "
        f"{float(r.get('monthly_profit_ratio',0)):.0%} green months"
        for _, r in best.iterrows()
    )
    tweets.append(lead[:TWEET_LIMIT])

    cta = "Score ANY Polymarket wallet free β€” consistent, whale, market-maker, "
    cta += "or just lucky?"
    if site_url:
        cta += f"\n\n{site_url}"
    cta += "\n\nFlow intelligence, not financial advice."
    tweets.append(cta[:TWEET_LIMIT])
    return tweets


def table_png(moves: list[Move], out: Path, hours: int) -> Path | None:
    """Render the top moves as a shareable dark card. Returns path or None."""
    if not moves:
        return None
    try:
        import matplotlib
        matplotlib.use("Agg")
        import matplotlib.pyplot as plt
    except Exception:
        return None

    # one clean row per wallet β€” no duplicate padding, so the card never shows
    # the same trader three times when only a few distinct wallets are active
    seen: set[str] = set()
    top = [m for m in moves if not (m.wallet in seen or seen.add(m.wallet))][:8]
    bg, ink, mut, acc = "#0b0e14", "#e6edf3", "#8b98a9", "#3fb950"

    n = len(top)
    fig, ax = plt.subplots(figsize=(10, 1.6 + 0.5 * n))
    fig.patch.set_facecolor(bg)
    ax.set_facecolor(bg)
    ax.axis("off")

    ax.text(0.02, 0.97, "β—† SharpFlow", color=acc, fontsize=17, fontweight="bold",
            transform=ax.transAxes, va="top")
    ax.text(0.02, 0.86, f"Consistent smart-money β€” top moves, last {hours}h",
            color=ink, fontsize=13, transform=ax.transAxes, va="top")

    y_hdr = 0.70
    ax.text(0.02, y_hdr, "SIGNAL", color=mut, fontsize=9, transform=ax.transAxes)
    ax.text(0.16, y_hdr, "TRADER", color=mut, fontsize=9, transform=ax.transAxes)
    ax.text(0.34, y_hdr, "MOVE", color=mut, fontsize=9, transform=ax.transAxes)
    ax.text(0.82, y_hdr, "SIZE", color=mut, fontsize=9, transform=ax.transAxes)

    y_top, y_bot = 0.62, 0.10
    step = (y_top - y_bot) / max(1, n - 1) if n > 1 else 0.0
    for i, m in enumerate(top):
        y = y_top - i * step
        bar = "β–ˆ" * max(1, int(round(m.signal_quality * 5)))
        ax.text(0.02, y, f"{bar} {m.signal_quality:.2f}", color=acc, fontsize=10,
                transform=ax.transAxes, family="monospace")
        ax.text(0.16, y, _short(m.wallet), color=ink, fontsize=10,
                transform=ax.transAxes, family="monospace")
        title = (m.title[:34] + "…") if len(m.title) > 35 else m.title
        ax.text(0.34, y, f"{title} ({m.outcome[:12]})", color=ink, fontsize=10,
                transform=ax.transAxes)
        ax.text(0.82, y, f"${m.usdc:,.0f}", color=ink, fontsize=10,
                transform=ax.transAxes)

    ax.text(0.02, 0.02, "Flow intelligence, not financial advice Β· sharpflow",
            color=mut, fontsize=8, transform=ax.transAxes)
    fig.savefig(out, dpi=150, facecolor=bg, pad_inches=0)
    plt.close(fig)
    return out