Skip to content

Commit

Permalink
Merge pull request sphinx-labs#245 from chugsplash/sg/develop
Browse files Browse the repository at this point in the history
fix(pg): improve the harhdat node task
  • Loading branch information
sam-goldman authored Nov 22, 2022
2 parents af6bfa2 + 8b81504 commit 2d838da
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-oranges-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chugsplash/plugins': patch
---

Improve the hardhat node task
30 changes: 24 additions & 6 deletions packages/plugins/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
/**
* Removes all command line args in the `process.argv` array that begin with '--'. This is necessary
* to prevent an error that occurs when running the executor from within a Hardhat plugin task. This
* error occurs because the BaseServiceV2 (inherited by the executor) parses these command line
* arguments and throws an error when it sees arguments that it does not recognize.
* Removes command line args in the `process.argv` array beginning with the first argument that
* starts with '--'. This is necessary to prevent an error that occurs when running the executor
* from within a Hardhat plugin task. This error occurs because the BaseServiceV2 (inherited by the
* executor) parses these command line arguments and throws an error when it sees an unrecognized
* argument that begins with '--'.
*
* @returns An array containing the removed command line args.
*/
export const removeFlagsFromCommandLineArgs = (): void => {
process.argv = process.argv.filter((arg) => !arg.startsWith('--'))
export const removeFlagsFromCommandLineArgs = (): string[] => {
const indexToRemove = process.argv.findIndex((arg) => arg.startsWith('--'))
if (indexToRemove === -1) {
return []
}
const removed = process.argv.slice(indexToRemove)
process.argv = process.argv.slice(0, indexToRemove)
return removed
}

/**
* Adds the given array of arguments to `process.argv`.
*
* @param args The command line arguments to add.
*/
export const addCommandLineArgs = (args: string[]) => {
process.argv = process.argv.concat(args)
}
18 changes: 18 additions & 0 deletions packages/plugins/src/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ChugSplashExecutor } from '@chugsplash/executor'

import { addCommandLineArgs, removeFlagsFromCommandLineArgs } from './env'

export const instantiateExecutor = (): ChugSplashExecutor => {
// We must remove the command line arguments that begin with '--' from the process.argv array,
// or else the BaseServiceV2 (inherited by the executor) will throw an error when we instantiate
// it.
const removed = removeFlagsFromCommandLineArgs()

// Instantiate the executor.
const executor = new ChugSplashExecutor()

// Add the command line args back to the array.
addCommandLineArgs(removed)

return executor
}
17 changes: 8 additions & 9 deletions packages/plugins/src/hardhat/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
monitorTask,
TASK_CHUGSPLASH_VERIFY_BUNDLE,
} from './tasks'
import { removeFlagsFromCommandLineArgs } from '../env'
import { instantiateExecutor } from '../executor'

/**
* TODO
Expand All @@ -46,17 +46,15 @@ export const deployAllChugSplashConfigs = async (
hre: any,
silent: boolean,
ipfsUrl: string,
noCompile: boolean
noCompile: boolean,
spinner: ora.Ora = ora({ isSilent: true })
) => {
const remoteExecution = (await getChainId(hre.ethers.provider)) !== 31337
const fileNames = fs.readdirSync(hre.config.paths.chugsplash)

let executor: ChugSplashExecutor
if (!remoteExecution) {
// We must remove the command line arguments that begin with '--' from the process.argv array,
// or else the BaseServiceV2 (inherited by the executor) will throw an error.
removeFlagsFromCommandLineArgs()
executor = new ChugSplashExecutor()
executor = instantiateExecutor()
}

for (const fileName of fileNames) {
Expand All @@ -73,7 +71,8 @@ export const deployAllChugSplashConfigs = async (
remoteExecution,
ipfsUrl,
noCompile,
executor
executor,
spinner
)
}
}
Expand Down Expand Up @@ -106,7 +105,7 @@ export const deployChugSplashConfig = async (
parsedConfig.options.projectName
)

spinner.succeed('Parsed ChugSplash config file.')
spinner.succeed(`Parsed ${parsedConfig.options.projectName}.`)

if (projectPreviouslyRegistered === false) {
spinner.start(`Registering ${parsedConfig.options.projectName}...`)
Expand Down Expand Up @@ -228,7 +227,7 @@ export const deployChugSplashConfig = async (
{
privateKey:
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
logLevel: silent ? 'info' : 'error',
logLevel: 'error',
},
provider,
canonicalConfig
Expand Down
50 changes: 29 additions & 21 deletions packages/plugins/src/hardhat/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import {
successfulProposalMessage,
} from '../messages'
import { monitorRemoteExecution, postExecutionActions } from './execution'
import { removeFlagsFromCommandLineArgs } from '../env'
import { instantiateExecutor } from '../executor'

// Load environment variables from .env
dotenv.config()
Expand Down Expand Up @@ -183,10 +183,7 @@ export const chugsplashDeployTask = async (

let executor: ChugSplashExecutor
if (!remoteExecution) {
// We must remove the command line arguments that begin with '--' from the process.argv array,
// or else the BaseServiceV2 (inherited by the executor) will throw an error.
removeFlagsFromCommandLineArgs()
executor = new ChugSplashExecutor()
executor = instantiateExecutor()
}

spinner.succeed('ChugSplash is ready to go.')
Expand Down Expand Up @@ -280,7 +277,6 @@ export const chugsplashProposeTask = async (
const signer = provider.getSigner()

const spinner = ora({ isSilent: silent })

spinner.start('Booting up ChugSplash...')

await deployChugSplashPredeploys(provider, provider.getSigner())
Expand Down Expand Up @@ -960,12 +956,8 @@ task(TASK_CHUGSPLASH_FUND)
.addPositionalParam('configPath', 'Path to the ChugSplash config file')
.setAction(chugsplashFundTask)

// TODO: change 'any' type
task(TASK_NODE)
.addFlag(
'setupInternals',
'Setup the internal ChugSplash contracts. Skip executing all contracts defined in ChugSplash config files.'
)
.addFlag('deployAll', 'Deploy all ChugSplash config files on startup')
.addFlag(
'disableChugsplash',
"Completely disable all of ChugSplash's activity."
Expand All @@ -975,22 +967,32 @@ task(TASK_NODE)
.setAction(
async (
args: {
setupInternals: boolean
deployAll: boolean
disableChugsplash: boolean
hide: boolean
noCompile: boolean
},
hre: HardhatRuntimeEnvironment,
runSuper
) => {
const { setupInternals, disableChugsplash, hide, noCompile } = args
const { deployAll, disableChugsplash, hide, noCompile } = args

if (!disableChugsplash) {
const spinner = ora({ isSilent: hide })
spinner.start('Booting up ChugSplash...')

await deployChugSplashPredeploys(
hre.ethers.provider,
hre.ethers.provider.getSigner()
)
if (!setupInternals) {
await deployAllChugSplashConfigs(hre, hide, '', noCompile)

spinner.succeed('ChugSplash has been initialized.')

if (deployAll) {
if (!noCompile) {
await cleanThenCompile(hre)
}
await deployAllChugSplashConfigs(hre, hide, '', true, spinner)
}
await writeHardhatSnapshotId(hre)
}
Expand Down Expand Up @@ -1024,7 +1026,10 @@ task(TASK_TEST)
hre.ethers.provider,
hre.ethers.provider.getSigner()
)
await deployAllChugSplashConfigs(hre, !show, '', noCompile)
if (!noCompile) {
await cleanThenCompile(hre)
}
await deployAllChugSplashConfigs(hre, !show, '', true)
} finally {
await writeHardhatSnapshotId(hre)
}
Expand All @@ -1035,25 +1040,28 @@ task(TASK_TEST)

task(TASK_RUN)
.addFlag(
'enableChugsplash',
'deployAll',
'Deploy all ChugSplash configs before executing your script.'
)
.setAction(
async (
args: {
enableChugsplash: boolean
deployAll: boolean
noCompile: boolean
},
hre: any,
runSuper
) => {
const { enableChugsplash, noCompile } = args
if (enableChugsplash) {
const { deployAll, noCompile } = args
if (deployAll) {
await deployChugSplashPredeploys(
hre.ethers.provider,
hre.ethers.provider.getSigner()
)
await deployAllChugSplashConfigs(hre, true, '', noCompile)
if (!noCompile) {
await cleanThenCompile(hre)
}
await deployAllChugSplashConfigs(hre, true, '', true)
}
await runSuper(args)
}
Expand Down

0 comments on commit 2d838da

Please sign in to comment.