|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import { getLatestBlockNumber, fetchPaginatedContracts, getContractData, parseArguments } from '../utils.js' |
| 4 | + |
| 5 | +const toolName = process.argv[1].split('/').pop() |
| 6 | +const resultFileName = `contracts-data-${Date.now()}.json` |
| 7 | +const resultFilePath = path.join(__dirname, resultFileName) |
| 8 | + |
| 9 | +function printUsageAndExit () { |
| 10 | + console.log(`Usage: npx babel-node src/tools/contracts/${toolName} [options]`) |
| 11 | + console.log(`Options:`) |
| 12 | + console.log(` --pageSize <number> Number of contracts to process per page (default: 50)`) |
| 13 | + console.log(` --limit <number> Maximum number of contracts to process (default: 0 = no limit)`) |
| 14 | + console.log(` --save Save results to file (optional)`) |
| 15 | + console.log(`Examples:`) |
| 16 | + console.log(` npx babel-node src/tools/contracts/${toolName}`) |
| 17 | + console.log(` npx babel-node src/tools/contracts/${toolName} --pageSize 25`) |
| 18 | + console.log(` npx babel-node src/tools/contracts/${toolName} --pageSize 25 --limit 100`) |
| 19 | + console.log(` npx babel-node src/tools/contracts/${toolName} --save`) |
| 20 | + process.exit(1) |
| 21 | +} |
| 22 | + |
| 23 | +async function getContractsData ({ pageSize = 50, limit = 0, save = false } = {}) { |
| 24 | + try { |
| 25 | + const latestBlockNumber = await getLatestBlockNumber() |
| 26 | + console.log(`Processing contracts at block ${latestBlockNumber}`) |
| 27 | + |
| 28 | + const results = { |
| 29 | + totalContracts: 0, |
| 30 | + processedContracts: 0, |
| 31 | + successfulFetches: 0, |
| 32 | + failedFetches: 0, |
| 33 | + errors: {}, |
| 34 | + contracts: {} |
| 35 | + } |
| 36 | + |
| 37 | + let cursor = null |
| 38 | + let pageCount = 0 |
| 39 | + let totalProcessed = 0 |
| 40 | + |
| 41 | + do { |
| 42 | + pageCount++ |
| 43 | + const { contracts, next } = await fetchPaginatedContracts(pageSize, cursor) |
| 44 | + |
| 45 | + if (contracts.length === 0) { |
| 46 | + console.log('No more contracts to process') |
| 47 | + break |
| 48 | + } |
| 49 | + |
| 50 | + console.log(`Processing page ${pageCount} (${contracts.length} contracts)...`) |
| 51 | + |
| 52 | + for (const contract of contracts) { |
| 53 | + if (limit > 0 && totalProcessed >= limit) { |
| 54 | + console.log(`Reached limit of ${limit} contracts`) |
| 55 | + break |
| 56 | + } |
| 57 | + |
| 58 | + const contractAddress = contract.address |
| 59 | + results.totalContracts++ |
| 60 | + totalProcessed++ |
| 61 | + |
| 62 | + try { |
| 63 | + const contractData = await getContractData(contractAddress, latestBlockNumber) |
| 64 | + |
| 65 | + if (!contractData) { |
| 66 | + results.failedFetches++ |
| 67 | + results.errors[contractAddress] = 'Failed to fetch contract data' |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + results.successfulFetches++ |
| 72 | + results.contracts[contractAddress] = contractData |
| 73 | + console.log(`Contract ${contractAddress} fetched (${contractData.fetchTimeMs}ms).`) |
| 74 | + } catch (error) { |
| 75 | + results.failedFetches++ |
| 76 | + results.errors[contractAddress] = error.message |
| 77 | + console.log(`Contract ${contractAddress} fetch failed. Error: ${error.message}`) |
| 78 | + } |
| 79 | + |
| 80 | + results.processedContracts++ |
| 81 | + } |
| 82 | + |
| 83 | + // Save current page progress if --save flag is provided |
| 84 | + if (save) { |
| 85 | + fs.writeFileSync(resultFilePath, JSON.stringify(results, null, 2)) |
| 86 | + console.log(`Page ${pageCount} completed. Progress saved to ${resultFileName}`) |
| 87 | + } else { |
| 88 | + console.log(`Page ${pageCount} completed.`) |
| 89 | + } |
| 90 | + |
| 91 | + if (limit > 0 && totalProcessed >= limit) { |
| 92 | + break |
| 93 | + } |
| 94 | + |
| 95 | + cursor = next |
| 96 | + } while (cursor) |
| 97 | + |
| 98 | + return results |
| 99 | + } catch (error) { |
| 100 | + console.error('Error during contract data fetch:', error.message) |
| 101 | + throw error |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +async function main () { |
| 106 | + const validOptions = { |
| 107 | + '--pageSize': { name: 'pageSize', type: 'number', default: 50, min: 1 }, |
| 108 | + '--limit': { name: 'limit', type: 'number', default: 0, min: 0 }, |
| 109 | + '--save': { name: 'save', type: 'boolean', default: false } |
| 110 | + } |
| 111 | + |
| 112 | + let options |
| 113 | + try { |
| 114 | + options = parseArguments(validOptions) |
| 115 | + } catch (error) { |
| 116 | + console.log(`Error: ${error.message}`) |
| 117 | + printUsageAndExit() |
| 118 | + } |
| 119 | + |
| 120 | + try { |
| 121 | + console.log(`${toolName}`) |
| 122 | + console.log(`Page size: ${options.pageSize}`) |
| 123 | + if (options.limit > 0) { |
| 124 | + console.log(`Limit: ${options.limit} contracts`) |
| 125 | + } |
| 126 | + console.log('Depending on the page size and limit, this tool could take a while to complete.') |
| 127 | + if (options.save) { |
| 128 | + console.log(`Results will be saved to: ${resultFileName}`) |
| 129 | + } |
| 130 | + console.log('Starting...') |
| 131 | + |
| 132 | + const result = await getContractsData({ pageSize: options.pageSize, limit: options.limit, save: options.save }) |
| 133 | + |
| 134 | + console.log(`\n=== RESULTS ===`) |
| 135 | + console.log(`Total: ${result.totalContracts}`) |
| 136 | + console.log(`Processed: ${result.processedContracts}`) |
| 137 | + console.log(`Successful: ${result.successfulFetches}`) |
| 138 | + console.log(`Failed: ${result.failedFetches}`) |
| 139 | + |
| 140 | + if (Object.keys(result.errors).length > 0) { |
| 141 | + console.log(`\nErrors: ${Object.keys(result.errors).length}`) |
| 142 | + } |
| 143 | + |
| 144 | + if (options.save) { |
| 145 | + console.log(`Final results saved to: ${resultFileName}`) |
| 146 | + } |
| 147 | + |
| 148 | + process.exit(0) |
| 149 | + } catch (error) { |
| 150 | + console.log(`[${toolName}]: Error fetching contract data`) |
| 151 | + console.error(error) |
| 152 | + process.exit(1) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +main() |
0 commit comments