@nomicfoundation/hardhat-viem 2.0.5 → 3.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -16
- package/README.md +35 -247
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +10 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/internal/accounts.d.ts +4 -0
- package/dist/src/internal/accounts.d.ts.map +1 -0
- package/dist/src/internal/accounts.js +15 -0
- package/dist/src/internal/accounts.js.map +1 -0
- package/dist/src/internal/chains.d.ts +11 -0
- package/dist/src/internal/chains.d.ts.map +1 -0
- package/dist/src/internal/chains.js +116 -0
- package/dist/src/internal/chains.js.map +1 -0
- package/dist/src/internal/clients.d.ts +10 -0
- package/dist/src/internal/clients.d.ts.map +1 -0
- package/dist/src/internal/clients.js +91 -0
- package/dist/src/internal/clients.js.map +1 -0
- package/dist/src/internal/contracts.d.ts +11 -0
- package/dist/src/internal/contracts.d.ts.map +1 -0
- package/dist/src/internal/contracts.js +141 -0
- package/dist/src/internal/contracts.js.map +1 -0
- package/dist/src/internal/hook-handlers/network.d.ts +4 -0
- package/dist/src/internal/hook-handlers/network.d.ts.map +1 -0
- package/dist/src/internal/hook-handlers/network.js +12 -0
- package/dist/src/internal/hook-handlers/network.js.map +1 -0
- package/dist/src/internal/initialization.d.ts +6 -0
- package/dist/src/internal/initialization.d.ts.map +1 -0
- package/dist/src/internal/initialization.js +14 -0
- package/dist/src/internal/initialization.js.map +1 -0
- package/dist/src/type-extensions.d.ts +8 -0
- package/dist/src/type-extensions.d.ts.map +1 -0
- package/dist/src/type-extensions.js +2 -0
- package/dist/src/type-extensions.js.map +1 -0
- package/dist/src/types.d.ts +166 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/package.json +46 -52
- package/src/index.ts +10 -39
- package/src/internal/accounts.ts +20 -7
- package/src/internal/chains.ts +122 -62
- package/src/internal/clients.ts +135 -141
- package/src/internal/contracts.ts +126 -150
- package/src/internal/hook-handlers/network.ts +27 -0
- package/src/internal/initialization.ts +67 -0
- package/src/type-extensions.ts +10 -0
- package/src/types.ts +237 -72
- package/.eslintrc.js +0 -24
- package/index.d.ts +0 -3
- package/index.d.ts.map +0 -1
- package/index.js +0 -20
- package/index.js.map +0 -1
- package/internal/accounts.d.ts +0 -5
- package/internal/accounts.d.ts.map +0 -1
- package/internal/accounts.js +0 -9
- package/internal/accounts.js.map +0 -1
- package/internal/bytecode.d.ts +0 -13
- package/internal/bytecode.d.ts.map +0 -1
- package/internal/bytecode.js +0 -94
- package/internal/bytecode.js.map +0 -1
- package/internal/chains.d.ts +0 -7
- package/internal/chains.d.ts.map +0 -1
- package/internal/chains.js +0 -80
- package/internal/chains.js.map +0 -1
- package/internal/clients.d.ts +0 -45
- package/internal/clients.d.ts.map +0 -1
- package/internal/clients.js +0 -136
- package/internal/clients.js.map +0 -1
- package/internal/contracts.d.ts +0 -11
- package/internal/contracts.d.ts.map +0 -1
- package/internal/contracts.js +0 -164
- package/internal/contracts.js.map +0 -1
- package/internal/errors.d.ts +0 -36
- package/internal/errors.d.ts.map +0 -1
- package/internal/errors.js +0 -102
- package/internal/errors.js.map +0 -1
- package/internal/tasks.d.ts +0 -2
- package/internal/tasks.d.ts.map +0 -1
- package/internal/tasks.js +0 -240
- package/internal/tasks.js.map +0 -1
- package/internal/type-extensions.d.ts +0 -19
- package/internal/type-extensions.d.ts.map +0 -1
- package/internal/type-extensions.js +0 -5
- package/internal/type-extensions.js.map +0 -1
- package/src/internal/bytecode.ts +0 -138
- package/src/internal/errors.ts +0 -125
- package/src/internal/tasks.ts +0 -341
- package/src/internal/type-extensions.ts +0 -27
- package/src/tsconfig.json +0 -15
- package/types.d.ts +0 -52
- package/types.d.ts.map +0 -1
- package/types.js +0 -3
- package/types.js.map +0 -1
package/src/internal/accounts.ts
CHANGED
@@ -1,9 +1,22 @@
|
|
1
|
-
import type { EthereumProvider } from "hardhat/types";
|
2
|
-
import type { Address } from "viem";
|
1
|
+
import type { EthereumProvider } from "hardhat/types/providers";
|
2
|
+
import type { Address as ViemAddress } from "viem";
|
3
3
|
|
4
|
-
|
4
|
+
const accountsCache = new WeakMap<EthereumProvider, ViemAddress[]>();
|
5
5
|
|
6
|
-
export
|
7
|
-
|
8
|
-
|
9
|
-
);
|
6
|
+
export async function getAccounts(
|
7
|
+
provider: EthereumProvider,
|
8
|
+
): Promise<ViemAddress[]> {
|
9
|
+
const cachedAccounts = accountsCache.get(provider);
|
10
|
+
if (cachedAccounts !== undefined) {
|
11
|
+
return cachedAccounts;
|
12
|
+
}
|
13
|
+
|
14
|
+
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
15
|
+
-- We know that the provider is going to return an array of accounts */
|
16
|
+
const accounts = (await provider.request({
|
17
|
+
method: "eth_accounts",
|
18
|
+
})) as ViemAddress[];
|
19
|
+
accountsCache.set(provider, accounts);
|
20
|
+
|
21
|
+
return accounts;
|
22
|
+
}
|
package/src/internal/chains.ts
CHANGED
@@ -1,95 +1,155 @@
|
|
1
|
-
import type {
|
2
|
-
import type {
|
3
|
-
import type {
|
4
|
-
|
5
|
-
import memoize from "lodash.memoize";
|
1
|
+
import type { TestClientMode } from "../types.js";
|
2
|
+
import type { ChainType } from "hardhat/types/network";
|
3
|
+
import type { EthereumProvider } from "hardhat/types/providers";
|
4
|
+
import type { Chain as ViemChain } from "viem";
|
6
5
|
|
7
6
|
import {
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
} from "
|
7
|
+
assertHardhatInvariant,
|
8
|
+
HardhatError,
|
9
|
+
} from "@nomicfoundation/hardhat-errors";
|
10
|
+
import { extractChain } from "viem";
|
11
|
+
import * as chainsModule from "viem/chains";
|
12
|
+
import { hardhat, anvil, optimism } from "viem/chains";
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
15
|
+
-- TODO: this assertion should not be necessary */
|
16
|
+
const chains = Object.values(chainsModule) as ViemChain[];
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
18
|
+
const chainCache = new WeakMap<EthereumProvider, ViemChain>();
|
19
|
+
const chainIdCache = new WeakMap<EthereumProvider, number>();
|
20
|
+
const isHardhatNetworkCache = new WeakMap<EthereumProvider, boolean>();
|
21
|
+
const isAnvilNetworkCache = new WeakMap<EthereumProvider, boolean>();
|
22
|
+
|
23
|
+
const HARDHAT_METADATA_METHOD = "hardhat_metadata";
|
24
|
+
const ANVIL_NODE_INFO_METHOD = "anvil_nodeInfo";
|
25
|
+
|
26
|
+
export async function getChain(
|
27
|
+
provider: EthereumProvider,
|
28
|
+
chainType: ChainType | string,
|
29
|
+
): Promise<ViemChain> {
|
30
|
+
const cachedChain = chainCache.get(provider);
|
31
|
+
if (cachedChain !== undefined) {
|
32
|
+
return cachedChain;
|
25
33
|
}
|
26
34
|
|
27
|
-
const
|
28
|
-
|
29
|
-
|
35
|
+
const chainId = await getChainId(provider);
|
36
|
+
|
37
|
+
let chain = extractChain({
|
38
|
+
chains,
|
39
|
+
id: chainId,
|
40
|
+
});
|
30
41
|
|
31
|
-
if (
|
42
|
+
if ((await isDevelopmentNetwork(provider)) || chain === undefined) {
|
32
43
|
if (await isHardhatNetwork(provider)) {
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
44
|
+
// TODO: We shoud improve how we handle the chains for the different chain
|
45
|
+
// types, as this is both a hardhat and an optimism chain.
|
46
|
+
//
|
47
|
+
// We are currently creating our chain based off optimism's, but that's
|
48
|
+
// not always the correct behavior, as the user may be connecting to
|
49
|
+
// a different chain.
|
50
|
+
if (chainType === "optimism") {
|
51
|
+
chain = { ...optimism, id: chainId };
|
52
|
+
} else {
|
53
|
+
chain = {
|
54
|
+
...hardhat,
|
55
|
+
id: chainId,
|
56
|
+
};
|
57
|
+
}
|
58
|
+
} else if (await isAnvilNetwork(provider)) {
|
59
|
+
chain = {
|
60
|
+
...anvil,
|
40
61
|
id: chainId,
|
41
62
|
};
|
63
|
+
} else if (chain === undefined) {
|
64
|
+
// If the chain couldn't be found and we can't detect the development
|
65
|
+
// network we throw an error.
|
66
|
+
throw new HardhatError(HardhatError.ERRORS.VIEM.NETWORK_NOT_FOUND, {
|
67
|
+
chainId,
|
68
|
+
});
|
42
69
|
} else {
|
43
|
-
|
70
|
+
assertHardhatInvariant(
|
71
|
+
false,
|
72
|
+
"This should not happen, as we check in isDevelopmentNetwork that it's either hardhat or anvil",
|
73
|
+
);
|
44
74
|
}
|
45
75
|
}
|
46
76
|
|
47
|
-
|
48
|
-
throw new MultipleMatchingNetworksError(chainId);
|
49
|
-
}
|
77
|
+
chainCache.set(provider, chain);
|
50
78
|
|
51
|
-
return
|
79
|
+
return chain;
|
52
80
|
}
|
53
81
|
|
54
|
-
export function
|
55
|
-
|
82
|
+
export async function getChainId(provider: EthereumProvider): Promise<number> {
|
83
|
+
const cachedChainId = chainIdCache.get(provider);
|
84
|
+
if (cachedChainId !== undefined) {
|
85
|
+
return cachedChainId;
|
86
|
+
}
|
87
|
+
|
88
|
+
const chainId = Number(await provider.request({ method: "eth_chainId" }));
|
89
|
+
chainIdCache.set(provider, chainId);
|
90
|
+
|
91
|
+
return chainId;
|
56
92
|
}
|
57
93
|
|
58
|
-
export async function
|
59
|
-
provider: EthereumProvider
|
60
|
-
): Promise<
|
94
|
+
export async function isDevelopmentNetwork(
|
95
|
+
provider: EthereumProvider,
|
96
|
+
): Promise<boolean> {
|
61
97
|
if (await isHardhatNetwork(provider)) {
|
62
|
-
return
|
63
|
-
}
|
64
|
-
|
65
|
-
|
66
|
-
|
98
|
+
return true;
|
99
|
+
}
|
100
|
+
|
101
|
+
if (await isAnvilNetwork(provider)) {
|
102
|
+
return true;
|
67
103
|
}
|
104
|
+
|
105
|
+
return false;
|
68
106
|
}
|
69
107
|
|
70
|
-
|
71
|
-
|
72
|
-
)
|
108
|
+
export async function isHardhatNetwork(
|
109
|
+
provider: EthereumProvider,
|
110
|
+
): Promise<boolean> {
|
111
|
+
const cachedIsHardhat = isHardhatNetworkCache.get(provider);
|
112
|
+
if (cachedIsHardhat !== undefined) {
|
113
|
+
return cachedIsHardhat;
|
114
|
+
}
|
115
|
+
|
116
|
+
const isHardhat = await isMethodSupported(provider, HARDHAT_METADATA_METHOD);
|
117
|
+
isHardhatNetworkCache.set(provider, isHardhat);
|
73
118
|
|
74
|
-
|
75
|
-
|
76
|
-
|
119
|
+
return isHardhat;
|
120
|
+
}
|
121
|
+
|
122
|
+
export async function isAnvilNetwork(
|
123
|
+
provider: EthereumProvider,
|
124
|
+
): Promise<boolean> {
|
125
|
+
const cachedIsAnvil = isAnvilNetworkCache.get(provider);
|
126
|
+
if (cachedIsAnvil !== undefined) {
|
127
|
+
return cachedIsAnvil;
|
128
|
+
}
|
77
129
|
|
78
|
-
const
|
79
|
-
|
80
|
-
);
|
130
|
+
const isAnvil = await isMethodSupported(provider, ANVIL_NODE_INFO_METHOD);
|
131
|
+
isAnvilNetworkCache.set(provider, isAnvil);
|
81
132
|
|
82
|
-
|
83
|
-
HARDHAT_METADATA = "hardhat_metadata",
|
84
|
-
ANVIL_NODE_INFO = "anvil_nodeInfo",
|
133
|
+
return isAnvil;
|
85
134
|
}
|
86
135
|
|
87
|
-
async function
|
136
|
+
export async function getMode(
|
88
137
|
provider: EthereumProvider,
|
89
|
-
|
90
|
-
) {
|
138
|
+
): Promise<TestClientMode> {
|
139
|
+
if (await isHardhatNetwork(provider)) {
|
140
|
+
return "hardhat";
|
141
|
+
}
|
142
|
+
if (await isAnvilNetwork(provider)) {
|
143
|
+
return "anvil";
|
144
|
+
}
|
145
|
+
throw new HardhatError(
|
146
|
+
HardhatError.ERRORS.VIEM.UNSUPPORTED_DEVELOPMENT_NETWORK,
|
147
|
+
);
|
148
|
+
}
|
149
|
+
|
150
|
+
async function isMethodSupported(provider: EthereumProvider, method: string) {
|
91
151
|
try {
|
92
|
-
await provider.
|
152
|
+
await provider.request({ method });
|
93
153
|
return true;
|
94
154
|
} catch {
|
95
155
|
return false;
|
package/src/internal/clients.ts
CHANGED
@@ -1,176 +1,170 @@
|
|
1
|
-
import type { EthereumProvider } from "hardhat/types";
|
2
1
|
import type {
|
3
|
-
|
4
|
-
|
5
|
-
PublicClientConfig,
|
6
|
-
WalletClientConfig,
|
7
|
-
TestClientConfig,
|
8
|
-
} from "viem";
|
9
|
-
import type {
|
10
|
-
PublicClient,
|
2
|
+
GetPublicClientReturnType,
|
3
|
+
GetWalletClientReturnType,
|
11
4
|
TestClient,
|
12
|
-
|
13
|
-
|
14
|
-
} from "
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
const defaultParameters = isDevelopmentNetwork(chain.id)
|
23
|
-
? { pollingInterval: 50, cacheTime: 0 }
|
24
|
-
: {};
|
5
|
+
} from "../types.js";
|
6
|
+
import type { ChainType } from "hardhat/types/network";
|
7
|
+
import type { EthereumProvider } from "hardhat/types/providers";
|
8
|
+
import type {
|
9
|
+
Address as ViemAddress,
|
10
|
+
PublicClientConfig as ViemPublicClientConfig,
|
11
|
+
TestClientConfig as ViemTestClientConfig,
|
12
|
+
WalletClientConfig as ViemWalletClientConfig,
|
13
|
+
} from "viem";
|
25
14
|
|
26
|
-
|
27
|
-
|
28
|
-
|
15
|
+
import { HardhatError } from "@nomicfoundation/hardhat-errors";
|
16
|
+
import {
|
17
|
+
createPublicClient,
|
18
|
+
createWalletClient,
|
19
|
+
createTestClient,
|
20
|
+
custom as customTransport,
|
21
|
+
} from "viem";
|
22
|
+
import { publicActionsL2, walletActionsL2 } from "viem/op-stack";
|
29
23
|
|
30
|
-
|
31
|
-
|
32
|
-
transportParameters,
|
33
|
-
};
|
34
|
-
}
|
24
|
+
import { getAccounts } from "./accounts.js";
|
25
|
+
import { getChain, getMode, isDevelopmentNetwork } from "./chains.js";
|
35
26
|
|
36
|
-
|
37
|
-
* Get a PublicClient instance. This is a read-only client that can be used to
|
38
|
-
* query the blockchain.
|
39
|
-
*
|
40
|
-
* @param provider The Ethereum provider used to connect to the blockchain.
|
41
|
-
* @param publicClientConfig Optional configuration for the PublicClient instance. See the viem documentation for more information.
|
42
|
-
* @returns A PublicClient instance.
|
43
|
-
*/
|
44
|
-
export async function getPublicClient(
|
27
|
+
export async function getPublicClient<ChainTypeT extends ChainType | string>(
|
45
28
|
provider: EthereumProvider,
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
const chain =
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
chain: Chain,
|
56
|
-
publicClientConfig?: Partial<PublicClientConfig>
|
57
|
-
): Promise<PublicClient> {
|
58
|
-
const viem = await import("viem");
|
59
|
-
const { clientParameters, transportParameters } = await getParameters(
|
60
|
-
chain,
|
61
|
-
publicClientConfig
|
62
|
-
);
|
29
|
+
chainType: ChainTypeT,
|
30
|
+
publicClientConfig?: Partial<ViemPublicClientConfig>,
|
31
|
+
): Promise<GetPublicClientReturnType<ChainTypeT>> {
|
32
|
+
const chain =
|
33
|
+
publicClientConfig?.chain ?? (await getChain(provider, chainType));
|
34
|
+
const parameters = {
|
35
|
+
...(await getDefaultClientParameters(provider)),
|
36
|
+
...publicClientConfig,
|
37
|
+
};
|
63
38
|
|
64
|
-
|
39
|
+
let publicClient = createPublicClient({
|
65
40
|
chain,
|
66
|
-
transport:
|
67
|
-
...
|
41
|
+
transport: customTransport(provider),
|
42
|
+
...parameters,
|
68
43
|
});
|
69
44
|
|
70
|
-
|
71
|
-
|
45
|
+
if (chainType === "optimism") {
|
46
|
+
publicClient = publicClient.extend(publicActionsL2());
|
47
|
+
}
|
72
48
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
* with an account obtained from the provider using `eth_accounts`.
|
77
|
-
*
|
78
|
-
* @param provider The Ethereum provider used to connect to the blockchain.
|
79
|
-
* @param walletClientConfig Optional configuration for the WalletClient instances. See the viem documentation for more information.
|
80
|
-
* @returns A list of WalletClient instances.
|
81
|
-
*/
|
82
|
-
export async function getWalletClients(
|
83
|
-
provider: EthereumProvider,
|
84
|
-
walletClientConfig?: Partial<WalletClientConfig>
|
85
|
-
): Promise<WalletClient[]> {
|
86
|
-
const { getAccounts } = await import("./accounts");
|
87
|
-
const { getChain } = await import("./chains");
|
88
|
-
const chain = walletClientConfig?.chain ?? (await getChain(provider));
|
89
|
-
const accounts = await getAccounts(provider);
|
90
|
-
return innerGetWalletClients(provider, chain, accounts, walletClientConfig);
|
49
|
+
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions --
|
50
|
+
We need to assert the type because TS gets confused with the conditional type */
|
51
|
+
return publicClient as GetPublicClientReturnType<ChainTypeT>;
|
91
52
|
}
|
92
53
|
|
93
|
-
export async function
|
54
|
+
export async function getWalletClients<ChainTypeT extends ChainType | string>(
|
94
55
|
provider: EthereumProvider,
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
const
|
101
|
-
|
102
|
-
|
103
|
-
|
56
|
+
chainType: ChainTypeT,
|
57
|
+
walletClientConfig?: Partial<ViemWalletClientConfig>,
|
58
|
+
): Promise<Array<GetWalletClientReturnType<ChainTypeT>>> {
|
59
|
+
const chain =
|
60
|
+
walletClientConfig?.chain ?? (await getChain(provider, chainType));
|
61
|
+
const accounts = await getAccounts(provider);
|
62
|
+
const parameters = {
|
63
|
+
...(await getDefaultClientParameters(provider)),
|
64
|
+
...walletClientConfig,
|
65
|
+
};
|
104
66
|
|
105
|
-
|
106
|
-
|
67
|
+
let walletClients = accounts.map((account) =>
|
68
|
+
createWalletClient({
|
107
69
|
chain,
|
108
70
|
account,
|
109
|
-
transport:
|
110
|
-
...
|
111
|
-
})
|
71
|
+
transport: customTransport(provider),
|
72
|
+
...parameters,
|
73
|
+
}),
|
112
74
|
);
|
113
75
|
|
114
|
-
|
76
|
+
if (chainType === "optimism") {
|
77
|
+
walletClients = walletClients.map((walletClient) =>
|
78
|
+
walletClient.extend(walletActionsL2()),
|
79
|
+
);
|
80
|
+
}
|
81
|
+
|
82
|
+
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions --
|
83
|
+
We need to assert the type because TS gets confused with the conditional type */
|
84
|
+
return walletClients as Array<GetWalletClientReturnType<ChainTypeT>>;
|
115
85
|
}
|
116
86
|
|
117
|
-
|
118
|
-
* Get a WalletClient instance for a specific address. This is a read-write
|
119
|
-
* client that can be used to send transactions to the blockchain.
|
120
|
-
*
|
121
|
-
* @param provider The Ethereum provider used to connect to the blockchain.
|
122
|
-
* @param address The public address of the account to use.
|
123
|
-
* @param walletClientConfig Optional configuration for the WalletClient instance. See the viem documentation for more information.
|
124
|
-
* @returns A WalletClient instance.
|
125
|
-
*/
|
126
|
-
export async function getWalletClient(
|
87
|
+
export async function getWalletClient<ChainTypeT extends ChainType | string>(
|
127
88
|
provider: EthereumProvider,
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
const chain =
|
133
|
-
|
134
|
-
|
135
|
-
|
89
|
+
chainType: ChainTypeT,
|
90
|
+
address: ViemAddress,
|
91
|
+
walletClientConfig?: Partial<ViemWalletClientConfig>,
|
92
|
+
): Promise<GetWalletClientReturnType<ChainTypeT>> {
|
93
|
+
const chain =
|
94
|
+
walletClientConfig?.chain ?? (await getChain(provider, chainType));
|
95
|
+
const parameters = {
|
96
|
+
...(await getDefaultClientParameters(provider)),
|
97
|
+
...walletClientConfig,
|
98
|
+
};
|
99
|
+
|
100
|
+
let walletClient = createWalletClient({
|
101
|
+
chain,
|
102
|
+
account: address,
|
103
|
+
transport: customTransport(provider),
|
104
|
+
...parameters,
|
105
|
+
});
|
106
|
+
|
107
|
+
if (chainType === "optimism") {
|
108
|
+
walletClient = walletClient.extend(walletActionsL2());
|
109
|
+
}
|
110
|
+
|
111
|
+
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions --
|
112
|
+
We need to assert the type because TS gets confused with the conditional type */
|
113
|
+
return walletClient as GetWalletClientReturnType<ChainTypeT>;
|
136
114
|
}
|
137
115
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
*
|
142
|
-
* @param provider The Ethereum provider used to connect to the blockchain.
|
143
|
-
* @param testClientConfig Optional configuration for the TestClient instance. See the viem documentation for more information.
|
144
|
-
* @returns A TestClient instance.
|
145
|
-
*/
|
146
|
-
export async function getTestClient(
|
116
|
+
export async function getDefaultWalletClient<
|
117
|
+
ChainTypeT extends ChainType | string,
|
118
|
+
>(
|
147
119
|
provider: EthereumProvider,
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
const chain =
|
152
|
-
|
153
|
-
|
120
|
+
chainType: ChainTypeT,
|
121
|
+
walletClientConfig?: Partial<ViemWalletClientConfig>,
|
122
|
+
): Promise<GetWalletClientReturnType<ChainTypeT>> {
|
123
|
+
const chain =
|
124
|
+
walletClientConfig?.chain ?? (await getChain(provider, chainType));
|
125
|
+
const [defaultAccount] = await getAccounts(provider);
|
126
|
+
|
127
|
+
if (defaultAccount === undefined) {
|
128
|
+
throw new HardhatError(
|
129
|
+
HardhatError.ERRORS.VIEM.DEFAULT_WALLET_CLIENT_NOT_FOUND,
|
130
|
+
{
|
131
|
+
chainId: chain.id,
|
132
|
+
},
|
133
|
+
);
|
134
|
+
}
|
135
|
+
|
136
|
+
return getWalletClient(
|
137
|
+
provider,
|
138
|
+
chainType,
|
139
|
+
defaultAccount,
|
140
|
+
walletClientConfig,
|
141
|
+
);
|
154
142
|
}
|
155
143
|
|
156
|
-
export async function
|
144
|
+
export async function getTestClient<ChainTypeT extends ChainType | string>(
|
157
145
|
provider: EthereumProvider,
|
158
|
-
|
159
|
-
|
160
|
-
testClientConfig?: Partial<TestClientConfig>
|
146
|
+
chainType: ChainTypeT,
|
147
|
+
testClientConfig?: Partial<ViemTestClientConfig>,
|
161
148
|
): Promise<TestClient> {
|
162
|
-
const
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
);
|
149
|
+
const chain =
|
150
|
+
testClientConfig?.chain ?? (await getChain(provider, chainType));
|
151
|
+
const mode = await getMode(provider);
|
152
|
+
const parameters = { ...DEFAULT_CLIENT_PARAMETERS, ...testClientConfig };
|
167
153
|
|
168
|
-
const testClient =
|
169
|
-
mode,
|
154
|
+
const testClient = createTestClient({
|
170
155
|
chain,
|
171
|
-
|
172
|
-
|
156
|
+
mode,
|
157
|
+
transport: customTransport(provider),
|
158
|
+
...parameters,
|
173
159
|
});
|
174
160
|
|
175
161
|
return testClient;
|
176
162
|
}
|
163
|
+
|
164
|
+
const DEFAULT_CLIENT_PARAMETERS = { pollingInterval: 50, cacheTime: 0 };
|
165
|
+
|
166
|
+
async function getDefaultClientParameters(provider: EthereumProvider) {
|
167
|
+
return (await isDevelopmentNetwork(provider))
|
168
|
+
? DEFAULT_CLIENT_PARAMETERS
|
169
|
+
: {};
|
170
|
+
}
|