我正在测试我正在脱机签署的事务是否被Kovan网络所接受。我使用Web3.js (web3 3@1.20)提交事务,如下所示:
我连接到OpenEtalum3.0.1节点,然后使用web3.eth.sendSignedTransaction。根据文档,这应该返回一个PromiEvent。然而,提交所发出的事件并没有被捕获,并且承诺永远不会得到解决。尽管如此,交易还是提交给网络,并且是有效的(这是一个简单的事务,将资金从一个非合同帐户发送到另一个非合同帐户)。人们可以在任何区块浏览器中找到它,比如以太扫描。
以下代码的行为不像预期的那样:
函数sendTrans(_rawData,_txHash) {
try {
console.log("Before Asynch call");
web3.eth.sendSignedTransaction(_rawData)
.once('transactionHash', function(hash){ console.log("txHash", hash) })
.once('receipt', function(receipt){ console.log("receipt", receipt) })
.on('confirmation', function(confNumber, receipt){ console.log("confNumber",confNumber,"receipt",receipt) })
.on('error', function(error){ console.log("error", error) })
.then(function(receipt){
console.log("trasaction mined!", receipt);
});
console.log("After Asynch Call");
}
catch (error) {
console.log("Error Sending Transaction", error.message);
}
return { response: "OK", transHash: _txHash };
}//结果:代码不触发任何发射器事件。没有登录到控制台。
上面的代码只是挂起,因为sendSignedTransaction返回的承诺从未得到解决。也没有收到任何事件,因此永远不会触发.on(“接收”)。然而,事务被成功地提交到网络并进行挖掘。所以问题不在于提交,而在于web3.eth.sendSignedTransaction返回的web3.eth.sendSignedTransaction。
有人知道为什么会发生这种行为吗?
发布于 2020-06-22 04:29:56
来自正式文件:
// using the promise
web3.eth.sendTransaction({
from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
value: '1000000000000000'
})
.then(function(receipt){
...
});
// using the event emitter
web3.eth.sendTransaction({
from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
value: '1000000000000000'
})
.on('transactionHash', function(hash){
...
})
.on('receipt', function(receipt){
...
})
.on('confirmation', function(confirmationNumber, receipt){ ... })
.on('error', console.error); // If a out of gas error, the second parameter is the receipt.在您的代码中,您似乎试图同时使用“承诺”和“事件发射器”。
此外,对于事件发射器,您似乎使用的是once而不是on。
我首先要修复这两个问题(去掉then,用on替换once )。
发布于 2020-06-22 20:18:02
您可以将web3.eth.sendSignedTransaction包装在计算事务时解决的承诺中。
const hash = await new Promise(async (resolve) => {
await web3.eth.sendSignedTransaction(serializedTx)
.once('transactionHash', (hash) => {
resolve(hash)
})
console.log('We\'ve finished')
})
console.log('Hash: ', hash)不幸的是,由于https://github.com/ethereum/web3.js/issues/3204,web3将等待确认(如果某个任务处于挂起状态,节点将等待直到它完成,因此删除等待将不会修复它)。
如果您想跳过web3确认,您必须使用eth_sendRawTransaction api并直接与提供程序的send通信(未经测试)。
web3.currentProvider.send({
jsonrpc: '2.0',
method: 'eth_sendRawTransaction',
params: ['0xf8648080....'],
id: 253,
}, (err, result) => {
if (err) {
return reject(err)
}
return resolve(result)
})https://ethereum.stackexchange.com/questions/84437
复制相似问题