1import { writeFile } from "fs/promises";2import { join } from "path";3import { exit } from "process";4import { fileURLToPath } from "url";56const url = process.env.RPC_URL;7if (!url) throw new Error('RPC_URL is not set');89const srcDir = fileURLToPath(new URL('.', import.meta.url));1011for (let i = 0; i < 10; i++) {12 try {13 console.log(`Trying to fetch metadata, retry ${i + 1}/${10}`)14 const response = await fetch(url, {15 method: 'POST',16 headers: {17 'Content-Type': 'application/json',18 },19 redirect: 'follow',20 body: JSON.stringify({21 jsonrpc: '2.0',22 id: '1',23 method: 'state_getMetadata',24 params: [],25 }),26 });27 const json = await response.json();28 const output = join(srcDir, 'interfaces/metadata.json');29 console.log(`Received response, saving to ${output}`);30 await writeFile(output, JSON.stringify(json));31 exit(0);32 } catch (e) {33 console.error('Failed to request metadata:');34 console.error(e);35 console.error('Waiting 1 minute');36 await new Promise(res => setTimeout(res, 60 * 1000));37 }38}39console.error('Out of retries');40exit(1);