6

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?

2 Answers2

9

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.

Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143
2

If the contracts in your node_modules already contain compiled outputs (i.e., abi and bytecode), then you can set those compiled output files as variables in your test file and pass the abi and bytecode as parameters when setting up the contract object through ethers getContractFactory method.

As an example, I've done this when working with the npm package @ensdomains/ens-contracts:

const { expect } = require('chai');
const { ethers } = require('hardhat');
const ensRegistryCompiled = requires('@ensdomains/ens-contracts/artifacts/contracts/registry/ENSRegistry.sol/ENSRegistry.json');

describe('test', () => {

before(async () => { [deployer] = await ethers.getSigners();

const ENSRegistryFactory = await ethers.getContractFactory(ensRegistryCompiled.abi, ensRegistryCompiled.bytecode); const ENSRegistry = await ENSRegistryFactory.deploy(); await ENSRegistry.deployed();

/// rest of your test file

bold
  • 21
  • 1