Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto Verify After Deployment #895

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/plugin-hardhat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import '@nomicfoundation/hardhat-ethers';
import './type-extensions';
import { TASK_VERIFY_ETHERSCAN } from '@nomicfoundation/hardhat-verify/internal/task-names';
import { subtask, extendEnvironment, extendConfig } from 'hardhat/config';
import { TASK_COMPILE_SOLIDITY, TASK_COMPILE_SOLIDITY_COMPILE } from 'hardhat/builtin-tasks/task-names';
import { lazyObject } from 'hardhat/plugins';
Expand Down Expand Up @@ -193,7 +194,7 @@ extendConfig((config: HardhatConfig) => {
});

if (tryRequire('@nomicfoundation/hardhat-verify')) {
subtask('verify:etherscan').setAction(async (args, hre, runSuper) => {
subtask(TASK_VERIFY_ETHERSCAN).setAction(async (args, hre, runSuper) => {
const { verify } = await import('./verify-proxy');
return await verify(args, hre, runSuper);
});
Expand Down
8 changes: 7 additions & 1 deletion packages/plugin-hardhat/src/utils/deploy-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@openzeppelin/upgrades-core';
import type { ContractFactory, ethers } from 'ethers';
import type { EthereumProvider, HardhatRuntimeEnvironment } from 'hardhat/types';
import { runVerify } from '../verify-proxy';
import { deploy } from './deploy';
import { GetTxResponse, DefenderDeployOptions, StandaloneOptions, UpgradeOptions, withDefaults } from './options';
import { getRemoteDeployment } from '../defender/utils';
Expand Down Expand Up @@ -132,7 +133,7 @@ async function deployImpl(
remoteDeploymentId => getRemoteDeployment(hre, remoteDeploymentId),
);

let txResponse;
let txResponse: ethers.TransactionResponse | undefined;
if (opts.getTxResponse) {
if ('deployTransaction' in deployment) {
txResponse = deployment.deployTransaction ?? undefined;
Expand All @@ -141,5 +142,10 @@ async function deployImpl(
}
}

if (txResponse) {
await txResponse.wait(1);
await runVerify(hre, deployment.address, opts.constructorArgs);
}

return { impl: deployment.address, txResponse };
}
17 changes: 14 additions & 3 deletions packages/plugin-hardhat/src/utils/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,38 @@ import type { Deployment, RemoteDeploymentId } from '@openzeppelin/upgrades-core
import type { ethers, ContractFactory, ContractMethodArgs } from 'ethers';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { defenderDeploy } from '../defender/deploy';
import { runVerify } from '../verify-proxy';
import { EthersDeployOptions, DefenderDeployOptions, UpgradeOptions } from './options';

export interface DeployTransaction {
deployTransaction?: ethers.TransactionResponse;
}

export type DeployResponse = Required<Deployment> & DeployTransaction & RemoteDeploymentId;

export async function deploy(
hre: HardhatRuntimeEnvironment,
opts: UpgradeOptions & EthersDeployOptions & DefenderDeployOptions,
factory: ContractFactory,
...args: unknown[]
): Promise<Required<Deployment> & DeployTransaction & RemoteDeploymentId> {
): Promise<DeployResponse> {
// defender always includes RemoteDeploymentId, while ethers always includes DeployTransaction
let response: DeployResponse;
if (opts?.useDefenderDeploy) {
return await defenderDeploy(hre, factory, opts, ...args);
response = await defenderDeploy(hre, factory, opts, ...args);
} else {
if (opts.txOverrides !== undefined) {
args.push(opts.txOverrides);
}
return await ethersDeploy(factory, ...args);
response = await ethersDeploy(factory, ...args);
}

if (response.deployTransaction) {
await response.deployTransaction.wait(1);
await runVerify(hre, response.address, opts.constructorArgs);
}

return response;
}

async function ethersDeploy(
Expand Down
17 changes: 17 additions & 0 deletions packages/plugin-hardhat/src/verify-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TASK_VERIFY_VERIFY } from '@nomicfoundation/hardhat-verify/internal/task-names';
import {
getTransactionByHash,
getImplementationAddress,
Expand All @@ -11,6 +12,7 @@ import {
isEmptySlot,
} from '@openzeppelin/upgrades-core';
import artifactsBuildInfo from '@openzeppelin/upgrades-core/artifacts/build-info.json';
import { HARDHAT_NETWORK_NAME } from 'hardhat/plugins';

import { HardhatRuntimeEnvironment, RunSuperFunction } from 'hardhat/types';

Expand Down Expand Up @@ -61,6 +63,21 @@ const verifiableContracts = {
proxyAdmin: { artifact: ProxyAdmin, event: 'OwnershipTransferred(address,address)' },
};

export async function runVerify(hre: HardhatRuntimeEnvironment, address: string, constructorArguments: unknown[] = []) {
if (hre.network.name === HARDHAT_NETWORK_NAME) {
// Don't verify on hardhat network
return;
}
try {
await hre.run(TASK_VERIFY_VERIFY, {
address,
constructorArguments,
});
} catch (e) {
// fail silently
}
}

/**
* Overrides hardhat-verify's verify:etherscan subtask to fully verify a proxy or beacon.
*
Expand Down
Loading