diff --git a/.changeset/smart-apes-jump.md b/.changeset/smart-apes-jump.md new file mode 100644 index 000000000..be5a8b1a2 --- /dev/null +++ b/.changeset/smart-apes-jump.md @@ -0,0 +1,5 @@ +--- +'@chugsplash/plugins': patch +--- + +Add support for foundry-hardhat style artifacts diff --git a/packages/plugins/src/hardhat/artifacts.ts b/packages/plugins/src/hardhat/artifacts.ts index e00f9d5be..cc9e3a44b 100644 --- a/packages/plugins/src/hardhat/artifacts.ts +++ b/packages/plugins/src/hardhat/artifacts.ts @@ -1,3 +1,5 @@ +import path from 'path' + import * as semver from 'semver' import { SolidityStorageLayout, ContractConfig } from '@chugsplash/core' import { add0x, remove0x } from '@eth-optimism/core-utils' @@ -22,13 +24,38 @@ export const getContractArtifact = (name: string): ContractArtifact => { /** * Retrieves contract build info by name. * - * @param name Name of the contract. + * @param sourceName Source file name. + * @param contractName Contract name. * @returns Contract build info. */ -export const getBuildInfo = async (name: string): Promise => { +export const getBuildInfo = async ( + sourceName: string, + contractName: string +): Promise => { // eslint-disable-next-line @typescript-eslint/no-var-requires const hre = require('hardhat') - return hre.artifacts.getBuildInfo(name) + + let buildInfo: BuildInfo + try { + buildInfo = await hre.artifacts.getBuildInfo( + `${sourceName}:${contractName}` + ) + } catch (err) { + try { + // Try also loading with the short source name, necessary when using the foundry + // hardhat plugin + const shortSourceName = path.basename(sourceName) + buildInfo = await hre.artifacts.getBuildInfo( + `${shortSourceName}:${contractName}` + ) + } catch { + // Throwing the original error is probably more helpful here because using the + // foundry hardhat plugin is not a common usecase. + throw err + } + } + + return buildInfo } /** @@ -43,7 +70,7 @@ export const getStorageLayout = async ( name: string ): Promise => { const { sourceName, contractName } = getContractArtifact(name) - const buildInfo = await getBuildInfo(`${sourceName}:${contractName}`) + const buildInfo = await getBuildInfo(sourceName, contractName) const output = buildInfo.output.contracts[sourceName][contractName] if (!semver.satisfies(buildInfo.solcVersion, '>=0.4.x <0.9.x')) { @@ -68,7 +95,7 @@ export const getDeployedBytecode = async ( const { sourceName, contractName, bytecode, abi } = getContractArtifact( contractConfig.contract ) - const buildInfo = await getBuildInfo(`${sourceName}:${contractName}`) + const buildInfo = await getBuildInfo(sourceName, contractName) const output = buildInfo.output.contracts[sourceName][contractName] const immutableReferences: { [astId: number]: { @@ -227,7 +254,7 @@ export const getImmutableVariables = async ( const { sourceName, contractName } = getContractArtifact( contractConfig.contract ) - const buildInfo = await getBuildInfo(`${sourceName}:${contractName}`) + const buildInfo = await getBuildInfo(sourceName, contractName) const output = buildInfo.output.contracts[sourceName][contractName] const immutableReferences: { [astId: number]: {