@rocketh/verifier 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierignore +3 -0
- package/.prettierrc +7 -0
- package/CHANGELOG.md +9 -0
- package/README.md +1 -0
- package/dist/cli.cjs +123 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.mjs +120 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/index.cjs +671 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.mjs +669 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +45 -0
- package/src/blockscout.ts +157 -0
- package/src/cli.ts +65 -0
- package/src/etherscan.ts +431 -0
- package/src/index.ts +82 -0
- package/src/metadata.ts +30 -0
- package/src/sourcify.ts +109 -0
- package/src/utils/match-all.ts +72 -0
- package/tsconfig.json +15 -0
package/src/etherscan.ts
ADDED
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import qs from 'qs';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import {matchAll} from './utils/match-all';
|
|
6
|
+
import {Environment, UnknownDeployments} from 'rocketh';
|
|
7
|
+
import {EtherscanOptions} from '.';
|
|
8
|
+
|
|
9
|
+
const defaultEndpoints: {[chainId: string]: string} = {
|
|
10
|
+
'1': 'https://api.etherscan.io/api',
|
|
11
|
+
'3': 'https://api-ropsten.etherscan.io/api',
|
|
12
|
+
'4': 'https://api-rinkeby.etherscan.io/api',
|
|
13
|
+
'5': 'https://api-goerli.etherscan.io/api',
|
|
14
|
+
'10': 'https://api-optimistic.etherscan.io/api',
|
|
15
|
+
'42': 'https://api-kovan.etherscan.io/api',
|
|
16
|
+
'97': 'https://api-testnet.bscscan.com/api',
|
|
17
|
+
'56': 'https://api.bscscan.com/api',
|
|
18
|
+
'69': 'https://api-kovan-optimistic.etherscan.io/api',
|
|
19
|
+
'70': 'https://api.hooscan.com/api',
|
|
20
|
+
'77': 'https://blockscout.com/poa/sokol/api',
|
|
21
|
+
'128': 'https://api.hecoinfo.com/api',
|
|
22
|
+
'137': 'https://api.polygonscan.com/api',
|
|
23
|
+
'250': 'https://api.ftmscan.com/api',
|
|
24
|
+
'256': 'https://api-testnet.hecoinfo.com/api',
|
|
25
|
+
'420': 'https://api-goerli-optimism.etherscan.io/api',
|
|
26
|
+
'588': 'https://stardust-explorer.metis.io/api',
|
|
27
|
+
'1088': 'https://andromeda-explorer.metis.io/api',
|
|
28
|
+
'1285': 'https://api-moonriver.moonscan.io/api',
|
|
29
|
+
'80001': 'https://api-testnet.polygonscan.com/api',
|
|
30
|
+
'4002': 'https://api-testnet.ftmscan.com/api',
|
|
31
|
+
'42161': 'https://api.arbiscan.io/api',
|
|
32
|
+
'421611': 'https://api-testnet.arbiscan.io/api',
|
|
33
|
+
'421613': 'https://api-goerli.arbiscan.io/api',
|
|
34
|
+
'43113': 'https://api-testnet.snowtrace.io/api',
|
|
35
|
+
'43114': 'https://api.snowtrace.io/api',
|
|
36
|
+
'11155111': 'https://api-sepolia.etherscan.io/api',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function log(...args: any[]) {
|
|
40
|
+
console.log(...args);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function logError(...args: any[]) {
|
|
44
|
+
console.log(chalk.red(...args));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function logInfo(...args: any[]) {
|
|
48
|
+
console.log(chalk.yellow(...args));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function logSuccess(...args: any[]) {
|
|
52
|
+
console.log(chalk.green(...args));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function sleep(ms: number) {
|
|
56
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function writeRequestIfRequested(
|
|
60
|
+
write: boolean,
|
|
61
|
+
networkName: string,
|
|
62
|
+
name: string,
|
|
63
|
+
request: string,
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
65
|
+
postData: any
|
|
66
|
+
) {
|
|
67
|
+
if (write) {
|
|
68
|
+
try {
|
|
69
|
+
fs.mkdirSync('etherscan_requests');
|
|
70
|
+
} catch (e) {}
|
|
71
|
+
const folder = `etherscan_requests/${networkName}`;
|
|
72
|
+
try {
|
|
73
|
+
fs.mkdirSync(folder);
|
|
74
|
+
} catch (e) {}
|
|
75
|
+
fs.writeFileSync(`${folder}/${name}.formdata`, request);
|
|
76
|
+
fs.writeFileSync(`${folder}/${name}.json`, JSON.stringify(postData));
|
|
77
|
+
fs.writeFileSync(`${folder}/${name}_multi-source.json`, postData.sourceCode);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function extractOneLicenseFromSourceFile(source: string): string | undefined {
|
|
82
|
+
const licenses = extractLicenseFromSources(source);
|
|
83
|
+
if (licenses.length === 0) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
return licenses[0]; // TODO error out on multiple SPDX ?
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function extractLicenseFromSources(metadata: string): string[] {
|
|
90
|
+
const regex = /\/\/\s*\t*SPDX-License-Identifier:\s*\t*(.*?)[\s\\]/g;
|
|
91
|
+
const matches = matchAll(metadata, regex).toArray();
|
|
92
|
+
const licensesFound: {[license: string]: boolean} = {};
|
|
93
|
+
const licenses = [];
|
|
94
|
+
if (matches) {
|
|
95
|
+
for (const match of matches) {
|
|
96
|
+
if (!licensesFound[match]) {
|
|
97
|
+
licensesFound[match] = true;
|
|
98
|
+
licenses.push(match);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return licenses;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function getLicenseType(license: string): undefined | number {
|
|
106
|
+
const licenseType = (() => {
|
|
107
|
+
if (license === 'None') {
|
|
108
|
+
return 1;
|
|
109
|
+
}
|
|
110
|
+
if (license === 'UNLICENSED') {
|
|
111
|
+
return 2;
|
|
112
|
+
}
|
|
113
|
+
if (license === 'MIT') {
|
|
114
|
+
return 3;
|
|
115
|
+
}
|
|
116
|
+
if (license === 'GPL-2.0') {
|
|
117
|
+
return 4;
|
|
118
|
+
}
|
|
119
|
+
if (license === 'GPL-3.0') {
|
|
120
|
+
return 5;
|
|
121
|
+
}
|
|
122
|
+
if (license === 'LGPL-2.1') {
|
|
123
|
+
return 6;
|
|
124
|
+
}
|
|
125
|
+
if (license === 'LGPL-3.0') {
|
|
126
|
+
return 7;
|
|
127
|
+
}
|
|
128
|
+
if (license === 'BSD-2-Clause') {
|
|
129
|
+
return 8;
|
|
130
|
+
}
|
|
131
|
+
if (license === 'BSD-3-Clause') {
|
|
132
|
+
return 9;
|
|
133
|
+
}
|
|
134
|
+
if (license === 'MPL-2.0') {
|
|
135
|
+
return 10;
|
|
136
|
+
}
|
|
137
|
+
if (license === 'OSL-3.0') {
|
|
138
|
+
return 11;
|
|
139
|
+
}
|
|
140
|
+
if (license === 'Apache-2.0') {
|
|
141
|
+
return 12;
|
|
142
|
+
}
|
|
143
|
+
if (license === 'AGPL-3.0') {
|
|
144
|
+
return 13;
|
|
145
|
+
}
|
|
146
|
+
if (license === 'BUSL-1.1') {
|
|
147
|
+
return 14;
|
|
148
|
+
}
|
|
149
|
+
})();
|
|
150
|
+
return licenseType;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export async function submitSourcesToEtherscan(
|
|
154
|
+
env: {
|
|
155
|
+
deployments: UnknownDeployments;
|
|
156
|
+
networkName: string;
|
|
157
|
+
chainId: string;
|
|
158
|
+
deploymentNames?: string[];
|
|
159
|
+
minInterval?: number;
|
|
160
|
+
logErrorOnFailure?: boolean;
|
|
161
|
+
},
|
|
162
|
+
config?: EtherscanOptions
|
|
163
|
+
): Promise<void> {
|
|
164
|
+
config = config || {type: 'etherscan'};
|
|
165
|
+
const licenseOption = config.license;
|
|
166
|
+
const forceLicense = config.forceLicense;
|
|
167
|
+
const etherscanApiKey = config.apiKey;
|
|
168
|
+
const all = env.deployments;
|
|
169
|
+
const networkName = env.networkName;
|
|
170
|
+
let endpoint = config.endpoint;
|
|
171
|
+
if (!endpoint) {
|
|
172
|
+
endpoint = defaultEndpoints[env.chainId];
|
|
173
|
+
if (!endpoint) {
|
|
174
|
+
return logError(`Network with chainId: ${env.chainId} not supported. Please specify the endpoint manually.`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function submit(name: string) {
|
|
179
|
+
const deployment = all[name];
|
|
180
|
+
const {address, metadata: metadataString} = deployment;
|
|
181
|
+
const abiResponse = await fetch(
|
|
182
|
+
`${endpoint}?module=contract&action=getabi&address=${address}&apikey=${etherscanApiKey}`
|
|
183
|
+
);
|
|
184
|
+
const json = await abiResponse.json();
|
|
185
|
+
let contractABI;
|
|
186
|
+
if (json.status !== '0') {
|
|
187
|
+
try {
|
|
188
|
+
contractABI = JSON.parse(json.result);
|
|
189
|
+
} catch (e) {
|
|
190
|
+
logError(e);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (contractABI && contractABI !== '') {
|
|
195
|
+
log(`already verified: ${name} (${address}), skipping.`);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (!metadataString) {
|
|
200
|
+
logError(`Contract ${name} was deployed without saving metadata. Cannot submit to etherscan, skipping.`);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const metadata = JSON.parse(metadataString);
|
|
204
|
+
const compilationTarget = metadata.settings?.compilationTarget;
|
|
205
|
+
|
|
206
|
+
let contractFilepath;
|
|
207
|
+
let contractName;
|
|
208
|
+
if (compilationTarget) {
|
|
209
|
+
contractFilepath = Object.keys(compilationTarget)[0];
|
|
210
|
+
contractName = compilationTarget[contractFilepath];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (!contractFilepath || !contractName) {
|
|
214
|
+
return logError(
|
|
215
|
+
`Failed to extract contract fully qualified name from metadata.settings.compilationTarget for ${name}. Skipping.`
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const contractNamePath = `${contractFilepath}:${contractName}`;
|
|
220
|
+
|
|
221
|
+
const contractSourceFile = metadata.sources[contractFilepath].content;
|
|
222
|
+
const sourceLicenseType = extractOneLicenseFromSourceFile(contractSourceFile);
|
|
223
|
+
|
|
224
|
+
let license = licenseOption;
|
|
225
|
+
if (!sourceLicenseType) {
|
|
226
|
+
if (!license) {
|
|
227
|
+
return logError(
|
|
228
|
+
`no license speccified in the source code for ${name} (${contractNamePath}), Please use option --license <SPDX>`
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
} else {
|
|
232
|
+
if (license && license !== sourceLicenseType) {
|
|
233
|
+
if (!forceLicense) {
|
|
234
|
+
return logError(
|
|
235
|
+
`mismatch for --license option (${licenseOption}) and the one specified in the source code for ${name}.\nLicenses found in source : ${sourceLicenseType}\nYou can use option --force-license to force option --license`
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
} else {
|
|
239
|
+
license = sourceLicenseType;
|
|
240
|
+
if (!getLicenseType(license)) {
|
|
241
|
+
return logError(
|
|
242
|
+
`license :"${license}" found in source code for ${name} (${contractNamePath}) but this license is not supported by etherscan, list of supported license can be found here : https://etherscan.io/contract-license-types . This tool expect the SPDX id, except for "None" and "UNLICENSED"`
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const licenseType = getLicenseType(license);
|
|
249
|
+
|
|
250
|
+
if (!licenseType) {
|
|
251
|
+
return logError(
|
|
252
|
+
`license :"${license}" not supported by etherscan, list of supported license can be found here : https://etherscan.io/contract-license-types . This tool expect the SPDX id, except for "None" and "UNLICENSED"`
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
let solcInput: {
|
|
257
|
+
language: string;
|
|
258
|
+
settings: any;
|
|
259
|
+
sources: Record<string, {content: string}>;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const settings = {...metadata.settings};
|
|
263
|
+
delete settings.compilationTarget;
|
|
264
|
+
solcInput = {
|
|
265
|
+
language: metadata.language,
|
|
266
|
+
settings,
|
|
267
|
+
sources: {},
|
|
268
|
+
};
|
|
269
|
+
for (const sourcePath of Object.keys(metadata.sources)) {
|
|
270
|
+
const source = metadata.sources[sourcePath];
|
|
271
|
+
// only content as this fails otherwise
|
|
272
|
+
solcInput.sources[sourcePath] = {
|
|
273
|
+
content: source.content,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Adding Libraries ....
|
|
278
|
+
if (deployment.libraries) {
|
|
279
|
+
const settings = solcInput.settings;
|
|
280
|
+
settings.libraries = settings.libraries || {};
|
|
281
|
+
for (const libraryName of Object.keys(deployment.libraries)) {
|
|
282
|
+
if (!settings.libraries[contractNamePath]) {
|
|
283
|
+
settings.libraries[contractNamePath] = {};
|
|
284
|
+
}
|
|
285
|
+
settings.libraries[contractNamePath][libraryName] = deployment.libraries[libraryName];
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
const solcInputString = JSON.stringify(solcInput);
|
|
289
|
+
|
|
290
|
+
logInfo(`verifying ${name} (${address}) ...`);
|
|
291
|
+
|
|
292
|
+
let constructorArguments: string | undefined;
|
|
293
|
+
if (deployment.argsData) {
|
|
294
|
+
constructorArguments = deployment.argsData.slice(2);
|
|
295
|
+
} else {
|
|
296
|
+
// TODO ?
|
|
297
|
+
// logInfo(`no argsData found, falling back on args (hardhat-deploy v1)`);
|
|
298
|
+
// if ((deployment as any).args) {
|
|
299
|
+
// const constructorABI: {inputs: any[]} = deployment.abi.find(
|
|
300
|
+
// (v) => v.type === 'constructor'
|
|
301
|
+
// );
|
|
302
|
+
// if (constructorABI) {
|
|
303
|
+
// constructorArguements = encode
|
|
304
|
+
// .encode(constructor.inputs, deployment.args)
|
|
305
|
+
// .slice(2);
|
|
306
|
+
// }
|
|
307
|
+
// } else {
|
|
308
|
+
// logInfo(`no args found, assuming empty constructor...`);
|
|
309
|
+
// }
|
|
310
|
+
logInfo(`no args found, assuming empty constructor...`);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const postData: {
|
|
314
|
+
[fieldName: string]: string | number | void | undefined; // TODO type
|
|
315
|
+
} = {
|
|
316
|
+
apikey: etherscanApiKey,
|
|
317
|
+
module: 'contract',
|
|
318
|
+
action: 'verifysourcecode',
|
|
319
|
+
contractaddress: address,
|
|
320
|
+
sourceCode: solcInputString,
|
|
321
|
+
codeformat: 'solidity-standard-json-input',
|
|
322
|
+
contractname: contractNamePath,
|
|
323
|
+
compilerversion: `v${metadata.compiler.version}`, // see http://etherscan.io/solcversions for list of support versions
|
|
324
|
+
constructorArguements: constructorArguments, // note the spelling mistake by etherscan
|
|
325
|
+
licenseType,
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const formDataAsString = qs.stringify(postData);
|
|
329
|
+
const data = new URLSearchParams();
|
|
330
|
+
for (const entry of Object.entries(postData)) {
|
|
331
|
+
if (entry[1]) {
|
|
332
|
+
if (typeof entry[1] === 'number') {
|
|
333
|
+
data.append(entry[0], entry[1].toString());
|
|
334
|
+
} else {
|
|
335
|
+
data.append(entry[0], entry[1]);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
const submissionResponse = await fetch(`${endpoint}`, {
|
|
340
|
+
method: 'POST',
|
|
341
|
+
headers: {'content-type': 'application/x-www-form-urlencoded'},
|
|
342
|
+
body: data,
|
|
343
|
+
});
|
|
344
|
+
const submissionJson = await submissionResponse.json();
|
|
345
|
+
|
|
346
|
+
let guid: string;
|
|
347
|
+
if (submissionJson.status === '1') {
|
|
348
|
+
guid = submissionJson.result;
|
|
349
|
+
} else {
|
|
350
|
+
logError(
|
|
351
|
+
`contract ${name} failed to submit : "${submissionJson.message}" : "${submissionJson.result}"`,
|
|
352
|
+
submissionJson
|
|
353
|
+
);
|
|
354
|
+
writeRequestIfRequested(env?.logErrorOnFailure || false, networkName, name, formDataAsString, postData);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (!guid) {
|
|
358
|
+
logError(`contract submission for ${name} failed to return a guid`);
|
|
359
|
+
writeRequestIfRequested(env?.logErrorOnFailure || false, networkName, name, formDataAsString, postData);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async function checkStatus(): Promise<string | undefined> {
|
|
364
|
+
// TODO while loop and delay :
|
|
365
|
+
const statusResponse = await fetch(
|
|
366
|
+
`${endpoint}?apikey=${etherscanApiKey}&guid=${guid}&module=contract&action=checkverifystatus`
|
|
367
|
+
);
|
|
368
|
+
const json = await statusResponse.json();
|
|
369
|
+
|
|
370
|
+
// blockscout seems to return status == 1 in case of failure
|
|
371
|
+
// so we check string first
|
|
372
|
+
if (json.result === 'Pending in queue') {
|
|
373
|
+
return undefined;
|
|
374
|
+
}
|
|
375
|
+
if (json.result !== 'Fail - Unable to verify') {
|
|
376
|
+
if (json.status === '1') {
|
|
377
|
+
// console.log(statusData);
|
|
378
|
+
return 'success';
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
logError(`Failed to verify contract ${name}: ${json.message}, ${json.result}`);
|
|
382
|
+
|
|
383
|
+
logError(
|
|
384
|
+
JSON.stringify(
|
|
385
|
+
{
|
|
386
|
+
apikey: 'XXXXXX',
|
|
387
|
+
module: 'contract',
|
|
388
|
+
action: 'verifysourcecode',
|
|
389
|
+
contractaddress: address,
|
|
390
|
+
sourceCode: '...',
|
|
391
|
+
codeformat: 'solidity-standard-json-input',
|
|
392
|
+
contractname: contractNamePath,
|
|
393
|
+
compilerversion: `v${metadata.compiler.version}`, // see http://etherscan.io/solcversions for list of support versions
|
|
394
|
+
constructorArguements: constructorArguments, // note the spelling mistake by etherscan
|
|
395
|
+
licenseType,
|
|
396
|
+
},
|
|
397
|
+
null,
|
|
398
|
+
' '
|
|
399
|
+
)
|
|
400
|
+
);
|
|
401
|
+
// logError(JSON.stringify(postData, null, " "));
|
|
402
|
+
// logInfo(postData.sourceCode);
|
|
403
|
+
return 'failure';
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
logInfo('waiting for result...');
|
|
407
|
+
let result;
|
|
408
|
+
while (!result) {
|
|
409
|
+
await new Promise((resolve) => setTimeout(resolve, 10 * 1000));
|
|
410
|
+
result = await checkStatus();
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (result === 'success') {
|
|
414
|
+
logSuccess(` => contract ${name} is now verified`);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (result === 'failure') {
|
|
418
|
+
writeRequestIfRequested(env?.logErrorOnFailure || false, networkName, name, formDataAsString, postData);
|
|
419
|
+
logInfo('Etherscan failed to verify the source provided');
|
|
420
|
+
} else {
|
|
421
|
+
writeRequestIfRequested(env?.logErrorOnFailure || false, networkName, name, formDataAsString, postData);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
for (const name of env.deploymentNames ? env.deploymentNames : Object.keys(all)) {
|
|
426
|
+
await submit(name);
|
|
427
|
+
if (env.minInterval) {
|
|
428
|
+
await sleep(env.minInterval);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {ResolvedConfig, loadDeployments} from 'rocketh';
|
|
2
|
+
import {submitSourcesToEtherscan} from './etherscan';
|
|
3
|
+
import {submitSourcesToSourcify} from './sourcify';
|
|
4
|
+
import {submitSourcesToBlockscout} from './blockscout';
|
|
5
|
+
|
|
6
|
+
export type EtherscanOptions = {
|
|
7
|
+
type: 'etherscan';
|
|
8
|
+
endpoint?: string;
|
|
9
|
+
apiKey?: string;
|
|
10
|
+
license?: string;
|
|
11
|
+
forceLicense?: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type SourcifyOptions = {
|
|
15
|
+
type: 'sourcify';
|
|
16
|
+
endpoint?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type BlockscoutOptions = {
|
|
20
|
+
type: 'blockscout';
|
|
21
|
+
endpoint?: string;
|
|
22
|
+
// version?: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type VerificationOptions = {
|
|
26
|
+
verifier: EtherscanOptions | SourcifyOptions | BlockscoutOptions;
|
|
27
|
+
deploymentNames?: string[];
|
|
28
|
+
minInterval?: number;
|
|
29
|
+
logErrorOnFailure?: boolean;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export async function run(config: ResolvedConfig, options: VerificationOptions) {
|
|
33
|
+
const {deployments, chainId} = loadDeployments(config.deployments, config.network.name, false);
|
|
34
|
+
|
|
35
|
+
if (Object.keys(deployments).length === 0) {
|
|
36
|
+
console.log(`the network ${config.network.name} has zero deployments`);
|
|
37
|
+
process.exit();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!chainId) {
|
|
41
|
+
console.error(`the network ${config.network.name} has no chainId recorded`);
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (options.verifier.type === 'etherscan') {
|
|
46
|
+
await submitSourcesToEtherscan(
|
|
47
|
+
{
|
|
48
|
+
chainId,
|
|
49
|
+
deployments,
|
|
50
|
+
networkName: config.network.name,
|
|
51
|
+
deploymentNames: options.deploymentNames,
|
|
52
|
+
minInterval: options.minInterval,
|
|
53
|
+
logErrorOnFailure: options.logErrorOnFailure,
|
|
54
|
+
},
|
|
55
|
+
options.verifier
|
|
56
|
+
);
|
|
57
|
+
} else if (options.verifier.type === 'sourcify') {
|
|
58
|
+
await submitSourcesToSourcify(
|
|
59
|
+
{
|
|
60
|
+
chainId,
|
|
61
|
+
deployments,
|
|
62
|
+
networkName: config.network.name,
|
|
63
|
+
deploymentNames: options.deploymentNames,
|
|
64
|
+
minInterval: options.minInterval,
|
|
65
|
+
logErrorOnFailure: options.logErrorOnFailure,
|
|
66
|
+
},
|
|
67
|
+
options.verifier
|
|
68
|
+
);
|
|
69
|
+
} else if (options.verifier.type === 'blockscout') {
|
|
70
|
+
await submitSourcesToBlockscout(
|
|
71
|
+
{
|
|
72
|
+
chainId,
|
|
73
|
+
deployments,
|
|
74
|
+
networkName: config.network.name,
|
|
75
|
+
deploymentNames: options.deploymentNames,
|
|
76
|
+
minInterval: options.minInterval,
|
|
77
|
+
logErrorOnFailure: options.logErrorOnFailure,
|
|
78
|
+
},
|
|
79
|
+
options.verifier
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
package/src/metadata.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {ResolvedConfig, loadDeployments} from 'rocketh';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
export function exportMetadata(config: ResolvedConfig, {out}: {out: string}) {
|
|
6
|
+
const {deployments, chainId} = loadDeployments(config.deployments, config.network.name, false);
|
|
7
|
+
|
|
8
|
+
if (Object.keys(deployments).length === 0) {
|
|
9
|
+
console.log(`the network ${config.network.name} has zero deployments`);
|
|
10
|
+
process.exit();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (!chainId) {
|
|
14
|
+
console.error(`the network ${config.network.name} has no chainId recorded`);
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const folder = path.join(out, config.network.name);
|
|
19
|
+
fs.emptyDirSync(folder);
|
|
20
|
+
const deploymentNames = Object.keys(deployments);
|
|
21
|
+
for (const deploymentName of deploymentNames) {
|
|
22
|
+
const deployment = deployments[deploymentName];
|
|
23
|
+
if (deployment.metadata) {
|
|
24
|
+
fs.writeFileSync(
|
|
25
|
+
path.join(folder, `${deploymentName}_at_${deployment.address}.metadata.json`),
|
|
26
|
+
deployment.metadata
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/sourcify.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import {UnknownDeployments} from 'rocketh';
|
|
5
|
+
import {SourcifyOptions} from '.';
|
|
6
|
+
|
|
7
|
+
function sleep(ms: number) {
|
|
8
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function log(...args: any[]) {
|
|
12
|
+
console.log(...args);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function logError(...args: any[]) {
|
|
16
|
+
console.log(chalk.red(...args));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function logInfo(...args: any[]) {
|
|
20
|
+
console.log(chalk.yellow(...args));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function logSuccess(...args: any[]) {
|
|
24
|
+
console.log(chalk.green(...args));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function ensureTrailingSlash(s: string): string {
|
|
28
|
+
const lastChar = s.substr(-1);
|
|
29
|
+
if (lastChar != '/') {
|
|
30
|
+
s = s + '/';
|
|
31
|
+
}
|
|
32
|
+
return s;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// const defaultEndpoint = 'https://server.verificationstaging.shardlabs.io/';
|
|
36
|
+
const defaultEndpoint = 'https://sourcify.dev/server/';
|
|
37
|
+
|
|
38
|
+
export async function submitSourcesToSourcify(
|
|
39
|
+
env: {
|
|
40
|
+
deployments: UnknownDeployments;
|
|
41
|
+
networkName: string;
|
|
42
|
+
chainId: string;
|
|
43
|
+
deploymentNames?: string[];
|
|
44
|
+
minInterval?: number;
|
|
45
|
+
logErrorOnFailure?: boolean;
|
|
46
|
+
},
|
|
47
|
+
config?: SourcifyOptions
|
|
48
|
+
): Promise<void> {
|
|
49
|
+
config = config || {type: 'sourcify'};
|
|
50
|
+
const all = env.deployments;
|
|
51
|
+
const url = config.endpoint ? ensureTrailingSlash(config.endpoint) : defaultEndpoint;
|
|
52
|
+
|
|
53
|
+
async function submit(name: string) {
|
|
54
|
+
const deployment = all[name];
|
|
55
|
+
const {address, metadata: metadataString} = deployment;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const checkResponse = await fetch(
|
|
59
|
+
`${url}checkByAddresses?addresses=${address.toLowerCase()}&chainIds=${env.chainId}`
|
|
60
|
+
);
|
|
61
|
+
const json = await checkResponse.json();
|
|
62
|
+
if (json[0].status === 'perfect') {
|
|
63
|
+
log(`already verified: ${name} (${address}), skipping.`);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
} catch (e) {
|
|
67
|
+
logError(((e as any).response && JSON.stringify((e as any).response.data)) || e);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!metadataString) {
|
|
71
|
+
logError(`Contract ${name} was deployed without saving metadata. Cannot submit to sourcify, skipping.`);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
logInfo(`verifying ${name} (${address} on chain ${env.chainId}) ...`);
|
|
76
|
+
|
|
77
|
+
const formData = new FormData();
|
|
78
|
+
formData.append('address', address);
|
|
79
|
+
formData.append('chain', env.chainId);
|
|
80
|
+
const metadataBlob = new Blob([metadataString], {
|
|
81
|
+
type: 'application/json',
|
|
82
|
+
});
|
|
83
|
+
formData.append('files', metadataBlob, 'metadata.json');
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
const submissionResponse = await fetch(url, {body: formData, method: 'POST'});
|
|
87
|
+
const json = await submissionResponse.json();
|
|
88
|
+
if (json.result[0].status === 'perfect') {
|
|
89
|
+
logSuccess(` => contract ${name} is now verified`);
|
|
90
|
+
} else {
|
|
91
|
+
logError(` => contract ${name} is not verified`);
|
|
92
|
+
}
|
|
93
|
+
} catch (e) {
|
|
94
|
+
if (env?.logErrorOnFailure) {
|
|
95
|
+
const failingMetadataFolder = path.join('failing_metadata', env.chainId);
|
|
96
|
+
fs.ensureDirSync(failingMetadataFolder);
|
|
97
|
+
fs.writeFileSync(path.join(failingMetadataFolder, `${name}_at_${address}.json`), metadataString);
|
|
98
|
+
}
|
|
99
|
+
logError(((e as any).response && JSON.stringify((e as any).response.data)) || e);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
for (const name of env.deploymentNames ? env.deploymentNames : Object.keys(all)) {
|
|
104
|
+
await submit(name);
|
|
105
|
+
if (env.minInterval) {
|
|
106
|
+
await sleep(env.minInterval);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|