Skip to content

Commit

Permalink
feat(helpers): introduce rpcEndpointUrl factory
Browse files Browse the repository at this point in the history
  • Loading branch information
tk-o committed Jan 17, 2025
1 parent 141e57c commit 77cc78b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
29 changes: 29 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/base.eth/ponder.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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),
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/eth/ponder.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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),
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/linea.eth/ponder.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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),
},
},
Expand Down

0 comments on commit 77cc78b

Please sign in to comment.