gpt4 book ai didi

javascript - 重复事务挂起 - web3js,本地 geth

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:54:13 24 4
gpt4 key购买 nike

我的本​​地以太坊网络上的交易有问题 - 在某些时候,交易挂起并从我的账户中花费大量 ETH。

这是一个示例代码:

async function send(toAccount, weiVal) {
let account = await w3.getDefAccount();

for (let i = 0; i < 100; i++) {
let res = await web3.eth.sendTransaction({
from: account,
to: toAccount,
value: weiVal
});
await helper.timeout(2000);
}
}

send('0x5648...', 100000000000000);

它在一些随机迭代中卡在 sendTransaction 调用( promise 永远不会解决)。

脚本重启后情况保持不变 - 事务通过几次然后挂起。

获取版本:1.7.3

最佳答案

如果您从同一个账户连续发送交易,您需要手动设置随机数,因为节点不会正确跟踪它。

示例代码

async function send(toAccount, weiVal) {
const account = await web3.getDefAccount();
// the transaction count does not include *pending* transactions
// but is a good starting point for the nonce
const nonce = await web3.eth.getTransactionCount(account);

let promises = [];
for (let i = 0; i < 100; i++) {
promises.push(web3.eth.sendTransaction({
from: account,
to: toAccount,
nonce: nonce++, // increment the nonce for every transaction
value: weiVal
}));
}

return Promise.all(promises);
}

await send('0x5648...', 100000000000000);

关于javascript - 重复事务挂起 - web3js,本地 geth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49495879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com