---
title: "什么是 WEI？"
description: "Wei 是以太币的最小单位，以太币是用于 Ethereum 上交易的货币。"
datePublished: "2023-08-03"
dateModified: "2023-08-03"
---

## 什么是 WEI？

[Wei](https://www.alchemy.com/docs/web3-glossary) 是以太币（ether）的最小单位，以太币是用于在 Ethereum 区块链网络上进行交易操作的货币，其中 10^18 即 1,000,000,000,000,000,000 wei 等于 1 个 ether。

Wei 以 Wei Dai 的名字命名，他是华盛顿大学计算机科学专业的毕业生，曾在微软密码学研究部门任职。Wei Dai 曾是 20 世纪 90 年代密码朋克运动的一员，该运动的成员为 2010 年代加密货币的出现奠定了理论基础。由于他提出了 b-money 这一创新构想，并在一份自行发表的加密原型宣言中进行了阐述，为智能合约的发展奠定了基础，ether 的最小单位便以他的名字命名。

由于所有构建在 Ethereum 网络之上的服务和应用都需要计算能力，而计算能力并非免费提供，因此需要某种形式的支付来获取这些计算能力。

尽管 ether 是该网络的货币，但它常被比喻为 Ethereum 网络的“燃料”。其作为加密燃料的效用价值通常被称为“gas”。

Gas 是所有网络请求的核心组成部分，请求发送者需要为所消耗的计算资源付费。总的 gas 费用根据请求的数据量和复杂度，乘以当前的 gas 价格计算得出。

<EmbeddedTable
  table={{
    columns: [
      { key: "1", width: 200, title: "Unit", dataType: "object" },
      { key: "2", width: 200, title: "Alternative Name", dataType: "object" },
      {
        key: "1703851661082",
        width: 180,
        title: "Wei Value",
        dataType: "object",
      },
      {
        key: "1703851670504",
        width: 180,
        title: "Gwei Value",
        dataType: "object",
      },
      {
        key: "1703851679972",
        width: 180,
        title: "Ether Value",
        dataType: "object",
      },
    ],
    data: [
      {
        "1": { title: "<p>Wei</p>", tooltip: "", icon: "" },
        "2": { title: "<p>Wei</p>", tooltip: "", icon: "" },
        id: 0,
        "1703851661082": { title: "<p>1 Wei</p>", tooltip: "", icon: "" },
        "1703851670504": { title: "<p>10^-9 Gwei</p>", tooltip: "", icon: "" },
        "1703851679972": { title: "<p>10^-18 ETH</p>", tooltip: "", icon: "" },
      },
      {
        "1": { title: "<p>Kwei</p>", tooltip: "", icon: "" },
        "2": { title: "Babbage", tooltip: "", icon: "" },
        id: 1,
        "1703851661082": { title: "<p>10^3 Wei</p>", tooltip: "", icon: "" },
        "1703851670504": { title: "<p>10^-6 Gwei</p>", tooltip: "", icon: "" },
        "1703851679972": { title: "<p>10^-15 ETH</p>", tooltip: "", icon: "" },
      },
      {
        "1": { title: "<p>Mwei</p>", tooltip: "", icon: "" },
        "2": { title: "<p>Lovelace</p>", tooltip: "", icon: "" },
        id: 2,
        "1703851661082": { title: "<p>10^6 Wei</p>", tooltip: "", icon: "" },
        "1703851670504": { title: "<p>10^-3 Gwei</p>", tooltip: "", icon: "" },
        "1703851679972": { title: "<p>10^-12 ETH</p>", tooltip: "", icon: "" },
      },
      {
        "1": { title: "<p>Gwei</p>", tooltip: "", icon: "" },
        "2": { title: "<p>Shannon</p>", tooltip: "", icon: "" },
        id: 3,
        "1703851661082": { title: "<p>10^9 Wei</p>", tooltip: "", icon: "" },
        "1703851670504": { title: "<p>1 Gwei</p>", tooltip: "", icon: "" },
        "1703851679972": { title: "<p>10^-9 ETH</p>", tooltip: "", icon: "" },
      },
      {
        "1": { title: "<p>Twei</p>", tooltip: "", icon: "" },
        "2": { title: "<p>Szabo</p>", tooltip: "", icon: "" },
        id: 4,
        "1703851661082": { title: "<p>10^12 Wei</p>", tooltip: "", icon: "" },
        "1703851679972": { title: "<p>10^-6 ETH</p>", tooltip: "", icon: "" },
        "1703851670504": { title: "<p>10^3 Gwei</p>", tooltip: "", icon: "" },
      },
      {
        "1": { title: "<p>Pwei</p>", tooltip: "", icon: "" },
        "2": { title: "<p>Finney</p>", tooltip: "", icon: "" },
        id: 5,
        "1703851661082": { title: "<p>10^15 Wei</p>", tooltip: "", icon: "" },
        "1703851679972": { title: "<p>10^-3 ETH</p>", tooltip: "", icon: "" },
        "1703851670504": { title: "<p>10^6 Gwei</p>", tooltip: "", icon: "" },
      },
      {
        "1": { title: "<p>Ether</p>", tooltip: "", icon: "" },
        "2": { title: "", tooltip: "", icon: "" },
        id: 6,
        "1703851661082": { title: "<p>10^18 Wei</p>", tooltip: "", icon: "" },
        "1703851679972": { title: "<p>1 ETH</p>", tooltip: "", icon: "" },
        "1703851670504": { title: "<p>10^9 Gwei</p>", tooltip: "", icon: "" },
      },
    ],
  }}
/>

### wei 有哪些单位？

美元最小只能划分为 100 等份，即分（penny），而加密货币可以划分为小得多的原子单位。

这些单位能够对 ether 进行恰当的计量——它看起来数值可能很小，但换算成美元或其他主要货币后实际上是一笔较大的金额。

因此，ether 有多个不同层级的 wei 单位。每个子单位代表不同的数量级，且如上所述，各自还有别名。

## 什么是 gwei？

Gwei 是 wei 和 giga（十亿的单位前缀）两个词的组合，是 Ethereum 区块链网络所用数字货币 ether 的另一个计量单位。1 gwei 等于 10 亿 wei，[1 个 ether 等于 10^9 即 1,000,000,000 gwei](https://www.alchemy.com/docs/web3-glossary)。gwei 也被称为 nanoether 或 Shannon，这一名称来自**克劳德·香农（Claude Shannon）**，他是美国著名的数学家和密码学家，被视为信息论之父。

由于 gas 价格的计算和表示方式，gwei 是 ether 中最常用的单位。gas 是在 Ethereum 网络上执行合约或其他交易所需支付的价格值，而支付计算能力就需要用到 gwei。

在 Ethereum 区块链网络上，标准交易费用为 21,000 gwei。通常，交易费用会随着交易本身复杂度的提高而相应上升。

2021 年 7 月，作为伦敦硬分叉（London Hard Fork）的一部分，[Ethereum 改进提案（EIP）1559](/overviews/what-is-eip-1559) 改变了 Ethereum 区块链网络的费用机制。此前，开发者及更广泛的生态系统只能依赖价格拍卖机制，即出价最高者的交易会被优先处理。

随着 EIP-1559 的引入，系统会为下一个区块中要打包的交易设定一个固定的基础费用（base fee）。不过，某些用户仍可以附加“小费”或“优先费”，以确保矿工优先处理自己的交易。希望深入了解[如何使用 EIP-1559 发送交易](https://www.alchemy.com/docs/how-to-send-transactions-on-ethereum)的开发者，可以阅读 Alchemy 的 EIP-1559 文档。

计算交易费用的方法是使用以下公式：

**Gas 单位数（Limit）x（基础费用 Base Fee + 小费 Tip）**

总体而言，基于区块需求的收费方式对今天的用户来说更加简单和透明。此外，开发者还可以通过 Alchemy [使用 EIP-1559 方法构建 gas 费用估算器](https://www.alchemy.com/docs/how-to-send-transactions-on-ethereum)。

## 如何将 wei 转换为 gwei

将 wei 转换为 gwei 的三个步骤是：将 wei 的总数除以 10 亿。

开发者可以使用换算计算器来理解 wei 和 gwei 之间的关系，也可以按照以下简单的分步流程操作：

1. 统计 wei 的数量
1. 使用基础除数 10^9，即 10 亿
1. 将 wei 数量除以该基础除数

这样就能得到对应的 gwei 总数。

例如，420,069,000,000 wei / 1,000,000,000 = 420.069 gwei。

要计算以美元计的价格，将 gwei 总数乘以当前 1 个 Ether 的价格再除以 1,000,000,000（10^9）。

例如，若 Ether 价值 1,000 美元，则 420.069 gwei 等于 0.000420069 美元，约为 1 美分的 1/25。

## 如何将 wei 转换为 ETH

与将 wei 转换为 gwei 类似，用户可以使用 wei 转换器，或按照以下 3 个步骤进行转换：

1. 统计 wei 的总数量
1. 使用基础除数 10^18，即 1 quintillion（百万的三次方）
1. 将 wei 数量除以该基础除数

这样就能得到 ETH 的总数量。

例如，100 trillion wei / 10^18 = 0.0001 ETH。

要计算以美元计的价格，将 ETH 数量乘以当前 ETH 的价格。
例如，当 1 ETH 价值 1,000 美元时，0.0001 ETH 等于 0.10 美元。

## 如何通过代码将 wei 转换为 ETH

以下是使用 ethers.js 将 wei 转换为 ETH 的方法：

1. 安装 ethers.js
1. 输入或获取要转换的数量
1. 调用 formatEther 方法转换为 ETH

下面的示例展示了如何获取 vitalik.eth 的 ether 余额（以 wei 形式返回），并将其以 ETH 形式打印出来：

<CodeSnippet
  language="javascript"
  code={`import { ethers } from "ethers";

const provider = new ethers.JsonRpcProvider(
  "https://eth-mainnet.g.alchemy.com/v2/<API_KEY>",
);

// getBalance returns the balance in wei
const balanceWei = await provider.getBalance("vitalik.eth");
console.log(ethers.formatEther(balanceWei));`}
/>
