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

add dependencyOnlyDeploy for external contracts config #492

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
136 changes: 94 additions & 42 deletions src/DeploymentsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export class DeploymentsManager {
migrations: {[id: string]: number};
onlyArtifacts?: string[];
runAsNode: boolean;
dependenciesScriptPathBags: {[tag: string]: string[]};
dependenciesFuncByFilePath: {[filename: string]: DeployFunction};
};

private env: HardhatRuntimeEnvironment;
Expand Down Expand Up @@ -134,6 +136,8 @@ export class DeploymentsManager {
savePendingTx: false,
gasPrice: undefined,
runAsNode: false,
dependenciesScriptPathBags: {},
dependenciesFuncByFilePath: {},
};
this.env = env;
this.deploymentsPath = env.config.paths.deployments;
Expand Down Expand Up @@ -1045,11 +1049,23 @@ export class DeploymentsManager {
if (this.env.config.external?.contracts) {
for (const externalContracts of this.env.config.external.contracts) {
if (externalContracts.deploy) {
this.db.onlyArtifacts = externalContracts.artifacts;
try {
await this.executeDeployScripts([externalContracts.deploy], tags, options.tagsRequireAll);
} finally {
this.db.onlyArtifacts = undefined;
if (externalContracts.execute) {
this.db.onlyArtifacts = externalContracts.artifacts;
try {
await this.executeDeployScripts(
[externalContracts.deploy],
tags,
options.tagsRequireAll
);
} finally {
this.db.onlyArtifacts = undefined;
}
} else {
this.loadDeployScripts(
[externalContracts.deploy],
this.db.dependenciesScriptPathBags,
this.db.dependenciesFuncByFilePath
);
}
}
}
Expand All @@ -1064,10 +1080,60 @@ export class DeploymentsManager {
return this.db.deployments;
}

private requireDeployScript(scriptFilePath: string): DeployFunction {
let deployFunc: DeployFunction;
try {
delete require.cache[scriptFilePath]; // ensure we reload it every time, so changes are taken in consideration
deployFunc = require(scriptFilePath);
if ((deployFunc as any).default) {
deployFunc = (deployFunc as any).default as DeployFunction;
}
} catch (e) {
throw new Error(
'ERROR loading deploy script at ' +
scriptFilePath +
':\n' +
((e as any).stack || e)
);
}
return deployFunc;
}

public loadDeployScripts(
deployScriptsPaths: string[],
scriptPathBags: {[tag: string]: string[]},
funcByFilePath: {[filename: string]: DeployFunction}
): void {
let filepaths;
try {
filepaths = traverseMultipleDirectory(deployScriptsPaths);
} catch (e) {
return;
}

for (const filepath of filepaths) {
const scriptFilePath = path.resolve(filepath);
const deployFunc = this.requireDeployScript(scriptFilePath);
funcByFilePath[scriptFilePath] = deployFunc;
let scriptTags = deployFunc.tags || [];
if (typeof scriptTags === 'string') {
scriptTags = [scriptTags];
}
for (const tag of scriptTags) {
if (tag.indexOf(',') >= 0) {
throw new Error('Tag cannot contain commas');
}
const bag = scriptPathBags[tag] || [];
scriptPathBags[tag] = bag;
bag.push(scriptFilePath);
}
}
}

public async executeDeployScripts(
deployScriptsPaths: string[],
tags: string[] = [],
tagsRequireAll = false,
tagsRequireAll = false
): Promise<void> {
const wasWrittingToFiles = this.db.writeDeploymentsToFiles;
// TODO loop over companion networks ?
Expand All @@ -1090,51 +1156,33 @@ export class DeploymentsManager {
});
log('deploy script folder parsed');

const funcByFilePath: {[filename: string]: DeployFunction} = {};
const scriptPathBags: {[tag: string]: string[]} = {};
const scriptFilePaths: string[] = [];

for (const filepath of filepaths) {
const scriptFilePath = path.resolve(filepath);
let deployFunc: DeployFunction;
// console.log("fetching " + scriptFilePath);
try {
delete require.cache[scriptFilePath]; // ensure we reload it every time, so changes are taken in consideration
deployFunc = require(scriptFilePath);
if ((deployFunc as any).default) {
deployFunc = (deployFunc as any).default as DeployFunction;
}
funcByFilePath[scriptFilePath] = deployFunc;
} catch (e) {
// console.error("require failed", e);
throw new Error(
'ERROR processing skip func of ' +
filepath +
':\n' +
((e as any).stack || e)
);
}
// console.log("get tags if any for " + scriptFilePath);
const deployFunc = this.requireDeployScript(scriptFilePath);
let scriptTags = deployFunc.tags || [];
if (typeof scriptTags === 'string') {
scriptTags = [scriptTags];
}
for (const tag of scriptTags) {
if (tag.indexOf(',') >= 0) {
throw new Error('Tag cannot contain commas');
}
const bag = scriptPathBags[tag] || [];
scriptPathBags[tag] = bag;
bag.push(scriptFilePath);
}
// console.log("tags found " + scriptFilePath, scriptTags);
if (tagsRequireAll && tags.every(tag => scriptTags.includes(tag))
|| !tagsRequireAll && (tags.length == 0 || tags.some(tag => scriptTags.includes(tag)))) {

if (
(tagsRequireAll && tags.every((tag) => scriptTags.includes(tag))) ||
(!tagsRequireAll &&
(tags.length == 0 || tags.some((tag) => scriptTags.includes(tag))))
) {
scriptFilePaths.push(scriptFilePath);
}
}
log('tag collected');
log('tags collected');

const scriptPathBags: {[tag: string]: string[]} = {};
const funcByFilePath: {[filename: string]: DeployFunction} = {};
this.loadDeployScripts(deployScriptsPaths, scriptPathBags, funcByFilePath);

const dependenciesFuncByFilePath = this.db.dependenciesFuncByFilePath;
const dependenciesScriptPathBags = this.db.dependenciesScriptPathBags;

// console.log({ scriptFilePaths });
const scriptsRegisteredToRun: {[filename: string]: boolean} = {};
const scriptsToRun: Array<{
func: DeployFunction;
Expand All @@ -1148,10 +1196,14 @@ export class DeploymentsManager {
if (scriptsRegisteredToRun[scriptFilePath]) {
return;
}
const deployFunc = funcByFilePath[scriptFilePath];
const deployFunc =
funcByFilePath[scriptFilePath] ||
dependenciesFuncByFilePath[scriptFilePath];
if (deployFunc.dependencies) {
for (const dependency of deployFunc.dependencies) {
const scriptFilePathsToAdd = scriptPathBags[dependency];
const scriptFilePathsToAdd =
scriptPathBags[dependency] ||
dependenciesScriptPathBags[dependency];
if (scriptFilePathsToAdd) {
for (const scriptFilenameToAdd of scriptFilePathsToAdd) {
recurseDependencies(scriptFilenameToAdd);
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ extendConfig(
config.external = {};
}
if (userConfig.external.contracts) {
const externalContracts: {artifacts: string[]; deploy?: string}[] = [];
const externalContracts: {
artifacts: string[];
deploy?: string;
execute?: boolean;
}[] = [];
config.external.contracts = externalContracts;
for (const userDefinedExternalContracts of userConfig.external
.contracts) {
Expand All @@ -135,6 +139,10 @@ extendConfig(
userDefinedExternalContracts.deploy
)
: undefined,
execute:
userDefinedExternalContracts.execute !== undefined
? userDefinedExternalContracts.execute
: true,
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ declare module 'hardhat/types/config' {
contracts?: {
artifacts: string | string[];
deploy?: string;
execute?: boolean;
}[];
};
verify?: {etherscan?: {apiKey?: string}};
Expand All @@ -52,6 +53,7 @@ declare module 'hardhat/types/config' {
contracts?: {
artifacts: string[];
deploy?: string;
execute?: boolean;
}[];
};
verify: {etherscan?: {apiKey?: string}};
Expand Down