-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_contract_code.js
More file actions
183 lines (153 loc) · 5.42 KB
/
fetch_contract_code.js
File metadata and controls
183 lines (153 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Fetch ERC20 contract bytecode from Ethereum RPC
* Reads addresses from erc20_metadata_failed.json and saves contract code to erc20_for_local_deploy.json
*/
const fs = require('fs');
const path = require('path');
// Configuration
const RPC_URL = process.env.ETHEREUM_RPC_URL || 'https://ethereum.publicnode.com';
const INPUT_FILE = path.join(__dirname, 'erc20_metadata_failed.json');
const OUTPUT_FILE = path.join(__dirname, 'erc20_for_local_deploy.json');
const PROGRESS_FILE = path.join(__dirname, 'erc20_fetch_progress.json');
const BATCH_SIZE = parseInt(process.env.BATCH_SIZE) || 10;
const BATCH_DELAY = parseInt(process.env.BATCH_DELAY) || 3000;
/**
* Make batch RPC calls
* @param {Array} addresses - Array of addresses
* @param {string} rpcUrl - RPC endpoint URL
* @returns {Promise<Array>} - Array of results
*/
async function fetchCodeBatch(addresses, rpcUrl) {
const batch = addresses.map((addr, idx) => ({
jsonrpc: '2.0',
method: 'eth_getCode',
params: [addr, 'latest'],
id: idx + 1,
}));
const response = await fetch(rpcUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(batch),
});
if (response.status === 429) {
throw new Error('Rate limited');
}
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (!Array.isArray(data)) {
throw new Error(`Unexpected response: ${JSON.stringify(data)}`);
}
return data;
}
/**
* Main function
*/
async function main() {
console.log('Starting ERC20 contract code fetcher...');
console.log(`RPC URL: ${RPC_URL}`);
console.log(`Input file: ${INPUT_FILE}`);
console.log(`Output file: ${OUTPUT_FILE}`);
console.log(`Batch size: ${BATCH_SIZE}`);
console.log(`Batch delay: ${BATCH_DELAY}ms`);
// Read input file
if (!fs.existsSync(INPUT_FILE)) {
console.error(`Input file not found: ${INPUT_FILE}`);
process.exit(1);
}
const fileContent = fs.readFileSync(INPUT_FILE, 'utf-8');
const lines = fileContent.trim().split('\n').filter(line => line.trim());
const contracts = lines.map(line => JSON.parse(line));
console.log(`Loaded ${contracts.length} contracts from input file`);
// Load progress if exists
let progress = [];
if (fs.existsSync(PROGRESS_FILE)) {
try {
progress = JSON.parse(fs.readFileSync(PROGRESS_FILE, 'utf-8'));
console.log(`Resuming from progress: ${progress.length} contracts already fetched`);
} catch (e) {
console.log('Could not load progress, starting fresh');
}
}
// Build result map
const results = new Map();
progress.forEach(p => results.set(p.address, p));
// Process in batches
let batchNum = 0;
const totalBatches = Math.ceil(contracts.length / BATCH_SIZE);
for (let i = 0; i < contracts.length; i += BATCH_SIZE) {
batchNum++;
const batch = contracts.slice(i, i + BATCH_SIZE);
const addresses = batch.map(c => c.address);
console.log(`Processing batch ${batchNum}/${totalBatches} (${addresses.length} addresses)...`);
let success = false;
let retries = 0;
const maxRetries = 5;
while (!success && retries < maxRetries) {
try {
const responses = await fetchCodeBatch(addresses, RPC_URL);
batch.forEach((contract, idx) => {
const result = responses[idx];
if (result && result.result) {
results.set(contract.address, {
address: contract.address,
number: contract.number,
code: result.result,
});
}
});
success = true;
} catch (error) {
retries++;
console.log(`Batch ${batchNum} failed (attempt ${retries}/${maxRetries}): ${error.message}`);
if (retries < maxRetries) {
await new Promise(resolve => setTimeout(resolve, BATCH_DELAY * retries));
}
}
}
if (!success) {
// Mark failed addresses
batch.forEach(contract => {
if (!results.has(contract.address)) {
results.set(contract.address, {
address: contract.address,
number: contract.number,
code: null,
error: 'Failed after retries',
});
}
});
}
// Save progress after each batch
const progressArray = Array.from(results.values());
fs.writeFileSync(PROGRESS_FILE, JSON.stringify(progressArray, null, 2));
// Delay between batches
if (i + BATCH_SIZE < contracts.length) {
await new Promise(resolve => setTimeout(resolve, BATCH_DELAY));
}
}
// Final save
const finalResults = Array.from(results.values());
fs.writeFileSync(OUTPUT_FILE, JSON.stringify(finalResults, null, 2));
console.log(`\nSaved ${finalResults.length} results to ${OUTPUT_FILE}`);
// Summary
const successCount = finalResults.filter(r => r.code && r.code !== '0x').length;
const emptyCount = finalResults.filter(r => r.code === '0x').length;
const errorCount = finalResults.filter(r => r.error).length;
console.log('\nSummary:');
console.log(` - Contracts with code: ${successCount}`);
console.log(` - Empty contracts (0x): ${emptyCount}`);
console.log(` - Errors: ${errorCount}`);
// Clean up progress file
if (fs.existsSync(PROGRESS_FILE)) {
fs.unlinkSync(PROGRESS_FILE);
console.log('Cleaned up progress file');
}
}
main().catch((error) => {
console.error('Fatal error:', error);
process.exit(1);
});