4

I follow the simple tutorial by hardhat here https://hardhat.org/hardhat-runner/docs/other-guides/waffle-testing by creating Greeter.sol and test/test.js. This leads to the following error:

  1) Greeter
       Should return the new greeting once it's changed:
     TypeError: (0 , ethers_1.getAddress) is not a function
      at new HardhatEthersSigner (node_modules/@nomicfoundation/hardhat-ethers/src/signers.ts:73:30)
      at Function.create (node_modules/@nomicfoundation/hardhat-ethers/src/signers.ts:65:12)
      at async getSigner (node_modules/@nomicfoundation/hardhat-ethers/src/internal/helpers.ts:60:29)
      at async Promise.all (index 0)
      at async getSigners (node_modules/@nomicfoundation/hardhat-ethers/src/internal/helpers.ts:45:30)
      at async getContractFactoryByAbiAndBytecode (node_modules/@nomicfoundation/hardhat-ethers/src/internal/helpers.ts:305:21)
      at async Context.<anonymous> (test/test.js:5:21)

The error is for this line:

const Greeter = await ethers.getContractFactory("Greeter");

It appears that the problem is related to getSigners. If I simply write

const [wallet1, wallet2] = ethers.getSigners();

I got the exactly same error.

Here are the code:

Greeter.sol

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract Greeter {
    string private greeting;

    constructor(string memory _greeting) {
        console.log("Deploying a Greeter with greeting:", _greeting);
        greeting = _greeting;
    }

    function greet() public view returns (string memory) {
        return greeting;
    }

    function setGreeting(string memory _greeting) public {
        console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
        greeting = _greeting;
    }
}

test/test.js

const { expect } = require("chai");

describe("Greeter", function () {
  it("Should return the new greeting once it's changed", async function () {
    const Greeter = await ethers.getContractFactory("Greeter");
    const greeter = await Greeter.deploy("Hello, world!");

    expect(await greeter.greet()).to.equal("Hello, world!");

    const setGreetingTx = await greeter.setGreeting("Hola, mundo!");

    // wait until the transaction is mined
    await setGreetingTx.wait();

    expect(await greeter.greet()).to.equal("Hola, mundo!");
  });
});

Here are the dependencies in my package.json

  "dependencies": {
    "@ethersproject/hdnode": "^5.7.0",
    "@nomicfoundation/hardhat-chai-matchers": "^1.0.0",
    "@nomicfoundation/hardhat-ethers": "^3.0.0",
    "@nomicfoundation/hardhat-network-helpers": "^1.0.0",
    "@nomicfoundation/hardhat-toolbox": "^3.0.0",
    "@nomicfoundation/hardhat-verify": "^1.0.0",
    "@nomiclabs/hardhat-ethers": "^2.0.0",
    "@nomiclabs/hardhat-etherscan": "^3.0.0",
    "@openzeppelin/contracts": "^4.9.1",
    "@typechain/ethers-v6": "^0.4.0",
    "@typechain/hardhat": "^8.0.0",
    "@types/mocha": ">=9.1.0",
    "@types/node": "^20.2.5",
    "circomlib": "^2.0.5",
    "dotenv": "^16.1.4",
    "hardhat": "^2.14.1",
    "hardhat-circom": "^3.3.2",
    "hardhat-gas-reporter": "^1.0.8",
    "js-sha3": "^0.8.0",
    "snarkjs": "^0.7.0",
    "solidity-coverage": "^0.8.1",
    "truffle-hdwallet-provider": "^1.0.17",
    "typechain": "^8.2.0",
    "typescript": "^5.1.3"
  },
  "devDependencies": {
    "ts-node": "^10.9.1"
  }
}

I'm stuck on this, any help will be appreciated, thanks!

--

Update:

Now it seems like I can circumvent this issue by providing real signers to the ethers.getContractFactory function. Like:

const hhprovider = new ethers.providers.JsonRpcProvider("HTTP://127.0.0.1:7545");
const signer = new ethers.Wallet(wallet._privKey.toString('Hex'), hhprovider);
const Greeter = await ethers.getContractFactory("Greeter", signer);
const greeter = await Greeter.deploy("Hello, world!");

Where the wallet is an actual wallet create from mnemonics on my local Ganache chain. Of cause this turns this into an integration test instead of an unit test. But that seems to the only thing that works for me at this point :(

4
  • That is working for me. Your dependencies are different from mine. I believe you might be importing Ether in two different ways, which could be causing the problem. Is that all of your code?
    – xana
    Jun 10, 2023 at 1:23
  • @xana these are not all the code, but these should be independent from the rest. The error was initially spotted on the rest of the code. Later I found that it is reproducible on this simple example so I only posted this example. Wonder if you mind posting your dependencies so that I can try them? Jun 10, 2023 at 17:47
  • I tried removed the three lines containing "ethers" in package.json and then "npx hardhat test" errors and asked me to add all of them back. One interesting thing is that for the "@nomiclabs/hardhat-ethers@^2.0.0" package. If I run "yarn add ..." then it errors with the message "error [email protected]: The platform "darwin" is incompatible with this module.". If I add it directly to package.json and run yarn, it can proceed to install it. Thanks! Jun 10, 2023 at 17:49
  • Please check Szymon Ligas answer.
    – xana
    Jun 11, 2023 at 11:00

4 Answers 4

9

Nomic foundation released a new version of hardhat (2.15.0)

Try:

npm install -g yarn

yarn add --dev hardhat @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers

which will force update the ethers package.

Then replace any instance of .address with .getAddress() and .deployed with .waitForDeployment()

For migrating from v5 to v6 you can use this docs by ethers

Let me know if this helped!

4
  • Thanks! that does solve the problem. It however brings some new problems, that "BigNumber" and "utils.randomBytes" are no longer exported from ethers and it breaks my code. Wonder where can I see the announcement for such changes? And what shall we use now in place of BigNumber? Jun 11, 2023 at 18:30
  • providers.JsonRpcProvider is also gone, LOL Jun 11, 2023 at 18:44
  • Found them in ethers.randomBytes, ethers.JsonRpcProvider. Jun 11, 2023 at 18:55
  • Same here, but when i entered this command and tried again, this error shows: Error: Package subpath './lib/utils' is not defined by "exports" in
    – Alireza
    Jun 21, 2023 at 11:22
1

If you don't want to use yarn, use npm, did the job for me today.

npm install --save-dev hardhat @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers

0

Sometimes the issue is with the ethers version.

ethers V5: ethers.providers.*

ethers V6: ethers.*

ethers migration

0

After installing the dependencies as suggested

npm install --save-dev hardhat @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers

I ran into some other bugs First you to install @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers, you will need to specify the version

yarn add -D @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers@latest

Lastly to deploy You need to import hre from hardhat

import hre from "hardhat";
await ethers.deployContract("Greeter", args)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.