Skip to main content
85 votes
Accepted

Time-dependent tests with Hardhat?

Using Hardhat Network Helpers The easiest way to do this is to use the time helpers in Hardhat Network Helpers. Install them with: npm install @nomicfoundation/hardhat-network-helpers And then you ...
Franco Victorio's user avatar
82 votes

Hardhat - How to interact with a deployed contract?

I ran into the same question and stumbled upon a section in the OpenZeppelin docs that may work for you (it did for me): For example: const MyContract = await ethers.getContractFactory("...
julianwyz's user avatar
  • 966
52 votes
Accepted

I see no way to obtain the return value of a non-view function (ethers.js)

The return value of a non-pure non-view function is available only when the function is called and validated on-chain. When you call such function off-chain (e.g. from an ethers.js script), you need ...
goodvibration's user avatar
30 votes

Hardhat - How to interact with a deployed contract?

hardhat-ethers ^2.0.0 has a special function getContractAt for exactly this purpose: const contractAddress = "0x...", const myContract = await hre.ethers.getContractAt("MyContract",...
Chris's user avatar
  • 449
28 votes
Accepted

Listening to events using ethers.js on a hardhat test network

While I did not find a solution on why the .on() method is not working, I found a workaround using the transaction's receipt. The following code describes this workaround: let tx: ContractTransaction =...
Sebastian Dine's user avatar
27 votes

I see no way to obtain the return value of a non-view function (ethers.js)

While the return value of a function call executed on-chain cannot be returned off-chain, you can however simulate a function call on-chain to see what that function call would return. In ethers, you ...
Jonas Hals's user avatar
27 votes
Accepted

How can I test sending Ether to a contract with a `payable` receive function?

You can make a transaction and send ether to the contract address const [owner] = await ethers.getSigners(); const transactionHash = await owner.sendTransaction({ to: "contract address", ...
trizin's user avatar
  • 924
27 votes
Accepted

Run a single hardhat test

Use .only(). For example, your test file would look like this: const { expect } = require("chai"); describe.only("contract tests", function () { it("does function one",...
Julissa DC's user avatar
  • 1,916
25 votes
Accepted

How to interact with the functions of my smart contract in Hardhat?

Basically, you need to deploy the contract to your local hardhat net just as you would to any other. That means currently you have to set up a deploy.js like in the official hardhat tutorial. I'm ...
ctholho's user avatar
  • 366
23 votes
Accepted

Where does the line blur between a task and a script in Hardhat?

The first thing to know is that almost everything you can do with a task can be done with a script, and vice versa. But there are things that can only be done with tasks or only with scripts, and ...
Franco Victorio's user avatar
22 votes

Testing for emitted events in Hardhat

This example shows how to work with events in Hardhat in general, and it includes a test that illustrates some scenarios. The tl;dr is that, given a contract like this one: contract EventEmitter { ...
Franco Victorio's user avatar
22 votes

Testing for custom error reverts in hardhat

Update to selected answer Custom error hardhat will check for the name of the error, not the contents of the message MyContract.sol error SomeCustomError(string message); contract MyContract { ...
8koi's user avatar
  • 331
21 votes
Accepted

How to get chainId / network name / network id etc in hardhat?

There are a few ways to get the network/chain name/id. We are going to assume you are in a script that you'd like to run, or a test. When running these, you must have a hardhat.config.ts or hardhat....
Patrick Collins's user avatar
20 votes
Accepted

Following the Hardhat tutorial, I get this error: TypeError: ethers.getSigners is not a function

Two things happened: require("@nomiclabs/hardhat-waffle"); was missing in hardhat.config.js (as mentionned at the end of https://hardhat.org/tutorial/creating-a-new-hardhat-project.html) ...
Stéphane Gerber's user avatar
19 votes
Accepted

ethers is not defined

I have been having the same issue. First make sure your hardhat.config.js file has this line: require("@nomiclabs/hardhat-waffle"); I fixed it by adding this to the top of the test file: ...
sabeansauce's user avatar
19 votes

How to get contract's ether balance at hardhat / waffle

const { ethers, waffle} = require("hardhat"); const provider = waffle.provider; const balance0ETH = await provider.getBalance(user1.address);
Russo's user avatar
  • 1,784
18 votes

Received invalid block tag 87. Latest block number is 0

Try changing the network in Metamask and then switching back to the network you are using to connect to the hardhat node (probably the Localhost 8545 network). There seems to be some sort of cache ...
Franco Victorio's user avatar
18 votes
Accepted

Hardhat get last block timestamp, block number, block hash, nonce, gas limit, gas used

If you are using @nomiclabs/hardhat-ethers (and you are if you have @nomiclabs/hardhat-waffle installed and imported in your config), then: const latestBlock = await hre.ethers.provider.getBlock("...
Franco Victorio's user avatar
17 votes
Accepted

from solidity: TypeError: Member "log" not found or not visible after argument-dependent lookup in type(library console)

You have this error, because console.sol doesn't accept struct type inside console.log method. You can see what types accept console.log in original smart contract code (here).
Antonio Carito's user avatar
16 votes
Accepted

Testing Error thrown with hardhat

When I test a function for revert I do the following in my javascript test file (e.g. myContractTest.js): const { expect } = require('chai'); const { ethers } = require('hardhat'); const { BigNumber } ...
Majd TL's user avatar
  • 3,237
16 votes

HardhatError: HH700: Artifact for contract "Greeter" not found - clarification [Hardhat, openzeppelin]

Keep in mind that your solidity contract's name has to match, too. If you try to deploy with const License = await ethers.getContractFactory('License'); this works contract License {...} this doesn'...
r.beer's user avatar
  • 161
15 votes

Listening to events using ethers.js on a hardhat test network

As far as I can tell it isn't possible to actually use the arguments which are emitted, however you can test for them being emitted using the following chai code example: await expect(token.transfer(...
nakulthebuilder's user avatar
15 votes

How to get contract's ether balance at hardhat / waffle

Maybe you can try: await contract.provider.getBalance(contract.address) contract is a 'Contract' class in @ethersproject.
amber's user avatar
  • 151
14 votes
Accepted

Cant validate authenticated message with ethers.js

I was able to get an answer from folks over on Openzeppelin. In case any body else runs into the situation, one issue is the way that the hash is calculated and the signature is computed on the client ...
GGizmos's user avatar
  • 741
14 votes

Run a single hardhat test

You'll be glad to know as of hardhat 2.9, the --grep parameter has been added to the test task! This adds the ability to pass in a regular expression through to mocha! This is cool because you can ...
Namaskar's user avatar
  • 249
14 votes
Accepted

How to mimic abi.encodePacked in ethers?

I found your solution on the ether.js doc. It has some functions that would help : ethers.utils.solidityPack( types,values) ethers.utils.solidityKeccak256(types,values) ethers.utils.soliditySha256(...
Munkh-Od Ganzorigt's user avatar
14 votes
Accepted

Hardhat vs foundry, which to use for testing?

When it comes to testing, Foundry excels compare to Hardhat, the speed difference is incredible. In my test with 1k queries and transactions on-chain the difference is from 15 min with hardhat to 1 ...
donoso.eth's user avatar
  • 1,493
13 votes

Error: missing argument: in Contract constructor

My Solution This problem occurred with Hardhat. The number of arguments in the contract constructor needed to match the number of arguments in the .deploy() method of the deployment script. Correct ...
Chiara Wilden's user avatar
13 votes
Accepted

Testing for custom error reverts in hardhat

Try moving the await before the expect. e.g. await expect(nftMarketplace.listItem(basicNft.address, TOKEN_ID, PRICE)).to.be.revertedWith(`AlreadyListed`) That's what my tests use for custom errors ...
helpful hint's user avatar
12 votes
Accepted

ProviderError: max code size exceeded

How can I adjust the code size or any other method to resolve this error. The contract code size limit is 24kB (24,576 bytes). You'll need to reduce the size of the contract before deploying, or ...
Richard Horrocks's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible