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
• 27,620 points
85 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 65,810 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
• 16,450 points
169 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
• 16,450 points
29 views
0 votes
1 answer

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

Here's a simple example for web3js 1.0.0.beta*: function handler ...READ MORE

answered Jun 8, 2018 in Blockchain by ariaholic
• 7,320 points
209 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 ariaholic
• 7,320 points
30 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
60 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
• 65,810 points
28 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,330 points
945 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
• 65,810 points
100 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.