Hardhat Configuration

Dheeraj Kumar
Coinmonks
6 min readJul 27, 2021

--

This article is about configuration of your hardhat.config.js file . This file is normally located in the root of your project and is needed for Hardhat to work. Everything about your Hardhat setup (i.e. your config, plugins and custom tasks) are contained in this file.

Available Options:

An object is exported from hardhat.config.js to set up your project. This object can have entities like defaultNetwork, networks, solidity, paths and mocha.

A sample config file is shown below:

A sample hardhat.config.js file

Network Configuration:

There are two kinds of networks in Hardhat: JSON-RPC based networks, and built-in Hardhat Network.

The default network is “hardhat” but you can customize which network to be used as default while running Hardhat by setting defaultNetwork field.

Hardhat Network:

Hardhat had got a built-in network called hardhat. When you are using this network, an instance of the Hardhat Network will get created automatically when you test your contracts or run a task or script.

The following fields could be set for the hardhat in config:

chainId : The chainId used by Hardhat Network’s blockchain. Default value is 31337.

from : This is the address that would act as a default sender. If this value is not set, it’ll consider first account of the Hardhat Network.

gas : It’s value is generally a number or should be “auto”. The number used will be the gas limit used in every transaction. If “auto” is used, the gas limit will be automatically estimated. Default value is same as that of blockGasLimit.

gasPrice : It needs to be “auto” or a number. Default value is 8000000000.

gasMultiplier : This is a number that is multiplied to the gas estimation results to give it some slack due to uncertainty of the estimation process. Default value of gasMultiplier is 1.

accounts : This field can be configured as:

  • An object describing an HD wallet. It is default and can have the following fields like mnemonic (a 12 or 24 word seed phrase as defined by BIP39), initialIndex (default value: 0), path (the HD parent of all derived keys), count (number of accounts to derive), accountsBalance (string with balance in wei for every derived account).
  • Array of initial accounts that Hardhat Network will create and each of this would be an object with privateKey and balance fields.

blockGasLimit : This is the gas limit to use in Hardhat Network’s blockchain. Default value : 12450000.

minGasPrice : The minimum gas Price accepted by the Hardhat Network. Transactions with lower gas Price are accepted but not mined.

hardfork : This is to set how Hardhat Network works. It can take values from “byzantium”, “petersburg”, “istanbul”, “muirGlacier”, “berlin”, “constantinople” and “london”. Default : “berlin”.

throwOnTransactionFailures : This is a boolean that controls if Hardhat Network throws on transaction failures. If it is true, Hardhat Network will throw Javascript ans Solidity stack traces on transaction failures. If it is false, it will return the failing transaction hash. Default : true.

throwOnCallFailures : This boolean controls if Hardhat Network throws on call failures. If it’s true, Network will throw JavaScript and Solidity stack traces when a call fails. If it is false, it will return the call’s return data with a revert reason. Default value: true.

loggingEnabled : This boolean controls if Hardhat Network logs every request or not. Default value : true (for Hardhat Network backed JSON-RPC server and false (for the main Hardhat Network).

initialDate : This is an optional string to set the date of the blockchain. Default value : current date and time if not forking another network.

allowUnlimitedContractSize : This boolean disables the size limit of the contract imposed by EIP 170. Default value : false

forking : This object describes forking configuration with fields like url (URL that points to a JSON-RPC node with state you want to fork off), blockNumber (a number to pin which block to fork from) and enabled (boolean to switch on or off fork functionality).

minGasPrice : The minimum gasPrice that a transaction must have. This field is not included if the “hardfork” is “london” or later one.

initialBaseFeePerGas : The baseFeePerGas of the first block. If forking a remote network, the first block is the one immediately after the block you forked from.

JSON-RPC based networks :

These kinds of networks are connected to external nodes. Nodes could run on your computer like Ganache, or remotely like Infura. To set such networks, you’ll have to configure the object with following fields :

url : This is the url of the node for custom networks.

chainId : This number is used to validate the network Hardhat connects to.

from : This address would act as default sender. If default sender is not instantiated, first account will be the default address.

gas : It can be “auto” or a number. If it’s a number, that would be gas limit for each transaction and the gas limit will be automatically estimated in case of “auto”. Default value: auto

gasPrice : Its value should be auto or a number. Default value: auto

gasMultiplier : Default value : 1

accounts : This field controls the account that Hardhat uses. It can use node’s accounts or an HD Wallet. Default : “remote”.

httpHeaders : This field can be used to set extra HTTP Headers to be used when making JSON-RPC requests. It accepts a JavaScript object which maps header names to their values. Default : undefined.

timeout : Timeout is represented in ms(millisecond) for requests sent to JSON-RPC serer. The requests taking longer than set times will be cancelled. Default value : 20000.

HD Wallet Configuration :

For using HD Wallet with Hardhat, you’ll have to set your network’s account with below fields.

mnemonic : A string that is your seed phrase of the wallet

path : the HD parent of all derived keys. Default value : “m/44'/60'/0'/0”.

initialIndex : Initial index to derive. Default value : 0

count : Number of accounts to derive. Default : 20

Solidity Configuration :

The solidity config is an optional fiels that can be a solc version to use like “0.8.0” . It can be an object that described configuration of a compiler like version (solc version), settings like optimizer with enabled and runs keys. Default value : { enabled : false, runs : 200 }. Another setting could be evmVersion which is a string controlling the target evm version. It may take values like homestead, spuriousDragon, byzantium, constantinople, etc. Default value: managed by solc.

It may also include an object that describes multiple compilers and their configurations.
It may include a list of compiler configuration objects and overrides which helps you map compiler objects with the file names so as to specify the compiler configuration for that particular file.

Path Configuration : Your hardhat project uses different paths and you can customize then by providing an object to the paths field with the following keys:
root : Default value is the directory that contains the config file. This is the root of your Hardhat project.

sources : This is the directory where your contracts are stored. The path is resolved from project’s root and has default value: ‘./contracts’ .

tests : This is the directory that has your tests. Default value: ‘./test’ .

cache : The directory that Hardhat uses to cache its internal stuff. Default value: ‘./cache’ .

artifacts : This directory contains compilation artifacts. Default value: ‘./artifacts’ .

Mocha configuration: You can configure how tests are running using mocha entity, it would accept the same options as Mocha.

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--