Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow importing module parameters with filepath in scripts #850

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading