@celo-tools/celo-ethers-wrapper

中文文档
2.3.0发布于 2024/08/13MITTypeScriptESM + CJS近 30 天下载 3112

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

中文使用文档
文档目录6 节
  1. 安装
  2. 基本用途
  3. 合同互动
  4. 替代天然气费货币
  5. 获取 Celoscan 的交易历史
  6. 实例

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 提供器数字 :

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,它基于 钱包 数字 :

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

const wallet = new CeloWallet(YOUR_PK, provider)

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

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 文件夹.

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)的交易费用,但也支持支付稳定的代币.

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

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

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 共发布 15 个版本,首次发布于 2020/10/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.3.0latest2024/08/132 年前
2.2.02024/06/122 年前
2.1.02024/05/022 年前
2.0.02023/10/193 年前
1.0.02023/10/063 年前
0.4.02023/02/233 年前

关于 @celo-tools/celo-ethers-wrapper 的常见问题

@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 和提交记录判断。