|
| 1 | +import type { ETHConfig } from "@babylonlabs-io/wallet-connector"; |
| 2 | +import { mainnet, sepolia } from "viem/chains"; |
| 3 | +import type { Chain } from "viem"; |
| 4 | +import { defineChain } from "viem"; |
| 5 | + |
| 6 | +// Define localhost/Anvil chain (31337) since viem's localhost is 1337 |
| 7 | +export const localhost = defineChain({ |
| 8 | + id: 31337, |
| 9 | + name: 'Localhost', |
| 10 | + nativeCurrency: { |
| 11 | + decimals: 18, |
| 12 | + name: 'Ether', |
| 13 | + symbol: 'ETH', |
| 14 | + }, |
| 15 | + rpcUrls: { |
| 16 | + default: { |
| 17 | + http: ['http://127.0.0.1:8545'], |
| 18 | + }, |
| 19 | + }, |
| 20 | +}); |
| 21 | + |
| 22 | +const defaultNetwork = "testnet"; |
| 23 | +// Export network for modules that need to know which network is active |
| 24 | +export const network = process.env.NEXT_PUBLIC_NETWORK ?? defaultNetwork; |
| 25 | + |
| 26 | +// Extended config type for UI-specific properties |
| 27 | +export type ExtendedETHConfig = ETHConfig & { |
| 28 | + name: string; |
| 29 | + displayUSD: boolean; |
| 30 | +}; |
| 31 | + |
| 32 | +type Config = ExtendedETHConfig; |
| 33 | + |
| 34 | +const config: Record<string, Config> = { |
| 35 | + mainnet: { |
| 36 | + name: "Ethereum", |
| 37 | + chainId: 1, |
| 38 | + chainName: "Ethereum Mainnet", |
| 39 | + rpcUrl: process.env.NEXT_PUBLIC_ETH_RPC_URL || "https://eth.llamarpc.com", |
| 40 | + explorerUrl: "https://etherscan.io", |
| 41 | + nativeCurrency: { |
| 42 | + name: "Ether", |
| 43 | + symbol: "ETH", |
| 44 | + decimals: 18, |
| 45 | + }, |
| 46 | + displayUSD: true, |
| 47 | + }, |
| 48 | + canary: { |
| 49 | + // Using Sepolia for canary |
| 50 | + name: "Ethereum Sepolia", |
| 51 | + chainId: 11155111, |
| 52 | + chainName: "Sepolia Testnet", |
| 53 | + rpcUrl: process.env.NEXT_PUBLIC_ETH_RPC_URL || "https://rpc.sepolia.org", |
| 54 | + explorerUrl: "https://sepolia.etherscan.io", |
| 55 | + nativeCurrency: { |
| 56 | + name: "Sepolia ETH", |
| 57 | + symbol: "ETH", |
| 58 | + decimals: 18, |
| 59 | + }, |
| 60 | + displayUSD: false, |
| 61 | + }, |
| 62 | + testnet: { |
| 63 | + name: "Ethereum Sepolia", |
| 64 | + chainId: 11155111, |
| 65 | + chainName: "Sepolia Testnet", |
| 66 | + rpcUrl: process.env.NEXT_PUBLIC_ETH_RPC_URL || "https://rpc.sepolia.org", |
| 67 | + explorerUrl: "https://sepolia.etherscan.io", |
| 68 | + nativeCurrency: { |
| 69 | + name: "Sepolia ETH", |
| 70 | + symbol: "ETH", |
| 71 | + decimals: 18, |
| 72 | + }, |
| 73 | + displayUSD: false, |
| 74 | + }, |
| 75 | + devnet: { |
| 76 | + name: "Ethereum Sepolia", |
| 77 | + chainId: 11155111, |
| 78 | + chainName: "Sepolia Testnet", |
| 79 | + rpcUrl: process.env.NEXT_PUBLIC_ETH_RPC_URL || "https://rpc.sepolia.org", |
| 80 | + explorerUrl: "https://sepolia.etherscan.io", |
| 81 | + nativeCurrency: { |
| 82 | + name: "Sepolia ETH", |
| 83 | + symbol: "ETH", |
| 84 | + decimals: 18, |
| 85 | + }, |
| 86 | + displayUSD: false, |
| 87 | + }, |
| 88 | + localhost: { |
| 89 | + name: "Local Anvil", |
| 90 | + chainId: 31337, |
| 91 | + chainName: "Localhost", |
| 92 | + rpcUrl: process.env.NEXT_PUBLIC_ETH_RPC_URL || "http://localhost:8545", |
| 93 | + explorerUrl: "", |
| 94 | + nativeCurrency: { |
| 95 | + name: "Ether", |
| 96 | + symbol: "ETH", |
| 97 | + decimals: 18, |
| 98 | + }, |
| 99 | + displayUSD: false, |
| 100 | + }, |
| 101 | +}; |
| 102 | + |
| 103 | +export function getNetworkConfigETH(): Config { |
| 104 | + // Use the chain ID from environment if available |
| 105 | + const chainId = parseInt(process.env.NEXT_PUBLIC_ETH_CHAIN_ID || "0"); |
| 106 | + |
| 107 | + // If chain ID is set, use chain ID-based config |
| 108 | + if (chainId === 1) { |
| 109 | + return config.mainnet; |
| 110 | + } else if (chainId === 31337) { |
| 111 | + return config.localhost; |
| 112 | + } else if (chainId === 11155111) { |
| 113 | + return config.testnet; |
| 114 | + } |
| 115 | + |
| 116 | + // Otherwise use the network-based config |
| 117 | + if (network === "localhost") { |
| 118 | + return config.localhost; |
| 119 | + } |
| 120 | + |
| 121 | + return config[network] ?? config[defaultNetwork]; |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Get viem Chain object for the current network configuration |
| 126 | + * Used by contract clients that need Chain object |
| 127 | + */ |
| 128 | +export function getETHChain(): Chain { |
| 129 | + const config = getNetworkConfigETH(); |
| 130 | + |
| 131 | + // Map chainId to viem chain object |
| 132 | + switch (config.chainId) { |
| 133 | + case 1: |
| 134 | + return mainnet; |
| 135 | + case 11155111: |
| 136 | + return sepolia; |
| 137 | + case 31337: |
| 138 | + return localhost; |
| 139 | + default: |
| 140 | + // Default to testnet (Sepolia) |
| 141 | + return sepolia; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Validate Ethereum address format |
| 147 | + * @param address - Address to validate |
| 148 | + * @throws Error if address format is invalid |
| 149 | + */ |
| 150 | +export function validateETHAddress(address: string): void { |
| 151 | + // Basic ETH address validation |
| 152 | + if (!address.match(/^0x[a-fA-F0-9]{40}$/)) { |
| 153 | + throw new Error( |
| 154 | + "Invalid Ethereum address format. Expected address to start with '0x' followed by 40 hexadecimal characters." |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + // TODO: Add checksum validation when needed |
| 159 | +} |
0 commit comments