|
| 1 | +import { Bot } from "@arb-protocol/core"; |
| 2 | +import { Bot as T } from "grammy"; |
| 3 | + |
| 4 | +const MARKER_CHAR = "▌"; |
| 5 | +const TELEGRAM_BOT_TOKEN = "GET THIS FROM ENV"; |
| 6 | +// TODO: get this from env |
| 7 | +const eventTitle = (title: string) => { |
| 8 | + return title.padEnd(26, " "); |
| 9 | +}; |
| 10 | + |
| 11 | +export const TelegramPlugin = <T extends Bot>(bot: T) => ({ |
| 12 | + ...bot, |
| 13 | + telegram: async () => { |
| 14 | + if (!TELEGRAM_BOT_TOKEN) { |
| 15 | + throw new Error("TELEGRAM_BOT_TOKEN env variable is not set"); |
| 16 | + } |
| 17 | + |
| 18 | + const t = new T(TELEGRAM_BOT_TOKEN); |
| 19 | + |
| 20 | + let output = ""; |
| 21 | + |
| 22 | + bot.store.subscribe( |
| 23 | + (state) => state.status.value, |
| 24 | + (status) => { |
| 25 | + output = ""; |
| 26 | + const state = bot.store.getState(); |
| 27 | + |
| 28 | + if (status === "history:successfulTx") { |
| 29 | + const tradeHistory = Object.values(state.tradeHistory); |
| 30 | + const successfulTransactions = tradeHistory.filter((trade) => trade.status === "success"); |
| 31 | + // sort by timestamp |
| 32 | + successfulTransactions.sort((a, b) => b.createdAt - a.createdAt); |
| 33 | + const latestSuccessfulTx = successfulTransactions[0]; |
| 34 | + const inTokenSymbol = latestSuccessfulTx?.inTokenSymbol ?? "N/A"; |
| 35 | + const outTokenSymbol = latestSuccessfulTx?.outTokenSymbol ?? "N/A"; |
| 36 | + const inAmount = latestSuccessfulTx?.inUiAmount?.toFixed(8) ?? "N/A"; |
| 37 | + const outAmount = latestSuccessfulTx?.outUiAmount?.toFixed(8) ?? "N/A"; |
| 38 | + const unrealizedProfitPercent = latestSuccessfulTx?.unrealizedProfitPercent; |
| 39 | + const profitPercent = latestSuccessfulTx?.profitPercent?.toFixed(8) ?? 0; |
| 40 | + |
| 41 | + output += eventTitle("Successful transaction"); |
| 42 | + output += |
| 43 | + unrealizedProfitPercent && unrealizedProfitPercent > 0 |
| 44 | + ? `Unrealized Profit: ${unrealizedProfitPercent} % | ` |
| 45 | + : `Profit: ${profitPercent} % | `; |
| 46 | + output += `${inAmount} ${inTokenSymbol} >>> `; |
| 47 | + output += `${outAmount} ${outTokenSymbol}`; |
| 48 | + |
| 49 | + output = MARKER_CHAR + output; |
| 50 | + } |
| 51 | + |
| 52 | + if (status === "strategy:stopLossExceeded") { |
| 53 | + output += eventTitle("Stop loss exceeded"); |
| 54 | + } |
| 55 | + |
| 56 | + if (status === "strategy:shouldReset") { |
| 57 | + output += eventTitle("Resetting strategy"); |
| 58 | + } |
| 59 | + |
| 60 | + if (status === "bot:error") { |
| 61 | + output += eventTitle("Bot error"); |
| 62 | + } |
| 63 | + |
| 64 | + if (status === "!shutdown") { |
| 65 | + output += eventTitle("Shutdown"); |
| 66 | + } |
| 67 | + |
| 68 | + output && |
| 69 | + t.api.sendMessage(1234567890, output).catch((err) => { |
| 70 | + console.error("TelegramPlugin: error sending message: ", err); |
| 71 | + }); |
| 72 | + } |
| 73 | + ); |
| 74 | + |
| 75 | + await t.start(); |
| 76 | + }, |
| 77 | +}); |
0 commit comments