Skip to content

Commit

Permalink
fix(plg): improve hh artifact functions (sphinx-labs#417)
Browse files Browse the repository at this point in the history
Slightly improves the hardhat artifact functions by making the hardhat
runtime envrionment an explicit input rather than an implicit
dependency. Has the result of making the function much more testable by
allowing people to pass in mock environments.

Co-authored-by: sam-goldman <106038229+sam-goldman@users.noreply.github.com>
  • Loading branch information
smartcontracts and sam-goldman authored Jan 27, 2023
1 parent ed17785 commit 120327d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-moose-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chugsplash/plugins': minor
---

Small updates to artifacts functions exposed by the hardhat plugin.
52 changes: 26 additions & 26 deletions packages/plugins/src/hardhat/artifacts.ts
Original file line number Diff line number Diff line change
@@ -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<BuildInfo> => {
// 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}`
Expand All @@ -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<ArtifactPaths> => {
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(
Expand Down
7 changes: 5 additions & 2 deletions packages/plugins/src/hardhat/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()
)
Expand Down
12 changes: 12 additions & 0 deletions packages/plugins/src/hardhat/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 120327d

Please sign in to comment.