11// SPDX-FileCopyrightText: 2024-2025 IEXEC BLOCKCHAIN TECH <[email protected] > 22// SPDX-License-Identifier: Apache-2.0
33
4- import fs from 'fs' ;
54import hre , { deployments } from 'hardhat' ;
6- import path from 'path' ;
7-
8- export interface ContractToVerify {
9- name : string ;
10- address : string ;
11- constructorArguments ?: any [ ] ;
12- }
5+ import { Deployment } from 'hardhat-deploy/dist/types' ;
136
147/**
158 * Verifies contracts on block explorer (e.g., Etherscan, Arbiscan).
@@ -18,7 +11,7 @@ export interface ContractToVerify {
1811 * @param contracts - Optional array of specific contracts to verify. If not provided,
1912 * will verify all contracts from the deployments/{network} directory.
2013 */
21- async function verify ( contracts ?: ContractToVerify [ ] ) : Promise < void > {
14+ async function verify ( contractNames ?: string [ ] ) : Promise < void > {
2215 const skippedNetworks : string [ ] = [
2316 'hardhat' ,
2417 'localhost' ,
@@ -30,34 +23,31 @@ async function verify(contracts?: ContractToVerify[]): Promise<void> {
3023 console . log ( `\nSkipping verification on development network: ${ hre . network . name } ` ) ;
3124 return ;
3225 }
33-
34- const contractsToVerify =
35- contracts && contracts . length > 0 ? contracts : await getContractsFromDeployments ( ) ;
36-
26+ let contracts : { [ name : string ] : Deployment } = { } ;
27+ if ( contractNames ) {
28+ // Get deployments of the specified contract names.
29+ for ( const name of contractNames ) {
30+ contracts [ name ] = await deployments . get ( name ) ;
31+ }
32+ } else {
33+ // If no specific contract names provided, verify all deployments.
34+ contracts = await deployments . all ( ) ;
35+ contractNames = Object . keys ( contracts ) ;
36+ }
3737 console . log ( '\n=== Verifying contracts on block explorer ===' ) ;
38- console . log ( `Contracts to verify: ${ contractsToVerify . map ( ( c ) => c . name ) . join ( ', ' ) } ` ) ;
38+ console . log ( `Contracts to verify: ${ contractNames . join ( ', ' ) } ` ) ;
3939 console . log ( 'Waiting for block explorer to index the contracts...' ) ;
40- await new Promise ( ( resolve ) => setTimeout ( resolve , 60000 ) ) ;
41-
42- for ( const contract of contractsToVerify ) {
40+ await new Promise ( ( resolve ) => setTimeout ( resolve , 60000 ) ) ; // 60s
41+ for ( const name of contractNames ) {
4342 try {
43+ const contract = contracts [ name ] ;
4444 await hre . run ( 'verify:verify' , {
4545 address : contract . address ,
46- constructorArguments : contract . constructorArguments || [ ] ,
46+ constructorArguments : contract . args ,
4747 } ) ;
48- console . log ( `${ contract . name } verified successfully` ) ;
48+ console . log ( `${ name } verified successfully` ) ;
4949 } catch ( error : any ) {
50- console . error ( `Error verifying ${ contract . name } :` , error . message || error ) ;
51- if (
52- typeof error . message === 'string' &&
53- error . message . includes ( 'has' ) &&
54- error . message . includes ( 'parameters but' ) &&
55- error . message . includes ( 'arguments were provided' )
56- ) {
57- console . error (
58- `${ contract . name } requires constructor arguments. Please add them to the deployment artifact.` ,
59- ) ;
60- }
50+ console . error ( `Error verifying ${ name } :` , error . message || error ) ;
6151 }
6252 }
6353 console . log ( '\nVerification completed!' ) ;
@@ -69,46 +59,17 @@ async function verify(contracts?: ContractToVerify[]): Promise<void> {
6959 *
7060 * @param contracts - Optional array of specific contracts to verify.
7161 */
72- export async function tryVerify ( contracts ?: ContractToVerify [ ] ) : Promise < void > {
62+ export async function tryVerify ( contractNames ?: string [ ] ) : Promise < void > {
7363 try {
74- await verify ( contracts ) ;
64+ await verify ( contractNames ) ;
7565 } catch ( error ) {
7666 console . error ( 'Verification failed, but continuing with deployment:' , error ) ;
7767 }
7868}
7969
80- /**
81- * Gets contracts to verify from deployments directory.
82- */
83- async function getContractsFromDeployments ( ) : Promise < ContractToVerify [ ] > {
84- const jsonExtension = '.json' ;
85- const contractNames = fs
86- . readdirSync ( path . resolve ( __dirname , `../deployments/${ hre . network . name } ` ) )
87- . filter ( ( file ) => file . endsWith ( jsonExtension ) )
88- . map ( ( filePath ) => filePath . replace ( jsonExtension , '' ) ) ;
89-
90- if ( contractNames . length === 0 ) {
91- console . log ( `\nNo contracts to verify on network: ${ hre . network . name } ` ) ;
92- return [ ] ;
93- }
94-
95- const contracts : ContractToVerify [ ] = [ ] ;
96- for ( const contractName of contractNames ) {
97- const deployment = await deployments . get ( contractName ) ;
98- contracts . push ( {
99- name : contractName ,
100- address : deployment . address ,
101- constructorArguments : deployment . args || [ ] ,
102- } ) ;
103- }
104- return contracts ;
105- }
106-
10770if ( require . main === module ) {
108- verify ( )
109- . then ( ( ) => process . exit ( 0 ) )
110- . catch ( ( error ) => {
111- console . error ( error ) ;
112- process . exit ( 1 ) ;
113- } ) ;
71+ verify ( ) . catch ( ( error ) => {
72+ console . error ( error ) ;
73+ process . exit ( 1 ) ;
74+ } ) ;
11475}
0 commit comments