import { Connection } from "@solana/web3.js"; export async function getRpcNode() { const nodes = [ "https://api.mainnet-beta.solana.com", "https://solana-api.projectserum.com", "https://try-rpc.mainnet.solana.blockdaemon.tech", "https://rpc.ankr.com/solana", 'https://raydium.rpcpool.com', 'https://mango-mango-d092.mainnet.rpcpool.com', 'https://zeta.rpcpool.com', 'https://symmetry.rpcpool.com', 'https://eu-west-1.genesysgo.net/ad93c9fc-8b53-4019-a2a1-ab580b17b1d3', 'https://step.rpcpool.com' ]; const all = await Promise.all( nodes.map(async (url) => { const start = new Date(); const ok = await pingWithTimeout(url); const end = new Date(); const time = end.getTime() - start.getTime(); return { time: ok ? time : Infinity, url, ok }; }), ); all.sort((a, b) => a.time - b.time); return all; } const delay = (time) => new Promise((resolve, reject) => setTimeout(resolve, time)); async function pingWithTimeout(url, timeout = 3000) { try { const connection = new Connection(url, { commitment: 'confirmed', confirmTransactionInitialTimeout: 3000, }); const racer = [connection.getVersion(), delay(timeout)]; const res = await Promise.race(racer); return res; } catch (err) { return null; } }