From fdecf9a0e9677f983d1a13f69c4dcadeac24e89a Mon Sep 17 00:00:00 2001 From: bmzig <57361391+bmzig@users.noreply.github.com> Date: Thu, 22 Aug 2024 07:06:59 -0500 Subject: [PATCH] feat: add drpc to list of rpc cheat codes (#706) --- src/providers/alchemy.ts | 12 ++++++------ src/providers/drpc.ts | 19 +++++++++++++++++++ src/providers/infura.ts | 12 ++++++------ src/providers/types.ts | 2 +- src/providers/utils.ts | 4 +++- 5 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 src/providers/drpc.ts diff --git a/src/providers/alchemy.ts b/src/providers/alchemy.ts index 719b4154..e3cff955 100644 --- a/src/providers/alchemy.ts +++ b/src/providers/alchemy.ts @@ -3,7 +3,7 @@ import { RPCTransport } from "./types"; const MAINNET_CHAIN_IDs = Object.values(_MAINNET_CHAIN_IDs); -// Chain-specific overrides for when the Alchemy endpoint does not match the canonical chain name. +// Chain-specific overrides for when the endpoint name does not match the canonical chain name. const endpoints: { [chainId: string]: string } = { [CHAIN_IDs.ARBITRUM]: "arb", [CHAIN_IDs.ARBITRUM_SEPOLIA]: "arb-sepolia", @@ -14,15 +14,15 @@ const endpoints: { [chainId: string]: string } = { }; export function getURL(chainId: number, apiKey: string, transport: RPCTransport): string { - let host = endpoints[chainId] ?? PUBLIC_NETWORKS[chainId]?.name; - if (!host) { + let chain = endpoints[chainId] ?? PUBLIC_NETWORKS[chainId]?.name; + if (!chain) { throw new Error(`No known Alchemy provider for chainId ${chainId}`); } if (MAINNET_CHAIN_IDs.includes(chainId)) { - host = `${host}-mainnet`; + chain = `${chain}-mainnet`; } - host = host.toLowerCase().replace(" ", "-"); + chain = chain.toLowerCase().replace(" ", "-"); - return `${transport}://${host}.g.alchemy.com/v2/${apiKey}`; + return `${transport}://${chain}.g.alchemy.com/v2/${apiKey}`; } diff --git a/src/providers/drpc.ts b/src/providers/drpc.ts new file mode 100644 index 00000000..1b68793b --- /dev/null +++ b/src/providers/drpc.ts @@ -0,0 +1,19 @@ +import { CHAIN_IDs, PUBLIC_NETWORKS } from "../constants"; +import { RPCTransport } from "./types"; + +// Chain-specific overrides for when the endpoint name does not match the canonical chain name. +const endpoints: { [chainId: string]: string } = { + [CHAIN_IDs.ARBITRUM]: "arbitrum", + [CHAIN_IDs.MAINNET]: "ethereum", +}; + +export function getURL(chainId: number, apiKey: string, transport: RPCTransport): string { + let chain = endpoints[chainId] ?? PUBLIC_NETWORKS[chainId]?.name; + if (!chain) { + throw new Error(`No known DRPC provider for chainId ${chainId}`); + } + chain = chain.toLowerCase().replace(" ", "-"); + const rpcType = transport === "https" ? "rpc" : "ws"; + + return `${transport}://lb.drpc.org/og${rpcType}?network=${chain}&dkey=${apiKey}`; +} diff --git a/src/providers/infura.ts b/src/providers/infura.ts index 97930b2f..3d7b84c0 100644 --- a/src/providers/infura.ts +++ b/src/providers/infura.ts @@ -3,21 +3,21 @@ import { RPCTransport } from "./types"; const MAINNET_CHAIN_IDs = Object.values(_MAINNET_CHAIN_IDs); -// Chain-specific overrides for when the Infura endpoint does not match the canonical chain name. +// Chain-specific overrides for when the endpoint name does not match the canonical chain name. const endpoints: { [chainId: string]: string } = { [CHAIN_IDs.ARBITRUM]: "arbitrum", }; export function getURL(chainId: number, apiKey: string, transport: RPCTransport): string { - let host = endpoints[chainId] ?? PUBLIC_NETWORKS[chainId]?.name; - if (!host) { + let chain = endpoints[chainId] ?? PUBLIC_NETWORKS[chainId]?.name; + if (!chain) { throw new Error(`No known Infura provider for chainId ${chainId}`); } if (chainId !== CHAIN_IDs.MAINNET && MAINNET_CHAIN_IDs.includes(chainId)) { - host = `${host}-mainnet`; + chain = `${chain}-mainnet`; } - host = host.toLowerCase().replace(" ", "-"); + chain = chain.toLowerCase().replace(" ", "-"); - return transport === "https" ? `https://${host}.infura.io/v3/${apiKey}` : `wss://${host}.infura.io/ws/v3/${apiKey}`; + return transport === "https" ? `https://${chain}.infura.io/v3/${apiKey}` : `wss://${chain}.infura.io/ws/v3/${apiKey}`; } diff --git a/src/providers/types.ts b/src/providers/types.ts index ca53bdcf..b61305a0 100644 --- a/src/providers/types.ts +++ b/src/providers/types.ts @@ -1,2 +1,2 @@ -export type RPCProvider = "INFURA" | "ALCHEMY"; +export type RPCProvider = "ALCHEMY" | "DRPC" | "INFURA" | "INFURA_DIN"; export type RPCTransport = "https" | "wss"; diff --git a/src/providers/utils.ts b/src/providers/utils.ts index b6b0f5e4..b1a6a927 100644 --- a/src/providers/utils.ts +++ b/src/providers/utils.ts @@ -6,6 +6,7 @@ import { isDefined } from "../utils"; import { RPCProvider, RPCTransport } from "./types"; import * as alchemy from "./alchemy"; import * as infura from "./infura"; +import * as drpc from "./drpc"; /** * Infura DIN is identified separately to allow it to be configured explicitly. @@ -14,6 +15,7 @@ const PROVIDERS = { ALCHEMY: alchemy.getURL, INFURA: infura.getURL, INFURA_DIN: infura.getURL, + DRPC: drpc.getURL, }; /** @@ -22,7 +24,7 @@ const PROVIDERS = { * @returns True if the provider string is a supported provider. */ export function isSupportedProvider(provider: string): provider is RPCProvider { - return ["ALCHEMY", "INFURA", "INFURA_DIN"].includes(provider); + return ["ALCHEMY", "INFURA", "INFURA_DIN", "DRPC"].includes(provider); } /**