1

I am using Truffle Contract to call a Smart Contract method that writes to the ethereum blockchain. My web3 client is connected to my own Open Ethereum node. Below is the code (nodejs) that I am using to send the transaction to blockchain. I would expect, since this is an asynchronous call that the response would be immediately and the transaction hash value would be returned. However, this is NOT happening. The only time I get a response is after the transaction is mined. I can't rely on this because my front-end client will time out (30 secs for AWS API Gateway). What am I doing wrong? I can see all the eth_getTransactionReceipt json calls in my OE log, being called back to back until the transaction is mined.

async function sendTransClaimProperty(_instance) {

    var result;
    
    try {
      
      result = await web3.eth.personal.unlockAccount(web3.eth.defaultAccount, process.env.oranges, null);
      console.log(web3.eth.defaultAccount + " Account UnLocked", result);
      
      if (result) {
            var claimState = "Property Claimed on " + new Date();
            console.log("Calling Ethereum Node at " + new Date());
            
            result = await _instance.claimProperty.sendTransaction(claimTitle,claimHash,claimTime,claimCreditCardTx,claimClientIP,claimState);
                
            console.log("Return from Ethereum Node result", result);
            console.log("Return from Ethereum Node. TX", result.tx);
            return { response: "OK", transHash: result.tx };
            
      } 
    }
    catch (error) {
      
      console.log("Error Sending Transaction");
      console.log(error.message);
      return { response: "FAIL", message: "Error claiming property " + error.message };
      
    }
    
  }

1 Answer 1

0

To obtain the transaction hash without waiting you have to use the events system

const res = _instance.claimProperty(claimTitle,...);
res.on('transactionHash', (hash) => {
  console.log('TxHash', hash);
});

You will find some other details at Truffle reference.

3
  • Ismael, Thanks for the advice. It did not work. The response from the async call returned this INFO Response is Promise { <pending>, _events: Events <[Object: null prototype] {}> {}, emit: [Function: emit], on: [Function: on], once: [Function: once], off: [Function: removeListener], listeners: [Function: listeners], addListener: [Function: on], removeListener: [Function: removeListener], removeAllListeners: [Function: removeAllListeners] This async call however, is part of a synchronous function call. Maybe that's the problem. I will continue to try Jun 21, 2020 at 13:02
  • @KevinMelberger What did you try? and why do you say it doesn't work?
    – Ismael
    Jun 21, 2020 at 15:48
  • I added the event emitter code, but it does not work. Everything I tried will not get me back the transaction hash BEFORE the transaction is mined. Code example below: _instance.claimProperty.sendTransaction(claimTitle,claimHash,claimTime,claimCreditCardTx,claimClientIP,claimState) .on('transactionHash', function(hash){ txHash = hash; console.log("Return from Ethereum Node. TX ", hash); }) .on('error',console.error); Jun 22, 2020 at 2:32

Your Answer

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

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