@@ -11,7 +11,7 @@ import { gnosisFork } from "src/chains/gnosisFork";
1111import { CreateWalletFn } from "src/wallets/CreateWalletFn" ;
1212import { capsuleWallet } from "src/wallets/capsule" ;
1313import { Chain , http , Transport } from "viem" ;
14- import { foundry , gnosis , mainnet , sepolia } from "wagmi/chains" ;
14+ import { foundry , gnosis , linea , mainnet , sepolia } from "wagmi/chains" ;
1515
1616const {
1717 VITE_LOCALHOST_NODE_RPC_URL ,
@@ -20,94 +20,139 @@ const {
2020 VITE_WALLET_CONNECT_PROJECT_ID ,
2121 VITE_SEPOLIA_RPC_URL ,
2222 VITE_MAINNET_RPC_URL ,
23+ VITE_LINEA_RPC_URL ,
2324 VITE_GNOSIS_FORK_NODE_RPC_URL ,
2425 VITE_GNOSIS_FORK_CHAIN_ID ,
2526 VITE_GNOSIS_NODE_RPC_URL ,
2627} = import . meta. env ;
2728
28- export const chains : Chain [ ] = [ ] ;
29- const transports : Record < string , Transport > = { } ;
29+ interface WagmiClientConfig {
30+ rpcUrl : string ;
31+ chain : Chain ;
32+ wallets ?: ( CreateWalletFn | undefined ) [ ] ;
33+ /**
34+ * used for fork chains only
35+ */
36+ chainId ?: number ;
37+ }
38+
39+ /* To add a new chain to this file, follow these steps:
40+
41+ * 1. Import the new chain from "wagmi/chains" or define it in "src/chains" if it's a custom chain.
42+ * 2. Add a new property to the `chainConfigs` object with the chain's name as the key.
43+ * 3. Define the chain's configuration, including the `rpcUrl` and chain properties.
44+ * 4. Optionally, add a `wallets` property to specify the wallets that should be supported on this chain.
45+ * 5. If the chain is a fork chain, add a `chainId` property to specify the chain's ID.
3046
47+ * Example:
48+ * myChain: {
49+ * rpcUrl: VITE_MY_CHAIN_RPC_URL,
50+ * chain: myChain,
51+ * wallets: [capsuleWallet],
52+ * chainId: 123, // only required for fork chains
53+ * }
54+ */
55+
56+ const chainConfigs : Record < string , WagmiClientConfig > = {
57+ mainnet : {
58+ rpcUrl : VITE_MAINNET_RPC_URL ,
59+ chain : mainnet ,
60+ wallets : [ capsuleWallet ] ,
61+ } ,
62+ gnosis : {
63+ rpcUrl : VITE_GNOSIS_NODE_RPC_URL ,
64+ chain : gnosis ,
65+ } ,
66+ linea : {
67+ rpcUrl : VITE_LINEA_RPC_URL ,
68+ chain : linea ,
69+ } ,
70+ foundry : {
71+ rpcUrl : VITE_LOCALHOST_NODE_RPC_URL ,
72+ chain : foundry ,
73+ } ,
74+ gnosisFork : {
75+ rpcUrl : VITE_GNOSIS_FORK_NODE_RPC_URL ,
76+ chain : gnosisFork ,
77+ chainId : VITE_GNOSIS_FORK_CHAIN_ID ,
78+ } ,
79+ cloudChain : {
80+ rpcUrl : VITE_CUSTOM_CHAIN_NODE_RPC_URL ,
81+ chain : cloudChain ,
82+ chainId : VITE_CUSTOM_CHAIN_CHAIN_ID ,
83+ } ,
84+ sepolia : {
85+ rpcUrl : VITE_SEPOLIA_RPC_URL ,
86+ chain : sepolia ,
87+ wallets : [ capsuleWallet ] ,
88+ } ,
89+ } ;
90+
91+ const chains : Chain [ ] = [ ] ;
92+ const transports : Record < string , Transport > = { } ;
93+ const customWallets : CreateWalletFn [ ] = [ ] ;
3194const recommendedWallets = [
3295 injectedWallet ,
3396 safeWallet ,
3497 rainbowWallet ,
3598 metaMaskWallet ,
3699] ;
37- const customWallets : CreateWalletFn [ ] = [ ] ;
38100
39101// WalletConnect
40102if ( VITE_WALLET_CONNECT_PROJECT_ID ) {
41103 recommendedWallets . push ( walletConnectWallet ) ;
42104}
43105
44- // mainnet
106+ // Handle mainnet first to ensure it's always the first chain. This ensures
107+ // wagmi falls back to mainnet if the user isn't connected.
45108if ( VITE_MAINNET_RPC_URL ) {
46109 chains . push ( mainnet ) ;
47110 transports [ mainnet . id ] = http ( VITE_MAINNET_RPC_URL ) ;
48111
49- // TODO: push this into the custom wallets for local and cloudchain once
50- // capsule support is verified
51- if ( capsuleWallet && ! customWallets . includes ( capsuleWallet ) ) {
112+ if (
113+ capsuleWallet &&
114+ chainConfigs [ "mainnet" ] . wallets ?. includes ( capsuleWallet )
115+ ) {
52116 customWallets . push ( capsuleWallet ) ;
53117 }
54118}
55119
56- // Local docker anvil node
57- if ( VITE_LOCALHOST_NODE_RPC_URL && VITE_LOCALHOST_NODE_RPC_URL ) {
58- chains . push ( foundry ) ;
59- transports [ foundry . id ] = http ( VITE_LOCALHOST_NODE_RPC_URL ) ;
60- }
61-
62- // Gnosis Fork
63- if ( VITE_GNOSIS_FORK_NODE_RPC_URL && VITE_GNOSIS_FORK_CHAIN_ID ) {
64- chains . push ( gnosisFork ) ;
65- transports [ gnosisFork . id ] = http ( VITE_GNOSIS_FORK_NODE_RPC_URL ) ;
66- }
120+ // Build the rest of the chains dynamically, excluding mainnet
121+ Object . values ( chainConfigs ) . forEach ( ( { rpcUrl, chain, wallets } ) => {
122+ // Exclude mainnet here
123+ if ( chain . id === mainnet . id ) {
124+ return ;
125+ }
67126
68- // CloudChain
69- if ( VITE_CUSTOM_CHAIN_NODE_RPC_URL && VITE_CUSTOM_CHAIN_CHAIN_ID ) {
70- chains . push ( cloudChain ) ;
71- transports [ cloudChain . id ] = http ( VITE_CUSTOM_CHAIN_NODE_RPC_URL ) ;
72- }
127+ chains . push ( chain ) ;
128+ transports [ chain . id ] = http ( rpcUrl ) ;
73129
74- // Sepolia
75- if ( VITE_SEPOLIA_RPC_URL ) {
76- chains . push ( sepolia ) ;
77- transports [ sepolia . id ] = http ( VITE_SEPOLIA_RPC_URL ) ;
78- // TODO: push this into the custom wallets for local and cloudchain once
79- // capsule support is verified
80- if ( capsuleWallet ) {
81- customWallets . push ( capsuleWallet ) ;
130+ // Add custom wallets if they exist
131+ if ( wallets ) {
132+ wallets . forEach ( ( wallet ) => {
133+ if ( wallet && ! customWallets . includes ( wallet ) ) {
134+ customWallets . push ( wallet ) ;
135+ }
136+ } ) ;
82137 }
83- }
84-
85- // Gnosis
86- if ( VITE_GNOSIS_NODE_RPC_URL ) {
87- chains . push ( gnosis ) ;
88- transports [ gnosis . id ] = http ( VITE_GNOSIS_NODE_RPC_URL ) ;
89- }
138+ } ) ;
90139
140+ // Prepare wallet list
91141const wallets : WalletList = [
92- {
93- groupName : "Other" ,
94- wallets : recommendedWallets ,
95- } ,
142+ { groupName : "Other" , wallets : recommendedWallets } ,
96143] ;
97- // If Capsule is not configured in your .env, then don't include it in the
98- // custom wallets
144+
99145if ( customWallets . length ) {
100146 wallets . push ( {
101147 groupName : "Log In or Sign Up with Capsule" ,
102148 wallets : customWallets ,
103149 } ) ;
104150}
151+
105152export const wagmiConfig = getDefaultConfig ( {
106153 appName : "Hyperdrive" ,
107154 projectId : VITE_WALLET_CONNECT_PROJECT_ID || "0" ,
108155 transports,
109- // Viem's type for `chains` requires at least one item in the array, but since
110- // we build the list up programmatically we must cast.
111156 chains : chains as [ Chain , ...restChains : Chain [ ] ] ,
112157 wallets,
113158} ) ;
0 commit comments