Skip to content

Commit 2e10bc2

Browse files
committed
telegram-plugin: init
1 parent 8b1c481 commit 2e10bc2

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
root: true,
3+
// This tells ESLint to load the config from the package `@arb-protocol/eslint-config`
4+
extends: ["custom"],
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
src
2+
.eslintrc.js
3+
tsconfig.json
4+
node_moudles
5+
.turbo
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "@arb-protocol/telegram-plugin",
3+
"version": "2.0.0-alpha.5",
4+
"license": "MIT",
5+
"description": "Telegram plugin for @arb-protocol/core",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/ARBProtocol/solana-jupiter-bot.git"
9+
},
10+
"main": "dist/index.js",
11+
"module": "dist/index.mjs",
12+
"types": "dist/index.d.ts",
13+
"files": [
14+
"dist"
15+
],
16+
"scripts": {
17+
"dev": "ts-node src/index.ts",
18+
"build": "tsup src/index.ts --format cjs,esm --dts",
19+
"lint": "tsc --noEmit --skipLibCheck"
20+
},
21+
"dependencies": {
22+
"@arb-protocol/core": "2.0.0-alpha.5",
23+
"grammy": "^1.16.2"
24+
},
25+
"devDependencies": {
26+
"@arb-protocol/eslint-config": "2.0.0-alpha.5",
27+
"@types/node": "^18.11.10",
28+
"@typescript-eslint/eslint-plugin": "^5.45.0",
29+
"@typescript-eslint/parser": "^5.45.0",
30+
"eslint": "^8.29.0",
31+
"ts-node": "^10.9.1",
32+
"tsconfig": "2.0.0-alpha.5",
33+
"tsup": "^6.7.0",
34+
"typescript": "5.0.2"
35+
}
36+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "tsconfig/base.json",
3+
"compilerOptions": {
4+
"outDir": "dist",
5+
"rootDir": "src",
6+
"baseUrl": "."
7+
},
8+
"include": ["src/**/*.ts"],
9+
"exclude": ["node_modules"]
10+
}

0 commit comments

Comments
 (0)