Skip to content

Commit

Permalink
min amount check changed to usd value
Browse files Browse the repository at this point in the history
  • Loading branch information
Alwin24 committed Jul 31, 2024
1 parent 88623f7 commit 903ddaf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ function startPing(ws: WebSocket) {
setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
console.log("Sending ping...");
ws.ping();
ws.ping(undefined, undefined, (err) => {
if (err) {
console.log("Failed to send ping:", err);
}
});
} else {
console.log("WebSocket is not open");
}
Expand Down
58 changes: 33 additions & 25 deletions src/utils/listenerCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,8 @@ const buyerUrl = "https://solscan.io/account/";
const dexTUrl = "https://www.dextools.io/app/en/solana/pair-explorer/";
const solTrendingUrl = "https://t.me/SOLTRENDING";

const getTokenInfo = async (tokenMint: string) => {
const getTokenPrice = async (tokenMint: string) => {
try {
const connection = new Connection(process.env.BACKEND_RPC!);

const accountInfoResult: any = await connection.getParsedAccountInfo(
new PublicKey(tokenMint)
);

if (!accountInfoResult.value) {
console.log("accountInfoResult", accountInfoResult);
throw new Error("Account info not found");
}

async function fetchTokenPrice(tokenMint: string) {
const url = `https://price.jup.ag/v6/price?ids=${tokenMint},SOL`;
let attempts = 0;
Expand Down Expand Up @@ -64,7 +53,7 @@ const getTokenInfo = async (tokenMint: string) => {
throw new Error("SOL price found, token price not found");
tokenPrice = tokenPriceResult.data[tokenMint].price;
} catch (error: any) {
console.log("Fetching token price failed. Trying dexscreener...");
console.log("Fetching token price failed. Trying birdseye...");

const options = {
method: "GET",
Expand All @@ -86,17 +75,35 @@ const getTokenInfo = async (tokenMint: string) => {
}
}

return { tokenPrice, solPrice };
} catch (error: any) {
console.log("Error in getTokenPrice", error.message);
return { tokenPrice: 0, solPrice: 0 };
}
};

const getTotalSupply = async (tokenMint: string) => {
try {
const connection = new Connection(process.env.BACKEND_RPC!);

const accountInfoResult: any = await connection.getParsedAccountInfo(
new PublicKey(tokenMint)
);

if (!accountInfoResult.value) {
console.log("accountInfoResult", accountInfoResult);
throw new Error("Account info not found");
}

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

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

return { marketCap, tokenPrice, solPrice };
return totalSupply;
} catch (error: any) {
console.log("Error in getTokenInfo", error.message);
return { marketCap: 0, tokenPrice: 0, solPrice: 0 };
console.log("Error in getTotalSupply", error.message);
return 0;
}
};

Expand Down Expand Up @@ -142,7 +149,7 @@ const callback = async (data: any) => {
if (preTokenAmount >= postTokenAmount) continue;

const isNewHolder = preTokenAmount === 0;
const amount = Math.abs(postTokenAmount - preTokenAmount);
const amount = postTokenAmount - preTokenAmount;
const positionIncrease = (amount * 100) / preTokenAmount;

tokenChanges[mint] = {
Expand All @@ -161,11 +168,15 @@ const callback = async (data: any) => {
const tokenMint = listeningGroup.tokenMint;
const tokenChange = tokenChanges[tokenMint];

if (tokenChange.amount < listeningGroup.minValue) {
const { tokenPrice, solPrice } = await getTokenPrice(tokenMint);

if (tokenChange.amount * tokenPrice < listeningGroup.minValue) {
continue;
}

const { marketCap, tokenPrice, solPrice } = await getTokenInfo(tokenMint);
const totalSupply = await getTotalSupply(tokenMint);
const marketCap = Math.floor(totalSupply * tokenPrice).toLocaleString();

let {
groupId,
image,
Expand Down Expand Up @@ -206,14 +217,11 @@ const callback = async (data: any) => {

let emojis = "";
const times = Math.min(
Math.floor(tokenChange.amount / minValue),
Math.floor(parseFloat(spentUsd) / minValue),
remainingLength / minValueEmojis.length
);
for (let i = 0; i < times; i++) emojis += minValueEmojis;

// emojis = emojis.match(/.{1,20}/g)?.join("\n") || "";

// emojis = emojis.slice(0, remainingLength);
caption = caption.replace("__emojis__", emojis);

if (!messageQueues[groupId]) {
Expand Down

0 comments on commit 903ddaf

Please sign in to comment.