• React
  • Providers
  • Configuring Chains

Configuring Chains

The configureChains function allows you to configure your chains with RPC Providers such as: Alchemy, Infura, or something else. This means you don't need to worry about defining RPC URLs and chain configuration in your Connector or Public Client. This is managed internally by wagmi.

import { configureChains } from 'wagmi'

Usage

configureChains accepts an array of chains and an array of RPC Providers. It returns:

  • chains: to pass to your connector(s)
  • publicClient: to pass to createConfig
  • webSocketPublicClient: to optionally pass to createConfig
import { configureChains, createConfig } from 'wagmi'
import { mainnet, polygon, optimism } from 'wagmi/chains'
import { alchemyProvider } from 'wagmi/providers/alchemy'
import { publicProvider } from 'wagmi/providers/public'
import { InjectedConnector } from 'wagmi/connectors/injected'
 
const { chains, publicClient } = configureChains(
  [mainnet, polygon, optimism],
  [alchemyProvider({ apiKey: 'yourAlchemyApiKey' }), publicProvider()],
)
 
const config = createConfig({
  autoConnect: true,
  connectors: [new InjectedConnector({ chains })],
  publicClient,
})

Find a list of supported chains in wagmi here.

Multiple RPC Providers

The configureChains function accepts multiple RPC Providers. This is useful if not all your chains support a single RPC Provider. For example, you may want to use Alchemy for Ethereum, and avax.network for Avalanche.

configureChains wraps the RPC Provider that you provide into viem's fallback Transport, that comes with support for falling back to another RPC Provider if the RPC Provider goes down (e.g. If Infura goes down, we can fall back to Alchemy)

import { configureChains } from 'wagmi'
import { avalanche, polygon } from 'wagmi/chains'
import { alchemyProvider } from 'wagmi/providers/alchemy'
import { infuraProvider } from 'wagmi/providers/infura'
import { publicProvider } from 'wagmi/providers/public'
 
const { publicClient } = configureChains(
  [mainnet, polygon, avalanche],
  [
    alchemyProvider({ apiKey: 'yourAlchemyApiKey' }),
    infuraProvider({ apiKey: 'yourInfuraApiKey' }),
    publicProvider(),
  ],
)

Arguments

chains

Chains that need to be configured.

import { configureChains } from 'wagmi'
import { mainnet, optimism, polygon } from 'wagmi/chains'
import { publicProvider } from 'wagmi/providers/public'
 
const { chains } = configureChains(
  [mainnet, optimism, polygon],
  [publicProvider()],
)

providers

The providers the app supports.

If a provider does not support a chain, it will fall back onto the next one in the array. If no RPC URLs are found, configureChains will throw an error.

import { configureChains } from 'wagmi'
import { mainnet, optimism, polygon } from 'wagmi/chains'
import { alchemyProvider } from 'wagmi/providers/alchemy'
import { publicProvider } from 'wagmi/providers/public'
 
const { chains } = configureChains(
  [mainnet, optimism, polygon],
  [alchemyProvider({ apiKey: 'yourAlchemyApiKey' }), publicProvider()],
)

Configuration

batch.multicall (optional)

Toggle to enable eth_call multicall aggregation. Default: true.

import { configureChains } from 'wagmi'
import { mainnet, optimism, polygon } from 'wagmi/chains'
import { alchemyProvider } from 'wagmi/providers/alchemy'
import { publicProvider } from 'wagmi/providers/public'
 
const { chains } = configureChains(
  [mainnet, optimism, polygon],
  [
    alchemyProvider({ apiKey: 'yourAlchemyApiKey' }),
    infuraProvider({ apiKey: 'yourInfuraApiKey' }),
    publicProvider(),
  ],
  { batch: { multicall: true } },
)

You can also pass custom multicall options.

pollingInterval (optional)

The frequency in milliseconds at which the provider polls. Defaults to 4000.

import { configureChains } from 'wagmi'
import { mainnet, optimism, polygon } from 'wagmi/chains'
import { alchemyProvider } from 'wagmi/providers/alchemy'
import { publicProvider } from 'wagmi/providers/public'
 
const { chains } = configureChains(
  [mainnet, optimism, polygon],
  [
    alchemyProvider({ apiKey: 'yourAlchemyApiKey' }),
    infuraProvider({ apiKey: 'yourInfuraApiKey' }),
    publicProvider(),
  ],
  { pollingInterval: 10_000 },
)

rank (optional)

Whether or not to automatically rank the RPC Providers based on their latency & stability. Default: false.

import { configureChains } from 'wagmi'
import { mainnet, optimism, polygon } from 'wagmi/chains'
import { alchemyProvider } from 'wagmi/providers/alchemy'
import { publicProvider } from 'wagmi/providers/public'
 
const { chains } = configureChains(
  [mainnet, optimism, polygon],
  [
    alchemyProvider({ apiKey: 'yourAlchemyApiKey' }),
    infuraProvider({ apiKey: 'yourInfuraApiKey' }),
    publicProvider(),
  ],
  { rank: true },
)

You can also pass custom rank options.

retryCount (optional)

The max number of times to retry when a request fails. Default: 3.

Note: wagmi will first try all the RPC Providers before retrying.

import { configureChains } from 'wagmi'
import { mainnet, optimism, polygon } from 'wagmi/chains'
import { alchemyProvider } from 'wagmi/providers/alchemy'
import { publicProvider } from 'wagmi/providers/public'
 
const { chains } = configureChains(
  [mainnet, optimism, polygon],
  [
    alchemyProvider({ apiKey: 'yourAlchemyApiKey' }),
    infuraProvider({ apiKey: 'yourInfuraApiKey' }),
    publicProvider(),
  ],
  { retryCount: 5 },
)

retryDelay (optional)

The base delay (in ms) between retries. By default, the RPC Provider will use exponential backoff (~~(1 << count) \* retryDelay), which means the time between retries is not constant.

import { configureChains } from 'wagmi'
import { mainnet, optimism, polygon } from 'wagmi/chains'
import { alchemyProvider } from 'wagmi/providers/alchemy'
import { publicProvider } from 'wagmi/providers/public'
 
const { chains } = configureChains(
  [mainnet, optimism, polygon],
  [
    alchemyProvider({ apiKey: 'yourAlchemyApiKey' }),
    infuraProvider({ apiKey: 'yourInfuraApiKey' }),
    publicProvider(),
  ],
  { retryDelay: 150 },
)

stallTimeout (optional)

The timeout in milliseconds after which another provider will be attempted.

import { configureChains } from 'wagmi'
import { mainnet, optimism, polygon } from 'wagmi/chains'
import { alchemyProvider } from 'wagmi/providers/alchemy'
import { publicProvider } from 'wagmi/providers/public'
 
const { chains } = configureChains(
  [mainnet, optimism, polygon],
  [
    alchemyProvider({ apiKey: 'yourAlchemyApiKey' }),
    infuraProvider({ apiKey: 'yourInfuraApiKey' }),
    publicProvider(),
  ],
  { stallTimeout: 5000 },
)