how hardhat works under the hood?

~ 3 min.

06.03.2023
edit
image

Create a new file -- hello.js in scripts folder. We'll require Hardhat's native environment in order to interact with our smart contract --- therefore, let's import it.

./scripts/hello.js
const hre = require("hardhat");

./scripts/hello.js
const hre = require("hardhat");
 
const main = async () => {
	console.log("Hello World");
};
 
main().catch((error) => {
	console.error(error);
	process.exitCode = 1;
});

To execute the script that we just made, just run the below command.

Terminal
npx hardhat run scripts/hello.js
  • npx - telling NodeJS that you wanna do something
  • hardhat - specifying the package that you want to use
  • run - stating that you wanna run something
  • scripts/hello.js - setting the path of the file that you wanna run
  • You will notice two folders - artifacts & cache in your project directory now.
  • Coz' whenever you run a script in hardhat, all the contracts stored in contracts folder get compiled + generate all the deployment info.
  • Wait a minute! -- we didn't specify any smart contracts to compile, then why were folders generated, huh? -- coz' Hardhat doesn't care it simply finds + compiles all contracts automagically :)
  • p.s., in order to integrate smart contract into your frontend -- you'll require only .json file stored @ below path.

    JSON Path
    artifacts/contracts/[CONTRACT_NAME].sol/[CONTRACT_NAME].json

    When you are dealing with many contracts + constantly executing some scripts -- they will run successfully on first execution; but after that, they might just keep emitting errors -- if it happens, just use clean command -- it deletes artifacts folder and removes all the files from cache folder.

    Terminal
    npx hardhat clean

    You know how everything works right from inside out. But, it ain't worth anything. Click here & let's get em' deployed -- within 3 min.