To monitor newly created liquidity pools (LPs) on Raydium and fetch the associated SPL token metadata using your QuickNode endpoint, you can follow these steps:
-
Set Up Your Project: Create a new Node.js project and install the necessary dependencies.
-
Connect to QuickNode: Utilize your QuickNode HTTP and WebSocket endpoints to establish a connection to the Solana blockchain.
-
Monitor Raydium for New LPs: Listen for specific instructions indicating the creation of new LPs on Raydium.
-
Fetch SPL Token Metadata: Retrieve and decode the metadata for the tokens involved in the new LPs.
Below is a comprehensive script that accomplishes these tasks:
import { Connection, PublicKey } from '@solana/web3.js';
import { Buffer } from 'buffer';
// Constants
const RAYDIUM_PROGRAM_ID = new PublicKey('675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8');
const METADATA_PROGRAM_ID = new PublicKey('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s');
const QUICKNODE_HTTP_URL = 'https://hidden-lingering-glade.solana-mainnet.quiknode.pro/7bbd1bfb767c727d6ac8c09a50dbae0c94bdbbe6/';
const QUICKNODE_WSS_URL = 'wss://hidden-lingering-glade.solana-mainnet.quiknode.pro/7bbd1bfb767c727d6ac8c09a50dbae0c94bdbbe6/';
const INSTRUCTION_NAME = 'initialize2';
// Establish connection
const connection = new Connection(QUICKNODE_HTTP_URL, {
wsEndpoint: QUICKNODE_WSS_URL,
});
// Function to fetch token metadata
async function fetchTokenMetadata(mintAddress) {
try {
const mintPublicKey = new PublicKey(mintAddress);
const [metadataPDA] = await PublicKey.findProgramAddress(
[Buffer.from('metadata'), METADATA_PROGRAM_ID.toBuffer(), mintPublicKey.toBuffer()],
METADATA_PROGRAM_ID
);
const accountInfo = await connection.getAccountInfo(metadataPDA);
if (accountInfo) {
return decodeMetadata(accountInfo.data);
}
} catch (error) {
console.error(`Error fetching metadata for token ${mintAddress}:`, error);
}
return { name: null, symbol: null };
}
// Function to decode metadata
function decodeMetadata(buffer) {
let offset = 0;
const readString = () => {
const length = buffer.readUInt32LE(offset);
offset += 4;
const str = buffer.slice(offset, offset + length).toString('utf8');
offset += length;
return str;
};
offset += 1; // Discriminator
offset += 32; // Mint
offset += 32; // Update authority
const name = readString();
const symbol = readString();
readString(); // URI
return { name: name.replace(/\0/g, '').trim(), symbol: symbol.replace(/\0/g, '').trim() };
}
// Function to fetch Raydium mints
async function fetchRaydiumMints(txId) {
try {
const tx = await connection.getParsedTransaction(txId, {
maxSupportedTransactionVersion: 0,
commitment: 'confirmed',
});
const accounts = tx?.transaction.message.instructions.find(
(ix) => ix.programId.toBase58() === RAYDIUM_PROGRAM_ID.toBase58()
).accounts;
if (!accounts) {
console.log('No accounts found in the transaction.');
return;
}
const tokenAIndex = 8;
const tokenBIndex = 9;
const tokenAAccount = accounts[tokenAIndex];
const tokenBAccount = accounts[tokenBIndex];
const tokenAMetadata = await fetchTokenMetadata(tokenAAccount.toBase58());
const tokenBMetadata = await fetchTokenMetadata(tokenBAccount.toBase58());
console.log('New LP Found');
console.table([
{ Token: 'A', Address: tokenAAccount.toBase58(), Name: tokenAMetadata.name, Symbol: tokenAMetadata.symbol },
{ Token: 'B', Address: tokenBAccount.toBase58(), Name: tokenBMetadata.name, Symbol: tokenBMetadata.symbol },
]);
} catch (error) {
console.error('Error fetching transaction:', txId, error);
}
}
// Function to start monitoring
async function startMonitoring() {
console.log('Monitoring logs for program:', RAYDIUM_PROGRAM_ID.toString());
connection.onLogs(
RAYDIUM_PROGRAM_ID,
({ logs, err, signature }) => {
if (err) return;
if (logs && logs.some((log) => log.includes(INSTRUCTION_NAME))) {
console.log('Signature for "initialize2":', `https://explorer.solana.com/tx/${signature}`);
fetchRaydiumMints(signature);
}
},
'finalized'
);
}
// Start the monitoring process
startMonitoring().catch(console.error);
Explanation:
-
Connection Setup: The script establishes a connection to the Solana blockchain using your QuickNode HTTP and WebSocket endpoints.
-
Monitoring Raydium: It listens for logs from the Raydium program, specifically looking for the 'initialize2' instruction, which indicates the creation of a new LP.
-
Fetching Token Metadata: Upon detecting a new LP, the script fetches the associated token mints and retrieves their metadata, including the token name and symbol.
Running the Script:
-
Ensure you have Node.js installed.
-
Save the script to a file, e.g., monitorRaydiumLPs.js.
-
Install the required dependencies:
npm install @solana/web3.js buffer
-
Run the script:
node monitorRaydiumLPs.js
This script will continuously monitor the Solana blockchain for new LPs created on Raydium and output the relevant token metadata to the console.
For a more detailed guide on tracking new liquidity pools on Raydium, refer to QuickNode's guide[1]:
Additionally, for fetching SPL token metadata, you can consult QuickNode's documentation: