@@ -3,34 +3,59 @@ import React from "react";
33import { createWeb3Modal } from "@web3modal/wagmi/react" ;
44import { type Chain } from "viem" ;
55import { 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" ;
77import { walletConnect } from "wagmi/connectors" ;
88
9- import { ALL_CHAINS } from "consts/chains" ;
9+ import { ALL_CHAINS , DEFAULT_CHAIN } from "consts/chains" ;
1010import { isProductionDeployment } from "consts/index" ;
1111
1212import { lightTheme } from "styles/themes" ;
1313
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 ( ) ;
1616
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+ } ;
1824
1925type 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+ } ;
3254} ;
3355
56+ const chains = ALL_CHAINS as [ Chain , ...Chain [ ] ] ;
57+ const transports = getTransports ( ) ;
58+ const projectId = import . meta. env . WALLETCONNECT_PROJECT_ID ?? "" ;
3459const wagmiConfig = createConfig ( {
3560 chains,
3661 transports,
0 commit comments