Skip to content

Commit

Permalink
feat: extractChain (wevm#1402)
Browse files Browse the repository at this point in the history
* feat: extractChain

* chore: changeset

* fix
  • Loading branch information
jxom authored Oct 27, 2023
1 parent bc483f6 commit 0977829
Show file tree
Hide file tree
Showing 184 changed files with 741 additions and 429 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-pens-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": minor
---

Added `extractChain` utility.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ Example:

```ts
// src/chains/definitions/example.ts
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const mainnet = /*#__PURE__*/ defineChain({
id: 1,
Expand Down
9 changes: 9 additions & 0 deletions site/.vitepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,15 @@ export const sidebar: DefaultTheme.Sidebar = {
},
],
},
{
text: 'Chain',
items: [
{
text: 'extractChain',
link: '/docs/utilities/extractChain',
},
],
},
{
text: 'Data',
items: [
Expand Down
4 changes: 4 additions & 0 deletions site/docs/clients/chains.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,7 @@ const example = defineChain(
}
)
```

## Utilities

- [`extractChain`](/docs/utilities/extractChain): Extracts a type-safe chain by ID.
63 changes: 63 additions & 0 deletions site/docs/utilities/extractChain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# extractChain

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

## Usage

```ts
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:

```ts
import { extractChain } from 'viem'
import { mainnet, base, optimism, zora } from 'viem/chains' // [!code --]
import * as chains from 'viem/chains' // [!code ++]
const optimism = extractChain({
chains: [mainnet, base, optimism, zora], // [!code --]
chains: Object.values(chains), // [!code ++]
id: 10,
})

optimism.id
// ^? (property) id: 10
optimism.name
// ^? (property) name: "OP Mainnet"
```

::: warning
By importing all chains from `viem/chains`, this will significantly increase the size of your bundle. It is only recommended to use this method where bundle size is not a concern (ie. server-side, scripts, etc).
:::

## 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.
2 changes: 1 addition & 1 deletion src/actions/ens/getEnsAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import {
type GetChainContractAddressErrorType,
getChainContractAddress,
} from '../../utils/chain.js'
} from '../../utils/chain/getChainContractAddress.js'
import { type TrimErrorType, trim } from '../../utils/data/trim.js'
import { type ToHexErrorType, toHex } from '../../utils/encoding/toHex.js'
import { isNullUniversalResolverError } from '../../utils/ens/errors.js'
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ens/getEnsName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { Prettify } from '../../types/utils.js'
import {
type GetChainContractAddressErrorType,
getChainContractAddress,
} from '../../utils/chain.js'
} from '../../utils/chain/getChainContractAddress.js'
import { type ToHexErrorType, toHex } from '../../utils/encoding/toHex.js'
import { isNullUniversalResolverError } from '../../utils/ens/errors.js'
import {
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ens/getEnsResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Prettify } from '../../types/utils.js'
import {
type GetChainContractAddressErrorType,
getChainContractAddress,
} from '../../utils/chain.js'
} from '../../utils/chain/getChainContractAddress.js'
import { type ToHexErrorType, toHex } from '../../utils/encoding/toHex.js'
import {
type PacketToBytesErrorType,
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ens/getEnsText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import {
type GetChainContractAddressErrorType,
getChainContractAddress,
} from '../../utils/chain.js'
} from '../../utils/chain/getChainContractAddress.js'
import { type ToHexErrorType, toHex } from '../../utils/encoding/toHex.js'
import { isNullUniversalResolverError } from '../../utils/ens/errors.js'
import { type NamehashErrorType, namehash } from '../../utils/ens/namehash.js'
Expand Down
2 changes: 1 addition & 1 deletion src/actions/public/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import type { RequestErrorType } from '../../utils/buildRequest.js'
import {
type GetChainContractAddressErrorType,
getChainContractAddress,
} from '../../utils/chain.js'
} from '../../utils/chain/getChainContractAddress.js'
import {
type NumberToHexErrorType,
numberToHex,
Expand Down
2 changes: 1 addition & 1 deletion src/actions/public/multicall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import {
type GetChainContractAddressErrorType,
getChainContractAddress,
} from '../../utils/chain.js'
} from '../../utils/chain/getChainContractAddress.js'
import {
type GetContractErrorReturnType,
getContractError,
Expand Down
2 changes: 1 addition & 1 deletion src/actions/test/sendUnsignedTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { accounts, address } from '~test/src/constants.js'
import { publicClient, testClient } from '~test/src/utils.js'
import { celo } from '../../chains/index.js'
import { createTestClient } from '../../clients/createTestClient.js'
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'
import { parseEther } from '../../utils/unit/parseEther.js'
import { getBalance } from '../public/getBalance.js'

Expand Down
2 changes: 1 addition & 1 deletion src/actions/wallet/sendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createWalletClient } from '../../clients/createWalletClient.js'
import { http } from '../../clients/transports/http.js'
import { type Hex } from '../../types/misc.js'
import { type TransactionSerializable } from '../../types/transaction.js'
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'
import { concatHex } from '../../utils/data/concat.js'
import { hexToNumber } from '../../utils/encoding/fromHex.js'
import { toHex } from '../../utils/encoding/toHex.js'
Expand Down
2 changes: 1 addition & 1 deletion src/actions/wallet/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type { RequestErrorType } from '../../utils/buildRequest.js'
import {
type AssertCurrentChainErrorType,
assertCurrentChain,
} from '../../utils/chain.js'
} from '../../utils/chain/assertCurrentChain.js'
import {
type GetTransactionErrorReturnType,
getTransactionError,
Expand Down
2 changes: 1 addition & 1 deletion src/actions/wallet/signTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type { RequestErrorType } from '../../utils/buildRequest.js'
import {
type AssertCurrentChainErrorType,
assertCurrentChain,
} from '../../utils/chain.js'
} from '../../utils/chain/assertCurrentChain.js'
import type { NumberToHexErrorType } from '../../utils/encoding/toHex.js'
import {
type FormattedTransactionRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/arbitrum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const arbitrum = /*#__PURE__*/ defineChain({
id: 42_161,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/arbitrumGoerli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const arbitrumGoerli = /*#__PURE__*/ defineChain({
id: 421_613,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/arbitrumNova.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const arbitrumNova = /*#__PURE__*/ defineChain({
id: 42_170,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/arbitrumSepolia.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const arbitrumSepolia = /*#__PURE__*/ defineChain({
id: 421_614,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/astarZkatana.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const astarZkatana = /*#__PURE__*/ defineChain({
id: 1_261_120,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/aurora.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const aurora = /*#__PURE__*/ defineChain({
id: 1313161554,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/auroraTestnet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const auroraTestnet = /*#__PURE__*/ defineChain({
id: 1313161555,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/avalanche.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const avalanche = /*#__PURE__*/ defineChain({
id: 43_114,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/avalancheFuji.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const avalancheFuji = /*#__PURE__*/ defineChain({
id: 43_113,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'
import { formattersOptimism } from '../optimism/formatters.js'

export const base = /*#__PURE__*/ defineChain(
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/baseGoerli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'
import { formattersOptimism } from '../optimism/formatters.js'

export const baseGoerli = /*#__PURE__*/ defineChain(
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/baseSepolia.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'
import { formattersOptimism } from '../optimism/formatters.js'

export const baseSepolia = /*#__PURE__*/ defineChain(
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/bearNetworkChainMainnet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const bearNetworkChainMainnet = /*#__PURE__*/ defineChain({
id: 641230,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/bearNetworkChainTestnet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const bearNetworkChainTestnet = /*#__PURE__*/ defineChain({
id: 751230,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/boba.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const boba = /*#__PURE__*/ defineChain({
id: 288,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/bronos.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const bronos = /*#__PURE__*/ defineChain({
id: 1039,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/bronosTestnet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const bronosTestnet = /*#__PURE__*/ defineChain({
id: 1038,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/bsc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const bsc = /*#__PURE__*/ defineChain({
id: 56,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/bscTestnet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const bscTestnet = /*#__PURE__*/ defineChain({
id: 97,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/bxn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const bxn = /*#__PURE__*/ defineChain({
id: 4999,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/bxnTestnet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const bxnTestnet = /*#__PURE__*/ defineChain({
id: 4777,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/canto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const canto = /*#__PURE__*/ defineChain({
id: 7_700,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/celo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'
import { formattersCelo } from '../celo/formatters.js'
import { serializersCelo } from '../celo/serializers.js'

Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/celoAlfajores.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'
import { formattersCelo } from '../celo/formatters.js'
import { serializersCelo } from '../celo/serializers.js'

Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/celoCannoli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'
import { formattersCelo } from '../celo/formatters.js'
import { serializersCelo } from '../celo/serializers.js'

Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/classic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const classic = /*#__PURE__*/ defineChain({
id: 61,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/confluxESpace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const confluxESpace = /*#__PURE__*/ defineChain({
id: 1_030,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/coreDao.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const coreDao = /*#__PURE__*/ defineChain({
id: 1116,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/cronos.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const cronos = /*#__PURE__*/ defineChain({
id: 25,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/cronosTestnet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const cronosTestnet = /*#__PURE__*/ defineChain({
id: 338,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/crossbell.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const crossbell = /*#__PURE__*/ defineChain({
id: 3_737,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/dfk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const dfk = /*#__PURE__*/ defineChain({
id: 53_935,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/dogechain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const dogechain = /*#__PURE__*/ defineChain({
id: 2_000,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/edgeware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from '../../utils/chain.js'
import { defineChain } from '../../utils/chain/defineChain.js'

export const edgeware = /*#__PURE__*/ defineChain({
id: 2021,
Expand Down
Loading

0 comments on commit 0977829

Please sign in to comment.