@@ -3,34 +3,59 @@ import React from "react";
3
3
import { createWeb3Modal } from "@web3modal/wagmi/react" ;
4
4
import { type Chain } from "viem" ;
5
5
import { createConfig , fallback , http , WagmiProvider , webSocket } from "wagmi" ;
6
- import { mainnet , arbitrumSepolia , arbitrum , gnosisChiado } from "wagmi/chains" ;
6
+ import { mainnet , arbitrumSepolia , arbitrum , gnosisChiado , gnosis , sepolia } from "wagmi/chains" ;
7
7
import { walletConnect } from "wagmi/connectors" ;
8
8
9
- import { ALL_CHAINS } from "consts/chains" ;
9
+ import { ALL_CHAINS , DEFAULT_CHAIN } from "consts/chains" ;
10
10
import { isProductionDeployment } from "consts/index" ;
11
11
12
12
import { lightTheme } from "styles/themes" ;
13
13
14
- const projectId = import . meta. env . WALLETCONNECT_PROJECT_ID ?? "" ;
15
- export const alchemyApiKey = import . meta . env . ALCHEMY_API_KEY ?? "" ;
14
+ const alchemyApiKey = import . meta. env . ALCHEMY_API_KEY ?? "" ;
15
+ const isProduction = isProductionDeployment ( ) ;
16
16
17
- const chains = ALL_CHAINS as [ Chain , ...Chain [ ] ] ;
17
+ // https://github.com/alchemyplatform/alchemy-sdk-js/blob/96b3f62/src/types/types.ts#L98-L119
18
+ const alchemyToViemChain = {
19
+ [ arbitrum . id ] : "arb-mainnet" ,
20
+ [ arbitrumSepolia . id ] : "arb-sepolia" ,
21
+ [ mainnet . id ] : "eth-mainnet" ,
22
+ [ sepolia . id ] : "eth-sepolia" ,
23
+ } ;
18
24
19
25
type AlchemyProtocol = "https" | "wss" ;
20
- type AlchemyChain = "arb-sepolia" | "eth-mainnet" | "arb" ;
21
- const alchemyURL = ( protocol : AlchemyProtocol , chain : AlchemyChain ) =>
22
- `${ protocol } ://${ chain } .g.alchemy.com/v2/${ alchemyApiKey } ` ;
23
- const alchemyTransport = ( chain : AlchemyChain ) =>
24
- fallback ( [ webSocket ( alchemyURL ( "wss" , chain ) ) , http ( alchemyURL ( "https" , chain ) ) ] ) ;
25
-
26
- const transports = {
27
- [ isProductionDeployment ( ) ? arbitrum . id : arbitrumSepolia . id ] : isProductionDeployment ( )
28
- ? alchemyTransport ( "arb" )
29
- : alchemyTransport ( "arb-sepolia" ) ,
30
- [ mainnet . id ] : alchemyTransport ( "eth-mainnet" ) ,
31
- [ gnosisChiado . id ] : fallback ( [ webSocket ( "wss://rpc.chiadochain.net/wss" ) , http ( "https://rpc.chiadochain.net" ) ] ) ,
26
+
27
+ // https://github.com/alchemyplatform/alchemy-sdk-js/blob/96b3f62/src/util/const.ts#L16-L18
28
+ const alchemyURL = ( protocol : AlchemyProtocol , chainId : number ) =>
29
+ `${ protocol } ://${ alchemyToViemChain [ chainId ] } .g.alchemy.com/v2/${ alchemyApiKey } ` ;
30
+
31
+ export const getChainRpcUrl = ( protocol : AlchemyProtocol , chainId : number ) => {
32
+ return alchemyURL ( protocol , chainId ) ;
33
+ } ;
34
+
35
+ export const getDefaultChainRpcUrl = ( protocol : AlchemyProtocol ) => {
36
+ return getChainRpcUrl ( protocol , DEFAULT_CHAIN ) ;
37
+ } ;
38
+
39
+ export const getTransports = ( ) => {
40
+ const alchemyTransport = ( chain : Chain ) =>
41
+ fallback ( [ http ( alchemyURL ( "https" , chain . id ) ) , webSocket ( alchemyURL ( "wss" , chain . id ) ) ] ) ;
42
+ const defaultTransport = ( chain : Chain ) =>
43
+ fallback ( [ http ( chain . rpcUrls . default ?. http ?. [ 0 ] ) , webSocket ( chain . rpcUrls . default ?. webSocket ?. [ 0 ] ) ] ) ;
44
+
45
+ return {
46
+ [ isProduction ? arbitrum . id : arbitrumSepolia . id ] : isProduction
47
+ ? alchemyTransport ( arbitrum )
48
+ : alchemyTransport ( arbitrumSepolia ) ,
49
+ [ isProduction ? gnosis . id : gnosisChiado . id ] : isProduction
50
+ ? defaultTransport ( gnosis )
51
+ : defaultTransport ( gnosisChiado ) ,
52
+ [ mainnet . id ] : alchemyTransport ( mainnet ) , // Always enabled for ENS resolution
53
+ } ;
32
54
} ;
33
55
56
+ const chains = ALL_CHAINS as [ Chain , ...Chain [ ] ] ;
57
+ const transports = getTransports ( ) ;
58
+ const projectId = import . meta. env . WALLETCONNECT_PROJECT_ID ?? "" ;
34
59
const wagmiConfig = createConfig ( {
35
60
chains,
36
61
transports,
0 commit comments