|
| 1 | +/** |
| 2 | + * Localnet configuration for synapse-core |
| 3 | + * |
| 4 | + * Loads localnet-specific configuration from environment variables. |
| 5 | + */ |
| 6 | + |
| 7 | +import type { Address } from 'viem' |
| 8 | + |
| 9 | +/** |
| 10 | + * Get environment variable value with Node.js and browser compatibility |
| 11 | + */ |
| 12 | +function getEnvVar(key: string): string | undefined { |
| 13 | + // Node.js environment |
| 14 | + if (typeof process !== 'undefined' && process.env != null) { |
| 15 | + return process.env[key] |
| 16 | + } |
| 17 | + // Browser environment (if using build tools like Vite) |
| 18 | + // @ts-expect-error - import.meta.env is provided by build tools like Vite |
| 19 | + if (typeof import.meta !== 'undefined' && import.meta.env != null) { |
| 20 | + // @ts-expect-error - import.meta.env is provided by build tools like Vite |
| 21 | + return (import.meta.env as Record<string, string>)[key] |
| 22 | + } |
| 23 | + return undefined |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Localnet chain ID (default: 1414) |
| 28 | + */ |
| 29 | +export function getLocalnetChainId(): number { |
| 30 | + const envValue = getEnvVar('LOCALNET_CHAIN_ID') |
| 31 | + if (envValue != null) { |
| 32 | + const parsed = Number.parseInt(envValue, 10) |
| 33 | + if (!Number.isNaN(parsed)) { |
| 34 | + return parsed |
| 35 | + } |
| 36 | + } |
| 37 | + return 1414 |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Localnet RPC URL (default: http://127.0.0.1:5700/rpc/v1) |
| 42 | + */ |
| 43 | +export function getLocalnetRpcUrl(): string { |
| 44 | + return getEnvVar('LOCALNET_RPC_URL') ?? 'http://127.0.0.1:5700/rpc/v1' |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Localnet WebSocket URL (default: ws://127.0.0.1:5700/rpc/v1) |
| 49 | + */ |
| 50 | +export function getLocalnetWsUrl(): string { |
| 51 | + return getEnvVar('LOCALNET_RPC_WS_URL') ?? 'ws://127.0.0.1:5700/rpc/v1' |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Localnet block explorer URL (default: http://localhost:8080) |
| 56 | + */ |
| 57 | +export function getLocalnetBlockExplorerUrl(): string { |
| 58 | + return getEnvVar('LOCALNET_BLOCK_EXPLORER_URL') ?? 'http://localhost:8080' |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Get localnet contract address from environment |
| 63 | + */ |
| 64 | +function getRequiredAddress(envVarName: string, contractName: string): Address { |
| 65 | + const address = getEnvVar(envVarName) |
| 66 | + if (address == null || address === '') { |
| 67 | + throw new Error(`${contractName} address not configured for localnet. Set ${envVarName} environment variable.`) |
| 68 | + } |
| 69 | + if (!address.startsWith('0x') || address.length !== 42) { |
| 70 | + throw new Error(`Invalid ${contractName} address in ${envVarName}: ${address}`) |
| 71 | + } |
| 72 | + return address as Address |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Get optional address with default |
| 77 | + */ |
| 78 | +function getOptionalAddress(envVarName: string, defaultValue: Address): Address { |
| 79 | + const address = getEnvVar(envVarName) |
| 80 | + if (address != null && address !== '') { |
| 81 | + if (!address.startsWith('0x') || address.length !== 42) { |
| 82 | + throw new Error(`Invalid address in ${envVarName}: ${address}`) |
| 83 | + } |
| 84 | + return address as Address |
| 85 | + } |
| 86 | + return defaultValue |
| 87 | +} |
| 88 | + |
| 89 | +export function getLocalnetMulticall3Address(): Address { |
| 90 | + return getOptionalAddress('LOCALNET_MULTICALL3_ADDRESS', '0xcA11bde05977b3631167028862bE2a173976CA11') |
| 91 | +} |
| 92 | + |
| 93 | +export function getLocalnetUSDFCAddress(): Address { |
| 94 | + return getRequiredAddress('LOCALNET_USDFC_ADDRESS', 'USDFC Token') |
| 95 | +} |
| 96 | + |
| 97 | +export function getLocalnetUSDFCName(): string { |
| 98 | + return getEnvVar('LOCALNET_USDFC_NAME') ?? 'Local USDFC' |
| 99 | +} |
| 100 | + |
| 101 | +export function getLocalnetUSDFCSymbol(): string { |
| 102 | + return getEnvVar('LOCALNET_USDFC_SYMBOL') ?? 'lUSDFC' |
| 103 | +} |
| 104 | + |
| 105 | +export function getLocalnetPaymentsAddress(): Address { |
| 106 | + return getRequiredAddress('LOCALNET_PAYMENTS_ADDRESS', 'Payments') |
| 107 | +} |
| 108 | + |
| 109 | +export function getLocalnetStorageAddress(): Address { |
| 110 | + return getRequiredAddress('LOCALNET_WARM_STORAGE_ADDRESS', 'Warm Storage') |
| 111 | +} |
| 112 | + |
| 113 | +export function getLocalnetStorageViewAddress(): Address { |
| 114 | + return getRequiredAddress('LOCALNET_STORAGE_VIEW_ADDRESS', 'Storage View') |
| 115 | +} |
| 116 | + |
| 117 | +export function getLocalnetSPRegistryAddress(): Address { |
| 118 | + return getRequiredAddress('LOCALNET_SP_REGISTRY_ADDRESS', 'Service Provider Registry') |
| 119 | +} |
| 120 | + |
| 121 | +export function getLocalnetSessionKeyRegistryAddress(): Address { |
| 122 | + return getRequiredAddress('LOCALNET_SESSION_KEY_REGISTRY_ADDRESS', 'Session Key Registry') |
| 123 | +} |
| 124 | + |
| 125 | +export function getLocalnetPDPAddress(): Address { |
| 126 | + return getRequiredAddress('LOCALNET_PDP_VERIFIER_ADDRESS', 'PDP Verifier') |
| 127 | +} |
0 commit comments