Below is a script to monitor newly created liquidity pools (LPs) on Raydium and fetch metadata for all associated SPL tokens using the QuickNode endpoint. The script is written in TypeScript and utilizes Solana Web3.js and Metaplex libraries.
Prerequisites
Script
import { Connection, PublicKey } from '@solana/web3.js';
import {
createV1,
findMetadataPda,
mplTokenMetadata,
} from '@metaplex-foundation/mpl-token-metadata';
// QuickNode endpoint
const HTTP_URL = 'https://hidden-lingering-glade.solana-mainnet.quiknode.pro/7bbd1bfb767c727d6ac8c09a50dbae0c94bdbbe6/';
const WSS_URL = 'wss://hidden-lingering-glade.solana-mainnet.quiknode.pro/7bbd1bfb767c727d6ac8c09a50dbae0c94bdbbe6/';
const RAYDIUM_PROGRAM_ID = new PublicKey('675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8');
const INSTRUCTION_NAME = 'initialize2';
// Initialize connection
const connection = new Connection(HTTP_URL, { wsEndpoint: WSS_URL });
// Function to fetch SPL token metadata
async function fetchTokenMetadata(mintAddress: PublicKey) {
const metadataPda = await findMetadataPda(mintAddress);
const metadataAccountInfo = await connection.getAccountInfo(metadataPda);
if (metadataAccountInfo) {
const metadata = mplTokenMetadata.decode(metadataAccountInfo.data);
console.log(`Token Metadata for ${mintAddress.toBase58()}:`, metadata);
} else {
console.log(`No metadata found for token: ${mintAddress.toBase58()}`);
}
}
// Function to monitor new Raydium LPs
async function monitorRaydiumPools() {
console.log(`Monitoring logs for program: ${RAYDIUM_PROGRAM_ID.toBase58()}`);
connection.onLogs(RAYDIUM_PROGRAM_ID, async (logInfo) => {
const logs = logInfo.logs;
if (logs.some((log) => log.includes(INSTRUCTION_NAME))) {
console.log('New Raydium LP detected:', logInfo.signature);
// Fetch transaction details
const transaction = await connection.getTransaction(logInfo.signature, {
commitment: 'confirmed',
});
if (transaction) {
const instructions = transaction.transaction.message.instructions;
for (const instruction of instructions) {
const accounts = instruction.accounts.map((acc) => acc.toBase58());
console.log('Accounts involved in the transaction:', accounts);
// Fetch metadata for each token account
for (const account of accounts) {
try {
const publicKey = new PublicKey(account);
await fetchTokenMetadata(publicKey);
} catch (error) {
console.error(`Error fetching metadata for account ${account}:`, error);
}
}
}
}
}
});
}
// Start monitoring
monitorRaydiumPools().catch(console.error);
Explanation
- Connection Setup: The script connects to the Solana blockchain using the QuickNode HTTP and WebSocket endpoints.
- Raydium LP Monitoring:
- Listens for logs from the Raydium program (
RAYDIUM_PROGRAM_ID).
- Filters logs containing the
initialize2 instruction, which indicates the creation of a new liquidity pool.
- Fetches transaction details and extracts associated accounts.
- Token Metadata Fetching:
- For each account involved in the transaction, the script attempts to fetch and decode its metadata using the Metaplex Token Metadata protocol.
Usage
- Save the script as
monitorRaydium.ts.
- Run the script using:
ts-node monitorRaydium.ts
This script will continuously monitor for new Raydium LPs and fetch metadata for all associated SPL tokens.