-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_erc20_metadata.js
More file actions
269 lines (230 loc) · 8.89 KB
/
fetch_erc20_metadata.js
File metadata and controls
269 lines (230 loc) · 8.89 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const { ethers } = require('ethers');
const fs = require('fs');
const path = require('path');
// ERC20 ABI for the functions we need
const ERC20_ABI = [
'function name() view returns (string)',
'function symbol() view returns (string)',
'function decimals() view returns (uint8)'
];
// Multicall3 contract ABI with tryAggregate for handling individual failures
const MULTICALL3_ABI = [
'function tryAggregate(bool requireSuccess, (address target, bytes callData)[]) view returns ((bool success, bytes returnData)[])'
];
// Standard Multicall3 address on Ethereum mainnet
const MULTICALL3_ADDRESS = '0xcA11bde05977b3631167028862bE2a173976CA11';
// Configuration
const BATCH_SIZE = 10; // Smaller batch size for reliability
const RETRY_DELAY = 100; // Delay between batches
// Fallback RPC URLs (will try in order)
const RPC_URLS = [
process.env.RPC_URL || 'https://ethereum.publicnode.com',
'https://cloudflare-eth.com',
'https://eth.llamarpc.com'
];
const INPUT_FILE = path.join(__dirname, 'erc20_with_blocks.json');
const OUTPUT_FILE = path.join(__dirname, 'erc20_metadata.json');
const FAILED_FILE = path.join(__dirname, 'erc20_metadata_failed.json');
// Create interface for ERC20
const erc20Interface = new ethers.Interface(ERC20_ABI);
// Load already processed addresses from output file
function loadProcessedAddresses() {
const processed = new Set();
if (fs.existsSync(OUTPUT_FILE)) {
const content = fs.readFileSync(OUTPUT_FILE, 'utf-8');
const lines = content.trim().split('\n').filter(l => l);
for (const line of lines) {
try {
const entry = JSON.parse(line);
processed.add(entry.address.toLowerCase());
} catch (e) { /* ignore */ }
}
}
return processed;
}
// Append a batch of results to the output file
function appendResults(metadataList) {
const jsonlContent = metadataList.map(m => JSON.stringify(m)).join('\n') + '\n';
fs.appendFileSync(OUTPUT_FILE, jsonlContent);
}
// Append failed batch addresses to the failed file for later retry
function appendFailed(addresses, contractsMap) {
const failedData = addresses.map(addr => ({
address: addr,
number: contractsMap[addr.toLowerCase()]
}));
const jsonlContent = failedData.map(m => JSON.stringify(m)).join('\n') + '\n';
fs.appendFileSync(FAILED_FILE, jsonlContent);
}
async function fetchMetadataWithMulticall3(provider, addresses, contractsMap) {
const multicall = new ethers.Contract(MULTICALL3_ADDRESS, MULTICALL3_ABI, provider);
// Prepare calls for name, symbol, decimals
const calls = [];
for (const address of addresses) {
calls.push({
target: address,
callData: erc20Interface.encodeFunctionData('name', [])
});
}
for (const address of addresses) {
calls.push({
target: address,
callData: erc20Interface.encodeFunctionData('symbol', [])
});
}
for (const address of addresses) {
calls.push({
target: address,
callData: erc20Interface.encodeFunctionData('decimals', [])
});
}
try {
// Use tryAggregate which handles individual call failures (false = don't require all to succeed)
const results = await multicall.tryAggregate.staticCall(false, calls);
const metadataList = [];
for (let i = 0; i < addresses.length; i++) {
const address = addresses[i];
const baseIdx = i;
const nameIdx = baseIdx;
const symbolIdx = baseIdx + addresses.length;
const decimalsIdx = baseIdx + addresses.length * 2;
// Safely decode each result - handle failures gracefully
let name = null, symbol = null, decimals = null;
// name
try {
const nameResult = results[nameIdx];
if (nameResult && nameResult.success && nameResult.returnData) {
const decoded = erc20Interface.decodeFunctionResult('name', nameResult.returnData)[0];
if (decoded && decoded.length > 0) name = decoded;
}
} catch (e) { /* name call failed, keep null */ }
// symbol
try {
const symbolResult = results[symbolIdx];
if (symbolResult && symbolResult.success && symbolResult.returnData) {
const decoded = erc20Interface.decodeFunctionResult('symbol', symbolResult.returnData)[0];
if (decoded && decoded.length > 0) symbol = decoded;
}
} catch (e) { /* symbol call failed, keep null */ }
// decimals
try {
const decimalsResult = results[decimalsIdx];
if (decimalsResult && decimalsResult.success && decimalsResult.returnData) {
decimals = erc20Interface.decodeFunctionResult('decimals', decimalsResult.returnData)[0];
}
} catch (e) { /* decimals call failed, keep null */ }
metadataList.push({
address: address,
number: contractsMap[address.toLowerCase()],
name,
symbol,
decimals: decimals !== null ? Number(decimals) : null
});
}
return metadataList;
} catch (error) {
console.error('Multicall3 tryAggregate error:', error.message);
// Return no values for all addresses so they get rechecked on next run
return [];
}
}
async function main() {
console.log('Starting ERC20 metadata fetch...');
console.log(`Input file: ${INPUT_FILE}`);
console.log(`Output file: ${OUTPUT_FILE}`);
console.log(`Failed file: ${FAILED_FILE}`);
// Load already processed addresses
const processedAddresses = loadProcessedAddresses();
console.log(`Already processed: ${processedAddresses.size} addresses`);
// 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');
// Parse addresses from JSONL and create contracts map
const contracts = lines.map(line => {
try {
return JSON.parse(line);
} catch (e) {
console.error('Error parsing line:', line);
return null;
}
}).filter(c => c !== null);
// Create address -> number mapping (keep first occurrence)
const contractsMap = {};
for (const contract of contracts) {
const addr = contract.address.toLowerCase();
if (!contractsMap[addr]) {
contractsMap[addr] = contract.number;
}
}
// Filter out already processed addresses
const uniqueAddresses = Object.keys(contractsMap).filter(addr => !processedAddresses.has(addr));
console.log(`Total contracts: ${contracts.length}`);
console.log(`Unique addresses: ${uniqueAddresses.length}`);
console.log(`Remaining to process: ${uniqueAddresses.length}`);
if (uniqueAddresses.length === 0) {
console.log('All contracts already processed!');
return;
}
// Create provider with fallback URLs
let provider;
let connectedUrl = null;
for (const url of RPC_URLS) {
try {
const testProvider = new ethers.JsonRpcProvider(url);
const network = await testProvider.getNetwork();
provider = testProvider;
connectedUrl = url;
console.log(`Connected to network: ${network.name} (chainId: ${network.chainId}) using ${url}`);
break;
} catch (error) {
console.log(`Failed to connect to ${url}: ${error.message}`);
}
}
if (!provider) {
console.error('Failed to connect to any RPC endpoint');
process.exit(1);
}
// Process in batches with retry logic
let processed = 0;
const maxRetries = 3;
const retryDelay = 1000;
const totalToProcess = uniqueAddresses.length;
for (let i = 0; i < uniqueAddresses.length; i += BATCH_SIZE) {
const batch = uniqueAddresses.slice(i, i + BATCH_SIZE);
const batchNum = Math.floor(i / BATCH_SIZE) + 1;
const totalBatches = Math.ceil(totalToProcess / BATCH_SIZE);
console.log(`Processing batch ${batchNum}/${totalBatches} (${batch.length} contracts)...`);
let success = false;
for (let retry = 0; retry < maxRetries && !success; retry++) {
try {
const batchMetadata = await fetchMetadataWithMulticall3(provider, batch, contractsMap);
if (batchMetadata.length > 0) {
appendResults(batchMetadata);
} else {
appendFailed(batch, contractsMap);
}
processed += batch.length;
success = true;
console.log(`Progress: ${processed}/${totalToProcess} contracts processed`);
} catch (error) {
console.error(`Retry ${retry + 1}/${maxRetries} for batch ${batchNum}:`, error.message);
if (retry < maxRetries - 1) {
const backoffDelay = retryDelay * Math.pow(2, retry);
console.log(` Waiting ${backoffDelay}ms before retry...`);
await new Promise(resolve => setTimeout(resolve, backoffDelay));
}
}
}
// Add delay between batches
if (i + BATCH_SIZE < uniqueAddresses.length) {
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
}
}
console.log(`\nCompleted! Total metadata fetched: ${processed}`);
console.log(`Output written to: ${OUTPUT_FILE}`);
}
main().catch(console.error);