Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

Add Telegram trade notifications to sniper executor

nirholas0987158aebbd
diff --git a/api/_lib/sniper/notify.js b/api/_lib/sniper/notify.js
new file mode 100644
index 000000000..0ffd3b524
--- /dev/null
+++ b/api/_lib/sniper/notify.js
@@ -0,0 +1,78 @@
+// Sniper trade notifications — Telegram.
+//
+// Fires when the agent-sniper worker opens or closes a position. Uses
+// TELEGRAM_SNIPER_CHAT_ID if set (a dedicated sniper alerts channel), falling
+// back to TELEGRAM_ALERTS_CHAT_ID (the general ops channel). Either way it's
+// fire-and-forget with a 3s abort — never delays the trade path.
+//
+// Env:
+// TELEGRAM_BOT_TOKEN — shared bot (same as oracle/alerts)
+// TELEGRAM_SNIPER_CHAT_ID — dedicated sniper notifications channel (preferred)
+// TELEGRAM_ALERTS_CHAT_ID — ops fallback if the dedicated channel is absent
+
+const TIMEOUT_MS = 3000;
+
+function chatId() {
+ return process.env.TELEGRAM_SNIPER_CHAT_ID || process.env.TELEGRAM_ALERTS_CHAT_ID || null;
+}
+
+function send(text) {
+ const token = process.env.TELEGRAM_BOT_TOKEN;
+ const id = chatId();
+ if (!token || !id) return;
+ const controller = new AbortController();
+ const timer = setTimeout(() => controller.abort(), TIMEOUT_MS);
+ fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
+ method: 'POST',
+ headers: { 'content-type': 'application/json' },
+ body: JSON.stringify({
+ chat_id: id,
+ text: text.slice(0, 4000),
+ disable_web_page_preview: true,
+ }),
+ signal: controller.signal,
+ keepalive: true,
+ })
+ .catch(() => {})
+ .finally(() => clearTimeout(timer));
+}
+
+const n2 = (v) => (v != null ? Number(v).toFixed(4) : '—');
+const pct = (v) => (v != null ? `${Number(v) >= 0 ? '+' : ''}${Number(v).toFixed(1)}%` : '—');
+const icon = (r) => ({ take_profit: '✅', trailing_stop: '✅', stop_loss: '🛑', timeout: '⏱', kill_switch: '☠️', graduated: '🎓' }[r] || '📤');
+
+/**
+ * Notify when the sniper opens a position (buy confirmed).
+ */
+export function notifyBuy({ agentName, symbol, mint, solSpent, mode, sig }) {
+ const modeTag = mode === 'live' ? '' : ' [sim]';
+ const pumpLink = mint ? `https://pump.fun/coin/${mint}` : null;
+ const solLink = sig && sig !== 'SIMULATED' ? `https://solscan.io/tx/${sig}` : null;
+ const lines = [
+ `🎯 Sniper BUY${modeTag}`,
+ `Agent: ${agentName || 'unknown'}`,
+ `Coin: ${symbol || '?'}`,
+ `Size: ${n2(solSpent)} SOL`,
+ ];
+ if (pumpLink) lines.push(`pump.fun: ${pumpLink}`);
+ if (solLink) lines.push(`tx: ${solLink}`);
+ send(lines.join('\n'));
+}
+
+/**
+ * Notify when the sniper closes a position (sell confirmed or failed).
+ */
+export function notifySell({ agentName, symbol, mint, pnlSol, pnlPct, exitReason, mode, sig }) {
+ const modeTag = mode === 'live' ? '' : ' [sim]';
+ const solLink = sig && sig !== 'SIMULATED' ? `https://solscan.io/tx/${sig}` : null;
+ const pumpLink = mint ? `https://pump.fun/coin/${mint}` : null;
+ const lines = [
+ `${icon(exitReason)} Sniper SELL${modeTag} (${exitReason || 'exit'})`,
+ `Agent: ${agentName || 'unknown'}`,
+ `Coin: ${symbol || '?'}`,
+ `PnL: ${n2(pnlSol)} SOL ${pct(pnlPct)}`,
+ ];
+ if (pumpLink) lines.push(`pump.fun: ${pumpLink}`);
+ if (solLink) lines.push(`tx: ${solLink}`);
+ send(lines.join('\n'));
+}
diff --git a/workers/agent-sniper/executor.js b/workers/agent-sniper/executor.js
index b26b81997..7c6649e76 100644
--- a/workers/agent-sniper/executor.js
+++ b/workers/agent-sniper/executor.js
@@ -10,6 +10,7 @@ import { log } from './log.js';
import { loadAgentKeypair } from './keys.js';
import { getTradeCtx, signAndSend } from './trade-client.js';
import { countOpenPositions, getDailySpend } from './strategy-store.js';
+import { notifyBuy, notifySell } from '../../api/_lib/sniper/notify.js';
// Fee + rent headroom we keep above the trade size so a buy doesn't fail for
// lack of lamports to pay fees / open the token ATA.
@@ -123,6 +124,7 @@ export async function executeBuy({ cfg, strat, mint, throttle }) {
WHERE id = ${posId}
`;
log.trade('buy', { ...tag, mode: cfg.mode, sig, sol: lamportsToSol(perTrade), base: baseAmount.toString(), impact: Number(quote.priceImpactPct).toFixed(2) });
+ notifyBuy({ agentName: strat.agent_name || strat.agent_id, symbol: mint.symbol, mint: mint.mint, solSpent: lamportsToSol(perTrade), mode: cfg.mode, sig });
return { status: 'open', sig };
} catch (err) {
return await fail(posId, tag, errCode(err), err);
@@ -180,6 +182,7 @@ export async function executeSell({ cfg, position, reason }) {
WHERE id = ${position.id}
`;
log.trade('sell', { ...tag, mode: cfg.mode, sig, pnl_sol: lamportsToSol(pnl), pnl_pct: pnlPct.toFixed(1) });
+ notifySell({ agentName: position.agent_name || position.agent_id, symbol: position.symbol, mint: position.mint, pnlSol: lamportsToSol(pnl), pnlPct, exitReason: reason, mode: cfg.mode, sig });
return { status: 'closed', sig, pnl: pnl.toString() };
} catch (err) {
// A failed sell must NOT terminate the position — leave it 'open' so the