Can anyone help me to figure out a way to monitor new pool creation on BSC using Websocket. It seems like my node receives one request, code still runes but it just sends one request, maybe problem is there i dont know, but its clearly connected to websocket, and new pools are gettign created, but its doesent working
After trying this, it doesent see new pool creations
const WebSocket = require('ws');
const fetch = require('node-fetch');
const BSC_HTTP_URL = 'https://go.getblock.io/63a569b81c6940b9b1f1230d6dd7f042'; // Replace with QuickNode HTTPS endpoint for BSC
const BSC_WSS_URL = 'wss://go.getblock.io/1718c1c14c0441dd8fb04dfd7146c32d'; // Replace with QuickNode WSS endpoint for BSC
const RAYDIUM_PUBLIC_KEY = '675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8'; // Raydium program ID
const SESSION_HASH = 'QNDEMO' + Math.ceil(Math.random() * 1e9); // Random unique identifier for your session
let credits = 0;
// Connect to WebSocket endpoint
let ws = new WebSocket(BSC_WSS_URL, {
headers: {
'x-session-hash': SESSION_HASH
}
});
ws.on('open', () => {
console.log('WebSocket connection established');
});
ws.on('message', async (data) => {
const message = JSON.parse(data);
if (message.method === 'pairCreated') {
const newPoolAddress = message.params.address;
console.log('New pool created:', newPoolAddress);
// Fetch pool details if needed
const pools = await fetchPools();
const newPool = pools.find(pool => pool.address === newPoolAddress);
if (newPool) {
console.log('New pool details:', newPool);
} else {
console.log('New pool details not available');
}
}
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
ws.on('close', () => {
console.log('WebSocket connection closed');
// Reconnect to WebSocket after a delay
setTimeout(connectWebSocket, 5000);
});
// Function to fetch list of pools
async function fetchPools() {
try {
const response = await fetch('https://api.pancakeswap.info/api/v2/pairs');
const pairsData = await response.json();
return pairsData.data;
} catch (error) {
console.error('Error fetching pools:', error);
return [];
}
}
// Start monitoring for new pools
function connectWebSocket() {
console.log('Reconnecting to WebSocket...');
ws = new WebSocket(BSC_WSS_URL, {
headers: {
'x-session-hash': SESSION_HASH
}
});
}
// Initial connection
connectWebSocket();
Want to create a js script to monitor new pool creations