7

How can I pass positional arguments to script run by npx hardhat run command?

Help displays only this:

Hardhat version 2.6.8

Usage: hardhat [GLOBAL OPTIONS] run [--no-compile] script

OPTIONS:

--no-compile Don't compile before running this task

POSITIONAL ARGUMENTS:

script A js file to be run within hardhat's environment

run: Runs a user-defined script after compiling the project

For global options help run: hardhat help [4:38 PM] If I try something like npx hardhat --network mumbai run scripts/collect.js 2 Error HH308: Unrecognized positional argument 2

For more info go to https://hardhat.org/HH308 or run Hardhat with --show-stack-traces

If I try

npx hardhat --network mumbai run scripts/collect.js 2
Error HH308: Unrecognized positional argument 2

For more info go to https://hardhat.org/HH308 or run Hardhat with --show-stack-traces

Any suggestions?

rlib
  • 289
  • 2
  • 10

2 Answers2

3

You can check out this link

Check out a custom example

task("erc20_balances", "Prints the list of erc20 balances").addParam("address", "Add token address").setAction(async (taskArgs, hre) => {
  try {
    const provider = new ethers.getDefaultProvider("http://127.0.0.1:8545/");
    const accounts = await hre.ethers.getSigners();
    const contract = new ethers.Contract(taskArgs.address, abi, provider);
    for (const account of accounts) {
      balance = await contract.balanceOf(account.address);
      console.log(account.address, " : ", balance.toString());
    }
  } catch (error) {
    console.log(error);
  }
})
0

running scripts are of 2 kinds:

  1. write a standalone scripts
  2. write hardhat tasks with their references in hardhat config file

here the question is about the option-1, where we run script via:

npx hardhat run --network testnet <scriptname> --acct value --symbol value

@rlib How are you accessing the arguments inside the script? is it like this way?

const [acct, symbol] = args;
defi star
  • 9
  • 2
  • 2
    `npx hardhat --network polygon run scripts/initLadder.js --symbol 1

    Error HH305: Unrecognized param --symbol`

    – rlib Aug 24 '22 at 12:25