-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b1c481
commit 2e10bc2
Showing
5 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
src | ||
.eslintrc.js | ||
tsconfig.json | ||
node_moudles | ||
.turbo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |