hardhat run with parameters
Asked Answered
S

3

12

I need to run a particular ts script using hardhat from the command line but I need to specify parameters... Similar to this:

npx hardhat run --network rinkeby scripts/task-executor.ts param1 param2

Where the --network rinkeby is the parameter for the hardhat run
And param1 and param2 are parameters for the task-executor.ts script.
I couldn't find any post regarding this issue and I cannot make it work.

I also tried defining a hardhat task and added those parameters but if I try to execute it I get:

Error HH9: Error while loading Hardhat's configuration.    
You probably tried to import the "hardhat" module from your config or a file imported from it.
This is not possible, as Hardhat can't be initialized while its config is being defined.

Because I need to import hre or ethers from hardhat in that particular task.

How to accomplish what i need?

Scone answered 9/9, 2021 at 3:11 Comment(1)
this does not help? With this you can get in the params and everything. Could you attach your code? Could be helpful to look where it is going wrong.Cyrus
M
17

According to Hardhat:

Hardhat scripts are useful for simple things that don't take user arguments, and for integrating with external tools that aren't well suited for the Hardhat CLI, like a Node.js debugger.

For scripts that require parameters, you should use Hardhat Tasks.

You can code the task in a different file than hardhat.config.ts. Here is an example task using positional parameters in the file sampleTask.ts:

import { task } from "hardhat/config";

task("sampleTask", "A sample task with params")
  .addPositionalParam("param1")
  .addPositionalParam("param2")
  .setAction(async (taskArgs) => {
    console.log(taskArgs);
  });

Remember to import it inside hardhat.config.ts:

import "./tasks/sampleTask";

Then run it with:

npx hardhat sampleTask hello world 

And it should print:

{ param1: 'hello', param2: 'world' }

You can read more about named, positional and optional parameters on tasks here.

If you need to use hre or ethers, you can get hre from the second parameter of the setAction function:

task("sampleTask", "A sample task with params")
  .addPositionalParam("param1")
  .addPositionalParam("param2")
  .setAction(async (taskArgs, hre) => {
    const ethers = hre.ethers;
  });
Mcmann answered 25/1, 2022 at 23:42 Comment(3)
sorry... i read the hardhat documentation... I put in my first post that i tried to implement the hardhat tasks and the error I got... and seems logical to get that errorScone
Say I have a contract that takes two arguments, a and b. If I am deploying this contract via a task using the hre: const assetContract = await AssetContract.deploy(a, b); how would I pass in these via the .addPositionalParam method calls?Maleficent
@Maleficent npx hardhat sampleTask a-param b-param You can also just use sampleTask.addParam('a', 'does a').addParam('b', 'does b thing') and use npx hardhat sampleTask --a aVal --b bValJeanajeanbaptiste
E
8

You can use environment and access via process.env

On Linux:

param1=some param2=thing npx hardhat run scripts/task-executor.ts

And print with:

console.log(process.env.param1);
Evolute answered 16/3, 2023 at 13:13 Comment(0)
C
0

I found a solution that we can deploy contracts by using tasks. I am not sure if the problem above mentioned is due to the difference between deployContract() and deploy().

This is my greeter_deploy.js

const fs = require("fs");
async function deploy(walletAddress, hre) {
    const studentWalletAddress = walletAddress;
    const greeter = await hre.ethers.deployContract("Greeter", [studentWalletAddress]);
    const contract = await greeter.waitForDeployment();
    console.log("Greeter is deployed on network: Goerli at address:", await contract.getAddress());
    result = { walletAddress: walletAddress, contractAddress: await contract.getAddress(), challenge: "greeter" };
    fs.appendFileSync("address.txt", JSON.stringify(result) + "\n");
}

task("greeter_deploy", "Deploys the Greeter contract with student wallet")
    .addPositionalParam("walletAddress", "The student wallet address")
    .setAction(async (taskArgs, hre) => {
        console.log("taskArgs", taskArgs);
        await hre.run("compile");
        await deploy(taskArgs.walletAddress, hre).catch(async (error) => {
            console.error(error);
            process.exitCode = 1;
        });
    });

Also, don't forget to import the tasks in hardhat.config.js.

require("./tasks/greeter_deploy");

Running tasks:

$ npx hardhat greeter_deploy 0xfBfE37229AE38F83E0C718efF6E8B5A1194C9127 --network goerli

We can also specify the default network in hardhat.config.js.

Casual answered 9/10, 2023 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.