【问题标题】:Ethers js set noonce when using contract objectEthers js 在使用合约对象时设置 noonce
【发布时间】:2022-12-16 08:04:58
【问题描述】:

我使用 ethers 与 solidity 合约进行交互。我想同时执行多个事务。要做到这一点,我知道我必须定义 noonce 对于每个下一个交易都是 +1。当我这样做时,我知道该怎么做

await signer.sendTransaction()

但是当我创建 Contract 对象并在其上执行函数时,如何使用自定义 noonce 执行多个事务?我试过了

contract.methodName(methodParams, {
      gasLimit: gasLimit,
      gasPrice,
      value: ethers.utils.parseEther(
        "0.01"
      ),
      noonce: nextNoonce
    })

但它不起作用,我收到错误 cannot override "noonce"。我怎样才能在这里定义它?

【问题讨论】:

标签: javascript blockchain solidity ethers.js


【解决方案1】:

要使用 ethers.js 设置 nonce 值,您可以通过 provider 获取它,如下所示:

阅读更多关于nonce的信息。

import { ethers } from "ethers";

const { ethereum } = window;
const provider = new ethers.providers.Web3Provider(ethereum);
const signer = provider?.getSigner();
const contract = new ethers.Contract(
    CONTRACT_ADDRESS,
    CONTRACT_ABI,
    signer
);
const nonce = await provider.getTransactionCount(contract.address);
const tx = await contract.methodName(param1, param2, {
                    gasLimit:gasLimit,
                    gasPrice:gasPrice,
                    value: ethers.utils.parseEther("0.01"),
                    nonce:nonce,
                });

第 2 步:重置您的 Metamask 帐户。

有时问题出在您的元掩码帐户上,需要重置才能获得正确的客户随机数值。

要重置您的帐户:

单击 Metamask 图标 > 高级设置 > 重置帐户

【讨论】:

    猜你喜欢
    • 2022-07-31
    • 2022-10-13
    • 2021-10-07
    • 2022-01-22
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 2021-07-23
    相关资源
    最近更新 更多