# @celo-tools/celo-ethers-wrapper 中文文档

规范地址：https://npmcn.com/package/%40celo-tools/celo-ethers-wrapper/

版本：2\.3\.0
npm 元数据更新时间：2024-08-13T14:31:56.778Z
中文译文更新时间：2026-11-09T09:26:41.804Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40celo-tools/celo-ethers-wrapper
原文对照：https://npmcn.com/package/%40celo-tools/celo-ethers-wrapper/?lang=original
项目主页：https://github.com/celo-tools/celo-ethers-wrapper#readme
源码仓库：https://github.com/celo-tools/celo-ethers-wrapper

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：174 kB，39 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=18\.14\.2
- 运行依赖：1 个
- peer 依赖：ethers ^6
- 首次发布：2020-10-07T19:11:51.730Z
- 最新版本发布：2024-08-13T14:31:56.450Z
- 近 30 天下载量：3112（2026-10-10 至 2026-11-08，npm 官方下载统计）
- 已发布版本数：15（近 12 个月 0 个）

## 中文简介

最低限度的包装来制作以太. JS与Celo网络兼容.

## 安装

```sh
npm install @celo-tools/celo-ethers-wrapper
```

也可以使用：`pnpm add @celo-tools/celo-ethers-wrapper`、`yarn add @celo-tools/celo-ethers-wrapper`、`bun add @celo-tools/celo-ethers-wrapper`

## README

# celo-ethers- 包装器

最初Celo网络与以太不完全相容. 联合来文。 自从2021年的"多努特"硬叉后,伊特鲁姆工具现在可以和克洛一起使用而不需要包装. 然而,Celo交易有可选的,额外的字段,可以使一些有用的特性,如用稳定的符来支付. 这个库可以使用这些额外的字段.

## 安装

`npm i @celo-tools/celo-ethers-wrapper`

或者说

`yarn add @celo-tools/celo-ethers-wrapper`

注意此包装有以太作为同伴依赖. **使用celo-ethers-wrapper v1版本为以太V5,使用v2版本为以太V6.**

## 基本用途

通过创建网络连接到网络 `CeloProvider`,基于 [JsonRpc 提供器](https://docs.ethers.org/v6/api/providers/jsonrpc)数字 :

```js
import { CeloProvider } from '@celo-tools/celo-ethers-wrapper'

// Connecting to Alfajores testnet
const provider = new CeloProvider('https://alfajores-forno.celo-testnet.org')
await provider.ready
```

下一个,创建 CeloWallet,它基于 [钱包](https://docs.ethers.org/v6/api/wallet) 数字 :

```js
import { CeloWallet } from '@celo-tools/celo-ethers-wrapper'

const wallet = new CeloWallet(YOUR_PK, provider)
```

使用提供者或钱包进行呼叫或发送交易:

```js
const txResponse = await wallet.sendTransaction({
    to: recipient,
    value: amountInWei,
  })
const txReceipt = await txResponse.wait()
console.info(`CELO transaction hash received: ${txReceipt.transactionHash}`)
```

## 合同互动

`CeloWallet` 可用于发送交易.

下面是用稳定托肯合同发送CUSD的例子. 为了与合同互动,你需要ABI和地址。 Celo核心合同的地址可与CLI查询 `network:contracts` 命令。 ABI可以使用固态代码构建,也可以在 ContractKit 中取出 `generated` 文件夹.

```js
import { Contract, ethers, utils, providers } from 'ethers'

const stableToken = new ethers.Contract(address, abi, wallet)
console.info(`Sending ${amountInWei} cUSD`)
const txResponse: providers.TransactionResponse = await stableToken.transferWithComment(recipient, amountInWei, comment)
const txReceipt = await txResponse.wait()
console.info(`cUSD payment hash received: ${txReceipt.transactionHash}`)
```

## 替代天然气费货币

Celo网络支持支付与原生资产(CELO)的交易费用,但也支持支付稳定的代币.

此包件目前对交易中指定收费是否真实有部分支持 .

```js
const gasPrice = await wallet.getGasPrice(stableTokenAddress)
const gasLimit = await wallet.estimateGas(tx)

// Gas estimation doesn't currently work properly for non-CELO currencies
// The gas limit must be padded to increase tx success rate
// TODO: Investigate more efficient ways to handle this case
const adjustedGasLimit = gasLimit.mul(10)

const txResponse = await signer.sendTransaction({
  ...tx,
  gasPrice,
  gasLimit: adjustedGasLimit,
  feeCurrency: stableTokenAddress,
})
```

## 获取 Celoscan 的交易历史

您也可以依赖 Ethers Providers 功能,例如获取账户的交易历史,使用我们的替代 Celoscan Provider

```js
import { CeloscanProvider } from '@celo-tools/celo-ethers-wrapper'

// You can use 'celo', 'alfajores' or 'baklava'.
// Default is 'celo' (mainnet)
const scanProvider = new CeloscanProvider('alfajores');

const history = await provider.getHistory(YOUR_ACCOUNT);
console.info("History:", history);
```

## 实例

见下面的测试 `/test` 更详细的例子.

## 常见问题

### @celo-tools/celo-ethers-wrapper 是什么？

最低限度的包装来制作以太\. JS与Celo网络兼容\.

### 如何安装 @celo-tools/celo-ethers-wrapper？

在项目目录运行 npm install @celo-tools/celo-ethers-wrapper，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：ethers（^6）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @celo-tools/celo-ethers-wrapper 支持 TypeScript 吗？

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

### @celo-tools/celo-ethers-wrapper 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @celo-tools/celo-ethers-wrapper 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=18\.14\.2。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @celo-tools/celo-ethers-wrapper 可以商用吗？

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

### @celo-tools/celo-ethers-wrapper 的下载量有多少？

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

### @celo-tools/celo-ethers-wrapper 还在维护吗？

最新版本 2\.3\.0 发布于 2024/08/13（2 年前）；至今共发布 15 个版本，其中过去 12 个月发布了 0 个；首次发布于 2020/10/08。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

