Skip to content

Commit

Permalink
feat(plg): add support for foundry-hardhat
Browse files Browse the repository at this point in the history
Adds support for foundry-hardhat style artifacts which require a
shorthand name instead of the long file name.
  • Loading branch information
smartcontracts committed Nov 5, 2022
1 parent 7891768 commit a7c6d18
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-apes-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chugsplash/plugins': patch
---

Add support for foundry-hardhat style artifacts
39 changes: 33 additions & 6 deletions packages/plugins/src/hardhat/artifacts.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<BuildInfo> => {
export const getBuildInfo = async (
sourceName: string,
contractName: string
): Promise<BuildInfo> => {
// 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
}

/**
Expand All @@ -43,7 +70,7 @@ export const getStorageLayout = async (
name: string
): Promise<SolidityStorageLayout> => {
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')) {
Expand All @@ -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]: {
Expand Down Expand Up @@ -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]: {
Expand Down

0 comments on commit a7c6d18

Please sign in to comment.