Skip to content

Commit

Permalink
feat: add drpc to list of rpc cheat codes (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmzig authored Aug 22, 2024
1 parent a5e512a commit fdecf9a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/providers/alchemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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}`;
}
19 changes: 19 additions & 0 deletions src/providers/drpc.ts
Original file line number Diff line number Diff line change
@@ -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}`;
}
12 changes: 6 additions & 6 deletions src/providers/infura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
2 changes: 1 addition & 1 deletion src/providers/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export type RPCProvider = "INFURA" | "ALCHEMY";
export type RPCProvider = "ALCHEMY" | "DRPC" | "INFURA" | "INFURA_DIN";
export type RPCTransport = "https" | "wss";
4 changes: 3 additions & 1 deletion src/providers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -14,6 +15,7 @@ const PROVIDERS = {
ALCHEMY: alchemy.getURL,
INFURA: infura.getURL,
INFURA_DIN: infura.getURL,
DRPC: drpc.getURL,
};

/**
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit fdecf9a

Please sign in to comment.