#!/usr/bin/env node
// Poll each GUID one at a time with proper rate limiting (1 call per sec).

import fs from 'fs';

const API_KEY = process.env.ETHERSCAN_API_KEY;
const submissions = JSON.parse(fs.readFileSync('verify-results.json','utf8'));
const sleep = ms => new Promise(r => setTimeout(r, ms));

async function checkStatus(guid) {
  const u = `https://api.etherscan.io/v2/api?chainid=8453&module=contract&action=checkverifystatus&apikey=${API_KEY}&guid=${guid}`;
  const r = await fetch(u);
  return await r.json();
}

console.log('Polling each GUID once...\n');
for (const s of submissions) {
  if (!s.guid) {
    console.log(`${s.contract.padEnd(40)} (no guid: ${s.error?.slice(0,80)})`);
    continue;
  }
  await sleep(1200);
  const c = await checkStatus(s.guid);
  console.log(`${s.contract.padEnd(40)} status=${c.status} result=${String(c.result).slice(0,100)}`);
}
