Skip to content

Commit 2fffd9e

Browse files
authored
RPC Overrides (#1057)
1 parent 938e785 commit 2fffd9e

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

packages/nextjs/scaffold.config.ts

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export type ScaffoldConfig = {
44
targetNetworks: readonly chains.Chain[];
55
pollingInterval: number;
66
alchemyApiKey: string;
7+
rpcOverrides?: Record<number, string>;
78
walletConnectProjectId: string;
89
onlyLocalBurnerWallet: boolean;
910
};
@@ -24,6 +25,13 @@ const scaffoldConfig = {
2425
// .env.local for local testing, and in the Vercel/system env config for live apps.
2526
alchemyApiKey: process.env.NEXT_PUBLIC_ALCHEMY_API_KEY || DEFAULT_ALCHEMY_API_KEY,
2627

28+
// If you want to use a different RPC for a specific network, you can add it here.
29+
// The key is the chain ID, and the value is the HTTP RPC URL
30+
rpcOverrides: {
31+
// Example:
32+
// [chains.mainnet.id]: "https://mainnet.buidlguidl.com",
33+
},
34+
2735
// This is ours WalletConnect's default project ID.
2836
// You can get your own at https://cloud.walletconnect.com
2937
// It's recommended to store it in an env variable:

packages/nextjs/services/web3/wagmiConfig.tsx

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { wagmiConnectors } from "./wagmiConnectors";
22
import { Chain, createClient, fallback, http } from "viem";
33
import { hardhat, mainnet } from "viem/chains";
44
import { createConfig } from "wagmi";
5-
import scaffoldConfig, { DEFAULT_ALCHEMY_API_KEY } from "~~/scaffold.config";
5+
import scaffoldConfig, { DEFAULT_ALCHEMY_API_KEY, ScaffoldConfig } from "~~/scaffold.config";
66
import { getAlchemyHttpUrl } from "~~/utils/scaffold-eth";
77

88
const { targetNetworks } = scaffoldConfig;
@@ -19,11 +19,16 @@ export const wagmiConfig = createConfig({
1919
client({ chain }) {
2020
let rpcFallbacks = [http()];
2121

22-
const alchemyHttpUrl = getAlchemyHttpUrl(chain.id);
23-
if (alchemyHttpUrl) {
24-
const isUsingDefaultKey = scaffoldConfig.alchemyApiKey === DEFAULT_ALCHEMY_API_KEY;
25-
// If using default Scaffold-ETH 2 API key, we prioritize the default RPC
26-
rpcFallbacks = isUsingDefaultKey ? [http(), http(alchemyHttpUrl)] : [http(alchemyHttpUrl), http()];
22+
const rpcOverrideUrl = (scaffoldConfig.rpcOverrides as ScaffoldConfig["rpcOverrides"])?.[chain.id];
23+
if (rpcOverrideUrl) {
24+
rpcFallbacks = [http(rpcOverrideUrl), http()];
25+
} else {
26+
const alchemyHttpUrl = getAlchemyHttpUrl(chain.id);
27+
if (alchemyHttpUrl) {
28+
const isUsingDefaultKey = scaffoldConfig.alchemyApiKey === DEFAULT_ALCHEMY_API_KEY;
29+
// If using default Scaffold-ETH 2 API key, we prioritize the default RPC
30+
rpcFallbacks = isUsingDefaultKey ? [http(), http(alchemyHttpUrl)] : [http(alchemyHttpUrl), http()];
31+
}
2732
}
2833

2934
return createClient({

0 commit comments

Comments
 (0)