Skip to content

extractChain

Extracts a type-safe chain by ID from a set of chains.

Usage

import { extractChain } from 'viem'
import { mainnet, base, optimism, zora } from 'viem/chains'
 
const optimism = extractChain({
  chains: [mainnet, base, optimism, zora],
  id: 10,
})
 
optimism.id
//       ^? (property) id: 10
optimism.name
//       ^? (property) name: "OP Mainnet"

It is also possible to use all chains from the viem/chains module:

import { extractChain } from 'viem'
import { mainnet, base, optimism, zora } from 'viem/chains'
import * as chains from 'viem/chains'
 
const optimism = extractChain({
  chains: [mainnet, base, optimism, zora], 
  chains: Object.values(chains), 
  id: 10,
})
 
optimism.id
//       ^? (property) id: 10
optimism.name
//       ^? (property) name: "OP Mainnet"

Returns

  • Type: Chain (inferred)

The extracted chain.

Parameters

chains

  • Type: readonly Chain[]

The set of chains where the chain will be extracted from.

id

  • Type: number

The ID of the chain to extract.