I just started using Hardhat to develop a smart contract and I know that you can interact dynamically (without a script) with your contract through the console once it's deployed.

However, I find quite annoying having to write the code to retrieve the contract instance and the owner address each time I deploy the contract in the Hardhat network.

Is there a way to, at least partially, automate this process? Maybe calling a parametrized script when I launch the console?

  • you no like run scripts? like npx hardhat run scripts/run.js?
    – sola24
    Sep 10, 2022 at 16:36
  • @sola24 Of course, but let's say I deployed the contract with a script and I would like to call a function (e.g. balanceOf) to get some info about the current state. I'd do that with the console. Sep 10, 2022 at 21:00

2 Answers 2


tasks are the answer.

If you need the same feature, read carefully (more than I did) the hardhat task page.

I found this answer useful, too.


If you meant by creating new instance every time you hardhat console, there is no way to automate it.

The most you can do is copy the boilerplate code and paste it in the console after you have deployed the contract.

In the deploy.js file, you can add this piece of code to print the deployed and contract address.

const { ethers } = require("hardhat");

async function main() {
  const [deployer] = await ethers.getSigners();
  console.log("Deployer address:", deployer.address);
  console.log("Account balance:", (await deployer.getBalance()).toString());
  const erc20 = await ethers.getContractFactory("erc20");
  const ERC20 = await erc20.deploy();

  console.log("The token address:", ERC20.address);
  .then(() => process.exit(0))
  .catch((error) => {

first two three lines in the main function lets you print the deployer address and deployer balance. The console.log line will give you contract address.

Later when you log in to the hardhat console with the command.

npx hardhat console --network

You can use this boilerplate code.

add = "Address of the contract"
name = "Name of the contract"

instance = await ethers.getContractAt(add, name);

That would create the instance of a contract and you can play with the functions.

Tell me if it helps!

  • 1
    Hi! Thank you for your answer. Actually something similar to what I wanted to achieve is described in the answer I linked in my own answer. Maybe it could be useful to you, too. Sep 11, 2022 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.