@celo-tools/celo-ethers-wrapper

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

A minimal wrapper to make Ethers.JS compatible with the Celo network.

原始使用文档
文档目录6 节
  1. Install
  2. Basic Usage
  3. Contract Interaction
  4. Alternative gas fee currencies
  5. Getting transaction history with CeloscanProvider
  6. Examples

celo-ethers-wrapper

Initially, the Celo network was not fully compatible with Ethers.JS. Since the Donut hard-fork in 2021, Ethereum tools can now be used with Celo without the need for a wrapper. However, Celo transactions have optional, additional fields that enable useful features like paying with stable tokens. This library enables the use of those extra fields.

Install

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

or

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

Note this wrapper has Ethers as a peer dependency. Use celo-ethers-wrapper v1 versions for Ethers V5 and v2 versions for Ethers V6.

Basic Usage

Connect to the network by creating a CeloProvider, which is based on JsonRpc-Provider:

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

Next, Create a CeloWallet, which is based on Wallet :

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

const wallet = new CeloWallet(YOUR_PK, provider)

Use the provider or wallet to make calls or send transactions:

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

Contract Interaction

CeloWallet can be used to send transactions.

Here's an example of sending cUSD with the StableToken contract. For interacting with contracts you need the ABI and address. Addresses for Celo core contracts can be found with the CLI's network:contracts command. The ABIs can be built from the solidity code or extracted in ContractKit's generated folder.

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}`)

Alternative gas fee currencies

The Celo network supports paying for transactions with the native asset (CELO) but also with the stable token (cUSD).

This wrapper currently has partial support for specifying feeCurrency in transactions.

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,
})

Getting transaction history with CeloscanProvider

You can also rely on EthersProviders functionality, such as getting an account's transaction history, using our alternative CeloscanProvider

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);

Examples

See the tests under /test for more detailed examples.

版本与发布记录

@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 是什么?

A minimal wrapper to make Ethers.JS compatible with the Celo network.

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