Skip to main content

Questions tagged [hardhat]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
54 votes
6 answers
40k 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
43 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,804
4 votes
3 answers
4k views

Hardhat deploy TypeError: ethers.getContract is not a function

I'd look for an alternative but not being able to use the ethers module is going to drive me nuts down the road. The code was copied from Patrick's Solidity Javascript course, the NFT section, Lesson ...
Domini's user avatar
  • 181
9 votes
3 answers
14k 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
  • 1,012
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
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
  • 241
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
4 votes
2 answers
4k views

How to mock smart contract function for testing (in hardhat)

In my Test I want to mock an external function that I call inside my smart contract to test both conditions without writing and deploying an extra fake smart contract. My Contract //SPDX-License-...
Majd TL's user avatar
  • 3,237
4 votes
4 answers
7k views

Followed hardhat test tutorial and got "TypeError: (0 , ethers_1.getAddress) is not a function" error

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) ...
Justin Zhang's user avatar
4 votes
1 answer
577 views

Hardhat chain fork on the test rather than config file

I saw that it is possible to test with Hardhat on a forked chain, via the config file. Is it possible to do it on the test file instead? In a beforeEach, maybe. I only want some of my tests to run on ...
Alvin Sartor's user avatar
3 votes
2 answers
4k views

How to get deployed contract address with ethers?

I using hardhat deploy (TypeScript) and am deploying two contracts: the first is a mock ERC20 token and a second is a crowdfunding contract that uses the mock when on a hardhat chain/localhost. I am ...
Kate Johnson's user avatar
2 votes
1 answer
3k views

Stuck trying to verify a smart contract through hardhat

I am trying to verify on hardhat an nft-tutorial by chainlink but am unable to. I have converted most of the code to hardhat compatible. The contract address on Rinkeby is ...
HallonDev's user avatar
2 votes
0 answers
186 views

Hardhat public network fork: Error calling view functions with ethers.js on browser

I have my hardhat network configured as a fork of Binance Smart Chain like this: const config: HardhatUserConfig = { solidity: { version: "0.8.0", settings: { optimizer: { ...
Alexander Herranz's user avatar
0 votes
1 answer
483 views

'ethers' is not defined no-undef

I am making a very basic React app, and I'm following this tutorial. Ethers has been updated since, however. Here is my code for App.js: import { useEffect } from 'react'; import { useState } from '...
That_Coder's user avatar
0 votes
0 answers
596 views

Arithmetic Overflow Error

We're forking and updating sushiswap contracts. Right now, I'm trying to correct an overflow arithmetic error in the Masterchef Contract. We have tried using an uncheck{...} in the following function ...
Kaleb Amarante'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
15 votes
3 answers
8k 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
12 votes
3 answers
9k 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
11 votes
5 answers
7k 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
8 votes
4 answers
11k 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
6 votes
3 answers
12k views

How to measure gas usage for each function call in Hardhat?

In Remix IDE, it's straightforward to deploy a smart contract and then execute a function call to see the debug log for gas usage. How may we see the gas consumption for function calls in hardhat? ...
Tharindu Madushanka's user avatar
6 votes
4 answers
3k views

Property 'revertedWith' does not exist on type 'Assertion'

I'm running a test using waffle in hardhat. import { assert, expect } from "chai" . . . it("can only be changed through governance", async () => { await expect(box.store(...
Patrick Collins's user avatar
5 votes
1 answer
5k views

Cant validate authenticated message with ethers.js

I need to be able to retrive a document from the contract, have it signed by a users wallet and then send back to the contract and the signature validated. Here's how I have address0 sign it on the ...
GGizmos's user avatar
  • 741
4 votes
2 answers
4k views

Wait X block confirmations ethers contract deployment

Right now, I can deploy a contract with ethers and hardhat like so: const SimpleStorageFactory = await ethers.getContractFactory("SimpleStorage") const simpleStorage = await ...
Patrick Collins's user avatar
4 votes
1 answer
304 views

If `provider` is essential in communicating with the blockchain, how is this following code working?

Here is the smart contract code: // SPDX-License-Identifier: MIT pragma solidity 0.8.19; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; interface Bucket { function drop(...
amit's user avatar
  • 390
3 votes
2 answers
2k views

How to find out the cost of deploying a contract in HardHat

I deploy the contract to the Rinkeby. Can I calculate the contract deployment cost from HardHat?
MetaverseM4ster's user avatar
3 votes
1 answer
8k views

Invalid ENS name when running hardhat test

Am testing my smart contract using Hardhat, Waffle(beginner) and I can't understand why am getting Invalid ENS name error. 'Should only allow tokencreator to change token creator: AssertionError: ...
HallonDev's user avatar
3 votes
3 answers
2k views

Hardhat: How to send multiple transactions to be mined in one block

How to send multiple transactions which will be mined in one block in Hardhat tests?
misterio's user avatar
3 votes
2 answers
7k views

How to get private key from hardhat ethers signer?

In hardhat I'd like to get the privateKey from a signer, the privateKeys are initialised in the hardhat.config networks.network.accounts. import { ethers } from "hardhat"; import { Signer } ...
MShakeG's user avatar
  • 1,733
3 votes
1 answer
3k views

Cannot read properties of undefined (reading 'gteHardfork') when deploying contract using hardhat

I'm following this tutorial - https://www.freecodecamp.org/news/solidity-tutorial-hardhat-nfts/ but when running npx hardhat test or npx hardhat run scripts/deploy.js I'm getting the following ...
GiorgiSh's user avatar
  • 129
3 votes
2 answers
5k views

Error: could not detect network

I am currently trying to deploy my DApp 'nftmarketplace' to the net using Vercel on the Goerli test network. However the app is failing to connect to the blockchain and I am getting the following ...
Beau Chaseling's user avatar
3 votes
4 answers
3k views

TypeError: no matching function (argument="key", value="deployed", code=INVALID_ARGUMENT, version=6.5.1)

When trying to deploy my smart contract, I get this error message: TypeError: no matching function (argument="key", value="deployed", code=INVALID_ARGUMENT, version=6.5.1) at main ...
Adam Schroeder's user avatar
2 votes
1 answer
303 views

TypeError: Cannot read properties of undefined (reading 'checkUpkeep')

i'm getting this error while testing patrick collins smart-contract-lottery project, below is the part where the error coming from. describe("checkUpkeep", async function () { ...
Gautam's user avatar
  • 33
2 votes
3 answers
4k views

how to hardhat test against a mainnet fork?

I'm using hardhat to fork the polygon mainnet by - npx hardhat node --fork <ALCHEMY RPC ENDPOINT> now i want to test on this local network. what is the command? npx hardhat test is not working.
eth_sign's user avatar
2 votes
2 answers
4k views

Ethers 6 transaction receipt events information

How i can get event name and params from tx receipt with Ethers 6? I wanna write test to check if event was emitted. Before each beforeEach(async () => { amount = tokens(10) const ...
HideME's user avatar
  • 135
2 votes
1 answer
641 views

TDD Solidity: expect internal struct state

I'm a big fan of TDD/BDD so I figured hardhat and chai/waffle are a good start to learning smart contract development. I've run into an issue though where I'm wondering what best practice would be to ...
TommyF's user avatar
  • 152
2 votes
1 answer
3k views

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/utils' is not defined by "exports" in node_modules\ethers\package.json

Hello everyone I am trying to deploy my ERC721Upgradeable NFT Marketplace Smart Contracts in the local hardhat network but I am getting a error regarding it. I am using Openzeppelin's Upgradeable ...
Nirban Chakraborty's user avatar
1 vote
1 answer
865 views

Using Multiple Initializers in Upgradable Smart Contract

For Upgradable Smart contract following pattern as specified here: https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable#avoid-initial-values-in-field-declarations "Constructor&...
Leon Africa's user avatar
1 vote
4 answers
317 views

Confused about the function of these lines of test code

Test file: const {expect} = require("chai"); const {ethers} = require("hardhat"); describe("NFT Marketplace", function () { let NFTMarket; let nftMarket; let ...
Will-Up-Vote-If-Good's user avatar
1 vote
1 answer
836 views

Prefunding wallets in HardHat

I know that Hardhat automatically prefunds each default wallet with ETHs, however I have a few questions with regards to that. How does this prefunding work when I change the network (Say from ...
neowenshun's user avatar
1 vote
1 answer
2k views

How to test sending eth to function using ethers.js?

I'm net to eth dev and trying to test sending eth while assigning a value to betOnTeam function: function betOnTeam(uint8 _team) public payable { // assign usre to _team and add betting ...
blnks's user avatar
  • 165
1 vote
1 answer
377 views

How to write tests for "msg.sender" being a contract?

I want to write unit-test (using ethers.js on top of an hardhat-project) that covers a hook-function for OpenZeppelin's ERC1155 contract: onERC1155Received. The special thing here is that the msg....
delete's user avatar
  • 327
1 vote
3 answers
1k views

Unable to verify contracts on polygon Amoy testnet

I have deployed some contracts on amoy testnet. To verify I followed this doc and I have also used the same approach for other chains as well. But in this case I am getting the following error: Error ...
Solidity Nerd's user avatar
1 vote
2 answers
2k views

Call A Smart Contract Function With Another Deployed Smart Contract Address As "msg.sender " From Hardhat Test

I have The Following Function in my Smart Contract function functionName(uint256 param1, address param2) external override { require(msg.sender == marketContract, "...
Kay Albertus's user avatar
1 vote
1 answer
140 views

Insufficient Liquidity after adding liquidity on Sepolia Uniswap V2

I created a token "TestMe" with 2 million tokens (18dec), minting the deployer address the whole stack. I used the "addLiquidityETH" function provided by uniswap router to add 500k ...
SolidityNewbie's user avatar
1 vote
1 answer
47 views

_burn() odd behaviour

I stumbled upon an issue with burn() in OpenZeppelin ERC20 contracts. I have the following test: import { time, loadFixture } from "@nomicfoundation/hardhat-network-helpers"; import { ...
Jacek's user avatar
  • 135
1 vote
2 answers
561 views

Best practices on testing an orchestrated contract

Let's define an orchestrated contract as one that contains at least one function that can be called by another contract - and that contract only. The function would implement the restriction like this:...
Paul Razvan Berg's user avatar
1 vote
2 answers
786 views

How do I get the price chainlink price feed when testin with hardhat?

I have written a smart contract utilizing the chainlink price feed. Now I want to test if the prices which I calculate in my smart contract are correct. In solidity I fetch the current USD / ETH price ...
rajohs's user avatar
  • 179
1 vote
3 answers
1k views

Hardhat test error, "TypeError: ABI not iterable" when running npx hardhat test

I am not sure where I am going wrong, but I think it is within my const [owner, _] = ... area. I could be wrong, but I am not sure where, if anywhere I should be putting another argument, or ...
Shawngilbert107's user avatar
0 votes
1 answer
217 views

How to properly use promises in loop contract calls when testing?

I've written this test but I've been told here that I should not use await inside a for loop; What I've found out is that using await will stop the execution of my code until the call to the network ...
Casareafer's user avatar