generated from NomicFoundation/hardhat-ts-plugin-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
49 lines (43 loc) · 1.95 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { extendEnvironment } from "hardhat/config";
import { createProvider } from "hardhat/internal/core/providers/construction";
import { HardhatPluginError, lazyFunction } from "hardhat/plugins";
import type { EthereumProvider } from "hardhat/types/provider";
import "./type-extensions";
extendEnvironment((hre) => {
// We store providers for faster future lookups
const providers: { [name: string]: EthereumProvider } = {
[hre.network.name]: hre.network.provider,
};
async function getProvider(name: string): Promise<EthereumProvider> {
if (!providers[name]) {
providers[name] = await createProvider(hre.config, name, hre.artifacts);
}
return providers[name];
}
hre.switchNetwork = lazyFunction(() => async (networkName: string) => {
// check if network config is set
if (!hre.config.networks[networkName]) {
throw new HardhatPluginError(
"hardhat-switch-network",
`Couldn't find network '${networkName}' in the Hardhat config`,
);
}
const toProvider = await getProvider(networkName);
// update hardhat's network data
hre.network.name = networkName;
hre.network.config = hre.config.networks[networkName];
hre.network.provider = toProvider;
// update underlying library's provider data
if ("ethers" in hre) {
const { HardhatEthersProvider } = await import(
"@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider"
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(hre.ethers as any).provider = new HardhatEthersProvider(toProvider, networkName);
}
if ("web3" in hre) {
hre.web3 = new (await import("web3")).Web3(toProvider);
}
// for viem there's no need to update anything, it uses the updated hre object to determine the target network
});
});