Hardhat
Hardhat is a development environment to compile, deploy, test, and debug your smart contract.
Hardhat is used through a local installation in your project.
To install it, you need to create an npm project by going to an empty folder, running
npm init
, and following its instructions. Once your project is ready, you should runnpm install --save-dev hardhat
To use your local installation of Hardhat, you need to use
npx
to run it (i.e. npx hardhat
).
Hardhat will let you know how, but, in case you missed it, you can install them with
npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers
To create your Hardhat project run
npx hardhat
in your project folder:$ npx hardhat
888 888 888 888 888
888 888 888 888 888
888 888 888 888 888
8888888888 8888b. 888d888 .d88888 88888b. 8888b. 888888
888 888 "88b 888P" d88" 888 888 "88b "88b 888
888 888 .d888888 888 888 888 888 888 .d888888 888
888 888 888 888 888 Y88b 888 888 888 888 888 Y88b.
888 888 "Y888888 888 "Y88888 888 888 "Y888888 "Y888
Welcome to Hardhat v2.0.8
? What do you want to do? …
❯ Create a sample project
Create an advanced sample project
Create an advanced sample project that uses TypeScript
Create an empty hardhat.config.js
Quit
If you select Create a sample project a simple project creation wizard will ask you some questions and create a project with the following structure:
contracts/
scripts/
test/
hardhat.config.js
contracts/
is where the source files for your contracts should be.test/
is where your tests should go.scripts/
is where simple automation scripts go
You can write your own smart contract or download any contract written by Solidity.
- Go to hardhat.config.js
- Update the config with crendentials.
module.exports = {
defaultNetwork: "mcpnet",
networks: {
hardhat: {
},
mcpnet: {
url: "https://mainnet.ccn.org",
accounts: [privateKey1, privateKey2, ...]
}
},
solidity: {
version: "0.5.15",
settings: {
optimizer: {
enabled: true,
runs: 200
}
}
},
paths: {
sources: "./contracts",
tests: "./test",
cache: "./cache",
artifacts: "./artifacts"
},
mocha: {
timeout: 40000
}
}
To compile your contracts in your Hardhat project, use the built-in
compile
task:$ npx hardhat compile
Compiling...
Compiled 1 contract successfully
You can deploy the
Greeter
contract from the sample project with a deploy script scripts/deploy.js
like this:async function main() {
// We get the contract to deploy
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, Hardhat!");
await greeter.deployed();
console.log("Greeter deployed to:", greeter.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
As general rule, you can target any network configured in the
hardhat.config.js
$npx hardhat run --network mcpnet scripts/deploy.js
Last modified 4mo ago