|
| 1 | +import {HttpTransportConfig, createClient, http} from 'viem'; |
| 2 | +import { |
| 3 | + mainnet, |
| 4 | + arbitrum, |
| 5 | + polygon, |
| 6 | + optimism, |
| 7 | + metis, |
| 8 | + base, |
| 9 | + baseSepolia, |
| 10 | + sepolia, |
| 11 | + goerli, |
| 12 | + bsc, |
| 13 | + avalanche, |
| 14 | + gnosis, |
| 15 | + polygonZkEvm, |
| 16 | + scroll, |
| 17 | + celo, |
| 18 | + zkSync, |
| 19 | + avalancheFuji, |
| 20 | + polygonMumbai, |
| 21 | + arbitrumGoerli, |
| 22 | + optimismGoerli, |
| 23 | + optimismSepolia, |
| 24 | + scrollSepolia, |
| 25 | + arbitrumSepolia, |
| 26 | + // deprecated |
| 27 | + harmonyOne, |
| 28 | + fantomTestnet, |
| 29 | + fantom, |
| 30 | +} from 'viem/chains'; |
| 31 | +import {Client} from 'viem'; |
| 32 | +import {ChainId} from '@bgd-labs/js-utils'; |
| 33 | + |
| 34 | +const commonConfig: HttpTransportConfig = {timeout: 30_000, batch: true}; |
| 35 | +const batchConfig = {batch: {multicall: true}}; |
| 36 | + |
| 37 | +export const mainnetClient = createClient({ |
| 38 | + chain: mainnet, |
| 39 | + transport: http(process.env.RPC_MAINNET, commonConfig), |
| 40 | + ...batchConfig, |
| 41 | +}); |
| 42 | + |
| 43 | +export const arbitrumClient = createClient({ |
| 44 | + chain: arbitrum, |
| 45 | + transport: http(process.env.RPC_ARBITRUM, commonConfig), |
| 46 | + ...batchConfig, |
| 47 | +}); |
| 48 | + |
| 49 | +export const polygonClient = createClient({ |
| 50 | + chain: polygon, |
| 51 | + transport: http(process.env.RPC_POLYGON, commonConfig), |
| 52 | + ...batchConfig, |
| 53 | +}); |
| 54 | + |
| 55 | +export const optimismClient = createClient({ |
| 56 | + chain: optimism, |
| 57 | + transport: http(process.env.RPC_OPTIMISM, commonConfig), |
| 58 | + ...batchConfig, |
| 59 | +}); |
| 60 | + |
| 61 | +export const metisClient = createClient({ |
| 62 | + chain: metis, |
| 63 | + transport: http(process.env.RPC_METIS, commonConfig), |
| 64 | + ...batchConfig, |
| 65 | +}); |
| 66 | + |
| 67 | +export const baseClient = createClient({ |
| 68 | + chain: base, |
| 69 | + transport: http(process.env.RPC_BASE, commonConfig), |
| 70 | + ...batchConfig, |
| 71 | +}); |
| 72 | + |
| 73 | +export const bnbClient = createClient({ |
| 74 | + chain: bsc, |
| 75 | + transport: http(process.env.RPC_BNB, commonConfig), |
| 76 | + ...batchConfig, |
| 77 | +}); |
| 78 | + |
| 79 | +export const avalancheClient = createClient({ |
| 80 | + chain: avalanche, |
| 81 | + transport: http(process.env.RPC_AVALANCHE, commonConfig), |
| 82 | + ...batchConfig, |
| 83 | +}); |
| 84 | + |
| 85 | +export const gnosisClient = createClient({ |
| 86 | + chain: gnosis, |
| 87 | + transport: http(process.env.RPC_GNOSIS, commonConfig), |
| 88 | + ...batchConfig, |
| 89 | +}); |
| 90 | + |
| 91 | +export const scrollClient = createClient({ |
| 92 | + chain: scroll, |
| 93 | + transport: http(process.env.RPC_SCROLL, commonConfig), |
| 94 | + ...batchConfig, |
| 95 | +}); |
| 96 | + |
| 97 | +export const zkEVMClient = createClient({ |
| 98 | + chain: polygonZkEvm, |
| 99 | + transport: http(process.env.RPC_ZKEVM, commonConfig), |
| 100 | + ...batchConfig, |
| 101 | +}); |
| 102 | + |
| 103 | +export const celoClient = createClient({ |
| 104 | + chain: celo, |
| 105 | + transport: http(process.env.RPC_CELO, commonConfig), |
| 106 | + ...batchConfig, |
| 107 | +}); |
| 108 | + |
| 109 | +export const zkSyncClient = createClient({ |
| 110 | + chain: zkSync, |
| 111 | + transport: http(process.env.RPC_ZKSYNC, commonConfig), |
| 112 | + ...batchConfig, |
| 113 | +}); |
| 114 | + |
| 115 | +// testnets |
| 116 | +export const fujiClient = createClient({ |
| 117 | + chain: avalancheFuji, |
| 118 | + transport: http(process.env.RPC_FUJI, commonConfig), |
| 119 | + ...batchConfig, |
| 120 | +}); |
| 121 | + |
| 122 | +export const mumbaiClient = createClient({ |
| 123 | + chain: polygonMumbai, |
| 124 | + transport: http(process.env.RPC_MUMBAI, commonConfig), |
| 125 | + ...batchConfig, |
| 126 | +}); |
| 127 | + |
| 128 | +export const sepoliaClient = createClient({ |
| 129 | + chain: sepolia, |
| 130 | + transport: http(process.env.RPC_SEPOLIA, commonConfig), |
| 131 | + ...batchConfig, |
| 132 | +}); |
| 133 | + |
| 134 | +export const goerliClient = createClient({ |
| 135 | + chain: goerli, |
| 136 | + transport: http(process.env.RPC_GOERLI, commonConfig), |
| 137 | + ...batchConfig, |
| 138 | +}); |
| 139 | + |
| 140 | +export const arbitrumGoerliClient = createClient({ |
| 141 | + chain: arbitrumGoerli, |
| 142 | + transport: http(process.env.RPC_ARBITRUM_GOERLI, commonConfig), |
| 143 | + ...batchConfig, |
| 144 | +}); |
| 145 | + |
| 146 | +export const arbitrumSepoliaClient = createClient({ |
| 147 | + chain: arbitrumSepolia, |
| 148 | + transport: http(process.env.RPC_ARBITRUM_SEPOLIA, commonConfig), |
| 149 | + ...batchConfig, |
| 150 | +}); |
| 151 | + |
| 152 | +export const optimismGoerliClient = createClient({ |
| 153 | + chain: optimismGoerli, |
| 154 | + transport: http(process.env.RPC_OPTIMISM_GOERLI, commonConfig), |
| 155 | + ...batchConfig, |
| 156 | +}); |
| 157 | + |
| 158 | +export const optimismSepoliaClient = createClient({ |
| 159 | + chain: optimismSepolia, |
| 160 | + transport: http(process.env.RPC_OPTIMISM_SEPOLIA, commonConfig), |
| 161 | + ...batchConfig, |
| 162 | +}); |
| 163 | + |
| 164 | +export const scrollSepoliaClient = createClient({ |
| 165 | + chain: scrollSepolia, |
| 166 | + transport: http(process.env.RPC_SCROLL_SEPOLIA, commonConfig), |
| 167 | + ...batchConfig, |
| 168 | +}); |
| 169 | + |
| 170 | +export const baseSepoliaClient = createClient({ |
| 171 | + chain: baseSepolia, |
| 172 | + transport: http(process.env.RPC_BASE_SEPOLIA, commonConfig), |
| 173 | + ...batchConfig, |
| 174 | +}); |
| 175 | + |
| 176 | +// deprecated |
| 177 | + |
| 178 | +export const fantomTestnetClient = createClient({ |
| 179 | + chain: fantomTestnet, |
| 180 | + transport: http(process.env.RPC_FANTOM_TESTNET, commonConfig), |
| 181 | + ...batchConfig, |
| 182 | +}); |
| 183 | + |
| 184 | +export const fantomClient = createClient({ |
| 185 | + chain: fantomTestnet, |
| 186 | + transport: http(process.env.RPC_FANTOM, commonConfig), |
| 187 | + ...batchConfig, |
| 188 | +}); |
| 189 | + |
| 190 | +export const harmonyClient = createClient({ |
| 191 | + chain: harmonyOne, |
| 192 | + transport: http(process.env.RPC_HARMONY, commonConfig), |
| 193 | + ...batchConfig, |
| 194 | +}); |
| 195 | + |
| 196 | +export const CHAIN_ID_CLIENT_MAP: Record<number, Client> = { |
| 197 | + [ChainId.mainnet]: mainnetClient, |
| 198 | + [ChainId.arbitrum_one]: arbitrumClient, |
| 199 | + [ChainId.arbitrum_goerli]: arbitrumGoerliClient, |
| 200 | + [ChainId.arbitrum_sepolia]: arbitrumSepoliaClient, |
| 201 | + [ChainId.polygon]: polygonClient, |
| 202 | + [ChainId.optimism]: optimismClient, |
| 203 | + [ChainId.optimism_goerli]: optimismGoerliClient, |
| 204 | + [ChainId.optimism_sepolia]: optimismSepoliaClient, |
| 205 | + [ChainId.metis]: metisClient, |
| 206 | + [ChainId.base]: baseClient, |
| 207 | + [ChainId.base_sepolia]: baseSepoliaClient, |
| 208 | + [ChainId.sepolia]: sepoliaClient, |
| 209 | + [ChainId.goerli]: goerliClient, |
| 210 | + [ChainId.bnb]: bnbClient, |
| 211 | + [ChainId.avalanche]: avalancheClient, |
| 212 | + [ChainId.gnosis]: gnosisClient, |
| 213 | + [ChainId.scroll]: scrollClient, |
| 214 | + [ChainId.scroll_sepolia]: scrollSepoliaClient, |
| 215 | + [ChainId.zkEVM]: zkEVMClient, |
| 216 | + [ChainId.celo]: celoClient, |
| 217 | + [ChainId.zkSync]: zkSyncClient, |
| 218 | + [ChainId.fuji]: fujiClient, |
| 219 | + [ChainId.mumbai]: mumbaiClient, |
| 220 | + // deprecated |
| 221 | + [ChainId.harmony]: harmonyClient, |
| 222 | + [ChainId.fantom]: fantomClient, |
| 223 | + [ChainId.fantom_testnet]: fantomTestnetClient, |
| 224 | +} as const; |
0 commit comments