@@ -5,41 +5,103 @@ import fs from 'fs';
55import hre , { deployments } from 'hardhat' ;
66import path from 'path' ;
77
8- async function verify ( ) {
9- const jsonExtension = '.json' ;
10- const contractNames = fs
11- . readdirSync ( path . resolve ( __dirname , `../deployments/${ hre . network . name } ` ) )
12- . filter ( ( file ) => file . endsWith ( jsonExtension ) )
13- . map ( ( filePath ) => filePath . replace ( jsonExtension , '' ) ) ;
8+ export interface ContractToVerify {
9+ name : string ;
10+ address : string ;
11+ constructorArguments ?: any [ ] ;
12+ }
13+
14+ /**
15+ * Verifies contracts on block explorer (e.g., Etherscan, Arbiscan).
16+ * Can verify either specific contracts or all contracts from deployments directory.
17+ *
18+ * @param contracts - Optional array of specific contracts to verify. If not provided,
19+ * will verify all contracts from the deployments/{network} directory.
20+ */
21+ async function verify ( contracts ?: ContractToVerify [ ] ) : Promise < void > {
22+ const skippedNetworks : string [ ] = [
23+ 'hardhat' ,
24+ 'localhost' ,
25+ 'external-hardhat' ,
26+ 'dev-native' ,
27+ 'dev-token' ,
28+ ] ;
29+ if ( skippedNetworks . includes ( hre . network . name ) ) {
30+ console . log ( `\nSkipping verification on development network: ${ hre . network . name } ` ) ;
31+ return ;
32+ }
1433
15- console . log ( `Contracts to verify: ${ contractNames } ` ) ;
34+ const contractsToVerify =
35+ contracts && contracts . length > 0 ? contracts : await getContractsFromDeployments ( ) ;
1636
17- for ( const contractName of contractNames ) {
37+ console . log ( '\n=== Verifying contracts on block explorer ===' ) ;
38+ console . log ( `Contracts to verify: ${ contractsToVerify . map ( ( c ) => c . name ) . join ( ', ' ) } ` ) ;
39+ 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 ) {
1843 try {
19- console . log ( `Verifying ${ contractName } ..` ) ;
20- const deployment = await deployments . get ( contractName ) ;
21- const address = deployment . address ;
22- const constructorArguments = deployment . args || [ ] ;
2344 await hre . run ( 'verify:verify' , {
24- address,
25- constructorArguments,
45+ address : contract . address ,
46+ constructorArguments : contract . constructorArguments || [ ] ,
2647 } ) ;
27-
28- console . log ( `${ contractName } verified successfully` ) ;
48+ console . log ( `${ contract . name } verified successfully` ) ;
2949 } catch ( error : any ) {
30- console . error ( `Error verifying ${ contractName } :` , error . message || error ) ;
50+ console . error ( `Error verifying ${ contract . name } :` , error . message || error ) ;
3151 if (
3252 typeof error . message === 'string' &&
3353 error . message . includes ( 'has' ) &&
3454 error . message . includes ( 'parameters but' ) &&
3555 error . message . includes ( 'arguments were provided' )
3656 ) {
3757 console . error (
38- `${ contractName } requires constructor arguments. Please add them to the deployment artifact.` ,
58+ `${ contract . name } requires constructor arguments. Please add them to the deployment artifact.` ,
3959 ) ;
4060 }
4161 }
4262 }
63+ console . log ( '\nVerification completed!' ) ;
64+ }
65+
66+ /**
67+ * Attempts to verify contracts without throwing errors.
68+ * This is useful when verification is optional and should not break the deployment process.
69+ *
70+ * @param contracts - Optional array of specific contracts to verify.
71+ */
72+ export async function tryVerify ( contracts ?: ContractToVerify [ ] ) : Promise < void > {
73+ try {
74+ await verify ( contracts ) ;
75+ } catch ( error ) {
76+ console . error ( 'Verification failed, but continuing with deployment:' , error ) ;
77+ }
78+ }
79+
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 ;
43105}
44106
45107if ( require . main === module ) {
@@ -50,5 +112,3 @@ if (require.main === module) {
50112 process . exit ( 1 ) ;
51113 } ) ;
52114}
53-
54- export default verify ;
0 commit comments