调用智能合约中的方法:nonce已被使用,Nonce太低

问题描述 投票:0回答:1

我有一个合同,有一个简单的方法:

function getOwner() external returns (address) {
  return owner;
}

我正在尝试使用

NodeJs
库在
ethers
中调用它:

// create the provider and the signer
const provider = new ethers.getDefaultProvider("http://127.0.0.1:8545/");
let signer =  new ethers.Wallet('[privatekey]',
    provider );
let manager = new NonceManager(signer);

// create contract
let contractAddress = "[contractaddress]";
let abiString = fs.readFileSync(contractAbiPath).toString();
let abi = JSON.parse(abiString);
let contract = new ethers.Contract(contractAddress, abi, signer);
await contract.waitForDeployment();

// get the current nonce
let walletNonce = await signer.getNonce(); 

// send the transaction

// error1: if I pass walletNonce + 1
let transaction = await contract.getOwner({
    nonce: walletNonce
});
// error2 : if in the line above I passed walletNonce
let response = await manager.sendTransaction(transaction);
await response.wait(1);

如果

walletNonce
6
,并且我像这样传递它:
nonce: walletNonce
,我在第
let response = await manager.sendTransaction(transaction);
行得到错误:

Nonce too low. Expected nonce to be 7 but got 6.'

但是,如果我通过了

nonce: walletNonce + 1
,我会在

行收到错误
let transaction = await contract.getOwnerAddress({
    nonce: walletNonce
});
Nonce too high. Expected nonce to be 6 but got 7. Note that transactions can't be queued when automining.

could not coalesce error

我不知道该做什么。

ethereum ethers.js nonce ether
1个回答
0
投票

重新迁移项目或从 nonce-too-high-error-with-metamask-and-hardhat

打开 MetaMask 窗口并单击右上角的图标 显示帐户。转到“设置”,然后转到“高级”并点击“重置帐户”。

现在您进行交易时就不会出现错误了。

© www.soinside.com 2019 - 2024. All rights reserved.