@@ -3,7 +3,10 @@ import * as types from 'hardhat/internal/core/params/argumentTypes'
3
3
import { submitSourcesToSourcify } from './sourcify'
4
4
import { isFullyQualifiedName , parseFullyQualifiedName } from 'hardhat/utils/contract-names'
5
5
import { TASK_COMPILE } from 'hardhat/builtin-tasks/task-names'
6
+ import { getAddressBook } from '../../cli/address-book'
7
+ import { cliOpts } from '../../cli/defaults'
6
8
import fs from 'fs'
9
+ import path from 'path'
7
10
8
11
task ( 'sourcify' , 'Verifies contract on sourcify' )
9
12
. addPositionalParam ( 'address' , 'Address of the smart contract to verify' , undefined , types . string )
@@ -27,3 +30,60 @@ task('sourcify', 'Verifies contract on sourcify')
27
30
fqn : args . contract ,
28
31
} )
29
32
} )
33
+
34
+ task ( 'sourcifyAll' , 'Verifies all contracts on sourcify' )
35
+ . addParam ( 'addressBook' , cliOpts . addressBook . description , cliOpts . addressBook . default )
36
+ . setAction ( async ( _args , hre ) => {
37
+ const chainId = hre . network . config . chainId
38
+ const chainName = hre . network . name
39
+
40
+ if ( ! chainId || ! chainName ) {
41
+ throw new Error ( 'Cannot verify contracts without a network' )
42
+ }
43
+ console . log ( `> Verifying all contracts on chain ${ chainName } [${ chainId } ]...` )
44
+ const addressBook = getAddressBook ( cliOpts . addressBook . default , chainId . toString ( ) )
45
+
46
+ for ( const contractName of addressBook . listEntries ( ) ) {
47
+ console . log ( `\n> Verifying contract ${ contractName } ...` )
48
+
49
+ const contractPath = getContractPath ( contractName )
50
+ if ( contractPath ) {
51
+ const contract = addressBook . getEntry ( contractName )
52
+ if ( contract . implementation ) {
53
+ console . log ( 'Contract is upgradeable, verifying proxy...' )
54
+
55
+ await hre . run ( 'sourcify' , {
56
+ address : contract . address ,
57
+ contract : 'contracts/upgrades/GraphProxy.sol:GraphProxy' ,
58
+ } )
59
+ }
60
+
61
+ // Verify implementation
62
+ await hre . run ( 'sourcify' , {
63
+ address : contract . implementation ?. address ?? contract . address ,
64
+ contract : `${ contractPath } :${ contractName } ` ,
65
+ } )
66
+ } else {
67
+ console . log ( `Contract ${ contractName } not found.` )
68
+ }
69
+ }
70
+ } )
71
+
72
+ function getContractPath ( contract : string ) : string | undefined {
73
+ const files = readDirRecursive ( 'contracts/' )
74
+ return files . find ( ( f ) => path . basename ( f ) === `${ contract } .sol` )
75
+ }
76
+
77
+ function readDirRecursive ( dir : string , allFiles : string [ ] = [ ] ) {
78
+ const files = fs . readdirSync ( dir )
79
+
80
+ for ( const file of files ) {
81
+ if ( fs . statSync ( path . join ( dir , file ) ) . isDirectory ( ) ) {
82
+ allFiles = readDirRecursive ( path . join ( dir , file ) , allFiles )
83
+ } else {
84
+ allFiles . push ( path . join ( dir , file ) )
85
+ }
86
+ }
87
+
88
+ return allFiles
89
+ }
0 commit comments