Repository navigation
Expand file tree
/
Copy pathgrpc.js
More file actions
204 lines (176 loc) · 6.26 KB
/
Copy pathgrpc.js
File metadata and controls
204 lines (176 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import "dotenv/config";
import Client from "@triton-one/yellowstone-grpc";
import { CommitmentLevel } from "@triton-one/yellowstone-grpc";
import { decodeInstruction } from '@solana/spl-token';
import { Connection, Keypair } from '@solana/web3.js';
import chalk from "chalk";
import { tOutPut } from "./parsingtransaction.js";
import { handleNewTokenLaunch } from "./main.js";
import dotenv from 'dotenv'
dotenv.config();
const GRPCTOKEN=process.env.GRPCTOKEN
const GRPC_ENDPOINT = process.env.GRPC_ENDPOINT
// Pre-define constants
const SOLANA_TOKEN = "So11111111111111111111111111111111111111112";
const RAYDIUM_FEE = "LanMV9sAd7wArD4vJFi2qDdfnVhFxYSUg6eADduJ3uj"//"7YttLkHDoNj9wyDur5pM1ejNaAvT9X4eqaYcHQqtj2G5";
const Raydium_launchpad_authority = "WLHv2UAZm6z4KyaaELi5pjdbJh6RESMva1Rnn8pJVVh"
// Create default client
const defaultClient = new Client(
GRPC_ENDPOINT,
GRPCTOKEN
);
export let isNewLaunchRunning = true;
export const stopNewLaunch = () => {
isNewLaunchRunning = false;
console.log(chalk.red("New launch monitoring stopped"));
};
// Default request args
const defaultArgs = {
accounts: {},
slots: {},
transactions: {
pumpfun: {
vote: false,
failed: false,
signature: undefined,
accountInclude: [RAYDIUM_FEE],
accountExclude: [],
accountRequired: [],
},
},
transactionsStatus: {},
entry: {},
blocks: {},
blocksMeta: {},
accountsDataSlice: [],
ping: undefined,
commitment: CommitmentLevel.PROCESSED,
};
// This function checks for MintTo instructions by comparing with log messages
async function checkMintTo(data) {
const tx = data.transaction?.transaction;
const meta = data.transaction;
if (!tx || !meta?.transaction?.meta?.logMessages) return;
// Find if MintTo is present in log messages
const mintToLog = meta.transaction.meta.logMessages.find((log) =>
typeof log === "string" && log.toLowerCase().includes("instruction: mintto")
);
if (mintToLog) {
console.log("🩸🩸🩸🩸🩸 MintTo found in logs!");
return true
} else {
// No MintTo found in logs
return false
}
}
async function handleStream(client = defaultClient, args = defaultArgs) {
const stream = await client.subscribe(args);
const streamClosed = new Promise((resolve, reject) => {
stream.on("error", (error) => {
console.error("Stream Error:", error);
reject(error);
stream.end();
});
stream.on("end", resolve);
stream.on("close", resolve);
});
stream.on("data", async (data) => {
// Return early if monitoring is disabled
if (!isNewLaunchRunning) {
stream.end();
return;
}
try {
// console.log(chalk.green("start__________new token streaming_________"));
if (!data?.transaction?.transaction) {
return null;
}
const mintTo = await checkMintTo(data)
if(!mintTo){
return null
}
console.log(`[${new Date().toISOString()}] 🩸🩸🩸🩸🩸 MintTo found in logs!`);
const preTokenBalances = data?.transaction?.transaction?.meta?.preTokenBalances;
const postTokenBalances = data?.transaction?.transaction?.meta?.postTokenBalances;
if (!preTokenBalances || !postTokenBalances) {
console.log("Token balances not found in transaction data");
return null;
}
let pre_sol = 0;
let post_sol = 0;
let pre_token = 0;
let post_token = 0;
let token_mint = "";
let token_owner = "";
for (const balance of postTokenBalances) {
if (balance.owner !== Raydium_launchpad_authority) {
if (balance.mint !== SOLANA_TOKEN) {
post_token = balance.uiTokenAmount.uiAmount || 0;
token_mint = balance.mint;
token_owner = balance.owner;
}
} else {
post_sol = balance.uiTokenAmount.uiAmount || 0;
}
}
for (const balance of preTokenBalances) {
if (balance.owner !== Raydium_launchpad_authority) {
if (balance.mint !== SOLANA_TOKEN) {
pre_token = balance.uiTokenAmount.uiAmount || 0;
}
}
}
const solChanges = post_sol-pre_sol;
const tokenChanges = post_token-pre_token;
console.log(chalk.bgBlue.bold(`🪙 Token Mint:`), chalk.white(token_mint));
console.log(chalk.bgMagenta.bold(`👤 Token Owner:`), chalk.white(token_owner));
console.log(chalk.bgYellow.bold(`💸 SOL Balance Change:`), chalk.yellow(`${solChanges > 0 ? "+" : ""}${solChanges}`));
console.log(chalk.bgCyan.bold(`🔄 Token Balance Change:`), chalk.cyan(`${tokenChanges > 0 ? "+" : ""}${tokenChanges}`));
if (solChanges> 0.1) {
console.log(chalk.bgGreen("Found large SOL transfer:", solChanges));
// Parse transaction data to get pool information
const parsedData = await tOutPut(data);
if (parsedData) {
console.log(chalk.cyan(`Pool status: Raydium launchpad`));
// Call the main bot logic to handle the new token launch
await handleNewTokenLaunch(token_mint, parsedData.pool_status, parsedData.context);
} else {
console.log(chalk.yellow("Failed to parse transaction data"));
}
return token_mint;
}
return null;
} catch (error) {
console.error("Error processing transaction data:", error);
return null;
}
});
try {
await stream.write(args);
} catch (error) {
console.error("Subscription request failed:", error);
throw error;
}
await streamClosed;
}
export async function newlunched_subscribeCommand(client = defaultClient, args = defaultArgs) {
// Set monitoring flag to true when starting
isNewLaunchRunning = true;
console.log(chalk.green("New launch monitoring started"));
while (isNewLaunchRunning) {
try {
await handleStream(client, args);
} catch (error) {
console.error("Stream error, restarting in 1 second...", error);
// Only wait and retry if monitoring is still enabled
if (isNewLaunchRunning) {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
}
console.log("New launch monitoring stopped");
}
// Export client and args for external use
export { defaultClient, defaultArgs };
// Remove the auto-execution to prevent conflicts
// newlunched_subscribeCommand()