10

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.

Does anybody know how to accomplish what i need ??

Thanks a lot!!

Anibal Estevez
  • 121
  • 1
  • 6
  • [this](https://hardhat.org/guides/create-task.html#advanced-usage) 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. – munanadi Sep 12 '21 at 17:36

3 Answers3

11

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;
  });
jpozzi
  • 309
  • 3
  • 7
  • 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 error – Anibal Estevez Feb 08 '22 at 16:07
  • 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? – bongoSLAP Dec 26 '22 at 02:32
  • 2
    @bongoSLAP ```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 bVal``` – Ally Haire Jan 11 '23 at 11:07
5

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);
Manolo Mollar
  • 51
  • 2
  • 1
0

It could be useful to check the diferences between a script and a task and how to choose: Hardhat has This on documentation.

  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](https://meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. – cursorrux Jan 11 '23 at 05:41
  • Thanks @cursorux I'll keep it in mind! – Betzabe Silva Jan 11 '23 at 21:53