Solidity – How to Run a Defined Hardhat Task Inside the Hardhat Console

hardhatsolidity

I would like to test a contract in the hardhat console, but in order to do so I need to deploy a number of supporting contracts which includes several libraries, and I handle that inside a deploymenment script.

What Im wondering if I can do that inside the console (started without a –network parameter) so its running its own isolated node inside console. I'm already using my running local node for something else and it already contains test data which I'd rather not have to recreate.

I tried un scripts/myDeployScript.ts but that doesnt work in the console.

Best Answer

You can do it the same way you do into any .js files:

await run('myTask', {foo: "bar"})

Example

If myTask is defined like this example:


task("myTask", "Prints the network name and a foo arg")
  .addOptionalParam("foo", "Will be printed if defined")
  .setAction(async ({foo}, { ethers, network }) => {

    console.log(` * network: ${network.name}`);
    if (foo) {
    console.log(` * foo: ${foo}`);
  }

  return {
    foo,
    network: network.name,
  };
});
$ hh console --network localhost
Welcome to Node.js v14.xx.xx.
Type ".help" for more information.
> const res = await run('myTask', {foo: "bar"})
 * network: localhost
 * foo: bar
undefined
> res
{ foo: 'bar', network: 'localhost' }
Related Topic