Created
April 23, 2025 05:59
-
-
Save YogaSakti/a6ed52297af16cb317f1140c8d50b05c to your computer and use it in GitHub Desktop.
Binance Smart Chain RPC Tester
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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