Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to change hre provider when running script? #4420

Closed
congtanwkm opened this issue Sep 27, 2023 · 2 comments
Closed

Is there any way to change hre provider when running script? #4420

congtanwkm opened this issue Sep 27, 2023 · 2 comments
Assignees

Comments

@congtanwkm
Copy link

congtanwkm commented Sep 27, 2023

Describe the feature

I'm running multiple hardhat nodes in different ports like: 0.0.0.0:8545, ...:8546, ...
My scripts is something like this and I use it in a nestjs project

import * as hre from 'hardhat';

async execute(port: number, from: string, to: string, txData: any) {
       // const provider = new hre.ethers.JsonRpcProvider(`http://0.0.00:${port}`);
       // change hre.ethers.provider => provider
        const sender = await hre.ethers.getImpersonatedSigner(from);
        const txData = {
          to: to,
          data: txData,
        };
    const tx = await sender.sendTransaction(txData);
}

hardhat.config.js

module.exports = {
  defaultNetwork: "localhost1",
  solidity: "0.8.19",
  networks: {
    localhost1: {
      url: "http://127.0.0.1:8545",
    },
    localhost2: {
      url: "http://127.0.0.1:8546",
    },
  },
}

I tried:

hre.ethers.provider = provider;

and the issue is:

Type 'JsonRpcProvider' is missing the following properties from type 'HardhatEthersProvider': _hardhatProvider, _networkName, _isHardhatNetworkCached, _latestBlockNumberPolled, and 25 more

My issue is: I can't change hre.ethers.provider to my custom provider, it always point to the default network came from hardhat config is localhost1. Is there anyway to set provider for hre?
I know we can change provider of sender by sender.connect(provider), but hre.ethers.getImpersonatedSigner still point to localhost1 and I will get error unknown account. I want to change provider inside hre.

Search terms

No response

@fvictorio
Copy link
Member

No, that's not possible right now, and it won't be possible without a breaking change. The alternative is to manually create and use those ethers providers, as you mentioned, but that means not using any of the helpers under hre.ethers.

@fvictorio
Copy link
Member

Tentatively closing because this already is in our roadmap for the next major version of Hardhat.

@fvictorio fvictorio closed this as not planned Won't fix, can't repro, duplicate, stale Oct 17, 2023
@github-project-automation github-project-automation bot moved this from Backlog to Done in Hardhat Oct 17, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

No branches or pull requests

3 participants