0

for my demo I need the transaction hash instantly without waiting for callback of sync call. Here my code:

aggiungiAllaBlockChain: async(u,n,c,e) => {

try {
  const web3     = new Web3(App.web3Provider);
  const contract_PartecipazioneContest = new web3.eth.Contract(JSON.parse(App.ABI_PartecipazioneContest), App.ADDR_PartecipazioneContest);
  const account  = App.account;

  //const receipt  = await App.sendUserInBlockChain(web3, account, contract_PartecipazioneContest.methods.addUser(u, n, c, e));
  
  const transaction = contract_PartecipazioneContest.methods.addUser(u, n, c, e);
  value = 0;

  const options = {
      to      : transaction._parent._address,
      data    : transaction.encodeABI(),
      gas     : await transaction.estimateGas({from: account.address, value: value}),
      gasPrice: await web3.eth.getGasPrice(),
      value   : value
  };
  const signed  = await web3.eth.accounts.signTransaction(options, account.privateKey);
  const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction)
  .on('transactionHash', function(hash){
      console.log("hash on(transactionHash " + hash);
      return hash;

  });


  
} catch (error) {

  console.log("errore Aggiungi utente alla blockchain" + error)
}
}

I print the hash of my transaction but the return value doesn't work.

I have got this result using truffle but now I need this following this path (not passing for meta mask)

Thanks a lot for your help.

1
  • The value returned by signTransaction contains the transactionHash.
    – Ismael
    Commented Sep 17, 2020 at 20:30

2 Answers 2

1

You can execute sendSignedTransaction using the event emitter instead of using the promise.

See this coding example from the official documentation.

It is given for sendTransaction, but it works the same for sendSignedTransaction.


BTW, you may want to extend this:

await transaction.estimateGas({from: account.address})

To this:

await transaction.estimateGas({from: account.address, value: value})
2
  • @godvibration i have modified the code after your suggestion but the return values doesn't return to my front end call Commented Sep 17, 2020 at 11:27
  • @alfo888_ibg: You need to learn how to write asynchronous code. This is not the way. Follow the example. There is no await required when you use the event emitter. Commented Sep 17, 2020 at 12:27
2

You can precalculate a transaction hash with all the transaction parameters and transaction signature. The transaction hash is the Keccak256 hash of the signed transaction: Keccak256(RLP(nonce, gasPrice, gasLimit, to, value, data, v, r, s)). Here's an example of how you could do it with Web3 and rlp.

import { encode } from 'rlp';

const transaction = {
  // Transaction parameters
  to: transaction._parent._address,
  gasPrice: await web3.eth.getGasPrice(),
  [...],

  // Signature
  v: ...,
  r: ...,
  s: ...
};

const rawTransaction = encode([transaction.nonce, transaction.gasPrice, transaction.gasLimit, transaction.to, transaction.value, transaction.data, transaction.v, transaction.r, transaction.s]);
const transactionHash = web3.sha3(rawTransaction);

console.log(transactionHash);
1
  • 1
    His/her code relies on using the current nonce (i.e., by not specifying nonce), so your suggestion might ultimately work out wrong for him/her, if they execute the actual transaction after executing some other transaction or transactions on the same account. Commented Sep 17, 2020 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.