Skip to content

Commit

Permalink
allow importing module parameters with filepath in scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
zoeyTM committed Dec 17, 2024
1 parent 4b5b2bf commit 08933d5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 36 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,
resolveConfigPath,
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 resolveConfigPath(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,
resolveConfigPath,
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 resolveConfigPath(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 { resolveConfigPath } from "./utils/resolve-config-path";
34 changes: 2 additions & 32 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 { resolveConfigPath } from "./utils/resolve-config-path";
import { resolveDeploymentId } from "./utils/resolve-deployment-id";
import { shouldBeHardhatPluginError } from "./utils/shouldBeHardhatPluginError";
import { verifyEtherscanContract } from "./utils/verifyEtherscanContract";
Expand Down Expand Up @@ -741,30 +735,6 @@ async function resolveParametersFromFileName(
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;
}
}

function resolveParametersString(paramString: string): DeploymentParameters {
try {
return json5Parse(paramString, bigintReviver);
Expand Down
30 changes: 30 additions & 0 deletions packages/hardhat-plugin/src/utils/resolve-config-path.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 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;
}
}

0 comments on commit 08933d5

Please sign in to comment.