Skip to content
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
5 changes: 4 additions & 1 deletion src/lib/network-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*
* Network details for Ethereum Mainnet with Fast Protocol RPC
*/
const alchemyApiKey = process.env.NEXT_PUBLIC_ALCHEMY_API_KEY

export const FAST_PROTOCOL_NETWORK = {
chainId: 1,
chainName: "Ethereum Mainnet",
Expand Down Expand Up @@ -30,4 +32,5 @@ export const NETWORK_CONFIG = {
* RPC endpoint for testing
*/
export const RPC_ENDPOINT = "https://fastrpc.mev-commit.xyz"
export const FALLBACK_RPC_ENDPOINT = "https://eth.llamarpc.com"
export const FALLBACK_RPC_ENDPOINT =
`https://eth-mainnet.g.alchemy.com/v2/${alchemyApiKey}` || "https://eth.llamarpc.com"
57 changes: 35 additions & 22 deletions src/lib/wagmi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client"

import { connectorsForWallets } from "@rainbow-me/rainbowkit"
import {
injectedWallet,
Expand All @@ -9,36 +11,24 @@ import {
} from "@rainbow-me/rainbowkit/wallets"
import { createConfig, http, fallback } from "wagmi"
import { mainnet } from "wagmi/chains"
import { RPC_ENDPOINT } from "@/lib/network-config"

// 1. Detect environment (Safe for SSR - defaults to desktop for server render)
// 1. Environment Detection & Constants
const isMobile =
typeof window !== "undefined" && /Android|iPhone|iPad|iPod/i.test(navigator.userAgent)

// Get WalletConnect Project ID - required for mobile deep links
// Falls back to placeholder if not set (mobile deep links won't work, but desktop will)
const projectId =
process.env.NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID || "00000000000000000000000000000000"

const alchemyApiKey = process.env.NEXT_PUBLIC_ALCHEMY_API_KEY

// 2. Connector Configuration
const connectors = connectorsForWallets(
[
{
groupName: "Recommended",
wallets: isMobile
? [
// MOBILE: Show specific apps to trigger Deep Links
// These wallets use WalletConnect v2 to facilitate handshake between browser and app
metaMaskWallet,
rabbyWallet,
rainbowWallet,
coinbaseWallet,
walletConnectWallet,
]
: [
// DESKTOP: Focus on browser extensions
// injectedWallet detects all installed extensions (MetaMask, Rabby, etc.)
injectedWallet,
],
? [metaMaskWallet, rabbyWallet, rainbowWallet, coinbaseWallet, walletConnectWallet]
: [injectedWallet],
},
],
{
Expand All @@ -47,14 +37,37 @@ const connectors = connectorsForWallets(
}
)

// 3. Transport Logic (Alchemy as Default/Primary)
const rpcFallbacks = [
// PRIMARY: Alchemy
http(`https://eth-mainnet.g.alchemy.com/v2/${alchemyApiKey}`, {
timeout: 10000,
fetchOptions: { cache: "no-store" },
}),

// SECONDARY: Public Nodes
// http("https://eth.llamarpc.com", { timeout: 10000 }),
http("https://rpc.ankr.com/eth", { timeout: 10000 }),
http("https://1rpc.io/eth", { timeout: 10000 }),

// LAST RESORT: Standard Public Node
http(),
]

// 4. Final Config
export const config = createConfig({
chains: [mainnet],
connectors,
transports: {
[mainnet.id]: fallback([
http(RPC_ENDPOINT), // Fast RPC
http(), // public fallback
]),
[mainnet.id]: fallback(rpcFallbacks, {
rank: true, // This will periodically test latency and pick the best one from the list
}),
},
batch: {
multicall: {
batchSize: 1024,
wait: 20, // Bundles multiple balance checks into one RPC call
},
},
ssr: true,
})