处理智能合约交易快递Node js



我一直在编写我的小项目,但我遇到了一个问题。 这是我的代码:

app.get('/thu', (req, res) => {
thu(function(err, output){
if(err){
res.json({"err": ""+err, "output": output});
return;
}
res.send("ket qua: ", output);
});
});
var thu = function(callback){
web3.eth.getTransactionCount(senderAddress).then((txnCount) => {
console.log("goi thu");
var method = contract.methods.thu();
var encodedABI = method.encodeABI();
var thuTx = {
from: senderAddress,
to: contractAddress,
nonce: web3.utils.toHex(txnCount),
gasLimit: web3.utils.toHex(GAS_LIMIT),
gasPrice: web3.utils.toHex(GAS_PRICE),
data: encodedABI,
};
sendTxn(thuTx, callback);
}).catch((err) => {
console.log("web3 err", err);
callback(err, null);
});
};
function sendTxn(rawTx, callback) {
var privateKeyBuffer = new Buffer(privateKey, 'hex');
var transaction = new tx(rawTx);
transaction.sign(privateKeyBuffer);
var serializedTx = transaction.serialize().toString('hex');
web3.eth.sendSignedTransaction(
'0x' + serializedTx, function(err, txnHash) {
if(err) {
console.log("txn err", err);
callback(err, null);
} else {
console.log("txn result", txnHash);
}
}).catch((err) => {
callback(err, null);
});
}

我确定我的智能合约运行正常。 当我点击提交代码时,将交易发送到 Rinkeby,没关系。 但我无法收到任何回复。 请帮助我解决我的问题。谢谢。

>sendSignedTransaction返回一个承诺组合事件发射器。

以太坊作为区块链具有不同程度的最终性和 因此需要返回操作的多个"阶段"。为了应对 要求我们为以下函数返回一个"promiEvent" web3.eth.send交易或合约方法。这个"促销事件"是一个 promise与事件发射器相结合,允许对不同的事件发射器进行操作 区块链上的行动阶段,如交易。

您可以在每个事件上放置一个控制台.log以查看正在发生的事情,或者是否收到错误。

web3.eth.sendSignedTransaction('0x' + serializedTx)
.once('transactionHash', hash => console.log(`Hash: ${hash}`)
.once('receipt', receipt => console.log(`Receipt: ${receipt}`)
.on('confirmation', (confNumber, receipt) => console.log(confNumber))
.on('error', error => console.error(error))
.then(receipt => {
// will be fired once the receipt its mined
});

问题已解决。 问题是我忘记将回调(...(放在其他{...}中。

最新更新