Hardhat – How to Compile External Contracts

hardhat

I want to use a contract in node_modules in my tests, but I'm getting:
HardhatError: HH700: Artifact for contract "SomeContract" not found.

This is probably because it's not used by any files under /contracts directory, hence not generating the artifact. Is there a way to compile these contracts inside node_module?

Best Answer

There are two solutions.

1. Imports Contract

This is what I'm doing in my personal contracts library

// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.4;

import "@prb/math/contracts/PRBMathUD60x18.sol";

As you can see, all this is doing is importing the external contract from an npm package, such that Hardhat will pick it up and generate an artifact for it.

2. Use hardhat-dependency-compiler

It's a Hardhat plugin developed by Nick Barry. Install it:

yarn add --dev hardhat-dependency-compiler

And add the following to your Hardhat config file:

dependencyCompiler: {
  paths: [
    'path/to/external/Contract.sol',
  ],
}

For what it's worth, I prefer the first solution. It's less verbose in that I don't add yet-another-node-package to my package.json file.