Skip to content

Commit 49ef530

Browse files
authored
chore: add hardhat task to verify all contracts with sourcify (#625)
Signed-off-by: Tomás Migone <[email protected]>
1 parent 2bbe2a8 commit 49ef530

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

hardhat.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const config: HardhatUserConfig = {
108108
},
109109
outputSelection: {
110110
'*': {
111-
'*': ['storageLayout'],
111+
'*': ['storageLayout', 'metadata'],
112112
},
113113
},
114114
},

tasks/verify/verify.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import * as types from 'hardhat/internal/core/params/argumentTypes'
33
import { submitSourcesToSourcify } from './sourcify'
44
import { isFullyQualifiedName, parseFullyQualifiedName } from 'hardhat/utils/contract-names'
55
import { TASK_COMPILE } from 'hardhat/builtin-tasks/task-names'
6+
import { getAddressBook } from '../../cli/address-book'
7+
import { cliOpts } from '../../cli/defaults'
68
import fs from 'fs'
9+
import path from 'path'
710

811
task('sourcify', 'Verifies contract on sourcify')
912
.addPositionalParam('address', 'Address of the smart contract to verify', undefined, types.string)
@@ -27,3 +30,60 @@ task('sourcify', 'Verifies contract on sourcify')
2730
fqn: args.contract,
2831
})
2932
})
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

Comments
 (0)