File size: 20,225 Bytes
c501fa1
2856a4c
bd7f8e7
2c1544f
2856a4c
2c1544f
 
2856a4c
2c1544f
 
2856a4c
34ba6be
bd7f8e7
 
2c1544f
bd7f8e7
2c1544f
 
 
bd7f8e7
2c1544f
 
34ba6be
2c1544f
 
996a8b3
2c1544f
 
110bc97
2c1544f
 
34ba6be
2c1544f
 
110bc97
2c1544f
 
110bc97
c501fa1
2c1544f
2856a4c
2c1544f
 
 
996a8b3
2c1544f
 
9da2f9d
2c1544f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110bc97
2c1544f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110bc97
2c1544f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9da2f9d
2c1544f
 
 
 
 
 
 
 
 
 
34ba6be
 
c501fa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd7f8e7
2c1544f
bd7f8e7
2856a4c
 
 
2c1544f
 
 
c501fa1
2c1544f
 
 
 
 
 
 
9da2f9d
2c1544f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110bc97
2c1544f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9da2f9d
2c1544f
 
 
 
 
 
 
c501fa1
 
 
 
 
 
 
2c1544f
 
 
 
 
 
 
 
 
9da2f9d
2c1544f
 
 
 
9da2f9d
2c1544f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c501fa1
 
2c1544f
 
c501fa1
2c1544f
 
 
 
 
 
 
 
 
c501fa1
2c1544f
 
 
 
 
c501fa1
 
 
 
 
 
 
 
 
 
2c1544f
 
 
 
 
 
9da2f9d
2c1544f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4981e4
110bc97
2c1544f
110bc97
2c1544f
 
 
 
6f8bc49
2c1544f
110bc97
2c1544f
 
 
 
110bc97
2c1544f
 
 
34ba6be
2c1544f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c501fa1
2c1544f
 
 
 
 
 
 
 
 
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# autotrack.py β€” Unlimited milestones + auto-delete on drawdown + video β‰₯3Γ— + daily summary hooks
import os
import re
import time
import asyncio
import aiohttp
import sqlite3
from dataclasses import dataclass, field
from typing import Optional, Dict, Tuple
from datetime import datetime, timezone, timedelta

from telethon import TelegramClient, events

# =========================
# ENV / CONFIG
# =========================
API_ID = int(os.environ.get("API_ID", "0"))              # TIDAK dipakai langsung di file ini
API_HASH = os.environ.get("API_HASH", "")                # β€” client disuntik dari botsignal
STRING_SESSION = os.environ.get("STRING_SESSION", "")    # β€”

# Announce ke grup kamu (dipakai juga sebagai sumber pesan)
TARGET_CHAT = os.environ.get("TARGET_CHAT", "https://t.me/MidasTouchsignalll")

# Polling interval harga
TRACK_POLL_SECS = int(os.environ.get("TRACK_POLL_SECS", "20"))

# Ambang reply awal (default 1.5Γ—)
REPLY_FROM_MULTIPLE = float(os.environ.get("REPLY_FROM_MULTIPLE", "1.5"))

# Unlimited mode: selalu lanjut 2Γ—, 3Γ—, 4Γ—, ...
STOP_WHEN_HIT = False

# Abaikan pesan lebih tua dari (startup - buffer)
BACKFILL_BUFFER_MINUTES = int(os.environ.get("BACKFILL_BUFFER_MINUTES", "3"))

# Marker antirecursive: ditambahkan ke semua pengumuman bot (milestone)
BOT_MARKER = "【MT-AUTOTRACK】"

# Lokasi DB milik botsignal (untuk ambil msg_id pesan awal + summary hooks)
BOTSIGNAL_DB = os.environ.get("BOTSIGNAL_DB", "/tmp/botsignal.db")

# NEW: Auto-delete bila drawdown >= threshold (rasio relatif terhadap entry)
# Contoh: 0.5 berarti -50%. Set 0 untuk mematikan.
DROP_DELETE_RATIO = float(os.environ.get("DROP_DELETE_RATIO", "0.5"))

# (opsional) juga hapus thread balasan bot yang bertanda BOT_MARKER
DELETE_THREAD_REPLIES = os.environ.get("DELETE_THREAD_REPLIES", "0") == "1"

# >>> VIDEO milestone β‰₯ 3Γ—
MILESTONE_VIDEO = os.environ.get("MILESTONE_VIDEO", "Generating_To_The_Moon_Animation.mp4")
# FIX: gunakan absolute agar aman di Docker/HF
VIDEO_PATH = os.path.abspath(MILESTONE_VIDEO)
VIDEO_MIN_MULTIPLE = float(os.environ.get("VIDEO_MIN_MULTIPLE", "3.0"))

# =========================
# HTTP endpoints
# =========================
DEXSCREENER_TOKEN_URL = "https://api.dexscreener.com/latest/dex/tokens/"
JUPITER_URL = "https://price.jup.ag/v6/price?ids="  # free (Solana only)

# =========================
# Helpers
# =========================
def _fmt_money(x: Optional[float]) -> str:
    if x is None:
        return "?"
    if x >= 1:
        return f"${x:,.4f}"
    return f"${x:.8f}"

def _fmt_big(x: Optional[float]) -> str:
    if x is None:
        return "?"
    if x >= 1_000_000_000:
        return f"${x/1_000_000_000:.2f}B"
    if x >= 1_000_000:
        return f"${x/1_000_000:.2f}M"
    if x >= 1_000:
        return f"${x/1_000:.2f}K"
    return f"${x:.0f}"

def _milestone_badge(m: float, basis: str) -> str:
    unit = "Market Cap" if basis == "mcap" else "Price"
    if m < 2.0 - 1e-9:       return f"🟨 **ARMED β€” {m:.1f}Γ— {unit}**"
    elif abs(m - 2.0) < 1e-9:return f"🟩 **DOUBLE UP β€” 2Γ— {unit}!**"
    elif abs(m - 3.0) < 1e-9:return f"πŸ”· **TRIPLE β€” 3Γ— {unit}!**"
    elif 3.0 < m < 6.0:     return f"πŸ”Ά **RALLY β€” {m:.0f}Γ— {unit}!**"
    elif 6.0 <= m < 10.0:   return f"πŸŸ₯ **OVERDRIVE β€” {m:.0f}Γ— {unit}!**"
    else:                   return f"🟣 **GOD CANDLE β€” {m:.0f}Γ— {unit}!!**"

# =========================
# CA extraction
# =========================
CA_SOL_RE = re.compile(r"\b[1-9A-HJ-NP-Za-km-z]{32,48}\b")
CA_EVM_RE = re.compile(r"\b0x[a-fA-F0-9]{40}\b")
CA_LABEL_RE = re.compile(r"\bCA\s*[:=]\s*(\S+)", re.IGNORECASE)

def extract_ca(text: str) -> Optional[str]:
    if not text:
        return None
    m = CA_LABEL_RE.search(text)
    if m:
        cand = m.group(1)
        if CA_EVM_RE.fullmatch(cand): return cand.lower()
        if CA_SOL_RE.fullmatch(cand): return cand
    m2 = CA_EVM_RE.search(text or "")
    if m2: return m2.group(0).lower()
    m3 = CA_SOL_RE.search(text or "")
    if m3: return m3.group(0)
    return None

def ca_key_for_db(ca: str) -> Optional[str]:
    if not ca:
        return None
    if CA_EVM_RE.fullmatch(ca): return f"ca:evm:{ca.lower()}"
    if CA_SOL_RE.fullmatch(ca): return f"ca:sol:{ca}"
    return None

def ensure_db():
    try:
        conn = sqlite3.connect(BOTSIGNAL_DB)
        conn.executescript("""
        CREATE TABLE IF NOT EXISTS last_posted (
            keyword TEXT PRIMARY KEY,
            msg_id INTEGER NOT NULL,
            tier TEXT
        );
        """)
        conn.commit()
        conn.close()
    except Exception as e:
        print(f"[AUTOTRACK] DB init error: {e}")

def lookup_origin_msg_id(keyword: str) -> Optional[int]:
    try:
        conn = sqlite3.connect(BOTSIGNAL_DB)
        cur = conn.cursor()
        cur.execute("SELECT msg_id FROM last_posted WHERE keyword = ?", (keyword,))
        row = cur.fetchone()
        conn.close()
        if row and isinstance(row[0], int):
            return row[0]
        return None
    except Exception as e:
        print(f"[TRACK][DB] lookup error: {e}")
        return None

# =========================
# SUMMARY HOOKS (harian)
# =========================
def _summary_db():
    conn = sqlite3.connect(BOTSIGNAL_DB)
    conn.execute("PRAGMA journal_mode=WAL;")
    return conn

def summary_init_tables():
    try:
        conn = _summary_db()
        conn.executescript("""
        CREATE TABLE IF NOT EXISTS summary_milestones (
            keyword TEXT NOT NULL,
            reply_msg_id INTEGER,
            multiple REAL,
            replied_at INTEGER NOT NULL
        );
        CREATE TABLE IF NOT EXISTS summary_outcomes (
            keyword TEXT PRIMARY KEY,
            is_deleted INTEGER DEFAULT 0
        );
        """)
        conn.commit()
        conn.close()
    except Exception as e:
        print(f"[SUMMARY] init tables failed: {e}")

def _summary_log_milestone(keyword: str, reply_msg_id: Optional[int], multiple: float, ts_utc: int):
    try:
        conn = _summary_db()
        conn.execute(
            "INSERT INTO summary_milestones(keyword, reply_msg_id, multiple, replied_at) VALUES(?,?,?,?)",
            (keyword, reply_msg_id, float(multiple), int(ts_utc))
        )
        conn.commit()
        conn.close()
    except Exception as e:
        print(f"[SUMMARY] log milestone failed: {e}")

def _summary_mark_deleted(keyword: str):
    try:
        conn = _summary_db()
        conn.execute(
            "INSERT INTO summary_outcomes(keyword, is_deleted) VALUES(?,1) "
            "ON CONFLICT(keyword) DO UPDATE SET is_deleted=1",
            (keyword,)
        )
        conn.commit()
        conn.close()
    except Exception as e:
        print(f"[SUMMARY] mark deleted failed: {e}")

# =========================
# Tracker core (UNLIMITED)
# =========================
@dataclass
class TrackItem:
    ca: str
    basis: str = "mcap"  # "mcap" or "price"
    entry_price: Optional[float] = None
    entry_mcap: Optional[float] = None
    symbol_hint: Optional[float] = None
    source_link: Optional[str] = None
    poll_secs: int = 20
    next_milestone: float = field(default_factory=lambda: REPLY_FROM_MULTIPLE)
    started_at: float = field(default_factory=time.time)

class PriceTracker:
    def __init__(self, client: TelegramClient, announce_chat):
        self.client = client
        self.announce_chat = announce_chat
        self._tasks: Dict[str, asyncio.Task] = {}
        self._items: Dict[str, TrackItem] = {}

    # -------- HTTP helpers --------
    async def _get(self, url: str, headers: Optional[Dict[str, str]] = None, timeout: int = 8):
        tout = aiohttp.ClientTimeout(total=timeout)
        async with aiohttp.ClientSession(timeout=tout, headers=headers) as sess:
            async with sess.get(url) as r:
                if r.status != 200:
                    return None
                return await r.json()

    async def _dexscreener_price(self, ca: str) -> Optional[Tuple[Optional[float], Optional[str], Optional[float]]]:
        data = await self._get(DEXSCREENER_TOKEN_URL + ca)
        if not data:
            return None
        pairs = data.get("pairs") or []
        if not pairs:
            return None
        best = max(pairs, key=lambda p: (p.get("liquidity", {}) or {}).get("usd", 0))
        price = float(best.get("priceUsd")) if best.get("priceUsd") else None
        symbol = ((best.get("baseToken") or {}).get("symbol")) or None

        mcap = None
        fdv = best.get("fdv")
        if isinstance(fdv, (int, float)) and fdv > 0:
            mcap = float(fdv)
        mc = best.get("marketCap")
        if isinstance(mc, (int, float)) and mc > 0:
            mcap = float(mc)
        return (price, symbol, mcap)

    async def _jupiter_price(self, ca: str) -> Optional[float]:
        try:
            data = await self._get(JUPITER_URL + ca)
            if not data:
                return None
            d = (data.get("data") or {}).get(ca)
            if not d:
                return None
            p = d.get("price")
            return float(p) if p is not None else None
        except:
            return None

    async def _get_snapshot(self, ca: str) -> Optional[Dict[str, Optional[float]]]:
        res = await self._dexscreener_price(ca)
        if res:
            price, sym, mcap = res
            if price is None:
                jp = await self._jupiter_price(ca)
                price = jp if jp is not None else None
            return {"price": price, "mcap": mcap, "symbol_hint": sym}
        jp = await self._jupiter_price(ca)
        if jp is not None:
            return {"price": jp, "mcap": None, "symbol_hint": None}
        return None

    # -------- Announce helpers --------
    def _name(self, item: TrackItem) -> str:
        s = item.symbol_hint or ""
        return f"{s} ({item.ca[:4]}…{item.ca[-4:]})" if s else item.ca

    def _milestone_text(self, item: TrackItem, m: float, ratio: float,
                        cur_price: Optional[float], cur_mcap: Optional[float]) -> str:
        # NOTE: Dexs link diset ke pola Solana untuk CA sol; untuk EVM bisa kamu tingkatkan bila mau
        dexs_link = f"https://dexscreener.com/solana/{item.ca}" if CA_SOL_RE.fullmatch(item.ca) else f"https://dexscreener.com/ethereum/{item.ca.lower()}"
        axiom_link = "https://axiom.trade/@1144321"
        if item.basis == "mcap":
            change = f"{_fmt_big(item.entry_mcap)} β†’ {_fmt_big(cur_mcap)}  (~{ratio:.2f}Γ—)"
        else:
            change = f"{_fmt_money(item.entry_price)} β†’ {_fmt_money(cur_price)}  (~{ratio:.2f}Γ—)"
        milestone_line = _milestone_badge(m, item.basis)
        elapsed = int(time.time() - item.started_at)
        mm, ss = divmod(elapsed, 60)
        header = "πŸ’Ž [MidasTouch Signal] πŸ’Ž\n━━━━━━━━━━━━━━━━"
        footer = "━━━━━━━━━━━━━━━━"
        body = (
            f"{header}\n"
            f"{milestone_line}\n"
            f"{self._name(item)}\n"
            f"{change}\n"
            f"⏱️ {mm}m {ss}s since call\n"
            f"πŸ”Ž [Dexscreener]({dexs_link})\n"
            f"πŸ›’ [Trade on Axiom]({axiom_link})\n"
            f"CA: `{item.ca}`\n"
            f"{footer}"
        )
        return body

    def _next_target_after(self, current_target: float) -> float:
        return 2.0 if current_target < 2.0 else current_target + 1.0

    async def _delete_origin_and_replies(self, ca: str):
        try:
            key = ca_key_for_db(ca)
            msg_id = lookup_origin_msg_id(key) if key else None
            if not msg_id:
                return
            await self.client.delete_messages(self.announce_chat, msg_id)
            print(f"[TRACK] Deleted origin message for {ca}")

            # === Summary: mark lose
            try:
                if key:
                    _summary_mark_deleted(key)
            except Exception as e:
                print(f"[SUMMARY] mark deleted hook err: {e}")

            if DELETE_THREAD_REPLIES:
                try:
                    async for m in self.client.iter_messages(self.announce_chat, limit=100, reply_to=msg_id):
                        txt = (m.message or "") if hasattr(m, "message") else ""
                        if BOT_MARKER in (txt or ""):
                            await self.client.delete_messages(self.announce_chat, m.id)
                    print(f"[TRACK] Deleted thread replies for {ca}")
                except Exception as e:
                    print(f"[TRACK] delete thread replies failed: {e}")
        except Exception as e:
            print(f"[TRACK] delete origin failed: {e}")

    # -------- Loop --------
    async def _loop(self, item: TrackItem):
        try:
            snap = await self._get_snapshot(item.ca)
            if not snap:
                print(f"[TRACK] init snapshot failed for {item.ca}")
                return

            if item.entry_price is None:
                item.entry_price = snap.get("price")
            if item.entry_mcap is None:
                item.entry_mcap = snap.get("mcap")
            if not item.symbol_hint and snap.get("symbol_hint"):
                item.symbol_hint = snap.get("symbol_hint")

            if item.basis == "mcap" and not item.entry_mcap:
                item.basis = "price"

            while True:
                try:
                    snap = await self._get_snapshot(item.ca)
                    if not snap:
                        await asyncio.sleep(item.poll_secs)
                        continue

                    cur_price = snap.get("price")
                    cur_mcap = snap.get("mcap")
                    if not item.symbol_hint and snap.get("symbol_hint"):
                        item.symbol_hint = snap.get("symbol_hint")

                    if item.basis == "mcap":
                        if not (item.entry_mcap and cur_mcap):
                            await asyncio.sleep(item.poll_secs); continue
                        ratio = (cur_mcap / item.entry_mcap) if item.entry_mcap > 0 else 0.0
                    else:
                        if not (item.entry_price and cur_price):
                            await asyncio.sleep(item.poll_secs); continue
                        ratio = (cur_price / item.entry_price) if item.entry_price > 0 else 0.0

                    # Auto-delete on drawdown
                    if DROP_DELETE_RATIO > 0 and ratio <= DROP_DELETE_RATIO:
                        await self._delete_origin_and_replies(item.ca)
                        return  # stop tracking setelah dihapus

                    # Milestones
                    while ratio >= item.next_milestone:
                        text = self._milestone_text(item, item.next_milestone, ratio, cur_price, cur_mcap)
                        key = ca_key_for_db(item.ca)
                        reply_to_id = lookup_origin_msg_id(key) if key else None

                        sent_msg = None
                        if item.next_milestone >= VIDEO_MIN_MULTIPLE and os.path.isfile(VIDEO_PATH):
                            try:
                                sent_msg = await self.client.send_file(
                                    self.announce_chat,
                                    VIDEO_PATH,
                                    caption=f"{text}\n\n{BOT_MARKER}",
                                    reply_to=reply_to_id if reply_to_id else None,
                                    force_document=False
                                )
                            except Exception as e:
                                print(f"[TRACK] gagal kirim video: {e}")
                        else:
                            sent_msg = await self.client.send_message(
                                self.announce_chat,
                                f"{text}\n\n{BOT_MARKER}",
                                reply_to=reply_to_id if reply_to_id else None,
                                link_preview=False
                            )

                        # === Summary: log milestone (win signal)
                        try:
                            if key:
                                ts_utc = int(time.time())
                                rep_id = getattr(sent_msg, "id", None) if sent_msg else None
                                _summary_log_milestone(key, rep_id, item.next_milestone, ts_utc)
                        except Exception as e:
                            print(f"[SUMMARY] milestone hook err: {e}")

                        item.next_milestone = self._next_target_after(item.next_milestone)

                    await asyncio.sleep(item.poll_secs)
                except Exception as e:
                    print(f"[TRACK] loop error: {e}")
                    await asyncio.sleep(item.poll_secs)
        except Exception as e:
            print(f"[TRACK] fatal loop init error for {item.ca}: {e}")

    # -------- Public --------
    def is_tracking(self, ca: str) -> bool:
        t = self._tasks.get(ca)
        return bool(t) and not t.done()

    async def start(self, ca: str, *, basis: str = "mcap",
                    entry_price: Optional[float] = None,
                    entry_mcap: Optional[float] = None,
                    poll_secs: int = 20,
                    source_link: Optional[str] = None,
                    symbol_hint: Optional[str] = None):
        ca = ca.strip()
        if self.is_tracking(ca):
            return
        item = TrackItem(
            ca=ca,
            basis=basis.lower(),
            entry_price=entry_price,
            entry_mcap=entry_mcap,
            poll_secs=poll_secs,
            source_link=source_link,
            symbol_hint=symbol_hint,
            next_milestone=REPLY_FROM_MULTIPLE,
        )
        self._tasks[ca] = asyncio.create_task(self._loop(item))

# =========================
# Setup: attach to existing client from botsignal
# =========================
client: TelegramClient | None = None
startup_time_utc: Optional[datetime] = None
me_user_id: Optional[int] = None
tracker: PriceTracker | None = None

CUTOFF_BUFFER = timedelta(minutes=BACKFILL_BUFFER_MINUTES)

def _is_old_message(msg_dt: Optional[datetime]) -> bool:
    if not isinstance(msg_dt, datetime):
        return False
    return msg_dt.replace(tzinfo=timezone.utc) < (startup_time_utc - CUTOFF_BUFFER)

def _is_bot_own_message(event) -> bool:
    txt = (event.raw_text or "") if hasattr(event, "raw_text") else ""
    return BOT_MARKER in (txt or "")

async def on_new_autotrack_message(event):
    try:
        if _is_bot_own_message(event):
            return
        msg = event.message
        if _is_old_message(getattr(msg, "date", None)):
            return
        text = msg.message or (getattr(msg, "raw_text", None) or "")
        ca = extract_ca(text)
        if not ca:
            return
        source_link = None
        try:
            chat = await event.get_chat()
            uname = getattr(chat, "username", None)
            mid = getattr(msg, "id", None)
            if uname and mid:
                source_link = f"https://t.me/{uname}/{mid}"
        except:
            pass
        await tracker.start(
            ca=ca,
            basis="mcap",
            poll_secs=TRACK_POLL_SECS,
            source_link=source_link,
        )
    except Exception as e:
        print(f"[AUTOTRACK] error: {e}")

def setup_autotrack(shared_client: TelegramClient, announce_chat: str | None = None):
    """
    Dipanggil dari botsignal.py setelah client dibuat & (idealnya) sebelum start().
    """
    global client, tracker, startup_time_utc
    client = shared_client
    ensure_db()
    summary_init_tables()  # <<< summary tables
    ac = announce_chat or TARGET_CHAT
    tracker = PriceTracker(client, announce_chat=ac)
    startup_time_utc = datetime.now(timezone.utc)
    # DAFTARKAN HANDLER DENGAN NAMA BERBEDA (FIX BENTROK)
    client.add_event_handler(on_new_autotrack_message, events.NewMessage(chats=(TARGET_CHAT,)))
    print("[AUTOTRACK] attached to shared client; listening on", ac)

# Tidak ada build_client() maupun main() di sini β€” kita sengaja
# agar autotrack SELALU menumpang client milik botsignal.