@api3/chains

中文文档
11.8.0发布于 2025/02/13MITTypeScriptCommonJS构建来源已验证近 30 天下载 3037

API3 chains

原始使用文档
文档目录14 节
  1. 📦 Installation
  2. 📖 API
  3. CHAINS
  4. hardhatConfig.networks()
  5. hardhatConfig.etherscan()
  6. hardhatConfig.getEnvVariableNames()
  7. viemConfig.chains()
  8. Types
  9. 📄 Scripts
  10. generate:chains
  11. providers:ping
  12. 🛠️ Development
  13. Validation
  14. 🚢 Releasing

API3 chains

npm version downloads per week continuous build provider checks license

The single source of truth for chain-related information across API3 projects

📦 Installation

npm install @api3/chains --save
yarn add @api3/chains
pnpm add @api3/chains

📖 API

The following variables/functions are exported from this package

CHAINS

The single source of truth for the list of supported chains. A static array of Chain objects.

import { CHAINS } from '@api3/chains';
console.log(CHAINS);
/*
[
  {
    name: 'Arbitrum Sepolia testnet',
    alias: 'arbitrum-sepolia-testnet',
    id: '421614',
    ...
  },
  ...
]
*/

hardhatConfig.networks()

Returns an object where the key is each chain's alias and the value is an object that can be used as the networks field of hardhat.config.js.

The default url values can be overridden with chain specific environment variables. These environment variables take the form of HARDHAT_HTTP_RPC_URL_${toUpperSnakeCase(chain.alias)}. e.g. HARDHAT_HTTP_RPC_URL_ARBITRUM_SEPOLIA_TESTNET.

import { hardhatConfig } from '@api3/chains';
console.log(hardhatConfig.networks());
/*
{
  "arbitrum-sepolia-testnet": {
      accounts: { mnemonic: '' },
      chainId: '421614',
      url: 'https://...',
  },
  ...
}
*/

hardhatConfig.etherscan()

Returns an object where the key is each chain's alias and the value is an object that can be used as the etherscan field of hardhat.config.js (requires the hardhat-etherscan plugin).

NOTE: hardhat-etherscan requires us to use a dummy API key with Blockscout block explorer APIs. We use "DUMMY_VALUE" but it could have been anything else.

import { hardhatConfig } from '@api3/chains';
console.log(hardhatConfig.etherscan());
/*
{
  apiKey: {
    'arbitrumSepolia': { ... }
  },
  customChains: [
    ...
  ]
}
*/

hardhatConfig.getEnvVariableNames()

Returns an array of expected environment variable names for chains that have an API key required for the explorer. The array also includes a single MNEMONIC variable that can be used to configure all networks.

NOTE: Each ETHERSCAN_API_KEY_ and HARDHAT_HTTP_RPC_URL_ environment variable has the chain alias as a suffix, where the alias has been converted to upper snake case.

import { hardhatConfig } from '@api3/chains';
console.log(hardhatConfig.getEnvVariableNames());
/*
[
  'MNEMONIC',
  'ETHERSCAN_API_KEY_ARBITRUM_SEPOLIA_TESTNET',
  ...
  'HARDHAT_HTTP_RPC_URL_ARBITRUM_SEPOLIA_TESTNET',
  ...
]
*/

viemConfig.chains()

Returns an array of chains in the format that Viem expects. Each Chain object can be used to create a Viem public client.

Additional rpcUrls values can (optionally) be added through the use of environment variables. These environment variables take the form of API3_CHAINS_HTTP_RPC_URL_${toUpperSnakeCase(chain.alias)}. If a matching environment variable is detected for a given chain, then it will be added to the http array of the rpcUrls.environment object. If no matching environment variable is detected, then the http array is left empty.

import { viemConfig } from '@api3/chains';
console.log(viemConfig.chains());
/*
[
  {
    id: 421613,
    name: 'arbitrum-sepolia-testnet',
    network: 'arbitrum-sepolia-testnet',
    rpcUrls: { default: ..., public: ..., environment: ... }
    ...
  },
  ...
]
*/

Types

Types are also exported and can be found in src/types.ts. Types are generated from zod schemas. These schemas are also used to validate each chain.

📄 Scripts

The following utility scripts are available

generate:chains

Generates the latest CHAINS array and outputs the file to src/generated/chains.ts

pnpm generate:chains

providers:ping

Iterates through the list of chains to check that the chain is configured correctly and is responsive.

pnpm providers:ping

🛠️ Development

The most common type of change would be adding or updating a chain. This can be done by creating or editing the relevant JSON file in the chains/ directory.

If any changes are made to chains, you will need to "regenerate" the chains. This will compile all of the JSON files into a single TypeScript file for projects to import. This can be done with the following command

pnpm generate:chains

The list of TypeScript chains is also validated against all of the list of JSON files to ensure that everything is in sync.

NOTE: You will not be able to push changes to chains without having regenerated the TypeScript chains.

Validation

Validations can be run with the following commands.

# Validate each chain JSON file conforms to the zod schemas
pnpm validate:chains

# Run all validations
pnpm validate

🚢 Releasing

Releasing new versions is handled automatically with changesets. Pull requests should include a changeset file before being merged. These can be generated by running pnpm changeset and following the instructions. Once a new version is ready to be released, simply merge main into the production branch. Changeset files will be consolidated into a single new version and that version released to npm.

More information is contained in the API3 guidelines.

版本与发布记录

@api3/chains 共发布 112 个版本,首次发布于 2023/03/06;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
11.8.0latest2025/02/131.7 年前
11.7.02025/02/121.7 年前
11.6.02025/02/061.7 年前
11.5.12025/02/041.8 年前
11.5.02025/01/151.8 年前
11.4.22025/01/061.8 年前

关于 @api3/chains 的常见问题

@api3/chains 是什么?

API3 chains

如何安装 @api3/chains?

在项目目录运行 npm install @api3/chains,或使用 pnpm add / yarn add / bun add。

@api3/chains 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@api3/chains 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@api3/chains 可以商用吗?

@api3/chains 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@api3/chains 的下载量有多少?

根据 npm 官方下载统计,@api3/chains 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,037 次,日均约 101 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@api3/chains 还在维护吗?

最新版本 11.8.0 发布于 2025/02/13(1.7 年前);至今共发布 112 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/03/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。