hardhat icon indicating copy to clipboard operation
hardhat copied to clipboard

Compile contracts outside of `/src` or `/contracts` directories

Open RohanNero opened this issue 5 months ago • 0 comments

Describe the feature

I am using hardhat-viem to generate type definitions for my smart contracts every time they are compiled, however, it only works for contracts inside of my /src directory (/contracts).

Since I have integrated Hardhat into my Foundry project, I have smart contracts inside my /script and /test directories as well. Specifically, I'd like to import one of my deploy scripts for testing. It's highly likely that I will create hardhat deploy scripts written in Javascript/Typescript to accompany the hardhat tests, but currently, since all of my deploy scripts are written in Solidity, it would be extremely convenient if I could directly use them in my hardhat Typescript tests.

I know a similar issue was also considered in 2020 and was given this code snippet as a part of a possible solution:

subtask(
  TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS,
  async (_, { config }) => {
    const mainContracts = await glob(path.join(config.paths.root, "contracts/**/*.sol"));
    const testContracts = await glob(path.join(config.paths.root, "test/**/*.sol"));
    // and so on

    return [
      ...mainContracts,
      ...testContracts,
      // and so on
    ].map(path.normalize); // not sure if normalize is needed here
  }
);

However, since it is becoming more common for developers to use both Foundry and Hardhat in projects, it might also become more common for users to encounter this issue, see this StackExchange post as an example. For this reason, I think it would be beneficial to include support for this natively.

Search terms

compile artifacts types viem

RohanNero avatar Sep 18 '24 18:09 RohanNero