diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index 5d8dd65..b37993e 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -18,6 +18,35 @@ export const blockConfig = ( endBlock: end, }); +/** + * Gets the RPC endpoint URL for a given chain ID. + * + * @param chainId the chain ID to get the RPC URL for + * @returns the URL of the RPC endpoint + */ +export const rpcEndpointUrl = (chainId: number): string => { + /** + * Reads the RPC URL for a given chain ID from the environment variable: + * RPC_URL_{chainId}. For example, for Ethereum mainnet the chainId is `1`, + * so the env variable can be set as `RPC_URL_1=https://eth.drpc.org`. + */ + const envVarName = `RPC_URL_${chainId}`; + + // no RPC URL provided in env var + if (!process.env[envVarName]) { + // throw an error, as the RPC URL is required and no defaults apply + throw new Error(`Missing '${envVarName}' environment variable`); + } + + try { + return new URL(process.env[envVarName] as string).toString(); + } catch (e) { + throw new Error( + `Invalid '${envVarName}' environment variable. Please provide a valid URL.`, + ); + } +}; + // default request per second rate limit for RPC endpoints const DEFAULT_RPC_RATE_LIMIT = 50; diff --git a/src/plugins/base.eth/ponder.config.ts b/src/plugins/base.eth/ponder.config.ts index 2bf7f04..338f782 100644 --- a/src/plugins/base.eth/ponder.config.ts +++ b/src/plugins/base.eth/ponder.config.ts @@ -2,7 +2,7 @@ import { type ContractConfig, createConfig, factory } from "ponder"; import { http, getAbiItem } from "viem"; import { base } from "viem/chains"; -import { blockConfig, rpcRequestRateLimit } from "../../lib/helpers"; +import { blockConfig, rpcEndpointUrl, rpcRequestRateLimit } from "../../lib/helpers"; import { createPluginNamespace } from "../../lib/plugin-helpers"; import { BaseRegistrar } from "./abis/BaseRegistrar"; import { EarlyAccessRegistrarController } from "./abis/EARegistrarController"; @@ -23,7 +23,7 @@ export const config = createConfig({ networks: { base: { chainId: base.id, - transport: http(process.env[`RPC_URL_${base.id}`]), + transport: http(rpcEndpointUrl(base.id)), maxRequestsPerSecond: rpcRequestRateLimit(base.id), }, }, diff --git a/src/plugins/eth/ponder.config.ts b/src/plugins/eth/ponder.config.ts index 5c4a216..8ecc94b 100644 --- a/src/plugins/eth/ponder.config.ts +++ b/src/plugins/eth/ponder.config.ts @@ -2,7 +2,7 @@ import { ContractConfig, createConfig, factory, mergeAbis } from "ponder"; import { http, getAbiItem } from "viem"; import { mainnet } from "viem/chains"; -import { blockConfig, rpcRequestRateLimit } from "../../lib/helpers"; +import { blockConfig, rpcEndpointUrl, rpcRequestRateLimit } from "../../lib/helpers"; import { createPluginNamespace } from "../../lib/plugin-helpers"; import { BaseRegistrar } from "./abis/BaseRegistrar"; import { EthRegistrarController } from "./abis/EthRegistrarController"; @@ -27,7 +27,7 @@ export const config = createConfig({ networks: { mainnet: { chainId: mainnet.id, - transport: http(process.env[`RPC_URL_${mainnet.id}`]), + transport: http(rpcEndpointUrl(mainnet.id)), maxRequestsPerSecond: rpcRequestRateLimit(mainnet.id), }, }, diff --git a/src/plugins/linea.eth/ponder.config.ts b/src/plugins/linea.eth/ponder.config.ts index 56972b6..0b13359 100644 --- a/src/plugins/linea.eth/ponder.config.ts +++ b/src/plugins/linea.eth/ponder.config.ts @@ -2,7 +2,7 @@ import { ContractConfig, createConfig, factory, mergeAbis } from "ponder"; import { http, getAbiItem } from "viem"; import { linea } from "viem/chains"; -import { blockConfig, rpcRequestRateLimit } from "../../lib/helpers"; +import { blockConfig, rpcEndpointUrl, rpcRequestRateLimit } from "../../lib/helpers"; import { createPluginNamespace } from "../../lib/plugin-helpers"; import { BaseRegistrar } from "./abis/BaseRegistrar"; import { EthRegistrarController } from "./abis/EthRegistrarController"; @@ -23,7 +23,7 @@ export const config = createConfig({ networks: { linea: { chainId: linea.id, - transport: http(process.env[`RPC_URL_${linea.id}`]), + transport: http(rpcEndpointUrl(linea.id)), maxRequestsPerSecond: rpcRequestRateLimit(linea.id), }, },