How to handle smart contract transaction express nodejs

0 votes

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);
  });
}

When I hit submit the code send a transaction to I cannot receive any responses.

Oct 15, 2018 in Blockchain by digger
• 26,740 points
1,989 views

1 answer to this question.

0 votes

sendSignedTransaction returns a Promise combined event emitter.

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
    });
answered Oct 15, 2018 by Omkar
• 69,220 points

Related Questions In Blockchain

0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,100 points
3,624 views
0 votes
1 answer

How is a request sent from an app to a smart contract?

Yes, the contract is distributed by every node ...READ MORE

answered Jun 4, 2018 in Blockchain by Perry
• 17,100 points
731 views
0 votes
2 answers

How to get notified when an event triggers on ethereum smart contract?

Muchas gracias. ?Como puedo iniciar sesion? READ MORE

answered May 2, 2020 in Blockchain by aqowcmbevs
2,407 views
0 votes
1 answer

How to prevent the smart contract from being modified and deployed in the blockchain network?

To expand on Matthew's answer, each state ...READ MORE

answered Jul 6, 2018 in Blockchain by aryya
• 7,460 points
820 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,944 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,469 views
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,220 points
1,484 views
+1 vote
4 answers

How to estimate the cost for deploying smart contract on mainnet?

Since you have already deployed the contract ...READ MORE

answered Apr 10, 2018 in Blockchain by Shashank
• 10,400 points
15,680 views
0 votes
1 answer

How to run smart contract function using nethereum to return a result?

Try this: var result = getUserAtIndex.CallAsync<byte[]>(123) ...READ MORE

answered Oct 24, 2018 in Blockchain by Omkar
• 69,220 points
2,071 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP