2

I'm trying to retrieve the transaction hash value after I call my smart contract method.

My Promise object looks like below.

returnPromise:  
    Promise { "pending" }
    ​
    <state>: "pending"
    ​
    <prototype>: PromiseProto
    ​​
    catch: function catch()
    ​​
    constructor: function Promise()
    ​​
    finally: function finally()
    ​​
    then: function then()
    ​​
    Symbol(Symbol.toStringTag): "Promise"
    ​​
    <prototype>: Object { … }

Here's my code snippet:

const query = smartContract.methods.buyToken(Type, Title, Desc);
const encodedABI = query.encodeABI();
const tx = {
  from: walletAddress,
  to: contractAddress,
  gas: 2000000,
  data: encodedABI,
};
returnPromise = web3.eth.accounts.signTransaction(tx, privateKey).then(signed => {
  const tran = await web3.eth
    .sendSignedTransaction(signed.rawTransaction)
    .on('confirmation', (confirmationNumber, receipt) => {
      console.log('=> confirmation: ' + confirmationNumber);
    })
    .on('transactionHash', hash => {
      console.log('=> hash');
      console.log(hash);
      //console.log("HASH: ", hash);
      transHash = hash;
    })
    .on('receipt', receipt => {
      console.log('=> reciept');
      console.log(receipt);
    })
    .on('error', console.error);
});

console.log("returnPromise: " + returnPromise);
0

You need to await your promise.

Your code should read:

await returnPromise = web3.eth.accounts.signTransaction(tx, privateKey).then(signed => {
  • I tried but received the error "SyntaxError: await is only valid in async functions and async generators". I'm using web3js 1.0.0 beta 34 – TThom May 7 at 4:49
  • @TThom: That's got nothing to do with web3 version. As the error says, you can use await only from inside an async function. – goodvibration May 7 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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