Skip to content

Commit

Permalink
telegram-plugin: init
Browse files Browse the repository at this point in the history
  • Loading branch information
pmioduszewski committed Jun 18, 2023
1 parent 8b1c481 commit 2e10bc2
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/telegram-plugin/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
root: true,
// This tells ESLint to load the config from the package `@arb-protocol/eslint-config`
extends: ["custom"],
};
5 changes: 5 additions & 0 deletions packages/telegram-plugin/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
src
.eslintrc.js
tsconfig.json
node_moudles
.turbo
36 changes: 36 additions & 0 deletions packages/telegram-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@arb-protocol/telegram-plugin",
"version": "2.0.0-alpha.5",
"license": "MIT",
"description": "Telegram plugin for @arb-protocol/core",
"repository": {
"type": "git",
"url": "https://github.com/ARBProtocol/solana-jupiter-bot.git"
},
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"dev": "ts-node src/index.ts",
"build": "tsup src/index.ts --format cjs,esm --dts",
"lint": "tsc --noEmit --skipLibCheck"
},
"dependencies": {
"@arb-protocol/core": "2.0.0-alpha.5",
"grammy": "^1.16.2"
},
"devDependencies": {
"@arb-protocol/eslint-config": "2.0.0-alpha.5",
"@types/node": "^18.11.10",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"eslint": "^8.29.0",
"ts-node": "^10.9.1",
"tsconfig": "2.0.0-alpha.5",
"tsup": "^6.7.0",
"typescript": "5.0.2"
}
}
77 changes: 77 additions & 0 deletions packages/telegram-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Bot } from "@arb-protocol/core";
import { Bot as T } from "grammy";

const MARKER_CHAR = "▌";
const TELEGRAM_BOT_TOKEN = "GET THIS FROM ENV";
// TODO: get this from env
const eventTitle = (title: string) => {
return title.padEnd(26, " ");
};

export const TelegramPlugin = <T extends Bot>(bot: T) => ({
...bot,
telegram: async () => {
if (!TELEGRAM_BOT_TOKEN) {
throw new Error("TELEGRAM_BOT_TOKEN env variable is not set");
}

const t = new T(TELEGRAM_BOT_TOKEN);

let output = "";

bot.store.subscribe(
(state) => state.status.value,
(status) => {
output = "";
const state = bot.store.getState();

if (status === "history:successfulTx") {
const tradeHistory = Object.values(state.tradeHistory);
const successfulTransactions = tradeHistory.filter((trade) => trade.status === "success");
// sort by timestamp
successfulTransactions.sort((a, b) => b.createdAt - a.createdAt);
const latestSuccessfulTx = successfulTransactions[0];
const inTokenSymbol = latestSuccessfulTx?.inTokenSymbol ?? "N/A";
const outTokenSymbol = latestSuccessfulTx?.outTokenSymbol ?? "N/A";
const inAmount = latestSuccessfulTx?.inUiAmount?.toFixed(8) ?? "N/A";
const outAmount = latestSuccessfulTx?.outUiAmount?.toFixed(8) ?? "N/A";
const unrealizedProfitPercent = latestSuccessfulTx?.unrealizedProfitPercent;
const profitPercent = latestSuccessfulTx?.profitPercent?.toFixed(8) ?? 0;

output += eventTitle("Successful transaction");
output +=
unrealizedProfitPercent && unrealizedProfitPercent > 0
? `Unrealized Profit: ${unrealizedProfitPercent} % | `
: `Profit: ${profitPercent} % | `;
output += `${inAmount} ${inTokenSymbol} >>> `;
output += `${outAmount} ${outTokenSymbol}`;

output = MARKER_CHAR + output;
}

if (status === "strategy:stopLossExceeded") {
output += eventTitle("Stop loss exceeded");
}

if (status === "strategy:shouldReset") {
output += eventTitle("Resetting strategy");
}

if (status === "bot:error") {
output += eventTitle("Bot error");
}

if (status === "!shutdown") {
output += eventTitle("Shutdown");
}

output &&
t.api.sendMessage(1234567890, output).catch((err) => {
console.error("TelegramPlugin: error sending message: ", err);
});
}
);

await t.start();
},
});
10 changes: 10 additions & 0 deletions packages/telegram-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"baseUrl": "."
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}

0 comments on commit 2e10bc2

Please sign in to comment.