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 更详细的例子.