To fetch Raydium pool keys in real-time, use Solana's program accounts with filtering and WebSocket monitoring instead of relying on the static mainnet.json file. Here's how to implement this:
Real-Time Pool Detection Methods
1. Solana Program Accounts with Filtering
Use getProgramAccounts with memcmp filters to detect new pools. This method is demonstrated in the Raydium SDK demo[8][[1]][[5]].
Key Steps:
Query AMM Program Accounts
Filter accounts by the AMM program ID (e.g., 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 for AMM v4)[[3]][[7]].
Decode Account Data
Use the Raydium SDK's LiquidityPoolKeysV4 layout to parse pool metadata[[4]][[6]].
WebSocket Monitoring
Set up a WebSocket connection to Solana's RPC to listen for new accounts in real-time[[1]][[8]].
Example Code Snippet (simplified):
typescript
import { Connection, PublicKey } from '@solana/web3.js';
import { LIQUIDITY_POOL_KEYS_LAYOUT_V4 } from '@raydium-io/raydium-sdk';
const connection = new Connection('https://api.mainnet-beta.solana.com');
const AMM_PROGRAM_ID = new PublicKey('675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8');
// Filter accounts by AMM program and specific data fields
const accounts = await connection.getProgramAccounts(AMM_PROGRAM_ID, {
filters: [
{ memcmp: { offset: 0, bytes: AMM_PROGRAM_ID.toBase58() } },
{ memcmp: { offset: 8, bytes: '0x' } } // Example filter
]
});
accounts.forEach(account => {
const poolKeys = LIQUIDITY_POOL_KEYS_LAYOUT_V4.decode(account.data);
// Process pool keys (e.g., poolId, tokenMints, lpMint)
});
2. Raydium SDK WebSocket Integration
For event-driven updates, use the Raydium SDK's WebSocket implementation to listen for pool creation events[[1]][[5]].
Steps:
Subscribe to AMM Events
Use the SDK's subNewAmmPool function to monitor new pool creation[[8]].
Process Events
Extract pool metadata (e.g., token addresses, LP mint) from event logs.
Example Setup:
typescript
import { subNewAmmPool } from '@raydium-io/raydium-sdk';
// Initialize WebSocket connection
const subscription = subNewAmmPool(connection, (pool) => {
console.log(`New pool detected: ${pool.poolId}`);
// Fetch pool details using Raydium SDK methods
});
Comparison of Methods
Method
Pros
Cons
Solana Program Accounts
Direct access to on-chain data, no rate limits
Requires complex filtering and decoding
Raydium SDK WebSocket
Simplified event handling, real-time updates
Depends on SDK implementation
Post-Detection Processing
Once a pool is detected, use the Raydium SDK to:
Fetch Pool Info raydium.liquidity.fetchPoolInfo(poolId) to get token mints, LP mint, and reserves[[7]].
Decode LP Token State
Check if LP tokens are locked/burnt using LiquidityPoolKeysV4[[6]].
Key Considerations
Rate Limits: Avoid excessive RPC calls; use WebSocket for real-time updates[[1]][[5]].
Program IDs: Ensure correct AMM program ID (e.g., v4: 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8)[[3]][[7]].
Data Validation: Cross-check pool metadata with token lists to avoid invalid pools[[3]][[5]].
For production use, combine WebSocket monitoring with periodic checks against the Raydium SDK's token lists to validate pool legitimacy[[3]][[7]].