Skip to content

Commit 1df0e22

Browse files
committed
feat: use extractChain to return first match instead of filtering duplicates
1 parent 294ef52 commit 1df0e22

File tree

3 files changed

+27
-48
lines changed

3 files changed

+27
-48
lines changed

packages/hardhat-viem/src/internal/chains.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,46 @@ import type { TestClientMode } from "../types";
44

55
import memoize from "lodash.memoize";
66

7-
import {
8-
UnknownDevelopmentNetworkError,
9-
NetworkNotFoundError,
10-
MultipleMatchingNetworksError,
11-
} from "./errors";
7+
import { UnknownDevelopmentNetworkError, NetworkNotFoundError } from "./errors";
128

139
export async function getChain(provider: EthereumProvider): Promise<Chain> {
14-
const chains: Record<string, Chain> = require("viem/chains");
10+
const { extractChain } = await import("viem");
11+
const chainsModule = await import("viem/chains");
12+
const chains = Object.values(chainsModule) as Chain[];
1513
const chainId = await getChainId(provider);
1614

1715
if (isDevelopmentNetwork(chainId)) {
1816
if (await isHardhatNetwork(provider)) {
19-
return chains.hardhat;
17+
return chainsModule.hardhat;
2018
} else if (await isFoundryNetwork(provider)) {
21-
return chains.foundry;
19+
return chainsModule.foundry;
2220
} else {
2321
throw new UnknownDevelopmentNetworkError();
2422
}
2523
}
2624

27-
const matchingChains = Object.values(chains).filter(
28-
({ id }) => id === chainId
29-
);
25+
const chain = extractChain({
26+
chains,
27+
id: chainId,
28+
});
3029

31-
if (matchingChains.length === 0) {
30+
if (chain === undefined) {
3231
if (await isHardhatNetwork(provider)) {
3332
return {
34-
...chains.hardhat,
33+
...chainsModule.hardhat,
3534
id: chainId,
3635
};
3736
} else if (await isFoundryNetwork(provider)) {
3837
return {
39-
...chains.foundry,
38+
...chainsModule.foundry,
4039
id: chainId,
4140
};
4241
} else {
4342
throw new NetworkNotFoundError(chainId);
4443
}
4544
}
4645

47-
if (matchingChains.length > 1) {
48-
throw new MultipleMatchingNetworksError(chainId);
49-
}
50-
51-
return matchingChains[0];
46+
return chain;
5247
}
5348

5449
export function isDevelopmentNetwork(chainId: number) {

packages/hardhat-viem/src/internal/errors.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,6 @@ You can find a list of supported networks here: https://github.com/wevm/viem/blo
3131
}
3232
}
3333

34-
export class MultipleMatchingNetworksError extends HardhatViemError {
35-
constructor(chainId: number) {
36-
super(
37-
`Multiple networks with chain id ${chainId} found. You can override the chain by passing it as a parameter to the client getter:
38-
39-
import { someChain } from "viem/chains";
40-
const client = await hre.viem.getPublicClient({
41-
chain: someChain,
42-
...
43-
});
44-
45-
You can find a list of supported networks here: https://github.com/wevm/viem/blob/main/src/chains/index.ts`
46-
);
47-
}
48-
}
49-
5034
export class DefaultWalletClientNotFoundError extends HardhatViemError {
5135
constructor(networkName: string) {
5236
super(

packages/hardhat-viem/test/chains.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,41 +52,41 @@ describe("chains", () => {
5252
expect(chain).to.deep.equal(chains.foundry);
5353
});
5454

55-
it("should throw if the chain id is 31337 and the network is neither hardhat nor foundry", async () => {
55+
it("should return the first matching chain if the chain id is not 31337 and there are multiple chains with that id", async () => {
5656
const provider: EthereumProvider = new EthereumMockedProvider();
5757
const sendStub = sinon.stub(provider, "send");
58-
sendStub.withArgs("eth_chainId").returns(Promise.resolve("0x7a69")); // 31337 in hex
58+
// chain id 999 corresponds to wanchainTestnet but also zoraTestnet
59+
sendStub.withArgs("eth_chainId").returns(Promise.resolve("0x3e7"));
5960
sendStub.withArgs("hardhat_metadata").throws();
6061
sendStub.withArgs("anvil_nodeInfo").throws();
6162

62-
await expect(getChain(provider)).to.be.rejectedWith(
63-
`The chain id corresponds to a development network but we couldn't detect which one.
64-
Please report this issue if you're using Hardhat or Foundry.`
65-
);
63+
const chain = await getChain(provider);
64+
65+
expect(chain).to.deep.equal(chains.wanchainTestnet);
6666
});
6767

68-
it("should throw if the chain id is not 31337 and there is no chain with that id", async () => {
68+
it("should throw if the chain id is 31337 and the network is neither hardhat nor foundry", async () => {
6969
const provider: EthereumProvider = new EthereumMockedProvider();
7070
const sendStub = sinon.stub(provider, "send");
71-
sendStub.withArgs("eth_chainId").returns(Promise.resolve("0x0")); // fake chain id 0
71+
sendStub.withArgs("eth_chainId").returns(Promise.resolve("0x7a69")); // 31337 in hex
7272
sendStub.withArgs("hardhat_metadata").throws();
7373
sendStub.withArgs("anvil_nodeInfo").throws();
7474

7575
await expect(getChain(provider)).to.be.rejectedWith(
76-
/No network with chain id 0 found/
76+
`The chain id corresponds to a development network but we couldn't detect which one.
77+
Please report this issue if you're using Hardhat or Foundry.`
7778
);
7879
});
7980

80-
it("should throw if the chain id is not 31337 and there are multiple chains with that id", async () => {
81+
it("should throw if the chain id is not 31337 and there is no chain with that id", async () => {
8182
const provider: EthereumProvider = new EthereumMockedProvider();
8283
const sendStub = sinon.stub(provider, "send");
83-
// chain id 999 corresponds to Wanchain Testnet but also Zora Goerli Testnet
84-
sendStub.withArgs("eth_chainId").returns(Promise.resolve("0x3e7"));
84+
sendStub.withArgs("eth_chainId").returns(Promise.resolve("0x0")); // fake chain id 0
8585
sendStub.withArgs("hardhat_metadata").throws();
8686
sendStub.withArgs("anvil_nodeInfo").throws();
8787

8888
await expect(getChain(provider)).to.be.rejectedWith(
89-
/Multiple networks with chain id 999 found./
89+
/No network with chain id 0 found/
9090
);
9191
});
9292
});

0 commit comments

Comments
 (0)