I have been coding my small project but I'm facing a problem. here is my code:
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);
});
}
I'm sure that my smart contract runs ok. when I hit submit the code send a transaction to Rinkeby and it is ok. but I cannot receive any responses. Please help my solve my problems. thank you.
sendSignedTransaction
returns a Promise combined event emitter.
Ethereum as a blockchain has different levels of finality and therefore needs to return multiple “stages” of an action. To cope with requirement we return a “promiEvent” for functions like web3.eth.sendTransaction or contract methods. This “promiEvent” is a promise combined with an event emitter to allow acting on different stages of action on the blockchain, like a transaction.
You can place a console.log on every event, to see what is happening, or if you're getting an error.
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
});
Problems solved. the problem is I forgot the put the callback(...) in else {...}.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.