-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
374 lines (315 loc) · 12.7 KB
/
Copy pathindex.html
File metadata and controls
374 lines (315 loc) · 12.7 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Smart Contract Downloader - Etherscan API V2</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.container {
background: white;
border-radius: 20px;
padding: 40px;
max-width: 600px;
width: 100%;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
h1 {
color: #333;
margin-bottom: 10px;
font-size: 28px;
}
.subtitle {
color: #666;
margin-bottom: 30px;
font-size: 14px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
color: #333;
font-weight: 600;
font-size: 14px;
}
input, select {
width: 100%;
padding: 12px 16px;
border: 2px solid #e0e0e0;
border-radius: 8px;
font-size: 14px;
transition: border-color 0.3s;
}
input:focus, select:focus {
outline: none;
border-color: #667eea;
}
.custom-chain {
display: none;
margin-top: 10px;
}
.custom-chain.show {
display: block;
}
button {
width: 100%;
padding: 14px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 10px 20px rgba(102, 126, 234, 0.4);
}
button:active {
transform: translateY(0);
}
button:disabled {
background: #ccc;
cursor: not-allowed;
transform: none;
}
.status {
margin-top: 20px;
padding: 12px;
border-radius: 8px;
font-size: 14px;
display: none;
}
.status.show {
display: block;
}
.status.loading {
background: #fff3cd;
color: #856404;
border: 1px solid #ffeaa7;
}
.status.success {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status.error {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.info {
background: #e7f3ff;
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
font-size: 13px;
color: #004085;
}
.info a {
color: #667eea;
text-decoration: none;
font-weight: 600;
}
.info a:hover {
text-decoration: underline;
}
.footer {
text-align: center;
margin-top: 30px;
color: #666;
font-size: 12px;
}
.footer a {
color: #667eea;
text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<h1>📦 Smart Contract Downloader</h1>
<p class="subtitle">Download verified contracts from 50+ EVM chains using Etherscan API V2</p>
<div class="info">
ℹ️ Get your free API key at <a href="https://etherscan.io/myapikey" target="_blank">Etherscan.io</a>. One key works for all chains!
</div>
<form id="downloadForm">
<div class="form-group">
<label for="apiKey">Etherscan API Key</label>
<input type="text" id="apiKey" placeholder="Your Etherscan API key" required>
</div>
<div class="form-group">
<label for="network">Network</label>
<select id="network" required>
<option value="1">Ethereum Mainnet</option>
<option value="56">BNB Smart Chain</option>
<option value="137">Polygon</option>
<option value="8453">Base</option>
<option value="42161">Arbitrum One</option>
<option value="10">Optimism</option>
<option value="43114">Avalanche C-Chain</option>
<option value="59144">Linea</option>
<option value="81457">Blast</option>
<option value="custom">🔧 Custom Chain ID...</option>
</select>
<div id="customChainInput" class="custom-chain">
<input type="number" id="customChainId" placeholder="Enter custom chain ID (e.g., 534352 for Scroll)" min="1">
</div>
</div>
<div class="form-group">
<label for="contractAddress">Contract Address</label>
<input type="text" id="contractAddress" placeholder="0x..." pattern="^0x[a-fA-F0-9]{40}$" required>
</div>
<button type="submit" id="downloadBtn">Download Contract</button>
</form>
<div id="status" class="status"></div>
<div class="footer">
Made with 💜 for the community | <a href="https://github.com/mamolas/contract-downloader-simple" target="_blank">View on GitHub</a>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<script>
const form = document.getElementById('downloadForm');
const networkSelect = document.getElementById('network');
const customChainInput = document.getElementById('customChainInput');
const customChainId = document.getElementById('customChainId');
const statusDiv = document.getElementById('status');
const downloadBtn = document.getElementById('downloadBtn');
// Show/hide custom chain input
networkSelect.addEventListener('change', function() {
if (this.value === 'custom') {
customChainInput.classList.add('show');
customChainId.required = true;
} else {
customChainInput.classList.remove('show');
customChainId.required = false;
}
});
// Show status message
function showStatus(message, type) {
statusDiv.textContent = message;
statusDiv.className = `status show ${type}`;
}
// Hide status message
function hideStatus() {
statusDiv.className = 'status';
}
// Download contract
async function downloadContract(chainId, contractAddress, apiKey) {
showStatus('🔍 Fetching contract from blockchain...', 'loading');
downloadBtn.disabled = true;
try {
const url = `https://api.etherscan.io/v2/api?chainid=${chainId}&module=contract&action=getsourcecode&address=${contractAddress}&apikey=${apiKey}`;
const response = await fetch(url);
const data = await response.json();
if (data.status !== '1' || !data.result || !data.result[0]) {
throw new Error(data.message || 'Failed to fetch contract');
}
const contract = data.result[0];
if (!contract.SourceCode || contract.SourceCode === '') {
throw new Error('Contract is not verified on this network');
}
showStatus('📦 Creating ZIP file...', 'loading');
// Create ZIP file
const zip = new JSZip();
// Parse source code
let sourceCode = contract.SourceCode;
// Check if it's a multi-file contract (starts with {{ or {)
if (sourceCode.startsWith('{{')) {
sourceCode = sourceCode.slice(1, -1);
}
try {
// Try to parse as JSON (multi-file contract)
const sources = JSON.parse(sourceCode);
if (sources.sources) {
// Solidity Standard JSON format
Object.keys(sources.sources).forEach(filePath => {
zip.file(filePath, sources.sources[filePath].content);
});
} else {
// Alternative format
Object.keys(sources).forEach(filePath => {
zip.file(filePath, sources[filePath].content || sources[filePath]);
});
}
} catch (e) {
// Single file contract
const fileName = `${contract.ContractName}.sol`;
zip.file(fileName, sourceCode);
}
// Add metadata
const metadata = {
contractName: contract.ContractName,
contractAddress: contractAddress,
chainId: chainId,
compiler: contract.CompilerVersion,
optimization: contract.OptimizationUsed === '1',
runs: contract.Runs,
constructorArguments: contract.ConstructorArguments,
evmVersion: contract.EVMVersion,
library: contract.Library,
licenseType: contract.LicenseType,
proxy: contract.Proxy,
implementation: contract.Implementation,
swarmSource: contract.SwarmSource,
downloadedAt: new Date().toISOString(),
};
zip.file('metadata.json', JSON.stringify(metadata, null, 2));
// Generate ZIP
const blob = await zip.generateAsync({ type: 'blob' });
// Download
const downloadUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = downloadUrl;
a.download = `${contract.ContractName}_${contractAddress.slice(0, 10)}_chain${chainId}.zip`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(downloadUrl);
showStatus(`✅ Successfully downloaded ${contract.ContractName}!`, 'success');
} catch (error) {
showStatus(`❌ Error: ${error.message}`, 'error');
} finally {
downloadBtn.disabled = false;
}
}
// Form submission
form.addEventListener('submit', function(e) {
e.preventDefault();
const apiKey = document.getElementById('apiKey').value.trim();
const contractAddress = document.getElementById('contractAddress').value.trim();
let chainId = networkSelect.value;
if (chainId === 'custom') {
chainId = customChainId.value.trim();
if (!chainId) {
showStatus('❌ Please enter a custom chain ID', 'error');
return;
}
}
if (!contractAddress.match(/^0x[a-fA-F0-9]{40}$/)) {
showStatus('❌ Invalid contract address format', 'error');
return;
}
hideStatus();
downloadContract(chainId, contractAddress, apiKey);
});
</script>
</body>
</html>