Skip to content

Commit ec1dd6e

Browse files
committed
Add support to all chains supported by viem
1 parent 9f74ef2 commit ec1dd6e

File tree

4 files changed

+56
-83
lines changed

4 files changed

+56
-83
lines changed

apps/evmcrispr-terminal/src/components/ActionButtons/index.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,15 @@ export default function ActionButtons({
9090
) => {
9191
if (isProviderAction(action)) {
9292
const chainId = Number(action.params[0].chainId);
93-
await walletClient.switchChain({ id: chainId });
9493
const { chain, transport } = getChainAndTransport(chainId);
94+
try {
95+
await walletClient.switchChain({ id: chainId });
96+
} catch (e: unknown) {
97+
if ((e as Error).name === "UserRejectedRequestError") {
98+
throw new Error(`Switch to ${chain.name} chain rejected by user`);
99+
}
100+
await walletClient.addChain({ chain });
101+
}
95102
publicClient = createPublicClient({ chain, transport });
96103
} else {
97104
const chainId = await walletClient.getChainId();

apps/evmcrispr-terminal/src/wagmi.ts

+36-58
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
import { createConfig, http } from "wagmi";
22
import { injected, walletConnect } from "wagmi/connectors";
33

4-
import {
5-
arbitrum,
6-
base,
7-
gnosis,
8-
goerli,
9-
mainnet,
10-
optimism,
11-
polygon,
12-
polygonZkEvm,
13-
sepolia,
14-
} from "wagmi/chains";
4+
import * as _chains from "wagmi/chains";
5+
import type { Chain, Transport } from "viem";
156

167
import { safe } from "./overrides/safe";
178

@@ -20,55 +11,42 @@ const ALCHEMY_API_KEY = import.meta.env.VITE_ALCHEMY_API_KEY;
2011

2112
const isIframe = window.self !== window.top;
2213

23-
export const transports = ALCHEMY_API_KEY
24-
? {
25-
[mainnet.id]: http(
26-
`https://eth-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
27-
),
28-
[sepolia.id]: http(
29-
`https://eth-sepolia.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
30-
),
31-
[goerli.id]: http(),
32-
[gnosis.id]: http(),
33-
[polygon.id]: http(
34-
`https://polygon-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
35-
),
36-
[polygonZkEvm.id]: http(
37-
`https://polygonzkevm-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
38-
),
39-
[optimism.id]: http(
40-
`https://opt-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
41-
),
42-
[arbitrum.id]: http(
43-
`https://arb-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
44-
),
45-
[base.id]: http(
46-
`https://base-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
47-
),
48-
}
49-
: {
50-
[mainnet.id]: http(),
51-
[sepolia.id]: http(),
52-
[goerli.id]: http(),
53-
[gnosis.id]: http(),
54-
[polygon.id]: http(),
55-
[polygonZkEvm.id]: http(),
56-
[optimism.id]: http(),
57-
[arbitrum.id]: http(),
58-
[base.id]: http(),
59-
};
14+
const alchemyTransports = ALCHEMY_API_KEY && {
15+
[_chains.mainnet.id]:
16+
`https://eth-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
17+
[_chains.sepolia.id]:
18+
`https://eth-sepolia.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
19+
[_chains.polygon.id]:
20+
`https://polygon-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
21+
[_chains.polygonAmoy.id]:
22+
`https://polygon-amoy.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
23+
[_chains.polygonZkEvm.id]:
24+
`https://polygonzkevm-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
25+
[_chains.polygonZkEvmCardona.id]:
26+
`https://polygonzkevm-cardona.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
27+
[_chains.optimism.id]:
28+
`https://opt-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
29+
[_chains.optimismSepolia.id]:
30+
`https://opt-sepolia.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
31+
[_chains.arbitrum.id]:
32+
`https://arb-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
33+
[_chains.arbitrumSepolia.id]:
34+
`https://arb-sepolia.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
35+
[_chains.base.id]: `https://base-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
36+
[_chains.baseSepolia.id]:
37+
`https://base-sepolia.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
38+
};
39+
const chains = Object.values(_chains) as unknown as [Chain, ...Chain[]];
40+
export const transports = chains.reduce(
41+
(acc, { id }) => {
42+
acc[id] = alchemyTransports?.[id] ? http(alchemyTransports[id]) : http();
43+
return acc;
44+
},
45+
{} as Record<number, Transport>,
46+
);
6047

6148
export const config = createConfig({
62-
chains: [
63-
mainnet,
64-
sepolia,
65-
goerli,
66-
gnosis,
67-
polygon,
68-
polygonZkEvm,
69-
optimism,
70-
arbitrum,
71-
],
49+
chains,
7250
connectors: [
7351
!isIframe && injected(),
7452
!isIframe &&

packages/evmcrispr/src/modules/aragonos/Connector.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class Connector {
7272

7373
if (errors?.length) {
7474
throw new ErrorException(
75-
`An error happened while querying subgraph: ${errors[0]}`,
75+
`An error happened while querying subgraph: ${JSON.stringify(errors[0])}`,
7676
);
7777
}
7878

packages/evmcrispr/src/modules/std/commands/switch.ts

+11-23
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1+
import * as chains from "viem/chains";
2+
13
import { ErrorException } from "../../../errors";
24
import type { ICommand, ProviderAction } from "../../../types";
35
import { ComparisonType, checkArgsLength } from "../../../utils";
46
import type { Std } from "../Std";
57

6-
const nameToChainId = {
7-
mainnet: 1,
8-
ropsten: 3,
9-
rinkeby: 4,
10-
goerli: 5,
11-
kovan: 42,
12-
optimism: 10,
13-
optimismKovan: 69,
14-
gnosis: 100,
15-
polygon: 137,
16-
polygonMumbai: 80001,
17-
arbitrum: 42161,
18-
arbitrumRinkeby: 421611,
19-
zkevm: 1101,
20-
};
8+
const nameToChainId = Object.entries(chains).reduce(
9+
(acc, [name, { id }]) => {
10+
acc[name] = id;
11+
return acc;
12+
},
13+
{} as Record<string, number>,
14+
);
2115

2216
export const _switch: ICommand<Std> = {
2317
async run(module, c, { interpretNodes }): Promise<ProviderAction[]> {
@@ -43,10 +37,7 @@ export const _switch: ICommand<Std> = {
4337
`Invalid chain id. Expected a string or number, but got ${typeof networkNameOrId}`,
4438
);
4539
}
46-
chainId =
47-
nameToChainId[
48-
networkNameOrId?.toLowerCase() as keyof typeof nameToChainId
49-
];
40+
chainId = nameToChainId[networkNameOrId as keyof typeof nameToChainId];
5041
if (!chainId) {
5142
throw new ErrorException(`chain "${networkNameOrId}" not found`);
5243
}
@@ -64,10 +55,7 @@ export const _switch: ICommand<Std> = {
6455
buildCompletionItemsForArg(argIndex) {
6556
switch (argIndex) {
6657
case 0:
67-
return [
68-
...Object.keys(nameToChainId),
69-
...Object.values(nameToChainId).map((chainId) => chainId.toString()),
70-
];
58+
return [...Object.keys(nameToChainId)];
7159
default:
7260
return [];
7361
}

0 commit comments

Comments
 (0)