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

[hardhat-ethers] Printing the provider always seems to indicate that it's pointing to localhost #1138

Open
alcuadrado opened this issue Dec 29, 2020 · 4 comments
Labels
status:ready This issue is ready to be worked on type:improvement

Comments

@alcuadrado
Copy link
Member

alcuadrado commented Dec 29, 2020

If you console.log ethers.provider, it prints a localhost url. This is confusing and many times incorrect. We should try to improve this by setting the url to something else. Maybe a message explaining that the value is bogus.


Search terms: localhost:8545, connection, url, ethers.

@fvictorio
Copy link
Member

Copying my comment from #1798 in case someone lands here.


You can use hre.network.name to know which network is being used.

You can also use hre.network.config.url to get the URL, but that will be undefined when connecting to the hardhat network.

@aspiers
Copy link
Contributor

aspiers commented Mar 16, 2022

@fvictorio commented on August 20, 2021 6:55 PM:

Copying my comment from #1798 in case someone lands here.

Also noting #2309 for reference.

You can use hre.network.name to know which network is being used.

You can also use hre.network.config.url to get the URL, but that will be undefined when connecting to the hardhat network.

Yes, surely there should be a single way to get the URL regardless of whether it's Hardhat network or something else.

Also, referencing hre.network.config.url in a Hardhat task where hre is declared as the HardhatRuntimeEnvironment type results in my editor (and probably any modern editor, since they all tend to use the same LSP servers these days) complaining that url doesn't exist:

image

I suspect HttpNetworkConfig is the actual type which the compiler would need to consider hre.network.config as being, but this should work out of the box and doesn't for some reason.

@fvictorio
Copy link
Member

surely there should be a single way to get the URL regardless of whether it's Hardhat network or something else

But the Hardhat network doesn't have a URL. It's an in-process network, so there's no networking involved.

Also, referencing hre.network.config.url in a Hardhat task where hre is declared as the HardhatRuntimeEnvironment type results in my editor (and probably any modern editor, since they all tend to use the same LSP servers these days) complaining that url doesn't exist:

I think the types of the networks behave like this:

hre.networks.hardhat // HardhatNetworkConfig, doesn't have a URL
hre.networks["something"] // HttpNetworkConfig, has a URL
const someNetwork = hre.networks[stringVariable] // HardhatNetworkConfig | HttpNetworkConfig

// you should be able to narrow the type
if ('url' in someNetwork) {
  // someNetwork: HttpNetworkConfig
} else {
  // someNetwork: HardhatNetworkConfig
}

I haven't checked that this is correct in an actual typescript project, so take it with a grain of salt.

@fvictorio fvictorio added type:improvement status:ready This issue is ready to be worked on and removed priority:low labels Dec 30, 2022
@aspiers
Copy link
Contributor

aspiers commented Jan 8, 2023

Just checked - you were indeed correct, and the 'url' in networkConfig trick worked! When this condition is true, then (networkConfig as HttpNetworkConfig).url allows access without tsc complaining.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready This issue is ready to be worked on type:improvement
Projects
Status: No status
Development

No branches or pull requests

3 participants