Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Alwin24 committed Jul 26, 2024
1 parent f3777ef commit 3106190
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Metaplex } from "@metaplex-foundation/js";
configDotenv();

export const bot = new Telegraf(process.env.BOT_TOKEN!);
export const connection = new Connection(process.env.BACKEND_RPC!);

const app = express();

Expand Down Expand Up @@ -71,6 +70,8 @@ bot.command("register", async (ctx) => {
}

const mintAddress = new PublicKey(tokenMint);

const connection = new Connection(process.env.BACKEND_RPC!);
const metaplex = Metaplex.make(connection);

try {
Expand Down
64 changes: 61 additions & 3 deletions src/utils/listenerCallback.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,62 @@
import { Connection, PublicKey } from "@solana/web3.js";
import { bot } from "..";
import Token from "../models/token";
import TxnSignature from "../models/txnSignature";
import connectToDatabase from "./database";
import fs from "fs";
import { configDotenv } from "dotenv";
configDotenv();

const dexscreenerUrl = "https://dexscreener.com/solana/";
const jupiterUrl = "https://jup.ag/swap/USDC-";
const txnUrl = "https://solscan.io/tx/";
const buyerUrl = "https://solscan.io/account/";

const messageQueue = new Array<{
groupId: number;
image: string;
caption: string;
}>();

const getMarketCap = async (tokenMint: PublicKey) => {
const token = tokenMint.toBase58();
const connection = new Connection(process.env.BACKEND_RPC!);

const accountInfoPromise = connection.getParsedAccountInfo(tokenMint);
const tokenPricePromise = fetch(
`https://price.jup.ag/v6/price?ids=${token}`
).then((res) => res.json());

const [accountInfoResult, tokenPriceResult] = await Promise.allSettled([
accountInfoPromise,
tokenPricePromise,
]);

if (
accountInfoResult.status !== "fulfilled" ||
!accountInfoResult.value.value
) {
throw new Error("Account info not found");
}

const accountInfo = (accountInfoResult.value.value?.data as any).parsed.info;
const decimals = accountInfo.decimals;
const totalSupply = parseInt(accountInfo.supply) / 10 ** decimals;

if (
tokenPriceResult.status !== "fulfilled" ||
!tokenPriceResult.value.data[token]
) {
throw new Error("Token price not found");
}

const tokenPrice = tokenPriceResult.value.data[token].price;

if (!totalSupply) throw new Error("Total supply not found");
const marketCap = totalSupply * tokenPrice;

return marketCap;
};

const callback = async (data: any) => {
try {
const txnSignature = data.signature;
Expand Down Expand Up @@ -72,14 +120,24 @@ const callback = async (data: any) => {

const { groupId, image, name, symbol } = listeningGroup;

await bot.telegram.sendPhoto(groupId, image, {
messageQueue.push({
groupId,
image,
caption: `*${name} ${
tokenChange.buyOrSell
}!*\nGot: *${tokenChange.amount.toFixed(2)} ${symbol}*\n${
tokenChange.isNewHolder ? "New Holder" : "Existing Holder"
}\n[Buyer](${buyerUrl}${signer}) / [Txn](${txnUrl}${txnSignature})\n\n[Buy](${jupiterUrl}${txnSignature}) | [Dexscreener](${dexscreenerUrl}${txnSignature})`,
parse_mode: "Markdown",
});

// await bot.telegram.sendPhoto(groupId, image, {
// caption: `*${name} ${
// tokenChange.buyOrSell
// }!*\nGot: *${tokenChange.amount.toFixed(2)} ${symbol}*\n${
// tokenChange.isNewHolder ? "New Holder" : "Existing Holder"
// }\n[Buyer](${buyerUrl}${signer}) / [Txn](${txnUrl}${txnSignature})\n\n[Buy](${jupiterUrl}${txnSignature}) | [Dexscreener](${dexscreenerUrl}${txnSignature})`,
// parse_mode: "Markdown",
// });
}
return;
} catch (error: any) {
Expand Down

0 comments on commit 3106190

Please sign in to comment.