Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It is written in JavaScript language.
Hardhat:
- Faster migration process.
- Has low gas consumption
- Debugging is easy
To install hardhat:
npm install --save-dev hardhat
npx hardhat (choose JavaScript project)
After this install the dependencies that are listed by the hardhat as additional dependencies.
Now make contracts in the contracts folder of the hardhat. Use the following command to first compile the contract.
cd contracts
npx hardhat compile
Then run the tests in the test folder, while use the script folder to deploy the contract on the testnet. For testing use command:
npx hardhat test
First import the following file in the test. Then write the statements and then check using following.
import "hardhat/console.sol"
npx hardhat test
First write deployment code in the deploy folder. In the deploy script, this code is universal:
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
npx hardhat run scripts/deploy.js
First use the alchemy, to take the GOERLI (chain) URL and API_KEY and save them in .env file, using names:
GOERLI_URL
GOERLI_API_KEY
PRIVATE_KEY (from Metamask wallet)
And in hardhat config.js, run following:
require("dotenv").config();
/** @type import('hardhat/config').HardhatUserConfig */
require("@nomiclabs/hardhat-waffle");
const GOERLI_URL = process.env.GOERLI_URL;
const PRIVATE_KEY = process.env.PRIVATE_KEY;
module.exports = {
solidity: "0.8.17", [use lastest version]
networks: {
goerli: {
url: GOERLI_URL,
accounts: [PRIVATE_KEY],
},
},
};
Also, remember to first install .env package in the project and then include this:
npm i dotenv
GOERLI_URL=[URL_FROM_ALCHEMY]
GOERLI_API_KEY= [API_KEY_FROM_ALCHEMY]
PRIVATE_KEY= [wallet PRIVATE_KEY]
Then run following command, to deploy on the testnet
npx hardhat run scripts/deploy.js --network goerli
Check the address of the contact on the goerli etherscan, by pasting token address.