Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC Overrides #1057

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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