diff --git a/.changeset/thin-moose-rescue.md b/.changeset/thin-moose-rescue.md new file mode 100644 index 000000000..8023845fc --- /dev/null +++ b/.changeset/thin-moose-rescue.md @@ -0,0 +1,5 @@ +--- +'@chugsplash/plugins': minor +--- + +Small updates to artifacts functions exposed by the hardhat plugin. diff --git a/packages/plugins/src/hardhat/artifacts.ts b/packages/plugins/src/hardhat/artifacts.ts index 53c14d396..af88629af 100644 --- a/packages/plugins/src/hardhat/artifacts.ts +++ b/packages/plugins/src/hardhat/artifacts.ts @@ -1,39 +1,21 @@ import path from 'path' -import { BuildInfo } from 'hardhat/types' -import { - ArtifactPaths, - ContractArtifact, - UserContractConfigs, -} from '@chugsplash/core' - -/** - * Retrieves an artifact by name. - * - * @param Name Name of the contract. - * @returns Artifact. - */ -export const getContractArtifact = (name: string): ContractArtifact => { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const hre = require('hardhat') - return hre.artifacts.readArtifactSync(name) -} +import { BuildInfo, HardhatRuntimeEnvironment } from 'hardhat/types' +import { ArtifactPaths, UserContractConfigs } from '@chugsplash/core' /** * Retrieves contract build info by name. * * @param sourceName Source file name. - * @param contractName Contract name. + * @param contractName Contract name within the source file. * @returns Contract build info. */ export const getBuildInfo = async ( + hre: HardhatRuntimeEnvironment, sourceName: string, contractName: string ): Promise => { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const hre = require('hardhat') - - let buildInfo: BuildInfo + let buildInfo: BuildInfo | undefined try { buildInfo = await hre.artifacts.getBuildInfo( `${sourceName}:${contractName}` @@ -53,19 +35,37 @@ export const getBuildInfo = async ( } } + // Shouldn't happen, but might as well be safe. + if (buildInfo === undefined) { + throw new Error( + `unable to find build info for contract ${contractName} in ${sourceName}` + ) + } + return buildInfo } +/** + * Finds the path to the build info file and the contract artifact file for each contract + * referenced in the given contract configurations. + * + * @param hre Hardhat runtime environment. + * @param contractConfigs Contract configurations. + * @param artifactFolder Path to the artifact folder. + * @param buildInfoFolder Path to the build info folder. + * @returns Paths to the build info and contract artifact files. + */ export const getArtifactPaths = async ( + hre: HardhatRuntimeEnvironment, contractConfigs: UserContractConfigs, artifactFolder: string, buildInfoFolder: string ): Promise => { const artifactPaths: ArtifactPaths = {} - for (const { contract } of Object.values(contractConfigs)) { - const { sourceName, contractName } = getContractArtifact(contract) - const buildInfo = await getBuildInfo(sourceName, contractName) + const { sourceName, contractName } = + hre.artifacts.readArtifactSync(contract) + const buildInfo = await getBuildInfo(hre, sourceName, contractName) artifactPaths[contract] = { buildInfoPath: path.join(buildInfoFolder, `${buildInfo.id}.json`), contractArtifactPath: path.join( diff --git a/packages/plugins/src/hardhat/deployments.ts b/packages/plugins/src/hardhat/deployments.ts index e6425a39d..81155820a 100644 --- a/packages/plugins/src/hardhat/deployments.ts +++ b/packages/plugins/src/hardhat/deployments.ts @@ -18,7 +18,7 @@ import { getChainId } from '@eth-optimism/core-utils' import { HardhatRuntimeEnvironment } from 'hardhat/types' import { initializeExecutor } from '../executor' -import { getArtifactPaths, getContractArtifact } from './artifacts' +import { getArtifactPaths } from './artifacts' /** * TODO @@ -56,6 +56,7 @@ export const deployAllChugSplashConfigs = async ( const userConfig = readUserChugSplashConfig(configPath) const artifactPaths = await getArtifactPaths( + hre, userConfig.contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info') @@ -136,7 +137,9 @@ ${configsWithFileNames.map( const Proxy = new ethers.Contract( proxyAddress, new ethers.utils.Interface( - getContractArtifact(userCfg.contracts[referenceName].contract).abi + hre.artifacts.readArtifactSync( + userCfg.contracts[referenceName].contract + ).abi ), provider.getSigner() ) diff --git a/packages/plugins/src/hardhat/tasks.ts b/packages/plugins/src/hardhat/tasks.ts index 64bb9dc14..a1486cb9b 100644 --- a/packages/plugins/src/hardhat/tasks.ts +++ b/packages/plugins/src/hardhat/tasks.ts @@ -166,6 +166,7 @@ export const chugsplashDeployTask = async ( const userConfig = readUserChugSplashConfig(configPath) const artifactPaths = await getArtifactPaths( + hre, userConfig.contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info') @@ -234,6 +235,7 @@ export const chugsplashRegisterTask = async ( const signer = provider.getSigner() const userConfig = readUserChugSplashConfig(configPath) const artifactPaths = await getArtifactPaths( + hre, userConfig.contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info') @@ -301,6 +303,7 @@ export const chugsplashProposeTask = async ( const canonicalConfigPath = hre.config.paths.canonicalConfigs const artifactPaths = await getArtifactPaths( + hre, userConfig.contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info') @@ -374,6 +377,7 @@ export const chugsplashApproveTask = async ( const userConfig = readUserChugSplashConfig(configPath) const artifactPaths = await getArtifactPaths( + hre, userConfig.contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info') @@ -636,6 +640,7 @@ export const monitorTask = async ( const userConfig = readUserChugSplashConfig(configPath) const artifactPaths = await getArtifactPaths( + hre, userConfig.contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info') @@ -681,6 +686,7 @@ export const chugsplashFundTask = async ( const userConfig = readUserChugSplashConfig(configPath) const artifactPaths = await getArtifactPaths( + hre, userConfig.contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info') @@ -860,6 +866,7 @@ export const chugsplashCancelTask = async ( const signer = provider.getSigner() const artifactPaths = await getArtifactPaths( + hre, readUserChugSplashConfig(configPath).contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info') @@ -896,6 +903,7 @@ export const chugsplashWithdrawTask = async ( const userConfig = readUserChugSplashConfig(configPath) const artifactPaths = await getArtifactPaths( + hre, userConfig.contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info') @@ -943,6 +951,7 @@ export const listProposersTask = async ( const signer = provider.getSigner() const artifactPaths = await getArtifactPaths( + hre, readUserChugSplashConfig(configPath).contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info') @@ -979,6 +988,7 @@ export const addProposerTask = async ( const signer = provider.getSigner() const artifactPaths = await getArtifactPaths( + hre, readUserChugSplashConfig(configPath).contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info') @@ -1017,6 +1027,7 @@ export const claimProxyTask = async ( const signer = provider.getSigner() const artifactPaths = await getArtifactPaths( + hre, readUserChugSplashConfig(configPath).contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info') @@ -1061,6 +1072,7 @@ export const transferOwnershipTask = async ( const signer = provider.getSigner() const artifactPaths = await getArtifactPaths( + hre, readUserChugSplashConfig(configPath).contracts, hre.config.paths.artifacts, path.join(hre.config.paths.artifacts, 'build-info')