Questions tagged [hardhat]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
66 votes
14 answers
62k views

Hardhat - How to interact with a deployed contract?

I can deploy a contract using ethers and Hardhat with the following: const myContract = await hre.ethers.getContractFactory("SomeContract"); const deployedContract = await myContract.deploy()...
JohnWLIV's user avatar
  • 763
54 votes
6 answers
39k views

Time-dependent tests with Hardhat?

For Ganache, there are several solutions. What about Hardhat? They implemented their own local blockchain, Hardhat Network, which is different to Ganache.
Paul Razvan Berg's user avatar
42 votes
3 answers
21k views

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

In a Buidler/Hardhat test I have: const { expect } = require("chai"); describe("SumOfTokens", function() { it("Checks correct transfers", async function() { const ...
porton's user avatar
  • 1,774
31 votes
5 answers
19k views

Run a single hardhat test

I have a test file in hardhat like so: const { expect } = require("chai"); describe("contract tests", function () { it("does function one", async function () { ...
Patrick Collins's user avatar
24 votes
3 answers
23k views

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

I am currently migrating a truffle project to hardhat. I am facing a problem with listening to events. I have a simple ERC20 token and want to listen to the Transfer event. I am running the contract ...
Sebastian Dine's user avatar
24 votes
1 answer
3k views

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

This is a question about Hardhat, the task runner that facilitates building on Ethereum. From Writing scripts with Hardhat: You can write your custom scripts that can use all of Hardhat's ...
Paul Razvan Berg's user avatar
21 votes
9 answers
30k views

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

I writing hardhat/ethers/waffle unit tests for my contract and need to know Ether balance of my contract. Chai matcher changeEtherBalance needs Signer object to check balance, but I have address of my ...
Bogdoslavik's user avatar
17 votes
3 answers
16k views

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

I'm looking to get the ID of the chain/network I'm running a script on in hardhat. How can I get the names and IDs?
Patrick Collins's user avatar
15 votes
3 answers
7k views

Testing for custom error reverts in hardhat

I have a custom error described as such: error AlreadyListed(address nftAddress, uint256 tokenId); And in my tests, I'd like to check to see that it's thrown: expect(await nftMarketplace.listItem(...
Patrick Collins's user avatar
15 votes
5 answers
4k views

Hardhat vs foundry, which to use for testing?

I'm looking into smart contract testing and saw that Hardhat and Foundry are the main kits developers are using for testing. Are the two comparable when it comes to testing? Should I include both ...
Nal Luksic's user avatar
  • 1,127
14 votes
3 answers
13k views

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

I've written Hardhat tests for a Solidity contract that contains a receive() external payable { ... } function. How can I call it within the test with an Ether amount?
vadersfather's user avatar
14 votes
5 answers
15k views

Testing for emitted events in Hardhat

I have a Solidity contract that inherits from ERC721PresetMinterPauserAutoId I want to write a test that verifies that a Transfer event has been emitted by the contract after the public mint(to) ...
imaginative's user avatar
14 votes
5 answers
32k views

Why is an 'UNPREDICTABLE_GAS_LIMIT' error thrown when calling a view function created from a public variable?

I am using the Hardhat console to test my smart contract, which worked flawlessly until I removed the getter functions I wrote for my public state variables in favor of the automatically generated ...
josephawallace's user avatar
13 votes
3 answers
10k views

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

How to get last block information in a Hardhat Waffle test? I checked here https://hardhat.org/hardhat-network/#json-rpc-methods-support And here https://hardhat.org/hardhat-network/reference/ it ...
Russo's user avatar
  • 1,784
13 votes
1 answer
19k views

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

I am learning to program in Solidity and have followed a basic tutorial on creating a smart contract from a number counter. The tutorial was followed by using Remix and I was very clear about the ...
Diego Aaron's user avatar
12 votes
3 answers
8k views

Find address of a contract before deployment in Hardhat and Ethers.js

I have 2 contracts which take each other's addresses as constructor arguments. Here's the pseudocode, constructor A(address B) constructor B(address A) I am deploying contracts using const Contract = ...
secretshardul's user avatar
12 votes
8 answers
15k views

ProviderError: Error: Transaction reverted: function selector was not recognized and there's no fallback function

I'm trying to call a function[propose] from Openzeppelin governor.sol. The function is like this... function propose( address[] memory targets, uint256[] memory values, bytes[] ...
Muhammed Adil's user avatar
12 votes
4 answers
6k views

hardhat mainnet forking and impersonating an account isnt working, help!

So I'm trying to fork the mainnet using hardhat, impersonate a Dai whale, and transfer its Dai to a hardhat wallet for testing a smart contract, but I'm running into problems. My code looks like this: ...
Nolan Jannotta's user avatar
11 votes
5 answers
17k views

Error: Transaction reverted: function call to a non-contract account

I've been trying to debug this for the past 2 days, it has driven me insane. I'm getting this error when testing my contract: Error: Transaction reverted: function call to a non-contract account. It's ...
Colin Steidtmann's user avatar
11 votes
4 answers
6k views

Received invalid block tag 87. Latest block number is 0

I ran a hardhat node just fine using npx hardhat node, but after I shut it down and restarted it, whenever I try to deploy something on the chain, I get the following error: eth_getTransactionCount ...
zendevil.eth's user avatar
11 votes
3 answers
8k views

Hardhat Test returns transaction instead of return value

I just came from truffle to hardhat. Like the title says, I have a return value (address) of a contract and getting the transaction back. Any idea what I can do about that? it("Check all tested&...
MaTok's user avatar
  • 321
11 votes
1 answer
13k views

Testing Error thrown with hardhat

How would you test the following function with solidity v0.7 and hardhat v2.3.3? function myFunction(int8 _num) public { require(_num > 5, "Num should be bigger than 5"); ....
ntonnelier's user avatar
11 votes
4 answers
9k views

How to console log output in hardhat test?

I'm using running hardhat tests. When I add console logs to the test cases, nothing actually gets outputted to console. Why's that?
doctopus's user avatar
  • 315
11 votes
1 answer
4k views

Hardhat: Error: unresolved libraries, but why?

I'm attempting to deploy a contract using Hardhat (via scaffold-eth), but am getting the following error: Error: Factory contains unresolved libraries. You must deploy and link the following libraries ...
The Renaissance's user avatar
11 votes
9 answers
12k views

Invalid value undefined for HardhatConfig.networks.mainnet_fork.url - Expected a value of type string

I have an ALCHEMY_MAINNET_RPC_URL environment that is of type string. I can verify it as such with: const ALCHEMY_MAINNET_RPC_URL = process.env.ALCHEMY_MAINNET_RPC_URL || '' console.log(typeof (...
Patrick Collins's user avatar
10 votes
5 answers
13k views

How do I use BigNumber values in Hardhat tests?

I'm using Hardhat with TypeScript/Ethers/Chai. I can't find a way to test BigNumber values. For example, expect(someBigNumber).to.be.equal.to.(otherBigNumber) will (understandably) throw an error like ...
frostpad's user avatar
  • 201
10 votes
1 answer
8k views

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

from solidity: TypeError: Member "log" not found or not visible after argument-dependent lookup in type(library console). Im using hardhats lib "hardhat/console.sol"; I want to ...
Syed Murtaza Ali Shah's user avatar
10 votes
3 answers
12k views

How to fix "TypeError: Cannot read property 'sendTransaction' of null" when deploying a contract on Ropsten with hardHat?

I am getting this error: TypeError: Cannot read property 'sendTransaction' of null when running this command: $ npx hardhat run scripts/deploy.js --network ropsten with HardHat on Ropsten network. ...
Andrea D_'s user avatar
  • 231
9 votes
10 answers
14k views

Hardhat console can't be resolved in VSCode

I created a new Hardhat project and used the advanced TypeScript example. I also have a VSCode installation with the Solidity extension running. //SPDX-License-Identifier: Unlicense pragma solidity ^0....
K..'s user avatar
  • 191
9 votes
3 answers
13k views

ProviderError: max code size exceeded

I am trying to deploy an upgradeable smart contract on bsc testnet using hardhat. But after running npx hardhat run --network testnet scripts/deploy.js it gives me the following error. ProviderError: ...
mzaidi's user avatar
  • 992
9 votes
6 answers
3k views

Error: Hydration failed because the initial UI does not match what was rendered on the server, Wagmi and Nextjs

I was trying to implement wagmi in my dapp, after I copied the example in the documentation https://wagmi.sh/examples/connect-wallet, I got this error: Error: Hydration failed because the initial UI ...
Paulliano's user avatar
  • 209
9 votes
4 answers
13k views

How to get ABI of deployed contract using ethersjs in hardhat?

I have to update my front-end file with deployed latest ABI in json format. I am using following code in my deploy script (Run after the original smart contract is deployed) import { DeployFunction } ...
SanjayDefi's user avatar
9 votes
3 answers
17k views

An unexpected error occurred: Error: Cannot find module '@nomiclabs/hardhat-waffle' [Hardhat, openzeppelin]

I keep getting this error when doing: npx hardhat run scripts/sample-script.js --network testnet. Edited the name of my computer for security reasons. How do I put in the module 'nomiclabs/hardhat-...
Feynman's user avatar
  • 231
8 votes
5 answers
17k views

ethers is not defined

In hardhat, I want to access a contract outside my project in my tests. In my hardhat.config.js I am able to reference external contracts no problem with an ABI and a contract address, like so: const ...
Patrick Collins's user avatar
8 votes
9 answers
18k views

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

I keep getting this error when doing: npx hardhat run scripts/sample-script.js --network testnet. I got an answer "You have named your contract 'Nikola' and not 'Greeter'. The actual file-names ...
Feynman's user avatar
  • 231
8 votes
5 answers
6k views

Is it possible to log how much gas was used during hardhat/ethers transaction?

I am trying to estimate average gas cost per 10000 transactions i.e. const callMany = async () => { for (let i = 0; i <= 10000; i++) { const tx = await contract.doThing(1, `Val1`, { ...
Ilja's user avatar
  • 426
8 votes
3 answers
6k views

How to get more than 20 Signers with ethers.js / hardhat / waffles

I'm using Hardhat with the waffle plugin to test my smart contract. To get Signers (ether.js) I'm using : await ethers.getSigners(); This return an array with 20 signers. If I call again, I get the ...
Kaizoku Gambare's user avatar
8 votes
2 answers
9k views

Error: missing argument: in Contract constructor

Documenting a solution to this error I received while working on a Chainlink contract: Error Message Error: missing argument: in Contract constructor (count=1, expectedCount=2, code=MISSING_ARGUMENT, ...
Chiara Wilden's user avatar
8 votes
2 answers
3k views

What is the difference between getSigners and getNamedAccounts?

I am finding it difficult to understand difference between these two.
amit's user avatar
  • 390
8 votes
4 answers
10k views

Warning: Contract code size exceeds 24576 bytes

I'm using hardhat and when trying to compile the code it shows me a warning containing @uniswap/v2-periphery/contracts/UniswapV2Router02.sol:12:1: Warning: Contract code size exceeds 24576 bytes (a ...
Yaya's user avatar
  • 109
8 votes
2 answers
6k views

Hardhat, ether.js - fetching balance of signers locally shows no ether

I am trying to fetch the balance of signers in a test locally. I didn't change the default value of eth for accounts in the hardhat configuration. Yet when I query the balance, I see that the accounts ...
td-bn's user avatar
  • 371
8 votes
1 answer
908 views

hardhat remappings option in configs

I know that solc can take remappings but I'm trying to force hardhat to take in my remappings.txt file each time it is compile. I've looked for a while and can't seem to find anything about that on ...
karma badger's user avatar
7 votes
2 answers
3k views

How to get implementation address after deployProxy()? (@open-zepplin/hardhat-upgrades)

I'm deploying my token using the proxy pattern via hardhat-upgrades. const TokenFactory = await ethers.getContractFactory('MyToken'); const token = await upgrades.deployProxy(TokenFactory); const ...
parliament's user avatar
7 votes
2 answers
7k views

Cannot override Hardhat Timeout of 40000ms

Issue: I cannot override the default timeout of 40000ms when testing using rinkeby network. I'm getting the error of Timeout of 40000ms exceeded. (Error: Timeout of 40000ms exceeded. For async tests ...
Hendry Lim's user avatar
7 votes
4 answers
20k views

How can I connect a local Hardhat network to Metamask?

I have started a Hardhat network with default configuration using npx hardhat node. After running this command, I see Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/ and a list of ...
vaer-k's user avatar
  • 231
7 votes
2 answers
3k views

How to test address(0) with Hard Hat?

This is my test, which fails and it should pass it('Should not allow 0 address to be added', async () => { await expect(greeter.addWhitelistAddress('0x0')).to.be.revertedWith( 'Invalid ...
Bill's user avatar
  • 449
7 votes
1 answer
9k views

How do I send ETH from an account to another in Hardhat?

I am trying those methods (both) and neither worked for me: signer.sendTransaction({ to, value }); (and) provider.send("eth_sendTransaction", [{ from, to, value }]); Would be great if your ...
Ahmed Ihsan Tawfeeq's user avatar
7 votes
3 answers
7k views

Modify block number when testing with Hardhat

I see that is it is possible to define the timestamp of the next block (see here) in this way: await network.provider.send("evm_setNextBlockTimestamp", [1625097600]); await network.provider....
Alvin Sartor's user avatar
7 votes
1 answer
1k views

What is the % Branch column for in solidity-coverage?

In the solidity-coverage package output, there is a column called % Branch, what is it for? yarn hardhat coverage Output: ---------------------------|----------|----------|----------|----------|------...
Patrick Collins's user avatar
7 votes
2 answers
8k views

How to create a signer object from an address in ethers?

I have a list of addresses, and I would like to use them to test my contract in my hardhat ethers project. Normally I get a list of addresses like this: [a, b, c] = await ethers.getSigners() but what ...
dd0sxx's user avatar
  • 185

1
2 3 4 5
28