Skip to content

Commit

Permalink
RPC Overrides (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
carletex authored Feb 25, 2025
1 parent 938e785 commit 2fffd9e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/nextjs/scaffold.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type ScaffoldConfig = {
targetNetworks: readonly chains.Chain[];
pollingInterval: number;
alchemyApiKey: string;
rpcOverrides?: Record<number, string>;
walletConnectProjectId: string;
onlyLocalBurnerWallet: boolean;
};
Expand All @@ -24,6 +25,13 @@ const scaffoldConfig = {
// .env.local for local testing, and in the Vercel/system env config for live apps.
alchemyApiKey: process.env.NEXT_PUBLIC_ALCHEMY_API_KEY || DEFAULT_ALCHEMY_API_KEY,

// If you want to use a different RPC for a specific network, you can add it here.
// The key is the chain ID, and the value is the HTTP RPC URL
rpcOverrides: {
// Example:
// [chains.mainnet.id]: "https://mainnet.buidlguidl.com",
},

// This is ours WalletConnect's default project ID.
// You can get your own at https://cloud.walletconnect.com
// It's recommended to store it in an env variable:
Expand Down
17 changes: 11 additions & 6 deletions packages/nextjs/services/web3/wagmiConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { wagmiConnectors } from "./wagmiConnectors";
import { Chain, createClient, fallback, http } from "viem";
import { hardhat, mainnet } from "viem/chains";
import { createConfig } from "wagmi";
import scaffoldConfig, { DEFAULT_ALCHEMY_API_KEY } from "~~/scaffold.config";
import scaffoldConfig, { DEFAULT_ALCHEMY_API_KEY, ScaffoldConfig } from "~~/scaffold.config";
import { getAlchemyHttpUrl } from "~~/utils/scaffold-eth";

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

const alchemyHttpUrl = getAlchemyHttpUrl(chain.id);
if (alchemyHttpUrl) {
const isUsingDefaultKey = scaffoldConfig.alchemyApiKey === DEFAULT_ALCHEMY_API_KEY;
// If using default Scaffold-ETH 2 API key, we prioritize the default RPC
rpcFallbacks = isUsingDefaultKey ? [http(), http(alchemyHttpUrl)] : [http(alchemyHttpUrl), http()];
const rpcOverrideUrl = (scaffoldConfig.rpcOverrides as ScaffoldConfig["rpcOverrides"])?.[chain.id];
if (rpcOverrideUrl) {
rpcFallbacks = [http(rpcOverrideUrl), http()];
} else {
const alchemyHttpUrl = getAlchemyHttpUrl(chain.id);
if (alchemyHttpUrl) {
const isUsingDefaultKey = scaffoldConfig.alchemyApiKey === DEFAULT_ALCHEMY_API_KEY;
// If using default Scaffold-ETH 2 API key, we prioritize the default RPC
rpcFallbacks = isUsingDefaultKey ? [http(), http(alchemyHttpUrl)] : [http(alchemyHttpUrl), http()];
}
}

return createClient({
Expand Down

0 comments on commit 2fffd9e

Please sign in to comment.