---
eip: 2015
label: EIP-2015
title: wallet_updateEthereumChain RPC Method
status: Stagnant
type: Standards Track
category: Interface
created: 2019-05-12
requires: 155
official: https://eips.ethereum.org/EIPS/eip-2015
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2015.md
canonical: https://eips.huatuo.xyz/zh/eip-2015/
lang: zh
tldr: 提出 wallet_updateEthereumChain：一个 RPC 方法，请求钱包切换到某条 EVM 链，如果钱包尚不认识该链，则先添加。
tags: [wallet, json-rpc, chain-switching, dapp, interface]
---

# EIP-2015: wallet_updateEthereumChain RPC Method

> Adds an RPC method to switch between EVM-compatible chains

**TL;DR:** 提出 wallet_updateEthereumChain：一个 RPC 方法，请求钱包切换到某条 EVM 链，如果钱包尚不认识该链，则先添加。

## 它解决什么问题？

部署在多条 EVM 链上的 dapp 往往需要用户的钱包处于特定链上。没有标准时，dapp 只能显示"请切换到网络 X"，并指望用户能找到正确的设置。如果钱包从未听说过这条链，用户还得手动输入 RPC URL、链 ID 和货币符号，既容易出错，也有钓鱼风险。

EIP-2015 定义了一个钱包方法，同时覆盖两种情况：切换到已知链，或注册一条新链并切换过去。

## 它如何运作

dapp 通过钱包的 provider 调用 `wallet_updateEthereumChain`，传入一个对象：

| 字段 | 必填 | 含义 |
| --- | --- | --- |
| `chainId` | 是 | 以 `0x` 开头的 [EIP-155](/eip-155/) 链 ID |
| `chainName` | 否 | 显示名称 |
| `rpcUrls` | 否 | 该链的 RPC 端点 |
| `nativeCurrency` | 否 | `{ name, symbol, decimals }`，读取方式与 ERC-20 元数据相同 |
| `blockExplorerUrl` | 否 | 区块浏览器 URL |

钱包的行为：

- 除 `chainId` 外，其余都只是建议，钱包可以忽略或覆盖。
- 钱包**应当**在切换或添加链之前询问用户，并且**应当**为知名链内置自己的默认值以抵御钓鱼。
- 钱包**必须**清理每个 RPC URL，包括检查它是否能正确响应 `net_version` 和 `eth_chainId`。
- 成功时，一旦活动链与请求一致，就返回 `true`。如果用户拒绝，则返回错误码 `4001`。

## 示例代码

通过 [EIP-1193](/eip-1193/) provider 从 dapp 调用：

```ts
const ethereum = (window as any).ethereum;

try {
  await ethereum.request({
    method: "wallet_updateEthereumChain",
    params: [{
      chainId: "0x2a",                       // example values only
      chainName: "Example Chain",
      rpcUrls: ["https://rpc.example.org"],
      nativeCurrency: { name: "Example Ether", symbol: "EETH", decimals: 18 },
      blockExplorerUrl: "https://explorer.example.org",
    }],
  });
} catch (err: any) {
  if (err.code === 4001) console.log("User rejected the switch");
  else throw err;
}
```

目前大多数钱包实际支持的做法：

```ts
try {
  await ethereum.request({ method: "wallet_switchEthereumChain", params: [{ chainId: "0x2a" }] });
} catch (err: any) {
  if (err.code === 4902) { // chain not added yet (wallet-specific code)
    await ethereum.request({ method: "wallet_addEthereumChain", params: [chainParams] });
  }
}
```

## 需要注意

- **状态：Stagnant。** 钱包并未采用 `wallet_updateEthereumChain`。
- 生态系统转而标准化为两个独立的方法：[EIP-3085](/eip-3085/) 的 `wallet_addEthereumChain` 和 [EIP-3326](/eip-3326/) 的 `wallet_switchEthereumChain`。两者本身也被标记为 Stagnant，但已被广泛实现。
- 切勿盲目信任 dapp 提供的 RPC URL。规范特别警告了服务端请求伪造和钓鱼。
- 第二段代码中的 `4902`"未识别的链"错误码是钱包的常见约定，并不属于本 EIP。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-2015
