Skip to content

Instantly share code, notes, and snippets.

@YogaSakti
Created April 23, 2025 05:59
Show Gist options
  • Save YogaSakti/a6ed52297af16cb317f1140c8d50b05c to your computer and use it in GitHub Desktop.
Save YogaSakti/a6ed52297af16cb317f1140c8d50b05c to your computer and use it in GitHub Desktop.
Binance Smart Chain RPC Tester
const fetch = require('node-fetch');
const rpcNodes = [
'***.***.***.***',
'***.***.***.***',
];
async function getReferenceBlock() {
try {
const res = await fetch('https://bsc-mainnet.public.blastapi.io', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_blockNumber',
params: [],
id: 1
})
});
const data = await res.json();
return parseInt(data.result, 16);
} catch (err) {
console.error('❌ Failed to fetch reference block height:', err.message);
return null;
}
}
async function measureLatency(ip, repeat = 10) {
let totalLatency = 0;
let successCount = 0;
let lastBlockHex = null;
for (let i = 0; i < repeat; i++) {
const start = Date.now();
try {
const res = await fetch(`http://${ip}:8545`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_blockNumber',
params: [],
id: 1
}),
timeout: 3000
});
const end = Date.now();
const latency = end - start;
const data = await res.json();
if (data.result) {
totalLatency += latency;
lastBlockHex = data.result;
successCount++;
}
} catch {
// Ignore failed attempt
}
}
return successCount > 0
? {
ip,
avgLatency: totalLatency / successCount,
successCount,
blockHex: lastBlockHex,
blockDecimal: parseInt(lastBlockHex, 16)
}
: null;
}
(async () => {
console.log('πŸ” Fetching reference block height from BlastAPI...');
const referenceBlock = await getReferenceBlock();
if (!referenceBlock) {
console.error('❌ Cannot proceed without reference block.');
return;
}
console.log(`πŸ“Œ Reference Block (BlastAPI): ${referenceBlock}\n`);
const results = [];
for (const ip of rpcNodes) {
const result = await measureLatency(ip, 10);
if (result) results.push(result);
}
results.sort((a, b) => a.avgLatency - b.avgLatency);
console.log('🏁 Ranked by Average Latency:\n');
results.forEach((r, i) => {
const behind = referenceBlock - r.blockDecimal;
const status = behind > 30 ? 'DIE' : 'OK ';
console.log(
`${String(i + 1).padStart(2)}. ${r.ip.padEnd(15)} ➜ Latency: ${r.avgLatency.toFixed(2)} ms | Block: ${r.blockDecimal} | Status: ${status}`
);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment