2

I want to add some nice output in my hardhat scripts for showing what my configurations are during a test run. I run the script locally while changing configuration variables and I want the console output to reflect those config changes so that I can more easily track things.

For example, here is my hardhat.config.js:

require("@nomiclabs/hardhat-waffle");
require("dotenv").config();

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: "0.8.4",
  networks: {
    rinkeby: {
      url: process.env.ALCHEMY_URL,
      // url: process.env.INFURA_URL,
      // url: process.env.QUICKNODE_URL,
      // url: process.env.MORALIS_URL,
      accounts: [process.env.PK]
    }  
  }
};

And here is my workflow:

  1. run the script with existing settings
thatguyintech@albert eth-call-analysis % npx hardhat run scripts/sample-script.js
rpc url: localhost
GasLimits contract deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
  1. change the url config and re-run
thatguyintech@albert eth-call-analysis % npx hardhat run scripts/sample-script.js --network rinkeby
rpc url:  https://eth-rinkeby.alchemyapi.io/v2/<my api key>
GasLimits contract deployed to: 0xeb8aeddb118b8141ea4e0e0e0d7e7ee685f214ac

In order to get the above output, my sample-script.js looks like this:

const hre = require("hardhat");

async function main() {
  console.log("rpc url: ", process.env.ALCHEMY_URL) // I have to manually change this on each run.

  // We get the contract to deploy
  const GasLimits = await hre.ethers.getContractFactory("GasLimits");
  const gasLimits = await GasLimits.deploy();

  await gasLimits.deployed();

  console.log("GasLimits contract deployed to:", gasLimits.address);
}

Is there a way to read the hardhat.config.js values at runtime so that I don't have to rely on dotenv and change my script manually on each run?

Specifically, is there a way to rewrite console.log("rpc url: ", process.env.ALCHEMY_URL) into something that can automatically update based on how I've set my hardhat.config.js? Maybe something like this: console.log("rpc url: ", hre.config.networks.url)?


Update: I do see in the hardhat runtime environment docs that there is an hre.config object I can use. However, I don't want to know about the original configuration, I want to know the network that is currently be used at runtime at the time that my script is invoked. Any tips for that?

5
  • You have access to the HRE in every Hardhat script or task. Read the docs. Commented Jan 26, 2022 at 22:25
  • I saw those docs! I added an update at the end of the question to clarify. I want to know at runtime in my script which network is being run. Commented Jan 27, 2022 at 0:13
  • 1
    That's not possible as of right now. See this conversation. Commented Jan 27, 2022 at 13:10
  • Thanks for the share. Reading this made me think of something else though, I might be able to hack what I need with hardhat tasks Commented Jan 27, 2022 at 19:49
  • @thatguyintech can you share the solution please? Commented Jul 29, 2022 at 1:29

4 Answers 4

3

Great question; try something like this:

Use hre.network.config.url;

Here is an example of how you can get the RPC Provider URL at runtime in your deploy.js script:

const hre = require("hardhat");

console.log("provider url: ", hre.network.config.url);

So then, for example if you run the deploy locally, the provider URL might be undefined:

% npx hardhat run scripts/deploy.js
provider url:  undefined

But if you switch to a network that you have defined, you will then see the api provider URL:

% npx hardhat run scripts/deploy.js --network rinkeby
provider url:  https://eth-rinkeby.alchemyapi.io/v2/<api-key>
1
  • thank you!! the answers from hexcowboy and @hellopeach helped a ton too but it was exactly the config.url attribute that I was looking for Commented Nov 23, 2022 at 22:40
3

If I understand correctly what you are trying to get in the updated part of the question, I think you can use hre.network.name, which is the name of the current network set in the config file that the hre is currently running with.

Basically, if you want to access the raw config values, use hre.config, if you want to know which network the hre is currently running with, use hre.network.name

2

The hre contains the default RPC at hre.ethers.provider.connection.url which will return something like http://localhost:8545.

0

This is how I did it in my testcase:

const hardhatConfig = require("../hardhat.config"); console.log(hardhatConfig.networks.hardhat.forking.url)

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.