|
| 1 | +import { |
| 2 | + alchemyApiKey, |
| 3 | + drpcApiPkey, |
| 4 | + infuraApiKey, |
| 5 | +} from "../utils/constants.js"; |
| 6 | +import { PublicClient, createPublicClient, fallback } from "viem"; |
| 7 | +import { ChainFactory } from "./chainFactory.js"; |
| 8 | +import { RpcClientFactory } from "./rpcClientFactory.js"; |
| 9 | +import { JsonRpcProvider } from "ethers"; |
| 10 | + |
| 11 | +interface RpcProvider { |
| 12 | + getUrl(chainId: number): string | undefined; |
| 13 | +} |
| 14 | + |
| 15 | +class AlchemyProvider implements RpcProvider { |
| 16 | + getUrl(chainId: number): string | undefined { |
| 17 | + const urls: Record<number, string> = { |
| 18 | + 10: `https://opt-mainnet.g.alchemy.com/v2/${alchemyApiKey}`, |
| 19 | + 8453: `https://base-mainnet.g.alchemy.com/v2/${alchemyApiKey}`, |
| 20 | + 42161: `https://arb-mainnet.g.alchemy.com/v2/${alchemyApiKey}`, |
| 21 | + 421614: `https://arb-sepolia.g.alchemy.com/v2/${alchemyApiKey}`, |
| 22 | + 84532: `https://base-sepolia.g.alchemy.com/v2/${alchemyApiKey}`, |
| 23 | + 11155111: `https://eth-sepolia.g.alchemy.com/v2/${alchemyApiKey}`, |
| 24 | + }; |
| 25 | + return urls[chainId]; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class InfuraProvider implements RpcProvider { |
| 30 | + getUrl(chainId: number): string | undefined { |
| 31 | + const urls: Record<number, string> = { |
| 32 | + 10: `https://optimism-mainnet.infura.io/v3/${infuraApiKey}`, |
| 33 | + 42220: `https://celo-mainnet.infura.io/v3/${infuraApiKey}`, |
| 34 | + 42161: `https://arbitrum-mainnet.infura.io/v3/${infuraApiKey}`, |
| 35 | + 421614: `https://arbitrum-sepolia.infura.io/v3/${infuraApiKey}`, |
| 36 | + }; |
| 37 | + return urls[chainId]; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +class DrpcProvider implements RpcProvider { |
| 42 | + getUrl(chainId: number): string | undefined { |
| 43 | + const networks: Record<number, string> = { |
| 44 | + 10: "optimism", |
| 45 | + 8453: "base", |
| 46 | + 42220: "celo", |
| 47 | + 42161: "arbitrum", |
| 48 | + 421614: "arbitrum-sepolia", |
| 49 | + }; |
| 50 | + const network = networks[chainId]; |
| 51 | + return network |
| 52 | + ? `https://lb.drpc.org/ogrpc?network=${network}&dkey=${drpcApiPkey}` |
| 53 | + : undefined; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +class GlifProvider implements RpcProvider { |
| 58 | + getUrl(chainId: number): string | undefined { |
| 59 | + const urls: Record<number, string> = { |
| 60 | + 314: `https://node.glif.io/space07/lotus/rpc/v1`, |
| 61 | + 314159: `https://calibration.node.glif.io/archive/lotus/rpc/v1`, |
| 62 | + }; |
| 63 | + return urls[chainId]; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +export class EvmClientFactory { |
| 68 | + private static readonly providers: RpcProvider[] = [ |
| 69 | + new AlchemyProvider(), |
| 70 | + new InfuraProvider(), |
| 71 | + new DrpcProvider(), |
| 72 | + new GlifProvider(), |
| 73 | + ]; |
| 74 | + |
| 75 | + static createViemClient(chainId: number): PublicClient { |
| 76 | + const urls = EvmClientFactory.getAllAvailableUrls(chainId); |
| 77 | + if (urls.length === 0) |
| 78 | + throw new Error(`No RPC URL available for chain ${chainId}`); |
| 79 | + |
| 80 | + const transports = urls.map((url) => |
| 81 | + RpcClientFactory.createViemTransport(chainId, url), |
| 82 | + ); |
| 83 | + |
| 84 | + return createPublicClient({ |
| 85 | + chain: ChainFactory.getChain(chainId), |
| 86 | + transport: fallback(transports), |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + static createEthersClient(chainId: number): JsonRpcProvider { |
| 91 | + const url = EvmClientFactory.getFirstAvailableUrl(chainId); |
| 92 | + if (!url) throw new Error(`No RPC URL available for chain ${chainId}`); |
| 93 | + return RpcClientFactory.createEthersJsonRpcProvider(chainId, url); |
| 94 | + } |
| 95 | + |
| 96 | + static getAllAvailableUrls(chainId: number): string[] { |
| 97 | + return EvmClientFactory.providers |
| 98 | + .map((provider) => provider.getUrl(chainId)) |
| 99 | + .filter((url): url is string => url !== undefined); |
| 100 | + } |
| 101 | + |
| 102 | + static getRpcUrl(chainId: number): string { |
| 103 | + const url = EvmClientFactory.getFirstAvailableUrl(chainId); |
| 104 | + if (!url) throw new Error(`No RPC URL available for chain ${chainId}`); |
| 105 | + return url; |
| 106 | + } |
| 107 | + |
| 108 | + static getPublicRpcUrl(chainId: number): string { |
| 109 | + const chain = ChainFactory.getChain(chainId); |
| 110 | + if (!chain.rpcUrls?.default?.http?.[0]) { |
| 111 | + throw new Error(`No public RPC URL available for chain ${chainId}`); |
| 112 | + } |
| 113 | + return chain.rpcUrls.default.http[0]; |
| 114 | + } |
| 115 | + |
| 116 | + // Keep this for backward compatibility |
| 117 | + static getFirstAvailableUrl(chainId: number): string | undefined { |
| 118 | + return EvmClientFactory.getAllAvailableUrls(chainId)[0]; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// Public API |
| 123 | +export const getSupportedChains = ChainFactory.getSupportedChains; |
0 commit comments