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 :(