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,700 points
843 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,110 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
1,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
171 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
1,072 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 5, 2018 in Blockchain by aryya
• 7,440 points
213 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,830 points
555 views
0 votes
1 answer
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,110 points
443 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 9, 2018 in Blockchain by Shashank
• 10,400 points
5,289 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,110 points
555 views