Count Polygon RPC calls by method (DATA_DIR/rpc-count.json every 5 min) for a 24 h measurement

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-06 08:47:15 -05:00
parent b43d3ba1af
commit e859ba4653
+6
View File
@@ -128,11 +128,17 @@ function saveState() {
// ranged getLogs: publicnode and tenderly take a 2000-block span; drpc and 1rpc cap at 50
const LOG_RPCS = ['https://polygon-bor-rpc.publicnode.com', 'https://polygon.gateway.tenderly.co'];
// RPC call counter (2026-10-06, Marty: measure 24 h of Polygon traffic before choosing a paid endpoint): counts every request
// this module sends, by method, and writes DATA_DIR/rpc-count.json every 5 minutes. Read-only bookkeeping; no behaviour change.
const RPC_COUNT = { since: Date.now(), m: {} };
const rpcCount = method => { RPC_COUNT.m[method] = (RPC_COUNT.m[method] || 0) + 1; };
setInterval(() => { try { fs.writeFileSync(path.join(DATA_DIR, 'rpc-count.json'), JSON.stringify(Object.assign({ at: Date.now() }, RPC_COUNT))); } catch (e) {} }, 300000).unref();
let rpcIdx = 0;
async function rpc(method, params, timeoutMs = 15000, urls = RPCS) {
let lastErr = new Error('no rpc');
for (let i = 0; i < urls.length; i++) {
const url = urls[(rpcIdx + i) % urls.length];
rpcCount(method);
try {
const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), timeoutMs);