Before proceeding, you need to install Node.js (we'll use v16.x) and the npm package manager. You can download directly from Node.js or in your terminal:
# You can use homebrew (https://docs.brew.sh/Installation)$brewinstallnode# Or you can use nvm (https://github.com/nvm-sh/nvm)$nvminstallnode
You can verify that everything is installed correctly by querying the version for each package:
$node-v...$npm-v...
If you haven't already, you will also need to install Uptick if you plan on deploying your smart contracts locally. Check this document for the full instructions.
Create Hardhat Project
To create a new project, navigate to your project directory and run:
$npxhardhat88888888888888888888888888888888888888888888888888888888888b.888d888.d8888888888b.8888b.888888888888"88b 888P"d88" 888 888 "88b"88b 888888 888 .d888888 888 888 888 888 888 .d888888 888888 888 888 888 888 Y88b 888 888 888 888 888 Y88b.888 888 "Y888888888"Y88888 888 888 "Y888888"Y888Welcome to Hardhat v2.0.8? What do you want to do? …❯ Create a sample project Create an empty hardhat.config.js
Following the prompts should create a new project structure in your directory. Consult the Hardhat config page for a list of configuration options to specify in hardhat.config.js. Most importantly, you should set the defaultNetwork entry to point to your desired JSON-RPC network:
You will see that a default smart contract, written in Solidity, has already been provided under contracts/Greeter.sol:
pragma solidity ^0.8.0;import"hardhat/console.sol";contract Greeter { string private greeting;constructor(string memory _greeting) {console.log("Deploying a Greeter with greeting:", _greeting); greeting = _greeting; }functiongreet() publicviewreturns (string memory) {return greeting; }functionsetGreeting(string memory _greeting) public {console.log("Changing greeting from '%s' to '%s'", greeting, _greeting); greeting = _greeting; }}
This contract allows you to set and query a string greeting. Hardhat also provides a script to deploy smart contracts to a target network; this can be invoked via the following command, targeting your default network:
npxhardhatrunscripts/sample-script.js
Hardhat also lets you manually specify a target network via the --network <your-network> flag: