我正在遵循文档,以便能够在Kovan testnet上签名和发送事务。当我控制txHash的时候,我得到了一个未定义的值。
web3.eth.getTransactionCount(account1, (err, txCount) => {
// 1)Build Transaction
const txObject = {
nonce: web3.utils.toHex(txCount),
to: account2,
value: web3.utils.toHex(web3.utils.toWei('0.05', 'ether')),
gasLimit: web3.utils.toHex(2100),
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei'))
}
// 2)Sign Transaction
const tx = new Tx(txObject,{'chain':42})
tx.sign(privateKey1)
const serializedTransaction = tx.serialize()
const raw = '0x' + serializedTransaction.toString('hex')
console.log("raw:", raw)
console.log("tx:", serializedTransaction)
// 3)Broadcast Transaction
web3.eth.sendSignedTransaction(raw, (err, txHash) =>{
console.log('txHash:', txHash)
})
// COMMENTED-OUT web3.eth.sendSignedTransaction('0x' + serializedTransaction .toString('hex'))
// .on('receipt', console.log);
})发布于 2021-11-14 08:28:21
signTransaction()只执行签名。它不会将(签名的)事务广播到网络。
为此,您可以使用sendSignedTransaction() (docs),它将(签名和序列化的) tx数据提交给提供商,然后提供商将其广播到网络。
web3.eth.sendSignedTransaction(signedTx.rawTransaction)
.on('receipt', console.log);https://stackoverflow.com/questions/69959320
复制相似问题