wagmiConfig.ts
1 import { Emitter } from '@wagmi/core/internal'; 2 import { getDefaultConfig } from 'connectkit'; 3 import { 4 ENABLE_TESTNET, 5 FORK_BASE_CHAIN_ID, 6 FORK_CHAIN_ID, 7 FORK_ENABLED, 8 FORK_RPC_URL, 9 networkConfigs, 10 } from 'src/utils/marketsAndNetworksConfig'; 11 import { type Chain } from 'viem'; 12 import { createConfig, CreateConfigParameters, http } from 'wagmi'; 13 import { injected, safe } from 'wagmi/connectors'; 14 15 import { prodNetworkConfig, testnetConfig } from './networksConfig'; 16 17 const testnetChains = Object.values(testnetConfig).map((config) => config.wagmiChain) as [ 18 Chain, 19 ...Chain[] 20 ]; 21 22 let prodChains = Object.values(prodNetworkConfig).map((config) => config.wagmiChain) as [ 23 Chain, 24 ...Chain[] 25 ]; 26 27 const { name, baseAssetDecimals, baseAssetSymbol } = networkConfigs[FORK_BASE_CHAIN_ID]; 28 29 const forkChain: Chain = { 30 id: FORK_CHAIN_ID, 31 name: `${name} Fork`, 32 nativeCurrency: { 33 decimals: baseAssetDecimals, 34 name: baseAssetSymbol, 35 symbol: baseAssetSymbol, 36 }, 37 rpcUrls: { 38 default: { http: [FORK_RPC_URL] }, 39 }, 40 testnet: false, 41 }; 42 43 if (FORK_ENABLED) { 44 prodChains = [forkChain, ...prodChains]; 45 } 46 47 const defaultConfig = { 48 walletConnectProjectId: process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID as string, 49 appName: 'Aave', 50 appDescription: 'Non-custodial liquidity protocol', 51 appUrl: 'https://app.aave.com', 52 appIcon: 'https://avatars.githubusercontent.com/u/47617460?s=200&v=4', 53 }; 54 55 const cypressConfig = createConfig( 56 getDefaultConfig({ 57 chains: [forkChain], 58 connectors: [injected()], 59 ...defaultConfig, 60 }) 61 ); 62 63 const getTransport = (chainId: number) => { 64 return networkConfigs[chainId].publicJsonRPCUrl[0]; 65 }; 66 67 const buildTransports = (chains: CreateConfigParameters['chains']) => 68 Object.fromEntries(chains.map((chain) => [chain.id, http(getTransport(chain.id))])); 69 70 const prodCkConfig = getDefaultConfig({ 71 chains: ENABLE_TESTNET ? testnetChains : prodChains, 72 transports: ENABLE_TESTNET ? undefined : buildTransports(prodChains), 73 ...defaultConfig, 74 }); 75 76 const familyConnectorId = 'familyAccountsProvider'; 77 78 const connectorConfig = { 79 chains: prodCkConfig.chains, 80 emitter: new Emitter(''), 81 }; 82 83 const connectors = prodCkConfig.connectors 84 ?.map((connector) => { 85 // initialize the connector with the emitter so we can access the id 86 const c = connector(connectorConfig); 87 if (c.id === 'safe') { 88 return safe({ 89 allowedDomains: [/gnosis-safe.io$/, /app.safe.global$/, /dhedge.org$/], 90 }); 91 } else { 92 return connector; 93 } 94 }) 95 .sort((a, b) => { 96 // sort connectors so the family connector is last 97 // fixes slow wallet connections when running in the Safe UI 98 if (a(connectorConfig).id === familyConnectorId) { 99 return 1; 100 } 101 if (b(connectorConfig).id === familyConnectorId) { 102 return -1; 103 } 104 return 0; 105 }); 106 107 const prodConfig = createConfig({ 108 ...prodCkConfig, 109 connectors, 110 }); 111 112 const isCypressEnabled = process.env.NEXT_PUBLIC_IS_CYPRESS_ENABLED === 'true'; 113 114 export const wagmiConfig = isCypressEnabled ? cypressConfig : prodConfig;