• Core
  • Providers
  • Configuring Chains

Configuring Chains

The configureChains function allows you to configure your chains with 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 provider. This is managed internally by wagmi.

import { configureChains } from '@wagmi/core'

Usage

configureChains accepts an array of chains and an array of providers. It returns:

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

Find a list of supported chains in wagmi here.

Multiple providers

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

configureChains wraps the providers that you provide into an ethers.js FallbackProvider, that comes with support for:

  • Falling back to another provider if a provider goes down (e.g. If Infura goes down, we can fall back to Alchemy)
  • Ensuring the responses are legitimate by setting a targetQuorum.
import { configureChains } from '@wagmi/core'
import { avalanche, polygon } from '@wagmi/core/chains'
import { alchemyProvider } from '@wagmi/core/providers/alchemy'
import { infuraProvider } from '@wagmi/core/providers/infura'
import { publicProvider } from '@wagmi/core/providers/public'
 
const { provider } = configureChains(
  [mainnet, polygon, avalanche],
  [
    alchemyProvider({ apiKey: 'yourAlchemyApiKey' }),
    infuraProvider({ apiKey: 'yourInfuraApiKey' }),
    publicProvider(),
  ],
)

Quorum

If the targetQuorum option is set to a value greater than 1, it will dispatch interactions to multiple providers, in which the responses are verified by comparing them to each other. If the quorum is reached, then the result will be returned to the consumer.

const { provider } = configureChains(
  [mainnet, avalanche],
  [
    alchemyProvider({ apiKey: 'yourAlchemyApiKey' }),
    infuraProvider({ apiKey: 'yourInfuraApiKey' }),
    publicProvider(),
  ],
  { targetQuorum: 2 },
)

By default, for a given chain, it will attempt to set the quorum value, but if the targetQuorum value is greater than the number of providers for the chain, it will default to the number of providers.

For instance, in the example provided above targetQuorum = 2, however there is only 1 available provider for Avalanche (jsonRpcProvider), so the quorum will get set to 1.

To guarantee a static quorum, you can provide a minQuorum as an config option.

Arguments

chains

Chains that need to be configured.

import { configureChains } from '@wagmi/core'
import { mainnet, optimism, polygon } from '@wagmi/core/chains'
import { publicProvider } from '@wagmi/core/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/core'
import { mainnet, optimism, polygon } from '@wagmi/core/chains'
import { alchemyProvider } from '@wagmi/core/providers/alchemy'
import { publicProvider } from '@wagmi/core/providers/public'
 
const { chains } = configureChains(
  [mainnet, optimism, polygon],
  [alchemyProvider({ apiKey: 'yourAlchemyApiKey' }), publicProvider()],
)

Configuration

pollingInterval (optional)

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

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

targetQuorum (optional)

Sets the target quorum. Defaults to 1.

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

minQuorum (optional)

Sets the minimum quorum that must be accepted by the providers. Defaults to 1.

import { configureChains } from '@wagmi/core'
import { mainnet, optimism, polygon } from '@wagmi/core/chains'
import { alchemyProvider } from '@wagmi/core/providers/alchemy'
import { publicProvider } from '@wagmi/core/providers/public'
 
const { chains } = configureChains(
  [mainnet, optimism, polygon],
  [
    alchemyProvider({ apiKey: 'yourAlchemyApiKey' }),
    infuraProvider({ apiKey: 'yourInfuraApiKey' }),
    publicProvider(),
  ],
  { targetQuorum: 3, minQuorum: 2 },
)

stallTimeout (optional)

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

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