Hardhat is a powerful tool for building decentralized applications (DApps) on the Ethereum blockchain, and one of the key features of the framework is its command-line interface (CLI). In this article, we’ll take a closer look at the basic Hardhat commands that you’ll use most frequently as a blockchain developer.
hardhat compile
The hardhat compile
command is used to compile your Solidity contract files into JSON artifacts that can be deployed to the Ethereum blockchain. When you run this command, Hardhat will search for contract files in the directories specified in your configuration file (hardhat.config.js
) and compile them using the Solidity compiler specified in the configuration.
To compile your contracts, you can run the following command:
hardhat compile
By default, this will compile all the contract files in your project. If you want to compile a specific contract, you can specify the contract name as an argument:
hardhat compile MyContract.sol
This will compile only the MyContract
contract.
hardhat test
The hardhat test
command is used to run your contract tests. When you run this command, Hardhat will search for test files in the directories specified in your configuration file and run them using the testing framework specified in the configuration.
To run your tests, you can use the following command:
hardhat test
By default, this will run all the tests in your project. If you want to run a specific test, you can specify the test file name as an argument:
hardhat test test/myTest.js
This will run only the myTest.js
test file.
hardhat run
The hardhat run
command is used to run custom tasks defined in your configuration file (hardhat.config.js
). To run a custom task, you can use the following command:
hardhat run [taskName]
Replace [taskName]
with the name of the task you want to run. For example, if you have a custom task named deploy
, you can run it with the following command:
hardhat run deploy
Custom tasks are defined in the tasks
object in the hardhat.config.js
file. They can take arguments and access the Hardhat runtime environment. Here is an example of a custom task that prints the current network name:
module.exports = {
tasks: {
async printNetworkName(args, hardhatArguments) {
console.log(await hardhatArguments.network.getNetworkName());
},
},
};
To run this task, you would use the following command:
hardhat run printNetworkName
hardhat debug
The hardhat debug
command is used to start a debugger for your contract tests. When you run this command, Hardhat will start a debugger session, allowing you to step through your tests and inspect their behavior.
To start the debugger, you can use the following command:
hardhat debug
By default, this will start the debugger for all the tests in your project. If you want to debug a specific test, you can specify the test file name as an argument:
hardhat debug test/myTest.js
This will start the debugger for only the myTest.js
test file.
Conclusion
These are just a few of the basic Hardhat commands that you’ll use as a blockchain developer. By mastering these commands, you’ll be able to compile, test, and debug your contracts, as well as run custom tasks to automate your workflow. With Hardhat, you’ll have all the tools you need to build powerful DApps on the Ethereum blockchain.
Exercises
To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.
What is the hardhat compile
command used for?
The hardhat compile
command is used to compile Solidity contract files into JSON artifacts that can be deployed to the Ethereum blockchain.
How do you compile a specific contract using the hardhat compile
command?
To compile a specific contract using the hardhat compile
command, you can specify the contract name as an argument. For example:
hardhat compile MyContract.sol
This will compile only the MyContract
contract.
What is the hardhat test
command used for?
The hardhat test
command is used to run contract tests.
How do you run a specific test using the hardhat test
command?
To run a specific test using the hardhat test
command, you can specify the test file name as an argument. For example:
hardhat test test/myTest.js
This will run only the myTest.js
test file.
What is the hardhat run
command used for?
The hardhat run
command is used to run custom tasks defined in the hardhat.config.js
file. To run a custom task, you can use the following command:
hardhat run [taskName]
Replace [taskName]
with the name of the task you want to run. For example, if you have a custom task named deploy
, you can run it with the following command:
hardhat run deploy