Logo

dev-resources.site

for different kinds of informations.

A task vs a script in Hardhat

Published at
3/25/2024
Categories
hardhat
smartcontract
javascript
Author
particle4dev
Categories
3 categories in total
hardhat
open
smartcontract
open
javascript
open
Author
12 person written this
particle4dev
open
A task vs a script in Hardhat

Understanding the Distinctions Between Tasks and Scripts in Hardhat

It's crucial to grasp that nearly everything achievable with a task can also be accomplished with a script, and vice versa. However, there are nuances: certain actions are exclusive to tasks or scripts, and some tasks are more suited to one or the other.

Tasks:

One fundamental capability exclusive to tasks is the ability to override another task. For instance, consider augmenting your test workflow:

task("test", async (args, hre, runSuper) => {
  // Perform additional actions
  return runSuper();
})
Enter fullscreen mode Exit fullscreen mode

What about custom tasks? In this scenario, tasks and scripts are essentially interchangeable. The primary advantage of utilizing a task lies in integrating with Hardhat's argument parser. For instance, when developing a token and needing to verify an address's balance on a specific network, you might execute:

hh balance --address 00x4403B5d2Fed270D18b6d83122C818c2413D9BC05 --network mainnet
Enter fullscreen mode Exit fullscreen mode

(Where 'hh' represents the Hardhat shorthand.)

Scripts:

One distinctive feature of scripts is their executability directly via Node.js. Instead of:

hh run my-script.js
Enter fullscreen mode Exit fullscreen mode

You have the option to use:

node my-script.js
Enter fullscreen mode Exit fullscreen mode

This flexibility enables additional functionalities such as passing extra flags to the Node.js binary. Furthermore, it facilitates execution with alternative binaries like ts-node, ndb, or mocha.

It's worth noting that when adopting this approach, explicit importation of the Hardhat Runtime Environment is necessary:

const hre = require("hardhat");
Enter fullscreen mode Exit fullscreen mode

Alternatively, utilize:

node --require hardhat/register my-scripts.js
Enter fullscreen mode Exit fullscreen mode

to ensure the availability of globally scoped variables, akin to those accessible when using 'hh run'.

Another rationale for employing scripts is the ability to leverage alternative argument-parsing libraries like commander, rather than relying on Hardhat's built-in parser.

However, bear in mind that this approach forfeits the ability to specify the network using the '--network' parameter. In such cases, setting the 'HARDHAT_NETWORK' environment variable becomes necessary:

HARDHAT_NETWORK=rinkeby node my-scripts.js
Enter fullscreen mode Exit fullscreen mode

Understanding these distinctions empowers developers to choose the appropriate toolโ€”task or scriptโ€”based on the specific requirements of their development workflow.

References:

hardhat Article's
30 articles in total
Favicon
How to deploy smart contracts using Foundry
Favicon
Node modules confusion??
Favicon
Simple Smart Contract and Hardhat
Favicon
Building Mystic Vault: A Step-by-Step Guide to Creating a Simple Ethereum DApp from Scratch ๐Ÿ”ฎโœจ ( 30 min )
Favicon
Building a Blockchain-Based Blog dApp with Hardhat: A Step-by-Step Guide
Favicon
Building a Decentralized Voting System with Solidity and Hardhat
Favicon
Ethereum Development: Foundry or Hardhat
Favicon
Full Stack Ethereum and Dapp Development. A comprehensive guide: 2024
Favicon
Roadmap to Blockchain Development: 2024
Favicon
A task vs a script in Hardhat
Favicon
Testing a solidity house-swap contract using HardHat and Typescript
Favicon
How to Change the Bytecode of an Already Deployed Smart Contract
Favicon
How to Fork Mainnet for Testing
Favicon
How to Add a New Pool to Uniswap V3
Favicon
Creating a house swap contract using Solidity and Hardhat
Favicon
Solution to the ConnectTimeoutError Encountered When Verifying Smart Contracts with Hardhat
Favicon
Verified Mainnet Polygon Contract
Favicon
Verify Your Smart Contract Code
Favicon
Programmatically Verifying Solidity Smart Contract Code with Hardhat
Favicon
Getting Started with Hardhat for Smart Contracts
Favicon
How Hardhat Simplifies Smart Contract Verification on Ethereum
Favicon
Hardhat vs Truffle: Which One Is the Best for Developing Ethereum dApps?
Favicon
Fix : TokenService Error: ChainId 0x539 is not supported :(
Favicon
How to mint tokens from the backend using Nest.js on Celo Alfajores network
Favicon
My first Solidity Smart Contract
Favicon
"Deploying a Blockchain Application Nextjs / Hardhat on Netlify: A Comprehensive Guide"
Favicon
How to convert Solidity JSON ABI to Human Readable ABI in Hardhat
Favicon
Optimize Your Gas Costs with eth-gas-reporter for Hardhat
Favicon
Introduction to EVM Smart Contract development
Favicon
Top Tools for Solidity Smart Contract Developers

Featured ones: