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

fix: fqn resolution of artifacts #822

Merged
merged 1 commit into from
Oct 11, 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
21 changes: 21 additions & 0 deletions packages/hardhat-plugin/src/hardhat-artifact-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ export class HardhatArtifactResolver implements ArtifactResolver {
public async getBuildInfo(
contractName: string
): Promise<BuildInfo | undefined> {
// If a fully qualified name is used, we can can
// leverage the artifact manager directly to load the build
// info.
if (this._isFullyQualifiedName(contractName)) {
return this._hre.artifacts.getBuildInfo(contractName);
}

// Otherwise we have only the contract name, and need to
// resolve the artifact for the contract ourselves.
// We can build on the assumption that the contract name
// is unique based on Module validation.
const artifactPath = await this._resolvePath(contractName);

if (artifactPath === undefined) {
Expand Down Expand Up @@ -51,4 +62,14 @@ export class HardhatArtifactResolver implements ArtifactResolver {
public loadArtifact(contractName: string): Promise<Artifact> {
return this._hre.artifacts.readArtifact(contractName);
}

/**
* Returns true if a name is fully qualified, and not just a bare contract name.
*
* This is based on Hardhat's own test for fully qualified names, taken
* from `contract-names.ts` in `hardhat-core` utils.
*/
private _isFullyQualifiedName(contractName: string): boolean {
return contractName.includes(":");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

// This contract name is the same as in `./Rocket2.sol`
contract Rocket {
string public name;

constructor(string memory _name) {
name = _name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

// This contract name is the same as in `./Rocket1.sol`
contract Rocket {
string public name;

constructor(string memory _name) {
name = _name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require("../../../src/index");

module.exports = {
solidity: "0.8.19",
};
39 changes: 39 additions & 0 deletions packages/hardhat-plugin/test/module-api/fully-qualified-names.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { buildModule } from "@nomicfoundation/ignition-core";
import { assert } from "chai";

import { useFileIgnitionProject } from "../test-helpers/use-ignition-project";

describe("fully qualified names", () => {
describe("where there are multiple contracts with the same name in the project", () => {
useFileIgnitionProject(
"multiple-contracts-with-same-name",
"contract-deploy"
);

it("should deploy contracts by referring using fully qualified names", async function () {
const LaunchModule = buildModule("Apollo", (m) => {
const rocket1 = m.contract(
"contracts/Rocket1.sol:Rocket",
["Rocket 1"],
{
id: "Rocket1",
}
);
const rocket2 = m.contract(
"contracts/Rocket2.sol:Rocket",
["Rocket 2"],
{
id: "Rocket2",
}
);

return { rocket1, rocket2 };
});

const result = await this.hre.ignition.deploy(LaunchModule);

assert.equal(await result.rocket1.read.name(), "Rocket 1");
assert.equal(await result.rocket2.read.name(), "Rocket 2");
});
});
});
Loading