NomicFoundation / hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.
https://hardhat.org
Other
7.04k stars 1.36k forks source link

`run` task #5359

Closed kanej closed 3 weeks ago

kanej commented 1 month ago

The Hardhat run task enables js/ts scripts to be run in a node environment and leverage a HardhatRuntimeEnvironment generated based on the given hardhat.config.{js,ts}:

# js
hardhat run ./path/to/script.js

# ts
hardhat --config ./Alternate.hardhat.config.ts run ./path/to/script.ts

In both JavaScript and Typescript the HardhatRuntimeEnvironment needs to be imported:

JavaScript:

// ./path/to/script.js

const hre = require("hardhat");

console.log(hre.config.solidity);

Typescript:

// ./path/to/script.ts

import hre from "hardhat";

console.log(hre.config.solidity);

The HardhatRuntimeEnvironment that is initialized will by default be based on the closest hardhat.config.{js,ts}, where closest mean looking in the current working directory, then moving to the parent directory recursively.

The hardhat.config.{js,ts} that is used can be overridden on the command line with the --config flag.

A Typescript script will be executed with tsx.

Running with bare node

Hardhat can also be used from plain node scripts, passing a config programmatically or relying on implicit file loading.

Passing the config programmatically:

import { createHardhatRuntimeEnvironment } from "hardhat/hre"

const main = async () => {
  const config = {
    solidity: "0.8.24"
  }

  const hre = await createHardhatRuntimeEnvironment(config)

  console.log(hre.config.solidity)
}

Reading the config from the local hardhat.config.{js,ts}:

import { createHardhatRuntimeEnvironment, readHardhatConfig } from "hardhat/hre"

const main = async () => {
  const config = await readHardhatConfig()

  const hre = await createHardhatRuntimeEnvironment(config)

  console.log(hre.config.solidity)
}
alcuadrado commented 1 month ago

Hardhat can also be used from plain node scripts, passing a config programmatically or relying on implicit file loading.

A small clarification. This can also be done with any script. These other lower-level apis are just for advanced/non-standard things.

readHardhatConfig

I think exporting a function that finds the config file is also helpful, and may even be a better alternative to readHardhatConfig.

kanej commented 1 month ago

Hardhat can also be used from plain node scripts, passing a config programmatically or relying on implicit file loading.

A small clarification. This can also be done with any script. These other lower-level apis are just for advanced/non-standard things.

readHardhatConfig

I think exporting a function that finds the config file is also helpful, and may even be a better alternative to readHardhatConfig.

Can you provide example code, I am not sure I have followed what your preference is here?

alcuadrado commented 1 month ago

Sure.

You can run a hardhat script with node script.js. As in v3 you have to import hardhat, that import will initialize the HRE.

/// script.js
import {config} from "hardhat";

console.log(config);

Or, if it was about the second part:

import { createHardhatRuntimeEnvironment, findHardhatConfigFile } from "hardhat/hre"

const configPath = await findHardhatConfigFile()  
const {default: config} = await import(configPath); 
const hre = await createHardhatRuntimeEnvironment(config, configPath) // Note the configPath, to be used to determine the project root. 
console.log(hre.config.solidity)