|
| 1 | +import { execSync } from 'child_process' |
| 2 | +import { task } from 'hardhat/config' |
| 3 | +import { HardhatRuntimeEnvironment as HRE } from 'hardhat/types' |
| 4 | +import { constants } from 'ethers' |
| 5 | +import { appendFileSync } from 'fs' |
| 6 | +import type { VerificationResponse } from '@openzeppelin/hardhat-defender/dist/verify-deployment' |
| 7 | + |
| 8 | +async function main(args: { referenceUrl?: string; contracts: string[] }, hre: HRE) { |
| 9 | + const { referenceUrl, contracts } = args |
| 10 | + const { defender, network, graph } = hre |
| 11 | + const summaryPath = process.env.GITHUB_STEP_SUMMARY |
| 12 | + if (summaryPath) appendFileSync(summaryPath, `# Contracts deployment verification\n\n`) |
| 13 | + |
| 14 | + const workflowUrl = |
| 15 | + referenceUrl || |
| 16 | + process.env.WORKFLOW_URL || |
| 17 | + execSync(`git config --get remote.origin.url`).toString().trim() |
| 18 | + const addressBook = graph().addressBook |
| 19 | + const errs = [] |
| 20 | + |
| 21 | + for (const contractName of contracts) { |
| 22 | + const entry = addressBook.getEntry(contractName) |
| 23 | + if (!entry || entry.address === constants.AddressZero) { |
| 24 | + errs.push([contractName, { message: `Entry not found on address book.` }]) |
| 25 | + continue |
| 26 | + } |
| 27 | + |
| 28 | + const addressToVerify = entry.implementation?.address ?? entry.address |
| 29 | + console.error(`Verifying artifact for ${contractName} at ${addressToVerify}`) |
| 30 | + |
| 31 | + try { |
| 32 | + const response = await defender.verifyDeployment(addressToVerify, contractName, workflowUrl) |
| 33 | + console.error(`Bytecode match for ${contractName} is ${response.matchType}`) |
| 34 | + if (summaryPath) { |
| 35 | + appendFileSync( |
| 36 | + summaryPath, |
| 37 | + `- ${contractName} at ${etherscanLink(network.name, addressToVerify)} is ${ |
| 38 | + response.matchType |
| 39 | + } ${emojiForMatch(response.matchType)}\n`, |
| 40 | + ) |
| 41 | + } |
| 42 | + if (response.matchType === 'NO_MATCH') { |
| 43 | + errs.push([contractName, { message: `No bytecode match.` }]) |
| 44 | + } |
| 45 | + } catch (err: any) { |
| 46 | + if (summaryPath) { |
| 47 | + appendFileSync( |
| 48 | + summaryPath, |
| 49 | + `- ${contractName} at ${etherscanLink( |
| 50 | + network.name, |
| 51 | + addressToVerify, |
| 52 | + )} failed to verify :x:\n`, |
| 53 | + ) |
| 54 | + } |
| 55 | + console.error(`Error verifying artifact: ${err.message}`) |
| 56 | + errs.push([contractName, err]) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (errs.length > 0) { |
| 61 | + throw new Error( |
| 62 | + `Some verifications failed:\n${errs |
| 63 | + .map(([name, err]) => ` ${name}: ${err.message}`) |
| 64 | + .join('\n')}`, |
| 65 | + ) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function etherscanLink(network: string, address: string): string { |
| 70 | + switch (network) { |
| 71 | + case 'mainnet': |
| 72 | + return `[\`${address}\`](https://etherscan.io/address/${address})` |
| 73 | + case 'arbitrum-one': |
| 74 | + return `[\`${address}\`](https://arbiscan.io/address/${address})` |
| 75 | + case 'goerli': |
| 76 | + return `[\`${address}\`](https://goerli.etherscan.io/address/${address})` |
| 77 | + case 'arbitrum-goerli': |
| 78 | + return `[\`${address}\`](https://goerli.arbiscan.io/address/${address})` |
| 79 | + default: |
| 80 | + return `\`${address}\`` |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function emojiForMatch(matchType: VerificationResponse['matchType']): string { |
| 85 | + switch (matchType) { |
| 86 | + case 'EXACT': |
| 87 | + return ':heavy_check_mark:' |
| 88 | + case 'PARTIAL': |
| 89 | + return ':warning:' |
| 90 | + case 'NO_MATCH': |
| 91 | + return ':x:' |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +task('verify-defender') |
| 96 | + .addVariadicPositionalParam('contracts', 'List of contracts to verify') |
| 97 | + .addOptionalParam( |
| 98 | + 'referenceUrl', |
| 99 | + 'URL to link to for artifact verification (defaults to $WORKFLOW_URL or the remote.origin.url of the repository)', |
| 100 | + ) |
| 101 | + .setDescription('Verifies deployed implementations on Defender') |
| 102 | + .setAction(main) |
0 commit comments