我正在使用Express框架编写Nodejs应用程序。我尝试将一个以太币从一个帐户发送到另一个帐户,即使我得到了交易哈希,该交易也没有通过。我已经读过汽油价格可能太低了,但是由于我将价格设置为5.000.000,所以它再次失效了。当我调用web3.eth.getTransactionReceipt('txHash')
时,它返回null,因此我猜交易失败。
这是我的代码:
router.get('/', (req, res, next) => {
// Build a transaction object
web3.eth.getTransactionCount(ad2, (err, txCount) => {
const txObject = {
nonce: toHex(txCount),
to: ad1,
value: toHex(web3.utils.toWei('1', 'ether')),
gasLimit: toHex(21000),
gasPrice: toHex(web3.utils.toWei('12', 'gwei'))
}
// Sign the transaction
const tx = new Tx(txObject);
tx.sign(pk2);
const serializedTx = tx.serialize();
const raw = '0x' + serializedTx.toString('hex')
// Broadchast the transaction to the network
web3.eth.sendSignedTransaction(raw, (err, txHash) => {
console.log('txHash:', txHash);
web3.eth.getTransactionReceipt(txHash, (err, receipt) => {
console.log('receipt: ', receipt);
if (receipt == null) {
// Render the page
res.render('sendTransaction', {
receiptSuccess: 0,
blockHash: 'No Receipt Found'
});
} else {
// Render the page
res.render('sendTransaction', {
receiptSuccess: 1,
blockHash: receipt.blockHash,
blockNumber: receipt.blockNumber,
transactionHash: receipt.transactionHash,
from: receipt.from,
to: receipt.to,
contractAddress: receipt.contractAddress,
gasUsed: receipt.gasUsed
});
}
});
});
});
});
我该怎么做才能成功地将交易从地址1发送到地址2?上面的代码缺少什么?
谢谢!