Skip to content

Commit

Permalink
Merge pull request #850 from NomicFoundation/module-params-fix
Browse files Browse the repository at this point in the history
Allow importing module parameters with filepath in scripts
  • Loading branch information
zoeyTM authored Dec 18, 2024
2 parents dc6ee26 + b73be3b commit 2d25dba
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 38 deletions.
13 changes: 11 additions & 2 deletions packages/hardhat-plugin-ethers/src/ethers-ignition-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
HardhatArtifactResolver,
PrettyEventHandler,
errorDeploymentResultToExceptionMessage,
readDeploymentParameters,
resolveDeploymentId,
} from "@nomicfoundation/hardhat-ignition/helpers";
import {
Expand Down Expand Up @@ -81,7 +82,7 @@ export class EthersIgnitionHelper {
deploymentId: givenDeploymentId = undefined,
displayUi = false,
}: {
parameters?: DeploymentParameters;
parameters?: DeploymentParameters | string;
config?: Partial<DeployConfig>;
defaultSender?: string;
strategy?: StrategyT;
Expand Down Expand Up @@ -142,14 +143,22 @@ export class EthersIgnitionHelper {
? new PrettyEventHandler()
: undefined;

let deploymentParameters: DeploymentParameters;

if (typeof parameters === "string") {
deploymentParameters = await readDeploymentParameters(parameters);
} else {
deploymentParameters = parameters;
}

const result = await deploy({
config: resolvedConfig,
provider: this._provider,
deploymentDir,
executionEventListener,
artifactResolver,
ignitionModule,
deploymentParameters: parameters,
deploymentParameters,
accounts,
defaultSender,
strategy,
Expand Down
13 changes: 11 additions & 2 deletions packages/hardhat-plugin-viem/src/viem-ignition-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
HardhatArtifactResolver,
PrettyEventHandler,
errorDeploymentResultToExceptionMessage,
readDeploymentParameters,
resolveDeploymentId,
} from "@nomicfoundation/hardhat-ignition/helpers";
import {
Expand Down Expand Up @@ -74,7 +75,7 @@ export class ViemIgnitionHelper {
deploymentId: givenDeploymentId = undefined,
displayUi = false,
}: {
parameters?: DeploymentParameters;
parameters?: DeploymentParameters | string;
config?: Partial<DeployConfig>;
defaultSender?: string;
strategy?: StrategyT;
Expand Down Expand Up @@ -132,14 +133,22 @@ export class ViemIgnitionHelper {
? new PrettyEventHandler()
: undefined;

let deploymentParameters: DeploymentParameters;

if (typeof parameters === "string") {
deploymentParameters = await readDeploymentParameters(parameters);
} else {
deploymentParameters = parameters;
}

const result = await deploy({
config: resolvedConfig,
provider: this._provider,
deploymentDir,
executionEventListener,
artifactResolver,
ignitionModule,
deploymentParameters: parameters,
deploymentParameters,
accounts,
defaultSender,
strategy,
Expand Down
1 change: 1 addition & 0 deletions packages/hardhat-plugin/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { HardhatArtifactResolver } from "./hardhat-artifact-resolver";
export { errorDeploymentResultToExceptionMessage } from "./utils/error-deployment-result-to-exception-message";
export { resolveDeploymentId } from "./utils/resolve-deployment-id";
export { PrettyEventHandler } from "./ui/pretty-event-handler";
export { readDeploymentParameters } from "./utils/read-deployment-parameters";
38 changes: 4 additions & 34 deletions packages/hardhat-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ import {
StatusResult,
} from "@nomicfoundation/ignition-core";
import debug from "debug";
import {
ensureDir,
pathExists,
readFile,
readdirSync,
rm,
writeJSON,
} from "fs-extra";
import { ensureDir, pathExists, readdirSync, rm, writeJSON } from "fs-extra";
import { extendConfig, extendEnvironment, scope } from "hardhat/config";
import { NomicLabsHardhatPluginError } from "hardhat/plugins";
import { parse as json5Parse } from "json5";
Expand All @@ -24,6 +17,7 @@ import "./type-extensions";
import { calculateDeploymentStatusDisplay } from "./ui/helpers/calculate-deployment-status-display";
import { bigintReviver } from "./utils/bigintReviver";
import { getApiKeyAndUrls } from "./utils/getApiKeyAndUrls";
import { readDeploymentParameters } from "./utils/read-deployment-parameters";
import { resolveDeploymentId } from "./utils/resolve-deployment-id";
import { shouldBeHardhatPluginError } from "./utils/shouldBeHardhatPluginError";
import { verifyEtherscanContract } from "./utils/verifyEtherscanContract";
Expand Down Expand Up @@ -734,7 +728,7 @@ async function resolveParametersFromModuleName(
const configFilename = `${moduleName}.config.json`;

return files.includes(configFilename)
? resolveConfigPath(path.resolve(ignitionPath, configFilename))
? readDeploymentParameters(path.resolve(ignitionPath, configFilename))
: undefined;
}

Expand All @@ -743,31 +737,7 @@ async function resolveParametersFromFileName(
): Promise<DeploymentParameters> {
const filepath = path.resolve(process.cwd(), fileName);

return resolveConfigPath(filepath);
}

async function resolveConfigPath(
filepath: string
): Promise<DeploymentParameters> {
try {
const rawFile = await readFile(filepath);

return await json5Parse(rawFile.toString(), bigintReviver);
} catch (e) {
if (e instanceof NomicLabsHardhatPluginError) {
throw e;
}

if (e instanceof Error) {
throw new NomicLabsHardhatPluginError(
"@nomicfoundation/hardhat-ignition",
`Could not parse parameters from ${filepath}`,
e
);
}

throw e;
}
return readDeploymentParameters(filepath);
}

function resolveParametersString(paramString: string): DeploymentParameters {
Expand Down
30 changes: 30 additions & 0 deletions packages/hardhat-plugin/src/utils/read-deployment-parameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { DeploymentParameters } from "@nomicfoundation/ignition-core";
import { readFile } from "fs-extra";
import { NomicLabsHardhatPluginError } from "hardhat/plugins";
import { parse as json5Parse } from "json5";

import { bigintReviver } from "./bigintReviver";

export async function readDeploymentParameters(
filepath: string
): Promise<DeploymentParameters> {
try {
const rawFile = await readFile(filepath);

return await json5Parse(rawFile.toString(), bigintReviver);
} catch (e) {
if (e instanceof NomicLabsHardhatPluginError) {
throw e;
}

if (e instanceof Error) {
throw new NomicLabsHardhatPluginError(
"@nomicfoundation/hardhat-ignition",
`Could not parse parameters from ${filepath}`,
e
);
}

throw e;
}
}

0 comments on commit 2d25dba

Please sign in to comment.